Skip to content

Commit

Permalink
ddl: fix issue that recover table by job id may cause panic (#56965)
Browse files Browse the repository at this point in the history
close #55113
  • Loading branch information
crazycs520 authored Oct 30, 2024
1 parent 79cdf84 commit 474aed5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions pkg/executor/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package executor_test
import (
"context"
"fmt"
"strconv"
"sync"
"testing"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 474aed5

Please # to comment.