Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/admin: fix the error of "invalid list index" when cancel ddl jobs (#12402) #12681

Merged
merged 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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