mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-22 08:42:32 -05:00
Fix generate index failure possibility on postgres (#21998)
@wxiaoguang Please review Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
64973cf18f
commit
f7ade6de7c
3 changed files with 40 additions and 2 deletions
|
@ -7,6 +7,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResourceIndex represents a resource index which could be used as issue/release and others
|
// ResourceIndex represents a resource index which could be used as issue/release and others
|
||||||
|
@ -55,8 +58,25 @@ func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxInd
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
|
||||||
|
res, err := GetEngine(ctx).Query(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
|
||||||
|
"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1 RETURNING max_index",
|
||||||
|
tableName, tableName), groupID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if len(res) == 0 {
|
||||||
|
return 0, ErrGetResourceIndexFailed
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
|
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
|
||||||
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
|
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
|
||||||
|
if setting.Database.UsePostgreSQL {
|
||||||
|
return postgresGetNextResourceIndex(ctx, tableName, groupID)
|
||||||
|
}
|
||||||
|
|
||||||
e := GetEngine(ctx)
|
e := GetEngine(ctx)
|
||||||
|
|
||||||
// try to update the max_index to next value, and acquire the write-lock for the record
|
// try to update the max_index to next value, and acquire the write-lock for the record
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -49,8 +50,25 @@ func init() {
|
||||||
db.RegisterModel(new(CommitStatusIndex))
|
db.RegisterModel(new(CommitStatusIndex))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
|
||||||
|
res, err := db.GetEngine(ctx).Query("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+
|
||||||
|
"VALUES (?,?,1) ON CONFLICT (repo_id, sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1 RETURNING max_index",
|
||||||
|
repoID, sha)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if len(res) == 0 {
|
||||||
|
return 0, db.ErrGetResourceIndexFailed
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
// GetNextCommitStatusIndex retried 3 times to generate a resource index
|
// GetNextCommitStatusIndex retried 3 times to generate a resource index
|
||||||
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
|
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
|
||||||
|
if setting.Database.UsePostgreSQL {
|
||||||
|
return postgresGetCommitStatusIndex(ctx, repoID, sha)
|
||||||
|
}
|
||||||
|
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
// try to update the max_index to next value, and acquire the write-lock for the record
|
// try to update the max_index to next value, and acquire the write-lock for the record
|
||||||
|
|
|
@ -135,8 +135,8 @@ func TestRepoCommitsStatusParallel(t *testing.T) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(t *testing.T, i int) {
|
go func(parentT *testing.T, i int) {
|
||||||
t.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
|
parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
|
||||||
runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending"))
|
runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending"))
|
||||||
runBody(t)
|
runBody(t)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
Loading…
Reference in a new issue