diff --git a/meta/meta.go b/meta/meta.go index ce2a4738c4e33..d5f9c77d36aee 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -485,8 +485,13 @@ func (m *Meta) enQueueDDLJob(key []byte, job *model.Job) error { } // EnQueueDDLJob adds a DDL job to the list. -func (m *Meta) EnQueueDDLJob(job *model.Job) error { - return m.enQueueDDLJob(m.jobListKey, job) +func (m *Meta) EnQueueDDLJob(job *model.Job, jobListKeys ...JobListKeyType) error { + listKey := m.jobListKey + if len(jobListKeys) != 0 { + listKey = jobListKeys[0] + } + + return m.enQueueDDLJob(listKey, job) } func (m *Meta) deQueueDDLJob(key []byte) (*model.Job, error) { diff --git a/util/admin/admin.go b/util/admin/admin.go index 96fe43452c5ff..88a521710fb3d 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -124,13 +124,18 @@ func CancelJobs(txn kv.Transaction, ids []int64) ([]error, error) { return nil, nil } - jobs, err := GetDDLJobs(txn) + errs := make([]error, len(ids)) + t := meta.NewMeta(txn) + generalJobs, err := getDDLJobsInQueue(t, meta.DefaultJobListKey) + if err != nil { + return nil, errors.Trace(err) + } + addIdxJobs, err := getDDLJobsInQueue(t, meta.AddIndexJobListKey) if err != nil { return nil, errors.Trace(err) } + jobs := append(generalJobs, addIdxJobs...) - errs := make([]error, len(ids)) - t := meta.NewMeta(txn) for i, id := range ids { found := false for j, job := range jobs { @@ -163,7 +168,8 @@ func CancelJobs(txn kv.Transaction, ids []int64) ([]error, error) { continue } if job.Type == model.ActionAddIndex { - err = t.UpdateDDLJob(int64(j), job, true, meta.AddIndexJobListKey) + offset := int64(j - len(generalJobs)) + err = t.UpdateDDLJob(offset, job, true, meta.AddIndexJobListKey) } else { err = t.UpdateDDLJob(int64(j), job, true) } diff --git a/util/admin/admin_test.go b/util/admin/admin_test.go index b30d63aa9ff34..5ec4aab505e1b 100644 --- a/util/admin/admin_test.go +++ b/util/admin/admin_test.go @@ -220,6 +220,38 @@ func (s *testSuite) TestCancelJobs(c *C) { c.Assert(err, IsNil) } + // When both types of jobs exist in the DDL queue, + // we first cancel the job with a larger ID. + job := &model.Job{ + ID: 1000, + SchemaID: 1, + TableID: 2, + Type: model.ActionAddIndex, + } + job1 := &model.Job{ + ID: 1001, + SchemaID: 1, + TableID: 2, + Type: model.ActionAddColumn, + } + job2 := &model.Job{ + ID: 1002, + SchemaID: 1, + TableID: 2, + Type: model.ActionAddIndex, + } + err = t.EnQueueDDLJob(job, meta.AddIndexJobListKey) + c.Assert(err, IsNil) + err = t.EnQueueDDLJob(job1) + c.Assert(err, IsNil) + err = t.EnQueueDDLJob(job2, meta.AddIndexJobListKey) + c.Assert(err, IsNil) + errs, err = CancelJobs(txn, []int64{job1.ID, job.ID, job2.ID}) + c.Assert(err, IsNil) + for _, err := range errs { + c.Assert(err, IsNil) + } + err = txn.Rollback() c.Assert(err, IsNil) }