Skip to content

Commit

Permalink
ddl: add context cancel check before commit (#53134) (#57020)
Browse files Browse the repository at this point in the history
close #52805
  • Loading branch information
ti-chi-bot authored Nov 11, 2024
1 parent 28496ed commit 700a409
Showing 1 changed file with 23 additions and 0 deletions.
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 *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 @@ -799,6 +802,11 @@ 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 @@ -852,6 +860,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

0 comments on commit 700a409

Please sign in to comment.