Skip to content

Commit f7ade6d

Browse files
lunnysilverwind
andauthored
Fix generate index failure possibility on postgres (#21998)
@wxiaoguang Please review Co-authored-by: silverwind <[email protected]>
1 parent 64973cf commit f7ade6d

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

models/db/index.go

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"strconv"
11+
12+
"code.gitea.io/gitea/modules/setting"
1013
)
1114

1215
// 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
5558
return nil
5659
}
5760

61+
func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
62+
res, err := GetEngine(ctx).Query(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
63+
"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1 RETURNING max_index",
64+
tableName, tableName), groupID)
65+
if err != nil {
66+
return 0, err
67+
}
68+
if len(res) == 0 {
69+
return 0, ErrGetResourceIndexFailed
70+
}
71+
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
72+
}
73+
5874
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
5975
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
76+
if setting.Database.UsePostgreSQL {
77+
return postgresGetNextResourceIndex(ctx, tableName, groupID)
78+
}
79+
6080
e := GetEngine(ctx)
6181

6282
// try to update the max_index to next value, and acquire the write-lock for the record

models/git/commit_status.go

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"net/url"
12+
"strconv"
1213
"strings"
1314
"time"
1415

@@ -49,8 +50,25 @@ func init() {
4950
db.RegisterModel(new(CommitStatusIndex))
5051
}
5152

53+
func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
54+
res, err := db.GetEngine(ctx).Query("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+
55+
"VALUES (?,?,1) ON CONFLICT (repo_id, sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1 RETURNING max_index",
56+
repoID, sha)
57+
if err != nil {
58+
return 0, err
59+
}
60+
if len(res) == 0 {
61+
return 0, db.ErrGetResourceIndexFailed
62+
}
63+
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
64+
}
65+
5266
// GetNextCommitStatusIndex retried 3 times to generate a resource index
5367
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
68+
if setting.Database.UsePostgreSQL {
69+
return postgresGetCommitStatusIndex(ctx, repoID, sha)
70+
}
71+
5472
e := db.GetEngine(ctx)
5573

5674
// try to update the max_index to next value, and acquire the write-lock for the record

tests/integration/repo_commits_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func TestRepoCommitsStatusParallel(t *testing.T) {
135135
var wg sync.WaitGroup
136136
for i := 0; i < 10; i++ {
137137
wg.Add(1)
138-
go func(t *testing.T, i int) {
139-
t.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
138+
go func(parentT *testing.T, i int) {
139+
parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
140140
runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending"))
141141
runBody(t)
142142
wg.Done()

0 commit comments

Comments
 (0)