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) #53581

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
22 changes: 22 additions & 0 deletions pkg/ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ func (w *JobContext) setDDLLabelForDiagnosis(jobType model.ActionType) {
}

func (w *worker) HandleJobDone(d *ddlCtx, job *model.Job, t *meta.Meta) error {
if err := w.checkBeforeCommit(); err != nil {
return err
}
err := w.finishDDLJob(t, job)
if err != nil {
w.sess.Rollback()
Expand Down Expand Up @@ -819,6 +822,10 @@ func (w *worker) HandleDDLJobTable(d *ddlCtx, job *model.Job) (int64, error) {
return 0, err
}

if err = w.checkBeforeCommit(); err != nil {
d.unlockSchemaVersion(job.ID)
return 0, err
}
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 @@ -866,6 +873,21 @@ func (w *worker) HandleDDLJobTable(d *ddlCtx, job *model.Job) (int64, error) {
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
}

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

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