From 6f48a36227b7427afd2020aa416afe49d4c81015 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 16 Oct 2022 18:44:16 +0800 Subject: [PATCH] Refactor GetNextResourceIndex to make it work properly with transaction (#21469) Related: * #21362 This PR uses a general and stable method to generate resource index (eg: Issue Index, PR Index) If the code looks good, I can add more tests ps: please skip the diff, only have a look at the new code. It's entirely re-written. Co-authored-by: Lunny Xiao --- models/db/index.go | 130 ++++++++++++++++--------------- models/db/index_test.go | 127 ++++++++++++++++++++++++++++++ models/issues/issue.go | 14 ++-- models/issues/issue_index.go | 8 +- models/issues/issue_xref_test.go | 9 ++- models/issues/pull.go | 14 ++-- models/repo.go | 2 +- 7 files changed, 215 insertions(+), 89 deletions(-) create mode 100644 models/db/index_test.go diff --git a/models/db/index.go b/models/db/index.go index 673c382b2..58a976ad5 100644 --- a/models/db/index.go +++ b/models/db/index.go @@ -8,45 +8,15 @@ import ( "context" "errors" "fmt" - - "code.gitea.io/gitea/modules/setting" ) // ResourceIndex represents a resource index which could be used as issue/release and others -// We can create different tables i.e. issue_index, release_index and etc. +// We can create different tables i.e. issue_index, release_index, etc. type ResourceIndex struct { GroupID int64 `xorm:"pk"` MaxIndex int64 `xorm:"index"` } -// UpsertResourceIndex the function will not return until it acquires the lock or receives an error. -func UpsertResourceIndex(ctx context.Context, tableName string, groupID int64) (err error) { - // An atomic UPSERT operation (INSERT/UPDATE) is the only operation - // that ensures that the key is actually locked. - switch { - case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL: - _, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ - "VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1", - tableName, tableName), groupID) - case setting.Database.UseMySQL: - _, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ - "VALUES (?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", tableName), - groupID) - case setting.Database.UseMSSQL: - // https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/ - _, err = Exec(ctx, fmt.Sprintf("MERGE %s WITH (HOLDLOCK) as target "+ - "USING (SELECT ? AS group_id) AS src "+ - "ON src.group_id = target.group_id "+ - "WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+ - "WHEN NOT MATCHED THEN INSERT (group_id, max_index) "+ - "VALUES (src.group_id, 1);", tableName), - groupID) - default: - return fmt.Errorf("database type not supported") - } - return err -} - var ( // ErrResouceOutdated represents an error when request resource outdated ErrResouceOutdated = errors.New("resource outdated") @@ -59,53 +29,85 @@ const ( MaxDupIndexAttempts = 3 ) -// GetNextResourceIndex retried 3 times to generate a resource index -func GetNextResourceIndex(tableName string, groupID int64) (int64, error) { - for i := 0; i < MaxDupIndexAttempts; i++ { - idx, err := getNextResourceIndex(tableName, groupID) - if err == ErrResouceOutdated { - continue - } - if err != nil { - return 0, err - } - return idx, nil +// SyncMaxResourceIndex sync the max index with the resource +func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxIndex int64) (err error) { + e := GetEngine(ctx) + + // try to update the max_index and acquire the write-lock for the record + res, err := e.Exec(fmt.Sprintf("UPDATE %s SET max_index=? WHERE group_id=? AND max_index