From 474aed53cf08cd2388c2ef8e027918b345cdf7d3 Mon Sep 17 00:00:00 2001 From: crazycs Date: Wed, 30 Oct 2024 17:15:50 +0800 Subject: [PATCH] ddl: fix issue that recover table by job id may cause panic (#56965) close pingcap/tidb#55113 --- pkg/executor/ddl.go | 2 +- pkg/executor/recover_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/executor/ddl.go b/pkg/executor/ddl.go index ab6b7019a61ec..24612996cf497 100644 --- a/pkg/executor/ddl.go +++ b/pkg/executor/ddl.go @@ -414,7 +414,7 @@ func (e *DDLExec) executeRecoverTable(s *ast.RecoverTableStmt) error { // Check the table ID was not exists. tbl, ok := dom.InfoSchema().TableByID(context.Background(), tblInfo.ID) if ok { - return infoschema.ErrTableExists.GenWithStack("Table '%-.192s' already been recover to '%-.192s', can't be recover repeatedly", s.Table.Name.O, tbl.Meta().Name.O) + return infoschema.ErrTableExists.GenWithStack("Table '%-.192s' already been recover to '%-.192s', can't be recover repeatedly", tblInfo.Name.O, tbl.Meta().Name.O) } m := domain.GetDomain(e.Ctx()).GetSnapshotMeta(job.StartTS) diff --git a/pkg/executor/recover_test.go b/pkg/executor/recover_test.go index b8db529dd7544..1aefb857a1bc7 100644 --- a/pkg/executor/recover_test.go +++ b/pkg/executor/recover_test.go @@ -17,6 +17,7 @@ package executor_test import ( "context" "fmt" + "strconv" "sync" "testing" "time" @@ -26,6 +27,7 @@ import ( "github.com/pingcap/tidb/pkg/errno" "github.com/pingcap/tidb/pkg/infoschema" "github.com/pingcap/tidb/pkg/kv" + "github.com/pingcap/tidb/pkg/meta/model" "github.com/pingcap/tidb/pkg/parser/auth" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/store/mockstore" @@ -130,6 +132,21 @@ func TestRecoverTable(t *testing.T) { err = tk.ExecToErr("recover table t_recover") require.True(t, infoschema.ErrTableExists.Equal(err)) + // Test drop table failed and then recover the table should also be failed. + tk.MustExec("drop table if exists t_recover2") + tk.MustExec("create table t_recover2 (a int);") + jobID := int64(0) + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) { + if job.Type == model.ActionDropTable && jobID == 0 { + jobID = job.ID + } + }) + tk.MustExec("drop table t_recover2") + tk.MustExec("recover table by job " + strconv.Itoa(int(jobID))) + err = tk.ExecToErr("recover table by job " + strconv.Itoa(int(jobID))) + require.Error(t, err) + require.Equal(t, "[schema:1050]Table 't_recover2' already been recover to 't_recover2', can't be recover repeatedly", err.Error()) + gcEnable, err := gcutil.CheckGCEnable(tk.Session()) require.NoError(t, err) require.False(t, gcEnable)