Skip to content

Commit

Permalink
ddl: fix canceling model.ActionRebaseAutoID and model.ActionShardRowI…
Browse files Browse the repository at this point in the history
…D ddl jobs (#9226) (#9506)
  • Loading branch information
winkyao authored Mar 1, 2019
1 parent 41747a7 commit e811eb7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
32 changes: 29 additions & 3 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ func checkCancelState(txn kv.Transaction, job *model.Job, test *testCancelJob) e
// 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.
// When the job satisfies this case of addIndexFirstReorg, the worker hasn't started to backfill indexes.
if test.cancelState == job.SchemaState && !addIndexFirstReorg {
if job.SchemaState == model.StateNone && job.State != model.JobStateDone && job.Type != model.ActionCreateTable && job.Type != model.ActionCreateSchema {
// If the schema state is none, we only test the job is finished.
if job.SchemaState == model.StateNone && job.State != model.JobStateDone && job.Type != model.ActionCreateTable && job.Type != model.ActionCreateSchema && job.Type != model.ActionRebaseAutoID {
// If the schema state is none and is not equal to model.JobStateDone, we only test the job is finished.
// Unless the job is model.ActionCreateTable, model.ActionCreateSchema, model.ActionRebaseAutoID, we do the cancel anyway.
} else {
errs, err := admin.CancelJobs(txn, test.jobIDs)
if err != nil {
Expand Down Expand Up @@ -353,6 +354,8 @@ func buildCancelJobTests(firstID int64) []testCancelJob {
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 13}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 13)}, cancelState: model.StateDeleteOnly, ddlRetErr: err},
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 14}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 14)}, cancelState: model.StateWriteOnly, ddlRetErr: err},
{act: model.ActionDropColumn, jobIDs: []int64{firstID + 15}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 15)}, cancelState: model.StateWriteReorganization, ddlRetErr: err},
{act: model.ActionRebaseAutoID, jobIDs: []int64{firstID + 16}, cancelRetErrs: noErrs, cancelState: model.StateNone, ddlRetErr: err},
{act: model.ActionShardRowID, jobIDs: []int64{firstID + 17}, cancelRetErrs: noErrs, cancelState: model.StateNone, ddlRetErr: err},
}

return tests
Expand Down Expand Up @@ -407,6 +410,11 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
ctx := testNewContext(d)
err := ctx.NewTxn()
c.Assert(err, IsNil)
tableAutoID := int64(100)
shardRowIDBits := uint64(5)
tblInfo.AutoIncID = tableAutoID
tblInfo.ShardRowIDBits = shardRowIDBits

job := testCreateTable(c, ctx, d, dbInfo, tblInfo)
// insert t values (1, 2, 3, 4, 5);
originTable := testGetTable(c, d, dbInfo.ID, tblInfo.ID)
Expand All @@ -424,7 +432,7 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
tests := buildCancelJobTests(firstJobID)
var checkErr error
var test *testCancelJob
tc.onJobUpdated = func(job *model.Job) {
hookCancelFunc := func(job *model.Job) {
if job.State == model.JobStateSynced || job.State == model.JobStateCancelled || job.State == model.JobStateCancelling {
return
}
Expand Down Expand Up @@ -455,6 +463,8 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
return
}
}
tc.onJobUpdated = hookCancelFunc
tc.onJobRunBefore = hookCancelFunc
d.SetHook(tc)

// for adding index
Expand Down Expand Up @@ -553,6 +563,22 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
testDropColumn(c, ctx, d, dbInfo, tblInfo, dropColName, false)
c.Check(errors.ErrorStack(checkErr), Equals, "")
s.checkCancelDropColumn(c, d, dbInfo.ID, tblInfo.ID, dropColName, true)

// cancel rebase auto id
test = &tests[13]
rebaseIDArgs := []interface{}{int64(200)}
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo.ID, model.ActionRebaseAutoID, rebaseIDArgs, &cancelState)
c.Check(errors.ErrorStack(checkErr), Equals, "")
changedTable := testGetTable(c, d, dbInfo.ID, tblInfo.ID)
c.Assert(changedTable.Meta().AutoIncID, Equals, tableAutoID)

// cancel shard bits
test = &tests[14]
shardRowIDArgs := []interface{}{uint64(7)}
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo.ID, model.ActionRebaseAutoID, shardRowIDArgs, &cancelState)
c.Check(errors.ErrorStack(checkErr), Equals, "")
changedTable = testGetTable(c, d, dbInfo.ID, tblInfo.ID)
c.Assert(changedTable.Meta().ShardRowIDBits, Equals, shardRowIDBits)
}

func (s *testDDLSuite) TestIgnorableSpec(c *C) {
Expand Down
12 changes: 12 additions & 0 deletions ddl/rollingback.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func cancelOnlyNotHandledJob(job *model.Job) (ver int64, err error) {
return ver, nil
}

func rollingbackRebaseAutoID(t *meta.Meta, job *model.Job) (ver int64, err error) {
return cancelOnlyNotHandledJob(job)
}

func rollingbackShardRowID(t *meta.Meta, job *model.Job) (ver int64, err error) {
return cancelOnlyNotHandledJob(job)
}

func rollingbackAddColumn(t *meta.Meta, job *model.Job) (ver int64, err error) {
job.State = model.JobStateRollingback
col := &model.ColumnInfo{}
Expand Down Expand Up @@ -266,6 +274,10 @@ func convertJob2RollbackJob(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job)
ver, err = rollingbackTruncateTable(t, job)
case model.ActionDropIndex:
ver, err = rollingbackDropIndex(t, job)
case model.ActionRebaseAutoID:
ver, err = rollingbackRebaseAutoID(t, job)
case model.ActionShardRowID:
ver, err = rollingbackShardRowID(t, job)
case model.ActionDropTable, model.ActionDropSchema:
job.State = model.JobStateRollingback
default:
Expand Down
4 changes: 4 additions & 0 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func isJobRollbackable(job *model.Job, id int64) error {
job.SchemaState == model.StateDeleteReorganization {
return ErrCannotCancelDDLJob.GenWithStackByArgs(id)
}
case model.ActionRebaseAutoID, model.ActionShardRowID:
if job.SchemaState != model.StateNone {
return ErrCannotCancelDDLJob.GenWithStackByArgs(id)
}
case model.ActionDropSchema, model.ActionDropTable:
// To simplify the rollback logic, cannot be canceled in the following states.
if job.SchemaState == model.StateWriteOnly ||
Expand Down

0 comments on commit e811eb7

Please sign in to comment.