Skip to content

Commit

Permalink
ddl: check owner status before commit DDL changes (#50251)
Browse files Browse the repository at this point in the history
close #47105, close #50055
  • Loading branch information
tangenta authored Jan 11, 2024
1 parent e3bd0df commit b4ce5d0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ func (w *JobContext) setDDLLabelForDiagnosis(jobType model.ActionType) {
}

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

if err = w.checkOwnerBeforeCommit(); err != nil {
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 @@ -989,6 +996,16 @@ func (w *worker) HandleDDLJobTable(d *ddlCtx, job *model.Job) (int64, error) {
return schemaVer, nil
}

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

// HandleDDLJobV2 handles v2 ddl job.
// Compare with v1:
// 1. directly insert the job to history job table(incompatible with CDC).
Expand Down

0 comments on commit b4ce5d0

Please sign in to comment.