Skip to content

Commit

Permalink
ddl: fix bug of run ddl job error but actually take effect (#17341) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Jun 3, 2020
1 parent cca6fdb commit ffccd60
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,15 @@ func (w *worker) handleDDLJobQueue(d *ddlCtx) error {
err = w.finishDDLJob(t, job)
return errors.Trace(err)
}
if runJobErr != nil && !job.IsRollingback() && !job.IsRollbackDone() {
// If the running job meets an error
// and the job state is rolling back, it means that we have already handled this error.
// Some DDL jobs (such as adding indexes) may need to update the table info and the schema version,
// then shouldn't discard the KV modification.
// And the job state is rollback done, it means the job was already finished, also shouldn't discard too.
// Otherwise, we should discard the KV modification when running job.
txn.Reset()
}
err = w.updateDDLJob(t, job, runJobErr != nil)
if err = w.handleUpdateJobError(t, job, err); err != nil {
return errors.Trace(err)
Expand Down
20 changes: 20 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
ddlutil "github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -777,6 +778,25 @@ func (s *testSerialSuite) TestCancelJobByErrorCountLimit(c *C) {
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job")
}

func (s *testSerialSuite) TestTruncateTableUpdateSchemaVersionErr(c *C) {
tk := testkit.NewTestKit(c, s.store)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/ddl/mockTruncateTableUpdateVersionError", `return(true)`), IsNil)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")

tk.MustExec("set @@global.tidb_ddl_error_count_limit = 5")
err := ddlutil.LoadDDLVars(tk.Se)
c.Assert(err, IsNil)

tk.MustExec("create table t (a int)")
_, err = tk.Exec("truncate table t")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job")
// Disable fail point.
c.Assert(failpoint.Disable("github.com/pingcap/tidb/ddl/mockTruncateTableUpdateVersionError"), IsNil)
tk.MustExec("truncate table t")
}

func (s *testSerialSuite) TestCanceledJobTakeTime(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
6 changes: 6 additions & 0 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro
return ver, errors.Trace(err)
}

failpoint.Inject("mockTruncateTableUpdateVersionError", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(ver, errors.New("mock update version error"))
}
})

ver, err = updateSchemaVersion(t, job)
if err != nil {
return ver, errors.Trace(err)
Expand Down

0 comments on commit ffccd60

Please sign in to comment.