From 4d04865c5eb487d208e639d73ac39c7728f2c4f7 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 10:58:45 +0800 Subject: [PATCH 1/7] ddl: fix ExistsTableRow and add tests for skip reorg checks --- pkg/ddl/index.go | 4 +- pkg/ddl/reorg.go | 17 +++--- pkg/ddl/tests/indexmerge/merge_test.go | 76 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 824cedfc2a56f..2471231b5a2cc 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -1156,7 +1156,7 @@ func checkIfPhysicalTableIsEmpty( store kv.Storage, tbl table.PhysicalTable, ) bool { - hasRecord, err := ExistsTableRow(ctx, store, math.MaxInt64, tbl) + hasRecord, err := ExistsTableRow(ctx, store, math.MaxUint64, tbl) if err != nil { logutil.DDLLogger().Info("check if table is empty failed", zap.Error(err)) return false @@ -1337,6 +1337,7 @@ func doReorgWorkForCreateIndex( return false, ver, errors.Trace(err) } } else { + failpoint.InjectCall("afterCheckTableReorgCanSkip") logutil.DDLLogger().Info("table is empty, skipping reorg work", zap.Int64("jobID", job.ID), zap.String("table", tbl.Meta().Name.O)) @@ -1374,6 +1375,7 @@ func doReorgWorkForCreateIndex( return false, ver, err } } else { + failpoint.InjectCall("afterCheckTempIndexReorgCanSkip") logutil.DDLLogger().Info("temp index is empty, skipping reorg work", zap.Int64("jobID", job.ID), zap.String("table", tbl.Meta().Name.O)) diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index ff2fae066f2de..3c6b285729aad 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -770,19 +770,16 @@ func GetTableMaxHandle(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl // ExistsTableRow checks if there is at least one row in the specified table. // In case of an error during the operation, it returns false along with the error. func ExistsTableRow(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl table.PhysicalTable) (bool, error) { - handleCols := buildHandleCols(tbl) - result, err := buildOneRowTableScan(ctx, store, startTS, tbl, handleCols, 1, false) - if err != nil { - return false, errors.Trace(err) - } - defer terror.Call(result.Close) - - chk := chunk.New(getColumnsTypes(handleCols), 1, 1) - err = result.Next(ctx.ddlJobCtx, chk) + found := false + err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), startTS, nil, nil, + func(_ kv.Handle, rowKey kv.Key, _ []byte) (bool, error) { + found = true + return false, nil + }) if err != nil { return false, errors.Trace(err) } - return chk.NumRows() != 0, nil + return found, nil } func buildHandleCols(tbl table.PhysicalTable) []*model.ColumnInfo { diff --git a/pkg/ddl/tests/indexmerge/merge_test.go b/pkg/ddl/tests/indexmerge/merge_test.go index c808dd79cb18b..1ae25ce31bed2 100644 --- a/pkg/ddl/tests/indexmerge/merge_test.go +++ b/pkg/ddl/tests/indexmerge/merge_test.go @@ -857,3 +857,79 @@ func TestAddUniqueIndexFalsePositiveDuplicate(t *testing.T) { tk.MustExec("alter table t add unique index idx(b);") tk.MustExec("admin check table t;") } + +func TestAddIndexSkipReorgCheck(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (a int);") + + skipTableReorg := false + skipTempIdxReorg := false + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip", func() { + skipTableReorg = true + }) + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip", func() { + skipTempIdxReorg = true + }) + tk.MustExec("alter table t add index idx1(a);") + require.True(t, skipTableReorg) + require.True(t, skipTempIdxReorg) + + skipTableReorg = false + skipTempIdxReorg = false + tk.MustExec("insert into t values (1);") + tk.MustExec("alter table t add index idx2(a);") + require.False(t, skipTableReorg) + require.True(t, skipTempIdxReorg) + + skipTableReorg = false + skipTempIdxReorg = false + var runDML bool + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunAfter", func(job *model.Job) { + if t.Failed() || runDML { + return + } + switch job.SchemaState { + case model.StateWriteReorganization: + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + tk2.MustExec("insert into t values (2);") + runDML = true + } + }) + tk.MustExec("alter table t add index idx3(a);") + require.False(t, skipTableReorg) + require.False(t, skipTempIdxReorg) +} + +func TestAddIndexInsertAfterReorgSkipCheck(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (a int);") + err := failpoint.EnableCall("github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip", func() { + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + tk2.MustExec("insert into t values (1);") + }) + require.NoError(t, err) + tk.MustExec("alter table t add index idx(a);") + tk.MustQuery("select * from t;").Check(testkit.Rows("1")) + tk.MustExec("admin check table t;") + err = failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip") + require.NoError(t, err) + + tk.MustExec("truncate table t;") + err = failpoint.EnableCall("github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip", func() { + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + tk2.MustExec("insert into t values (2);") + }) + require.NoError(t, err) + tk.MustExec("alter table t add index idx2(a);") + tk.MustQuery("select * from t;").Check(testkit.Rows("2")) + tk.MustExec("admin check table t;") + err = failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip") + require.NoError(t, err) +} From 4073dfa912bd9a9cbe530f0a8f79f16f8d5b64e9 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 11:04:23 +0800 Subject: [PATCH 2/7] refine code --- pkg/ddl/index.go | 5 ++++- pkg/ddl/reorg.go | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 2471231b5a2cc..e1538d0fd11c3 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -70,6 +70,7 @@ import ( "github.com/pingcap/tidb/pkg/util/codec" "github.com/pingcap/tidb/pkg/util/dbterror" "github.com/pingcap/tidb/pkg/util/generatedexpr" + "github.com/pingcap/tidb/pkg/util/intest" tidblogutil "github.com/pingcap/tidb/pkg/util/logutil" decoder "github.com/pingcap/tidb/pkg/util/rowDecoder" "github.com/pingcap/tidb/pkg/util/size" @@ -1156,7 +1157,8 @@ func checkIfPhysicalTableIsEmpty( store kv.Storage, tbl table.PhysicalTable, ) bool { - hasRecord, err := ExistsTableRow(ctx, store, math.MaxUint64, tbl) + hasRecord, err := existsTableRow(ctx, store, tbl) + intest.Assert(err == nil) if err != nil { logutil.DDLLogger().Info("check if table is empty failed", zap.Error(err)) return false @@ -1231,6 +1233,7 @@ func checkIfTempIndexIsEmptyForPhysicalTable( foundKey = true return false, nil }) + intest.Assert(err == nil) if err != nil { logutil.DDLLogger().Info("check if temp index is empty failed", zap.Error(err)) return false diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index 3c6b285729aad..a3e17817fa775 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -19,6 +19,7 @@ import ( "context" "encoding/hex" "fmt" + "math" "math/rand" "strconv" "strings" @@ -767,11 +768,11 @@ func GetTableMaxHandle(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl return kv.IntHandle(row.GetInt64(0)), false, nil } -// ExistsTableRow checks if there is at least one row in the specified table. +// existsTableRow checks if there is at least one row in the specified table. // In case of an error during the operation, it returns false along with the error. -func ExistsTableRow(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl table.PhysicalTable) (bool, error) { +func existsTableRow(ctx *ReorgContext, store kv.Storage, tbl table.PhysicalTable) (bool, error) { found := false - err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), startTS, nil, nil, + err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), math.MaxUint64, nil, nil, func(_ kv.Handle, rowKey kv.Key, _ []byte) (bool, error) { found = true return false, nil From a7717671c547529f9c34883bef486717e63a41a8 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 11:09:11 +0800 Subject: [PATCH 3/7] refine test --- pkg/ddl/tests/indexmerge/merge_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/ddl/tests/indexmerge/merge_test.go b/pkg/ddl/tests/indexmerge/merge_test.go index 1ae25ce31bed2..acedd5e55b873 100644 --- a/pkg/ddl/tests/indexmerge/merge_test.go +++ b/pkg/ddl/tests/indexmerge/merge_test.go @@ -901,6 +901,8 @@ func TestAddIndexSkipReorgCheck(t *testing.T) { tk.MustExec("alter table t add index idx3(a);") require.False(t, skipTableReorg) require.False(t, skipTempIdxReorg) + tk.MustQuery("select * from t;").Check(testkit.Rows("1", "2")) + tk.MustExec("admin check table t;") } func TestAddIndexInsertAfterReorgSkipCheck(t *testing.T) { From 0ac8bd9097052e75779e185aee6b71a33bac5513 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 11:46:27 +0800 Subject: [PATCH 4/7] fix linter and address comment --- pkg/ddl/reorg.go | 2 +- pkg/ddl/tests/indexmerge/merge_test.go | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index a3e17817fa775..2e33e6055deb7 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -773,7 +773,7 @@ func GetTableMaxHandle(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl func existsTableRow(ctx *ReorgContext, store kv.Storage, tbl table.PhysicalTable) (bool, error) { found := false err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), math.MaxUint64, nil, nil, - func(_ kv.Handle, rowKey kv.Key, _ []byte) (bool, error) { + func(_ kv.Handle, _ kv.Key, _ []byte) (bool, error) { found = true return false, nil }) diff --git a/pkg/ddl/tests/indexmerge/merge_test.go b/pkg/ddl/tests/indexmerge/merge_test.go index acedd5e55b873..5055cce81652b 100644 --- a/pkg/ddl/tests/indexmerge/merge_test.go +++ b/pkg/ddl/tests/indexmerge/merge_test.go @@ -910,28 +910,24 @@ func TestAddIndexInsertAfterReorgSkipCheck(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create table t (a int);") - err := failpoint.EnableCall("github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip", func() { + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip", func() { tk2 := testkit.NewTestKit(t, store) tk2.MustExec("use test") tk2.MustExec("insert into t values (1);") }) - require.NoError(t, err) tk.MustExec("alter table t add index idx(a);") tk.MustQuery("select * from t;").Check(testkit.Rows("1")) tk.MustExec("admin check table t;") - err = failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip") + err := failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/afterCheckTableReorgCanSkip") require.NoError(t, err) tk.MustExec("truncate table t;") - err = failpoint.EnableCall("github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip", func() { + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip", func() { tk2 := testkit.NewTestKit(t, store) tk2.MustExec("use test") tk2.MustExec("insert into t values (2);") }) - require.NoError(t, err) tk.MustExec("alter table t add index idx2(a);") tk.MustQuery("select * from t;").Check(testkit.Rows("2")) tk.MustExec("admin check table t;") - err = failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/afterCheckTempIndexReorgCanSkip") - require.NoError(t, err) } From d6bff3bdda7b8a0d5e6ec6319710b395cc5aceb2 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 12:31:17 +0800 Subject: [PATCH 5/7] address comment --- pkg/ddl/index.go | 42 ++++++++++++++++++++++++++++++------------ pkg/ddl/reorg.go | 5 ++--- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index e1538d0fd11c3..7b505422c812b 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -1120,6 +1120,7 @@ SwitchIndexState: func checkIfTableReorgWorkCanSkip( store kv.Storage, + sessCtx sessionctx.Context, tbl table.Table, job *model.Job, ) bool { @@ -1127,21 +1128,28 @@ func checkIfTableReorgWorkCanSkip( // Reorg work has begun. return false } + txn, err := sessCtx.Txn(false) + intest.Assert(err == nil) + var startTS uint64 = math.MaxUint64 + if txn != nil && txn.Valid() { + startTS = txn.StartTS() + } ctx := NewReorgContext() ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName ctx.setDDLLabelForTopSQL(job.Query) - return checkIfTableIsEmpty(ctx, store, tbl) + return checkIfTableIsEmpty(ctx, store, tbl, startTS) } func checkIfTableIsEmpty( ctx *ReorgContext, store kv.Storage, tbl table.Table, + startTS uint64, ) bool { if pTbl, ok := tbl.(table.PartitionedTable); ok { for _, pid := range pTbl.GetAllPartitionIDs() { pTbl := pTbl.GetPartition(pid) - if !checkIfPhysicalTableIsEmpty(ctx, store, pTbl) { + if !checkIfPhysicalTableIsEmpty(ctx, store, pTbl, startTS) { return false } } @@ -1149,15 +1157,16 @@ func checkIfTableIsEmpty( } //nolint:forcetypeassert plainTbl := tbl.(table.PhysicalTable) - return checkIfPhysicalTableIsEmpty(ctx, store, plainTbl) + return checkIfPhysicalTableIsEmpty(ctx, store, plainTbl, startTS) } func checkIfPhysicalTableIsEmpty( ctx *ReorgContext, store kv.Storage, tbl table.PhysicalTable, + startTS uint64, ) bool { - hasRecord, err := existsTableRow(ctx, store, tbl) + hasRecord, err := existsTableRow(ctx, store, tbl, startTS) intest.Assert(err == nil) if err != nil { logutil.DDLLogger().Info("check if table is empty failed", zap.Error(err)) @@ -1168,6 +1177,7 @@ func checkIfPhysicalTableIsEmpty( func checkIfTempIndexReorgWorkCanSkip( store kv.Storage, + sessCtx sessionctx.Context, tbl table.Table, allIndexInfos []*model.IndexInfo, job *model.Job, @@ -1181,6 +1191,12 @@ func checkIfTempIndexReorgWorkCanSkip( // Reorg work has begun. return false } + txn, err := sessCtx.Txn(false) + intest.Assert(err == nil) + var startTS uint64 = math.MaxUint64 + if txn != nil && txn.Valid() { + startTS = txn.StartTS() + } ctx := NewReorgContext() ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName ctx.setDDLLabelForTopSQL(job.Query) @@ -1192,7 +1208,7 @@ func checkIfTempIndexReorgWorkCanSkip( globalIdxIDs = append(globalIdxIDs, idxInfo.ID) } } - return checkIfTempIndexIsEmpty(ctx, store, tbl, firstIdxID, lastIdxID, globalIdxIDs) + return checkIfTempIndexIsEmpty(ctx, store, tbl, firstIdxID, lastIdxID, globalIdxIDs, startTS) } func checkIfTempIndexIsEmpty( @@ -1201,22 +1217,23 @@ func checkIfTempIndexIsEmpty( tbl table.Table, firstIdxID, lastIdxID int64, globalIdxIDs []int64, + startTS uint64, ) bool { tblMetaID := tbl.Meta().ID if pTbl, ok := tbl.(table.PartitionedTable); ok { for _, pid := range pTbl.GetAllPartitionIDs() { - if !checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, pid, firstIdxID, lastIdxID) { + if !checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, pid, firstIdxID, lastIdxID, startTS) { return false } } for _, globalIdxID := range globalIdxIDs { - if !checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, tblMetaID, globalIdxID, globalIdxID) { + if !checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, tblMetaID, globalIdxID, globalIdxID, startTS) { return false } } return true } - return checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, tblMetaID, firstIdxID, lastIdxID) + return checkIfTempIndexIsEmptyForPhysicalTable(ctx, store, tblMetaID, firstIdxID, lastIdxID, startTS) } func checkIfTempIndexIsEmptyForPhysicalTable( @@ -1224,11 +1241,12 @@ func checkIfTempIndexIsEmptyForPhysicalTable( store kv.Storage, pid int64, firstIdxID, lastIdxID int64, + startTS uint64, ) bool { start, end := encodeTempIndexRange(pid, firstIdxID, lastIdxID) foundKey := false idxPrefix := tablecodec.GenTableIndexPrefix(pid) - err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, idxPrefix, math.MaxUint64, start, end, + err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, idxPrefix, startTS, start, end, func(_ kv.Handle, _ kv.Key, _ []byte) (more bool, err error) { foundKey = true return false, nil @@ -1309,7 +1327,7 @@ func doReorgWorkForCreateIndex( return false, ver, err } if !reorgTp.NeedMergeProcess() { - skipReorg := checkIfTableReorgWorkCanSkip(w.store, tbl, job) + skipReorg := checkIfTableReorgWorkCanSkip(w.store, w.sess.Session(), tbl, job) if skipReorg { logutil.DDLLogger().Info("table is empty, skipping reorg work", zap.Int64("jobID", job.ID), @@ -1320,7 +1338,7 @@ func doReorgWorkForCreateIndex( } switch allIndexInfos[0].BackfillState { case model.BackfillStateRunning: - skipReorg := checkIfTableReorgWorkCanSkip(w.store, tbl, job) + skipReorg := checkIfTableReorgWorkCanSkip(w.store, w.sess.Session(), tbl, job) if !skipReorg { logutil.DDLLogger().Info("index backfill state running", zap.Int64("job ID", job.ID), zap.String("table", tbl.Meta().Name.O), @@ -1371,7 +1389,7 @@ func doReorgWorkForCreateIndex( ver, err = updateVersionAndTableInfo(jobCtx, job, tbl.Meta(), true) return false, ver, errors.Trace(err) case model.BackfillStateMerging: - skipReorg := checkIfTempIndexReorgWorkCanSkip(w.store, tbl, allIndexInfos, job) + skipReorg := checkIfTempIndexReorgWorkCanSkip(w.store, w.sess.Session(), tbl, allIndexInfos, job) if !skipReorg { done, ver, err = runReorgJobAndHandleErr(w, jobCtx, job, tbl, allIndexInfos, true) if !done { diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index 2e33e6055deb7..1f50b77f0e809 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -19,7 +19,6 @@ import ( "context" "encoding/hex" "fmt" - "math" "math/rand" "strconv" "strings" @@ -770,9 +769,9 @@ func GetTableMaxHandle(ctx *ReorgContext, store kv.Storage, startTS uint64, tbl // existsTableRow checks if there is at least one row in the specified table. // In case of an error during the operation, it returns false along with the error. -func existsTableRow(ctx *ReorgContext, store kv.Storage, tbl table.PhysicalTable) (bool, error) { +func existsTableRow(ctx *ReorgContext, store kv.Storage, tbl table.PhysicalTable, startTS uint64) (bool, error) { found := false - err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), math.MaxUint64, nil, nil, + err := iterateSnapshotKeys(ctx, store, kv.PriorityLow, tbl.RecordPrefix(), startTS, nil, nil, func(_ kv.Handle, _ kv.Key, _ []byte) (bool, error) { found = true return false, nil From a7bb629eb2b337f721ec9d69e7e93a5cd3f6db95 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 14:12:12 +0800 Subject: [PATCH 6/7] update bazel --- pkg/ddl/tests/indexmerge/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ddl/tests/indexmerge/BUILD.bazel b/pkg/ddl/tests/indexmerge/BUILD.bazel index ad5186396e2ca..9b9580df9f293 100644 --- a/pkg/ddl/tests/indexmerge/BUILD.bazel +++ b/pkg/ddl/tests/indexmerge/BUILD.bazel @@ -9,7 +9,7 @@ go_test( ], flaky = True, race = "on", - shard_count = 21, + shard_count = 23, deps = [ "//pkg/config", "//pkg/ddl", From 70a91644f842ba18b4cfb6dbc226192dd6ef0410 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 28 Nov 2024 15:06:55 +0800 Subject: [PATCH 7/7] prevent using MaxUint64 --- pkg/ddl/index.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 7b505422c812b..83f3a1ebc272c 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -21,7 +21,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "math" "os" "slices" "strings" @@ -1129,11 +1128,13 @@ func checkIfTableReorgWorkCanSkip( return false } txn, err := sessCtx.Txn(false) - intest.Assert(err == nil) - var startTS uint64 = math.MaxUint64 - if txn != nil && txn.Valid() { - startTS = txn.StartTS() + validTxn := err == nil && txn != nil && txn.Valid() + intest.Assert(validTxn) + if !validTxn { + logutil.DDLLogger().Warn("check if table is empty failed", zap.Error(err)) + return false } + startTS := txn.StartTS() ctx := NewReorgContext() ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName ctx.setDDLLabelForTopSQL(job.Query) @@ -1192,11 +1193,13 @@ func checkIfTempIndexReorgWorkCanSkip( return false } txn, err := sessCtx.Txn(false) - intest.Assert(err == nil) - var startTS uint64 = math.MaxUint64 - if txn != nil && txn.Valid() { - startTS = txn.StartTS() + validTxn := err == nil && txn != nil && txn.Valid() + intest.Assert(validTxn) + if !validTxn { + logutil.DDLLogger().Warn("check if temp index is empty failed", zap.Error(err)) + return false } + startTS := txn.StartTS() ctx := NewReorgContext() ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName ctx.setDDLLabelForTopSQL(job.Query)