Skip to content
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

ddl: add context cancel check before commit (#53134) #57020

Merged
Merged
Changes from all 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
23 changes: 23 additions & 0 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,9 @@
}

func (w *worker) HandleJobDone(d *ddlCtx, job *model.Job, t *meta.Meta) error {
if err := w.checkBeforeCommit(); err != nil {
return err
}

Check warning on line 710 in ddl/ddl_worker.go

View check run for this annotation

Codecov / codecov/patch

ddl/ddl_worker.go#L709-L710

Added lines #L709 - L710 were not covered by tests
err := w.finishDDLJob(t, job)
if err != nil {
w.sess.Rollback()
Expand Down Expand Up @@ -799,6 +802,11 @@
return 0, err
}

if err = w.checkBeforeCommit(); err != nil {
d.unlockSchemaVersion(job.ID)
return 0, err
}

Check warning on line 808 in ddl/ddl_worker.go

View check run for this annotation

Codecov / codecov/patch

ddl/ddl_worker.go#L806-L808

Added lines #L806 - L808 were not covered by tests

if runJobErr != nil && !job.IsRollingback() && !job.IsRollbackDone() {
// If the running job meets an error
// and the job state is rolling back, it means that we have already handled this error.
Expand Down Expand Up @@ -852,6 +860,21 @@
return schemaVer, nil
}

func (w *worker) checkBeforeCommit() error {
if !w.ddlCtx.isOwner() {
// Since this TiDB instance is not a DDL owner anymore,
// it should not commit any transaction.
w.sess.Rollback()
return dbterror.ErrNotOwner
}

Check warning on line 869 in ddl/ddl_worker.go

View check run for this annotation

Codecov / codecov/patch

ddl/ddl_worker.go#L865-L869

Added lines #L865 - L869 were not covered by tests

if err := w.ctx.Err(); err != nil {
// The worker context is canceled, it should not commit any transaction.
return err
}

Check warning on line 874 in ddl/ddl_worker.go

View check run for this annotation

Codecov / codecov/patch

ddl/ddl_worker.go#L872-L874

Added lines #L872 - L874 were not covered by tests
return nil
}

func (w *JobContext) getResourceGroupTaggerForTopSQL() tikvrpc.ResourceGroupTagger {
if !topsqlstate.TopSQLEnabled() || w.cacheDigest == nil {
return nil
Expand Down