Skip to content

Commit

Permalink
util/admin: fix the error of "invalid list index" when cancel ddl jobs (
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and sre-bot committed Oct 21, 2019
1 parent e584f43 commit 355e19c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
9 changes: 7 additions & 2 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
32 changes: 32 additions & 0 deletions util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 355e19c

Please sign in to comment.