Skip to content

Commit e811eb7

Browse files
authored
ddl: fix canceling model.ActionRebaseAutoID and model.ActionShardRowID ddl jobs (#9226) (#9506)
1 parent 41747a7 commit e811eb7

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

ddl/ddl_worker_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ func checkCancelState(txn kv.Transaction, job *model.Job, test *testCancelJob) e
303303
// If the action is adding index and the state is writing reorganization, it wants to test the case of cancelling the job when backfilling indexes.
304304
// When the job satisfies this case of addIndexFirstReorg, the worker hasn't started to backfill indexes.
305305
if test.cancelState == job.SchemaState && !addIndexFirstReorg {
306-
if job.SchemaState == model.StateNone && job.State != model.JobStateDone && job.Type != model.ActionCreateTable && job.Type != model.ActionCreateSchema {
307-
// If the schema state is none, we only test the job is finished.
306+
if job.SchemaState == model.StateNone && job.State != model.JobStateDone && job.Type != model.ActionCreateTable && job.Type != model.ActionCreateSchema && job.Type != model.ActionRebaseAutoID {
307+
// If the schema state is none and is not equal to model.JobStateDone, we only test the job is finished.
308+
// Unless the job is model.ActionCreateTable, model.ActionCreateSchema, model.ActionRebaseAutoID, we do the cancel anyway.
308309
} else {
309310
errs, err := admin.CancelJobs(txn, test.jobIDs)
310311
if err != nil {
@@ -353,6 +354,8 @@ func buildCancelJobTests(firstID int64) []testCancelJob {
353354
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 13}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 13)}, cancelState: model.StateDeleteOnly, ddlRetErr: err},
354355
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 14}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 14)}, cancelState: model.StateWriteOnly, ddlRetErr: err},
355356
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 15}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 15)}, cancelState: model.StateWriteReorganization, ddlRetErr: err},
357+
{act: model.ActionRebaseAutoID, jobIDs: []int64{firstID + 16}, cancelRetErrs: noErrs, cancelState: model.StateNone, ddlRetErr: err},
358+
{act: model.ActionShardRowID, jobIDs: []int64{firstID + 17}, cancelRetErrs: noErrs, cancelState: model.StateNone, ddlRetErr: err},
356359
}
357360

358361
return tests
@@ -407,6 +410,11 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
407410
ctx := testNewContext(d)
408411
err := ctx.NewTxn()
409412
c.Assert(err, IsNil)
413+
tableAutoID := int64(100)
414+
shardRowIDBits := uint64(5)
415+
tblInfo.AutoIncID = tableAutoID
416+
tblInfo.ShardRowIDBits = shardRowIDBits
417+
410418
job := testCreateTable(c, ctx, d, dbInfo, tblInfo)
411419
// insert t values (1, 2, 3, 4, 5);
412420
originTable := testGetTable(c, d, dbInfo.ID, tblInfo.ID)
@@ -424,7 +432,7 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
424432
tests := buildCancelJobTests(firstJobID)
425433
var checkErr error
426434
var test *testCancelJob
427-
tc.onJobUpdated = func(job *model.Job) {
435+
hookCancelFunc := func(job *model.Job) {
428436
if job.State == model.JobStateSynced || job.State == model.JobStateCancelled || job.State == model.JobStateCancelling {
429437
return
430438
}
@@ -455,6 +463,8 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
455463
return
456464
}
457465
}
466+
tc.onJobUpdated = hookCancelFunc
467+
tc.onJobRunBefore = hookCancelFunc
458468
d.SetHook(tc)
459469

460470
// for adding index
@@ -553,6 +563,22 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
553563
testDropColumn(c, ctx, d, dbInfo, tblInfo, dropColName, false)
554564
c.Check(errors.ErrorStack(checkErr), Equals, "")
555565
s.checkCancelDropColumn(c, d, dbInfo.ID, tblInfo.ID, dropColName, true)
566+
567+
// cancel rebase auto id
568+
test = &tests[13]
569+
rebaseIDArgs := []interface{}{int64(200)}
570+
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo.ID, model.ActionRebaseAutoID, rebaseIDArgs, &cancelState)
571+
c.Check(errors.ErrorStack(checkErr), Equals, "")
572+
changedTable := testGetTable(c, d, dbInfo.ID, tblInfo.ID)
573+
c.Assert(changedTable.Meta().AutoIncID, Equals, tableAutoID)
574+
575+
// cancel shard bits
576+
test = &tests[14]
577+
shardRowIDArgs := []interface{}{uint64(7)}
578+
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo.ID, model.ActionRebaseAutoID, shardRowIDArgs, &cancelState)
579+
c.Check(errors.ErrorStack(checkErr), Equals, "")
580+
changedTable = testGetTable(c, d, dbInfo.ID, tblInfo.ID)
581+
c.Assert(changedTable.Meta().ShardRowIDBits, Equals, shardRowIDBits)
556582
}
557583

558584
func (s *testDDLSuite) TestIgnorableSpec(c *C) {

ddl/rollingback.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ func cancelOnlyNotHandledJob(job *model.Job) (ver int64, err error) {
8888
return ver, nil
8989
}
9090

91+
func rollingbackRebaseAutoID(t *meta.Meta, job *model.Job) (ver int64, err error) {
92+
return cancelOnlyNotHandledJob(job)
93+
}
94+
95+
func rollingbackShardRowID(t *meta.Meta, job *model.Job) (ver int64, err error) {
96+
return cancelOnlyNotHandledJob(job)
97+
}
98+
9199
func rollingbackAddColumn(t *meta.Meta, job *model.Job) (ver int64, err error) {
92100
job.State = model.JobStateRollingback
93101
col := &model.ColumnInfo{}
@@ -266,6 +274,10 @@ func convertJob2RollbackJob(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job)
266274
ver, err = rollingbackTruncateTable(t, job)
267275
case model.ActionDropIndex:
268276
ver, err = rollingbackDropIndex(t, job)
277+
case model.ActionRebaseAutoID:
278+
ver, err = rollingbackRebaseAutoID(t, job)
279+
case model.ActionShardRowID:
280+
ver, err = rollingbackShardRowID(t, job)
269281
case model.ActionDropTable, model.ActionDropSchema:
270282
job.State = model.JobStateRollingback
271283
default:

util/admin/admin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func isJobRollbackable(job *model.Job, id int64) error {
107107
job.SchemaState == model.StateDeleteReorganization {
108108
return ErrCannotCancelDDLJob.GenWithStackByArgs(id)
109109
}
110+
case model.ActionRebaseAutoID, model.ActionShardRowID:
111+
if job.SchemaState != model.StateNone {
112+
return ErrCannotCancelDDLJob.GenWithStackByArgs(id)
113+
}
110114
case model.ActionDropSchema, model.ActionDropTable:
111115
// To simplify the rollback logic, cannot be canceled in the following states.
112116
if job.SchemaState == model.StateWriteOnly ||

0 commit comments

Comments
 (0)