Skip to content

Revert index when error occur on issue/PR creation #21362

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions models/db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ func UpsertResourceIndex(ctx context.Context, tableName string, groupID int64) (
return err
}

// UpsertDecrResourceIndex the function will not return until it acquires the lock or receives an error.
func UpsertDecrResourceIndex(ctx context.Context, tableName string, groupID, prevIdx int64) (err error) {
// An atomic UPSERT operation (INSERT/UPDATE) is the only operation
// that ensures that the key is actually locked.
// It will try to decrease the max_index, however if max_index is already higher,
// then don't try to decrease the index value.
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 = CASE WHEN %s.max_index = %d THEN %s.max_index-1 ELSE %s.max_index END",
tableName, tableName, prevIdx, 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 = CASE WHEN max_index = %d THEN max_index-1 ELSE max_index END", tableName, prevIdx),
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 = CASE WHEN target.max_index = %d THEN target.max_index-1 ELSE target.max_index END"+
"WHEN NOT MATCHED THEN INSERT (group_id, max_index) "+
"VALUES (src.group_id, 1);", tableName, prevIdx),
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")
Expand Down
19 changes: 19 additions & 0 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,19 +1077,38 @@ func NewIssue(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids
}
defer committer.Close()

// onFail will try to gracefully revert the update in the resource index.
onFail := func() error {
// Close commiter.
committer.Close()
// Try to revert the increase in resource index.
if err := db.UpsertDecrResourceIndex(db.DefaultContext, "issue_index", repo.ID, idx); err != nil {
return fmt.Errorf("UpsertDecrResourceIndex: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("UpsertDecrResourceIndex: %v", err)
return fmt.Errorf("UpsertDecrResourceIndex: %w", err)

}
return nil
}

if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Issue: issue,
LabelIDs: labelIDs,
Attachments: uuids,
}); err != nil {
if err := onFail(); err != nil {
return fmt.Errorf("couldn't gracefully handle error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("couldn't gracefully handle error: %v", err)
return fmt.Errorf("couldn't gracefully handle error: %w", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new code-style I haven't been aware of? Since when are we going to use the wrapping errors verb?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as I've learned: go seems to do some magic if you use %w instead of %v:
https://stackoverflow.com/a/61287626

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can wrap stuff my pr which tries to create a common hierarchy of errors will be a lot easier

}

if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
return err
}
return fmt.Errorf("newIssue: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap errors!

Suggested change
return fmt.Errorf("newIssue: %v", err)
return fmt.Errorf("newIssue: %w", err)

}

if err = committer.Commit(); err != nil {
if err := onFail(); err != nil {
return fmt.Errorf("couldn't gracefully handle error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("couldn't gracefully handle error: %v", err)
return fmt.Errorf("couldn't gracefully handle error: %w", err)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And wrap the rest of them too if possible please

}

return fmt.Errorf("Commit: %v", err)
}

Expand Down
22 changes: 22 additions & 0 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,27 @@ func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue
defer committer.Close()
ctx.WithContext(outerCtx)

// onFail will try to gracefully revert the update in the resource index.
onFail := func() error {
// Close commiter.
committer.Close()
// Try to revert the increase in resource index.
if err := db.UpsertDecrResourceIndex(outerCtx, "issue_index", repo.ID, idx); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that outerCtx isn't the correct thing here. In fact I'm certain it isn't.

db.DefaultContext is needed here.

return fmt.Errorf("UpsertDecrResourceIndex: %v", err)
}
return nil
}

if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Issue: issue,
LabelIDs: labelIDs,
Attachments: uuids,
IsPull: true,
}); err != nil {
if err := onFail(); err != nil {
return fmt.Errorf("couldn't gracefully handle error: %v", err)
}
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
return err
}
Expand All @@ -521,10 +535,18 @@ func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue
pr.BaseRepo = repo
pr.IssueID = issue.ID
if err = db.Insert(ctx, pr); err != nil {
if err := onFail(); err != nil {
return fmt.Errorf("couldn't gracefully handle error: %v", err)
}

return fmt.Errorf("insert pull repo: %v", err)
}

if err = committer.Commit(); err != nil {
if err := onFail(); err != nil {
return fmt.Errorf("couldn't gracefully handle error: %v", err)
}

return fmt.Errorf("Commit: %v", err)
}

Expand Down