Skip to content

Commit

Permalink
cherry-pick 57945
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Dec 12, 2024
1 parent 3cd6e2a commit 39c261c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 16 additions & 0 deletions pkg/ddl/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/pkg/ddl/util/callback"
"github.com/pingcap/tidb/pkg/errno"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/store/mockstore"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/testkit/external"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -350,3 +351,18 @@ func TestCancelForAddUniqueIndex(t *testing.T) {
tbl = external.GetTableByName(t, tk, "test", "t")
require.Equal(t, 0, len(tbl.Meta().Indices))
}

func TestSubmitJobAfterDDLIsClosed(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t, mockstore.WithStoreType(mockstore.EmbedUnistore))
tk := testkit.NewTestKit(t, store)

var ddlErr error
err := failpoint.EnableCall("github.com/pingcap/tidb/pkg/ddl/afterDDLCloseCancel", func() {
ddlErr = tk.ExecToErr("create database test2;")
})
require.NoError(t, err)
err = dom.DDL().Stop()
require.NoError(t, err)
require.Error(t, ddlErr)
require.Equal(t, "context canceled", ddlErr.Error())
}
15 changes: 10 additions & 5 deletions pkg/ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ func (d *ddl) close() {

startTime := time.Now()
d.cancel()
failpoint.InjectCall("afterDDLCloseCancel")
d.wg.Wait()
d.ownerManager.Cancel()
d.schemaSyncer.Close()
Expand Down Expand Up @@ -1030,7 +1031,7 @@ func setDDLJobQuery(ctx sessionctx.Context, job *model.Job) {
// - nil: found in history DDL job and no job error
// - context.Cancel: job has been sent to worker, but not found in history DDL job before cancel
// - other: found in history DDL job and return that job error
func (d *ddl) DoDDLJob(ctx sessionctx.Context, job *model.Job) error {
func (d *ddl) DoDDLJob(ctx sessionctx.Context, job *model.Job) (err error) {
job.TraceInfo = &model.TraceInfo{
ConnectionID: ctx.GetSessionVars().ConnectionID,
SessionAlias: ctx.GetSessionVars().SessionAlias,
Expand Down Expand Up @@ -1058,10 +1059,14 @@ func (d *ddl) DoDDLJob(ctx sessionctx.Context, job *model.Job) error {
})

// 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)
select {
case err := <-task.err:
if err != nil {
// The transaction of enqueuing job is failed.
return errors.Trace(err)
}
case <-d.ctx.Done():
return d.ctx.Err()
}

sessVars := ctx.GetSessionVars()
Expand Down

0 comments on commit 39c261c

Please sign in to comment.