From 700a4097631642249a21f231a6dfdd863651ca53 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 11 Nov 2024 17:45:17 +0800 Subject: [PATCH] ddl: add context cancel check before commit (#53134) (#57020) close pingcap/tidb#52805 --- ddl/ddl_worker.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index b442c128a010b..3761d1782ed58 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -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() @@ -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. @@ -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