-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add a new table issue_index to store the max issue index so that issue could be deleted with no duplicated index #15599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0bdfac1
Add a new table issue_index to store the max issue index so that issu…
lunny a78e6cb
Fix pull index
lunny 8d3df3c
Add tests for concurrent creating issues
lunny 497da8e
Fix lint
lunny f3fcec4
Fix tests
lunny f60ac9d
Fix postgres test
lunny 3253f5d
Add test for migration v180
lunny f5ab5d9
Rename wrong test file name
lunny 8e4e1f5
Merge branch 'main' into lunny/improve_issue_index
6543 5bff46e
Merge branch 'main' into lunny/improve_issue_index
lafriks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
- | ||
group_id: 1 | ||
max_index: 5 | ||
- | ||
group_id: 2 | ||
max_index: 2 | ||
- | ||
group_id: 3 | ||
max_index: 2 | ||
- | ||
group_id: 10 | ||
max_index: 1 | ||
- | ||
group_id: 48 | ||
max_index: 1 | ||
- | ||
group_id: 42 | ||
max_index: 1 | ||
- | ||
group_id: 50 | ||
max_index: 1 | ||
- | ||
group_id: 51 | ||
max_index: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package models | ||
|
||
import ( | ||
"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. | ||
type ResourceIndex struct { | ||
GroupID int64 `xorm:"unique"` | ||
MaxIndex int64 `xorm:"index"` | ||
} | ||
|
||
// IssueIndex represents the issue index table | ||
type IssueIndex ResourceIndex | ||
|
||
// upsertResourceIndex the function will not return until it acquires the lock or receives an error. | ||
func upsertResourceIndex(e Engine, 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 = e.Exec(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 = e.Exec(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 = e.Exec(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 | ||
} | ||
|
||
var ( | ||
// ErrResouceOutdated represents an error when request resource outdated | ||
ErrResouceOutdated = errors.New("resource outdated") | ||
// ErrGetResourceIndexFailed represents an error when resource index retries 3 times | ||
ErrGetResourceIndexFailed = errors.New("get resource index failed") | ||
) | ||
|
||
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 | ||
} | ||
return 0, ErrGetResourceIndexFailed | ||
} | ||
|
||
// deleteResouceIndex delete resource index | ||
func deleteResouceIndex(e Engine, tableName string, groupID int64) error { | ||
_, err := e.Exec(fmt.Sprintf("DELETE FROM %s WHERE group_id=?", tableName), groupID) | ||
return err | ||
} | ||
|
||
// getNextResourceIndex return the next index | ||
func getNextResourceIndex(tableName string, groupID int64) (int64, error) { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
if err := sess.Begin(); err != nil { | ||
return 0, err | ||
} | ||
var preIdx int64 | ||
_, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if err := upsertResourceIndex(sess, tableName, groupID); err != nil { | ||
return 0, err | ||
} | ||
|
||
var curIdx int64 | ||
has, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ? AND max_index=?", tableName), groupID, preIdx+1).Get(&curIdx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if !has { | ||
return 0, ErrResouceOutdated | ||
} | ||
if err := sess.Commit(); err != nil { | ||
return 0, err | ||
} | ||
return curIdx, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package models | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResourceIndex(t *testing.T) { | ||
assert.NoError(t, PrepareTestDatabase()) | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < 100; i++ { | ||
wg.Add(1) | ||
go func(i int) { | ||
testInsertIssue(t, fmt.Sprintf("issue %d", i+1), "my issue", 0) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.