Skip to content

Commit

Permalink
ddl: remove wait time after canceling a job (#9768)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and zz-jason committed Mar 19, 2019
1 parent 66e639e commit 346009f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
30 changes: 30 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4595,3 +4595,33 @@ func (s *testDBSuite) TestModifyColumnCharset(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

}

func (s *testDBSuite) TestCanceledJobTakeTime(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_cjtt(a int)")

hook := &ddl.TestDDLCallback{}
once := sync.Once{}
hook.OnJobUpdatedExported = func(job *model.Job) {
once.Do(func() {
err := kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
return t.DropTable(job.SchemaID, job.TableID, true)
})
c.Assert(err, IsNil)
})
}
s.dom.DDL().(ddl.DDLForTest).SetHook(hook)

originalWT := ddl.WaitTimeWhenErrorOccured
ddl.WaitTimeWhenErrorOccured = 1 * time.Second
defer func() { ddl.WaitTimeWhenErrorOccured = originalWT }()
startTime := time.Now()
s.testErrorCode(c, "alter table t_cjtt add column b int", mysql.ErrNoSuchTable)
sub := time.Since(startTime)
c.Assert(sub, Less, ddl.WaitTimeWhenErrorOccured)

hook = &ddl.TestDDLCallback{}
s.dom.DDL().(ddl.DDLForTest).SetHook(hook)
}
16 changes: 8 additions & 8 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (w *worker) handleDDLJobQueue(d *ddlCtx) error {
// If the job is done or still running or rolling back, we will wait 2 * lease time to guarantee other servers to update
// the newest schema.
w.waitSchemaChanged(nil, d, waitTime, schemaVer, job)
if job.IsSynced() {
if job.IsSynced() || job.IsCancelled() {
asyncNotify(d.ddlJobDoneCh)
}
}
Expand Down Expand Up @@ -519,15 +519,15 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64,

// Save errors in job, so that others can know errors happened.
if err != nil {
// If job is not cancelled, we should log this error.
if job.State != model.JobStateCancelled {
log.Errorf("[ddl-%s] run DDL job err %v", w, errors.ErrorStack(err))
} else {
log.Infof("[ddl-%s] the DDL job is normal to cancel because %v", w, errors.ErrorStack(err))
}

job.Error = toTError(err)
job.ErrorCount++

// If job is cancelled, we shouldn't return this error.
if job.State == model.JobStateCancelled {
log.Infof("[ddl-%s] the DDL job is normal to cancel because %v", w, errors.ErrorStack(err))
return ver, nil
}
log.Errorf("[ddl-%s] run DDL job err %v", w, errors.ErrorStack(err))
}
return
}
Expand Down

0 comments on commit 346009f

Please sign in to comment.