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

ddl: handle the error from addBatchDDLJobs() correctly #30401

Merged
merged 3 commits into from
Dec 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ func (d *ddl) doDDLJob(ctx sessionctx.Context, job *model.Job) error {
d.limitJobCh <- task
// worker should restart to continue handling tasks in limitJobCh, and send back through task.err
err := <-task.err
if err != nil {
// The transaction of enqueuing job is failed.
return errors.Trace(err)
}

ctx.GetSessionVars().StmtCtx.IsDDLJobInQueue = true

Expand Down
11 changes: 10 additions & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ func (d *ddl) addBatchDDLJobs(tasks []*limitJobTask) {
return errors.Trace(err)
}
}
failpoint.Inject("mockAddBatchDDLJobsErr", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(errors.Errorf("mockAddBatchDDLJobsErr"))
}
})
return nil
})
var jobs string
Expand All @@ -310,7 +315,11 @@ func (d *ddl) addBatchDDLJobs(tasks []*limitJobTask) {
metrics.DDLWorkerHistogram.WithLabelValues(metrics.WorkerAddDDLJob, task.job.Type.String(),
metrics.RetLabel(err)).Observe(time.Since(startTime).Seconds())
}
logutil.BgLogger().Info("[ddl] add DDL jobs", zap.Int("batch count", len(tasks)), zap.String("jobs", jobs))
if err != nil {
logutil.BgLogger().Warn("[ddl] add DDL jobs failed", zap.String("jobs", jobs), zap.Error(err))
} else {
logutil.BgLogger().Info("[ddl] add DDL jobs", zap.Int("batch count", len(tasks)), zap.String("jobs", jobs))
}
}

// getHistoryDDLJob gets a DDL job with job's ID from history queue.
Expand Down
26 changes: 26 additions & 0 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,32 @@ func (s *testDDLSuite) TestColumnError(c *C) {
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionDropColumns, []interface{}{[]model.CIStr{model.NewCIStr("c5"), model.NewCIStr("c6")}, make([]bool, 2)}, ctx, d)
}

func (s *testDDLSerialSuite) TestAddBatchJobError(c *C) {
store := testCreateStore(c, "test_add_batch_job_error")
defer func() {
err := store.Close()
c.Assert(err, IsNil)
}()
d, err := testNewDDLAndStart(
context.Background(),
WithStore(store),
WithLease(testLease),
)
c.Assert(err, IsNil)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
}()
ctx := testNewContext(d)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/ddl/mockAddBatchDDLJobsErr", `return(true)`), IsNil)
// Test the job runner should not hang forever.
job := &model.Job{SchemaID: 1, TableID: 1}
err = d.doDDLJob(ctx, job)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "mockAddBatchDDLJobsErr")
c.Assert(failpoint.Disable("github.com/pingcap/tidb/ddl/mockAddBatchDDLJobsErr"), IsNil)
}

func testCheckOwner(c *C, d *ddl, expectedVal bool) {
c.Assert(d.isOwner(), Equals, expectedVal)
}
Expand Down