From 51f6f579a6d701a8a5d93b1dff8fe9d77f47c3c6 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Sat, 21 Jan 2023 12:42:12 +0800 Subject: [PATCH] ddl: split some tests into new package Signed-off-by: Weizhen Wang --- ddl/BUILD.bazel | 4 +- ddl/cancel_test.go | 7 ++- ddl/cluster_test.go | 7 ++- ddl/column_change_test.go | 7 ++- ddl/column_modify_test.go | 8 +-- ddl/column_test.go | 14 ++--- ddl/column_type_change_test.go | 21 +++---- ddl/db_change_test.go | 29 +++++----- ddl/db_integration_test.go | 4 +- ddl/db_partition_test.go | 7 ++- ddl/db_table_test.go | 5 +- ddl/db_test.go | 17 +++--- ddl/ddl_worker_test.go | 3 +- ddl/fail_test.go | 4 +- ddl/foreign_key_test.go | 15 ++--- ddl/index_change_test.go | 3 +- ddl/indexmergetest/BUILD.bazel | 31 ++++++++++ ddl/indexmergetest/main_test.go | 56 +++++++++++++++++++ .../merge_test.go} | 46 ++++++--------- ddl/integration_test.go | 4 +- ddl/internal/callback/BUILD.bazel | 51 +++++++++++++++++ .../callback/callback.go} | 23 +++----- ddl/internal/callback/callback_test.go | 31 ++++++++++ ddl/job_table_test.go | 3 +- ddl/modify_column_test.go | 7 ++- ddl/multi_schema_change_test.go | 19 ++++--- ddl/mv_index_test.go | 4 +- ddl/placement_policy_test.go | 3 +- ddl/repair_table_test.go | 4 +- ddl/resource_group_test.go | 4 +- ddl/rollingback_test.go | 3 +- ddl/serial_test.go | 9 +-- ddl/table_modify_test.go | 3 +- ddl/testutil/BUILD.bazel | 2 + ddl/testutil/testutil.go | 12 ++++ ddl/tiflash_replica_test.go | 4 +- tests/realtikvtest/addindextest/BUILD.bazel | 2 - .../addindextest/integration_test.go | 13 +---- 38 files changed, 329 insertions(+), 160 deletions(-) create mode 100644 ddl/indexmergetest/BUILD.bazel create mode 100644 ddl/indexmergetest/main_test.go rename ddl/{index_merge_tmp_test.go => indexmergetest/merge_test.go} (94%) create mode 100644 ddl/internal/callback/BUILD.bazel rename ddl/{callback_test.go => internal/callback/callback.go} (93%) create mode 100644 ddl/internal/callback/callback_test.go diff --git a/ddl/BUILD.bazel b/ddl/BUILD.bazel index 32387949c14c3..d3636a9d64b2c 100644 --- a/ddl/BUILD.bazel +++ b/ddl/BUILD.bazel @@ -149,7 +149,6 @@ go_test( srcs = [ "attributes_sql_test.go", "backfilling_test.go", - "callback_test.go", "cancel_test.go", "cluster_test.go", "column_change_test.go", @@ -176,7 +175,6 @@ go_test( "foreign_key_test.go", "index_change_test.go", "index_cop_test.go", - "index_merge_tmp_test.go", "index_modify_test.go", "integration_test.go", "job_table_test.go", @@ -210,7 +208,7 @@ go_test( deps = [ "//autoid_service", "//config", - "//ddl/ingest", + "//ddl/internal/callback", "//ddl/placement", "//ddl/resourcegroup", "//ddl/schematracker", diff --git a/ddl/cancel_test.go b/ddl/cancel_test.go index 3a5c461ad8461..3f02029ffced7 100644 --- a/ddl/cancel_test.go +++ b/ddl/cancel_test.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" @@ -264,7 +265,7 @@ func TestCancel(t *testing.T) { require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockBackfillSlow")) }() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} i := atomicutil.NewInt64(0) cancel := atomicutil.NewBool(false) cancelResult := atomicutil.NewBool(false) @@ -282,12 +283,12 @@ func TestCancel(t *testing.T) { } dom.DDL().SetHook(hook.Clone()) - restHook := func(h *ddl.TestDDLCallback) { + restHook := func(h *callback.TestDDLCallback) { h.OnJobRunBeforeExported = nil h.OnJobUpdatedExported.Store(nil) dom.DDL().SetHook(h.Clone()) } - registHook := func(h *ddl.TestDDLCallback, onJobRunBefore bool) { + registHook := func(h *callback.TestDDLCallback, onJobRunBefore bool) { if onJobRunBefore { h.OnJobRunBeforeExported = hookFunc } else { diff --git a/ddl/cluster_test.go b/ddl/cluster_test.go index e2a4302e044ce..55c780d55e536 100644 --- a/ddl/cluster_test.go +++ b/ddl/cluster_test.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain/infosync" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/meta" @@ -84,7 +85,7 @@ func TestFlashbackCloseAndResetPDSchedule(t *testing.T) { defer resetGC() tk.MustExec(fmt.Sprintf(safePointSQL, timeBeforeDrop)) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionFlashbackCluster, job.Type) if job.SchemaState == model.StateWriteReorganization { @@ -136,7 +137,7 @@ func TestAddDDLDuringFlashback(t *testing.T) { defer resetGC() tk.MustExec(fmt.Sprintf(safePointSQL, timeBeforeDrop)) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionFlashbackCluster, job.Type) if job.SchemaState == model.StateWriteOnly { @@ -175,7 +176,7 @@ func TestGlobalVariablesOnFlashback(t *testing.T) { defer resetGC() tk.MustExec(fmt.Sprintf(safePointSQL, timeBeforeDrop)) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionFlashbackCluster, job.Type) if job.SchemaState == model.StateWriteReorganization { diff --git a/ddl/column_change_test.go b/ddl/column_change_test.go index be393dd488668..76a3b377a5abe 100644 --- a/ddl/column_change_test.go +++ b/ddl/column_change_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/parser/model" @@ -48,7 +49,7 @@ func TestColumnAdd(t *testing.T) { tk.MustExec("insert t values (1, 2);") d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} ct := testNewContext(store) // set up hook @@ -149,7 +150,7 @@ func TestModifyAutoRandColumnWithMetaKeyChanged(t *testing.T) { tk.MustExec("create table t (a bigint primary key clustered AUTO_RANDOM(5));") d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} var errCount int32 = 3 var genAutoRandErr error @@ -457,7 +458,7 @@ func TestIssue40135(t *testing.T) { tk.MustExec("CREATE TABLE t40135 ( a tinyint DEFAULT NULL, b varchar(32) DEFAULT 'md') PARTITION BY HASH (a) PARTITIONS 2") one := true - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if one { diff --git a/ddl/column_modify_test.go b/ddl/column_modify_test.go index 658039c1092a4..7f3125511c587 100644 --- a/ddl/column_modify_test.go +++ b/ddl/column_modify_test.go @@ -24,7 +24,7 @@ import ( "time" "github.com/pingcap/errors" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" testddlutil "github.com/pingcap/tidb/ddl/testutil" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -664,7 +664,7 @@ func TestTransactionWithWriteOnlyColumn(t *testing.T) { }, } - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -872,7 +872,7 @@ func TestAddGeneratedColumnAndInsert(t *testing.T) { tk1.MustExec("use test") d := dom.DDL() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} ctx := mock.NewContext() ctx.Store = store times := 0 @@ -916,7 +916,7 @@ func TestColumnTypeChangeGenUniqueChangingName(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} var checkErr error assertChangingColName := "_col$_c2_0" assertChangingIdxName := "_idx$_idx_0" diff --git a/ddl/column_test.go b/ddl/column_test.go index e6c48b1121595..d378c03e297b5 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -23,7 +23,7 @@ import ( "testing" "github.com/pingcap/errors" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" @@ -672,7 +672,7 @@ func TestAddColumn(t *testing.T) { checkOK := false - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { if checkOK { return @@ -740,7 +740,7 @@ func TestAddColumns(t *testing.T) { err = txn.Commit(context.Background()) require.NoError(t, err) - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { mu.Lock() defer mu.Unlock() @@ -810,7 +810,7 @@ func TestDropColumnInColumnTest(t *testing.T) { var mu sync.Mutex d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { mu.Lock() defer mu.Unlock() @@ -872,7 +872,7 @@ func TestDropColumns(t *testing.T) { var mu sync.Mutex d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { mu.Lock() defer mu.Unlock() @@ -998,7 +998,7 @@ func TestWriteDataWriteOnlyMode(t *testing.T) { originalCallback := dom.DDL().GetHook() defer dom.DDL().SetHook(originalCallback) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateWriteOnly { return @@ -1009,7 +1009,7 @@ func TestWriteDataWriteOnlyMode(t *testing.T) { dom.DDL().SetHook(hook) tk.MustExec("alter table t change column `col1` `col1` varchar(20)") - hook = &ddl.TestDDLCallback{Do: dom} + hook = &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateWriteOnly { return diff --git a/ddl/column_type_change_test.go b/ddl/column_type_change_test.go index 308a815773ce9..0aa303c984398 100644 --- a/ddl/column_type_change_test.go +++ b/ddl/column_type_change_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" @@ -121,7 +122,7 @@ func TestColumnTypeChangeStateBetweenInteger(t *testing.T) { require.Equal(t, 2, len(tbl.Cols())) require.NotNil(t, external.GetModifyColumn(t, tk, "test", "t", "c2", false)) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -185,7 +186,7 @@ func TestRollbackColumnTypeChangeBetweenInteger(t *testing.T) { require.Equal(t, 2, len(tbl.Cols())) require.NotNil(t, external.GetModifyColumn(t, tk, "test", "t", "c2", false)) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} // Mock roll back at model.StateNone. customizeHookRollbackAtState(hook, tbl, model.StateNone) dom.DDL().SetHook(hook) @@ -217,7 +218,7 @@ func TestRollbackColumnTypeChangeBetweenInteger(t *testing.T) { assertRollBackedColUnchanged(t, tk) } -func customizeHookRollbackAtState(hook *ddl.TestDDLCallback, tbl table.Table, state model.SchemaState) { +func customizeHookRollbackAtState(hook *callback.TestDDLCallback, tbl table.Table, state model.SchemaState) { hook.OnJobRunBeforeExported = func(job *model.Job) { if tbl.Meta().ID != job.TableID { return @@ -934,7 +935,7 @@ func TestColumnTypeChangeIgnoreDisplayLength(t *testing.T) { assertHasAlterWriteReorg := func(tbl table.Table) { // Restore assertResult to false. assertResult = false - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if tbl.Meta().ID != job.TableID { return @@ -1600,7 +1601,7 @@ func TestChangingColOriginDefaultValue(t *testing.T) { tbl := external.GetTableByName(t, tk, "test", "t") originalHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var ( once bool checkErr error @@ -1679,7 +1680,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) { tbl := external.GetTableByName(t, tk, "test", "t") originalHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var ( once bool checkErr error @@ -1764,7 +1765,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastFail(t *testing.T) { tbl := external.GetTableByName(t, tk, "test", "t") originalHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -1893,7 +1894,7 @@ func TestDDLExitWhenCancelMeetPanic(t *testing.T) { require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockExceedErrorLimit")) }() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var jobID int64 hook.OnJobRunBeforeExported = func(job *model.Job) { if jobID != 0 { @@ -1968,7 +1969,7 @@ func TestCancelCTCInReorgStateWillCauseGoroutineLeak(t *testing.T) { tk.MustExec("insert into ctc_goroutine_leak values(1),(2),(3)") tbl := external.GetTableByName(t, tk, "test", "ctc_goroutine_leak") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var jobID int64 hook.OnJobRunBeforeExported = func(job *model.Job) { if jobID != 0 { @@ -2210,7 +2211,7 @@ func TestCastDateToTimestampInReorgAttribute(t *testing.T) { var checkErr1 error var checkErr2 error - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr1 != nil || checkErr2 != nil || tbl.Meta().ID != job.TableID { return diff --git a/ddl/db_change_test.go b/ddl/db_change_test.go index da49688ccc608..2dc12c6cda9e8 100644 --- a/ddl/db_change_test.go +++ b/ddl/db_change_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/executor" @@ -74,7 +75,7 @@ func TestShowCreateTable(t *testing.T) { "CREATE TABLE `t2` (\n `a` int(11) DEFAULT NULL,\n `b` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,\n `c` varchar(1) COLLATE utf8mb4_general_ci DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"}, } prevState := model.StateNone - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} currTestCaseOffset := 0 onJobUpdatedExportedFunc := func(job *model.Job) { if job.SchemaState == prevState || checkErr != nil { @@ -143,7 +144,7 @@ func TestDropNotNullColumn(t *testing.T) { var checkErr error d := dom.DDL() originalCallback := d.GetHook() - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} sqlNum := 0 onJobUpdatedExportedFunc := func(job *model.Job) { if checkErr != nil { @@ -222,7 +223,7 @@ func TestTwoStates(t *testing.T) { key(c1, c2))`) tk.MustExec("insert into t values(1, 'a', 'N', '2017-07-01')") - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} prevState := model.StateNone require.NoError(t, testInfo.parseSQLs(parser.New())) @@ -809,7 +810,7 @@ func runTestInSchemaState( // Make sure these SQLs use the plan of index scan. tk.MustExec("drop stats t") - callback := &ddl.TestDDLCallback{Do: dom} + callback := &callback.TestDDLCallback{Do: dom} prevState := model.StateNone var checkErr error se, err := session.CreateSession(store) @@ -872,7 +873,7 @@ func TestShowIndex(t *testing.T) { tk.MustExec("use test_db_state") tk.MustExec(`create table t(c1 int primary key nonclustered, c2 int)`) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} prevState := model.StateNone showIndexSQL := `show index from t` var checkErr error @@ -1325,7 +1326,7 @@ func TestParallelAlterAndDropSchema(t *testing.T) { } func prepareTestControlParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain) (*testkit.TestKit, *testkit.TestKit, chan struct{}, ddl.Callback) { - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} times := 0 callback.OnJobRunBeforeExported = func(job *model.Job) { if times != 0 { @@ -1433,7 +1434,7 @@ func dbChangeTestParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Dom var err2, err3 error var wg util.WaitGroupWrapper - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} once := sync.Once{} onJobUpdatedExportedFunc := func(job *model.Job) { // sleep a while, let other job enqueue. @@ -1531,7 +1532,7 @@ func TestParallelDDLBeforeRunDDLJob(t *testing.T) { tk2 := testkit.NewTestKit(t, store) tk2.MustExec("use test_db_state") - intercept := &ddl.TestInterceptor{} + intercept := &callback.TestInterceptor{} var sessionToStart sync.WaitGroup // sessionToStart is a waitgroup to wait for two session to get the same information schema sessionToStart.Add(2) @@ -1574,7 +1575,7 @@ func TestParallelDDLBeforeRunDDLJob(t *testing.T) { wg.Wait() - intercept = &ddl.TestInterceptor{} + intercept = &callback.TestInterceptor{} d.(ddl.DDLForTest).SetInterceptor(intercept) } @@ -1666,7 +1667,7 @@ func TestCreateExpressionIndex(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if checkErr != nil { return @@ -1741,7 +1742,7 @@ func TestCreateUniqueExpressionIndex(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if checkErr != nil { return @@ -1850,7 +1851,7 @@ func TestDropExpressionIndex(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if checkErr != nil { return @@ -1922,7 +1923,7 @@ func TestParallelRenameTable(t *testing.T) { d2 := dom.DDL() originalCallback := d2.GetHook() defer d2.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{Do: dom} + callback := &callback.TestDDLCallback{Do: dom} callback.OnJobRunBeforeExported = func(job *model.Job) { switch job.SchemaState { case model.StateNone: @@ -2037,7 +2038,7 @@ func TestConcurrentSetDefaultValue(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{Do: dom} + callback := &callback.TestDDLCallback{Do: dom} skip := false callback.OnJobRunBeforeExported = func(job *model.Job) { switch job.SchemaState { diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 1d482f8cecada..16d3d7a2478e1 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -28,7 +28,7 @@ import ( "github.com/pingcap/errors" _ "github.com/pingcap/tidb/autoid_service" "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/ddl/schematracker" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -687,7 +687,7 @@ func TestUpdateMultipleTable(t *testing.T) { tk2.MustExec("use test") d := dom.DDL() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { if job.SchemaState == model.StateWriteOnly { tk2.MustExec("update t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1") diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index c6f50fcf71874..97cf16ed694bc 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/ddl/testutil" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -2447,7 +2448,7 @@ func TestExchangePartitionHook(t *testing.T) { tk.MustExec(`insert into pt values (0), (4), (7)`) tk.MustExec("insert into nt values (1)") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} dom.DDL().SetHook(hook) hookFunc := func(job *model.Job) { @@ -3733,7 +3734,7 @@ func TestTruncatePartitionMultipleTimes(t *testing.T) { dom := domain.GetDomain(tk.Session()) originHook := dom.DDL().GetHook() defer dom.DDL().SetHook(originHook) - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} dom.DDL().SetHook(hook) injected := false hook.OnJobRunBeforeExported = func(job *model.Job) { @@ -4543,7 +4544,7 @@ func TestIssue40135Ver2(t *testing.T) { tk.MustExec("CREATE TABLE t40135 ( a int DEFAULT NULL, b varchar(32) DEFAULT 'md', index(a)) PARTITION BY HASH (a) PARTITIONS 6") tk.MustExec("insert into t40135 values (1, 'md'), (2, 'ma'), (3, 'md'), (4, 'ma'), (5, 'md'), (6, 'ma')") one := true - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error var wg sync.WaitGroup wg.Add(1) diff --git a/ddl/db_table_test.go b/ddl/db_table_test.go index 7725eaf981a13..f4952e2e1d483 100644 --- a/ddl/db_table_test.go +++ b/ddl/db_table_test.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" testddlutil "github.com/pingcap/tidb/ddl/testutil" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -185,7 +186,7 @@ func TestTransactionOnAddDropColumn(t *testing.T) { originHook := dom.DDL().GetHook() defer dom.DDL().SetHook(originHook) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -888,7 +889,7 @@ func TestAddColumn2(t *testing.T) { originHook := dom.DDL().GetHook() defer dom.DDL().SetHook(originHook) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var writeOnlyTable table.Table hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState == model.StateWriteOnly { diff --git a/ddl/db_test.go b/ddl/db_test.go index 46cfe301ec4f4..4bfe194d5c626 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" ddlutil "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -269,7 +270,7 @@ func TestIssue22307(t *testing.T) { tk.MustExec("create table t (a int, b int)") tk.MustExec("insert into t values(1, 1);") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr1, checkErr2 error hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateWriteOnly { @@ -570,7 +571,7 @@ func TestAddExpressionIndexRollback(t *testing.T) { tk1.MustExec("use test") d := dom.DDL() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var currJob *model.Job ctx := mock.NewContext() ctx.Store = store @@ -958,7 +959,7 @@ func TestDDLJobErrorCount(t *testing.T) { }() var jobID int64 - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { jobID = job.ID } @@ -1090,7 +1091,7 @@ func TestCancelJobWriteConflict(t *testing.T) { var cancelErr error var rs []sqlexec.RecordSet - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} d := dom.DDL() originalHook := d.GetHook() d.SetHook(hook) @@ -1503,7 +1504,7 @@ func TestDDLBlockedCreateView(t *testing.T) { tk.MustExec("use test") tk.MustExec("create table t(a int)") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} first := true hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateWriteOnly { @@ -1528,7 +1529,7 @@ func TestHashPartitionAddColumn(t *testing.T) { tk.MustExec("use test") tk.MustExec("create table t(a int, b int) partition by hash(a) partitions 4") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateWriteOnly { return @@ -1551,7 +1552,7 @@ func TestSetInvalidDefaultValueAfterModifyColumn(t *testing.T) { var wg sync.WaitGroup var checkErr error one := false - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateDeleteOnly { return @@ -1588,7 +1589,7 @@ func TestMDLTruncateTable(t *testing.T) { var wg sync.WaitGroup - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} wg.Add(2) var timetk2 time.Time var timetk3 time.Time diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index e07d1661f7d99..0471740d41ddf 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" @@ -97,7 +98,7 @@ func TestParallelDDL(t *testing.T) { // set hook to execute jobs after all jobs are in queue. jobCnt := 11 - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} once := sync.Once{} var checkErr error tc.OnJobRunBeforeExported = func(job *model.Job) { diff --git a/ddl/fail_test.go b/ddl/fail_test.go index 39437b43a2b73..3c4ca0769bc1e 100644 --- a/ddl/fail_test.go +++ b/ddl/fail_test.go @@ -18,7 +18,7 @@ import ( "testing" "github.com/pingcap/failpoint" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" "github.com/stretchr/testify/require" @@ -38,7 +38,7 @@ func TestFailBeforeDecodeArgs(t *testing.T) { tableID = int64(tableIDi) d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} first := true stateCnt := 0 diff --git a/ddl/foreign_key_test.go b/ddl/foreign_key_test.go index 627c924b21871..032adfb296120 100644 --- a/ddl/foreign_key_test.go +++ b/ddl/foreign_key_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessiontxn" @@ -125,7 +126,7 @@ func TestForeignKey(t *testing.T) { var mu sync.Mutex checkOK := false var hookErr error - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if job.State != model.JobStateDone { return @@ -167,7 +168,7 @@ func TestForeignKey(t *testing.T) { checkOK = false mu.Unlock() // fix data race pr/#9491 - tc2 := &ddl.TestDDLCallback{} + tc2 := &callback.TestDDLCallback{} onJobUpdatedExportedFunc2 := func(job *model.Job) { if job.State != model.JobStateDone { return @@ -224,7 +225,7 @@ func TestTruncateOrDropTableWithForeignKeyReferred2(t *testing.T) { var wg sync.WaitGroup var truncateErr, dropErr error testTruncate := true - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} tc.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateNone { return @@ -280,7 +281,7 @@ func TestDropIndexNeededInForeignKey2(t *testing.T) { var wg sync.WaitGroup var dropErr error - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} tc.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StatePublic || job.Type != model.ActionDropIndex { return @@ -319,7 +320,7 @@ func TestDropDatabaseWithForeignKeyReferred2(t *testing.T) { tk.MustExec("create database test2") var wg sync.WaitGroup var dropErr error - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} tc.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StateNone { return @@ -360,7 +361,7 @@ func TestAddForeignKey2(t *testing.T) { tk.MustExec("create table t2 (id int key, b int, index(b));") var wg sync.WaitGroup var addErr error - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} tc.OnJobRunBeforeExported = func(job *model.Job) { if job.SchemaState != model.StatePublic || job.Type != model.ActionDropIndex { return @@ -400,7 +401,7 @@ func TestAddForeignKey3(t *testing.T) { var insertErrs []error var deleteErrs []error - tc := &ddl.TestDDLCallback{} + tc := &callback.TestDDLCallback{} tc.OnJobRunBeforeExported = func(job *model.Job) { if job.Type != model.ActionAddForeignKey { return diff --git a/ddl/index_change_test.go b/ddl/index_change_test.go index f9dcc99154dc5..dc1b98f205f08 100644 --- a/ddl/index_change_test.go +++ b/ddl/index_change_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" @@ -41,7 +42,7 @@ func TestIndexChange(t *testing.T) { tk.MustExec("insert t values (1, 1), (2, 2), (3, 3);") d := dom.DDL() - tc := &ddl.TestDDLCallback{Do: dom} + tc := &callback.TestDDLCallback{Do: dom} // set up hook prevState := model.StateNone addIndexDone := false diff --git a/ddl/indexmergetest/BUILD.bazel b/ddl/indexmergetest/BUILD.bazel new file mode 100644 index 0000000000000..25dfef99ecb3f --- /dev/null +++ b/ddl/indexmergetest/BUILD.bazel @@ -0,0 +1,31 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "indexmergetest_test", + timeout = "moderate", + srcs = [ + "main_test.go", + "merge_test.go", + ], + flaky = True, + shard_count = 4, + deps = [ + "//config", + "//ddl", + "//ddl/ingest", + "//ddl/internal/callback", + "//ddl/testutil", + "//domain", + "//kv", + "//meta/autoid", + "//parser/model", + "//tablecodec", + "//testkit", + "//testkit/testsetup", + "@com_github_pingcap_failpoint//:failpoint", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", + "@com_github_tikv_client_go_v2//tikv", + "@org_uber_go_goleak//:goleak", + ], +) diff --git a/ddl/indexmergetest/main_test.go b/ddl/indexmergetest/main_test.go new file mode 100644 index 0000000000000..b4de8700ce167 --- /dev/null +++ b/ddl/indexmergetest/main_test.go @@ -0,0 +1,56 @@ +// Copyright 2023 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package indexmergetest + +import ( + "testing" + "time" + + "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/meta/autoid" + "github.com/pingcap/tidb/testkit/testsetup" + "github.com/tikv/client-go/v2/tikv" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testsetup.SetupForCommonTest() + tikv.EnableFailpoints() + + domain.SchemaOutOfDateRetryInterval.Store(50 * time.Millisecond) + domain.SchemaOutOfDateRetryTimes.Store(50) + + autoid.SetStep(5000) + ddl.RunInGoTest = true + + config.UpdateGlobal(func(conf *config.Config) { + conf.Instance.SlowThreshold = 10000 + conf.TiKVClient.AsyncCommit.SafeWindow = 0 + conf.TiKVClient.AsyncCommit.AllowedClockDrift = 0 + conf.Experimental.AllowsExpressionIndex = true + }) + + opts := []goleak.Option{ + goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), + goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"), + goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), + goleak.IgnoreTopFunction("github.com/tikv/client-go/v2/txnkv/transaction.keepAlive"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), + } + + goleak.VerifyTestMain(m, opts...) +} diff --git a/ddl/index_merge_tmp_test.go b/ddl/indexmergetest/merge_test.go similarity index 94% rename from ddl/index_merge_tmp_test.go rename to ddl/indexmergetest/merge_test.go index b637a55d2925f..a31b3edcc23a4 100644 --- a/ddl/index_merge_tmp_test.go +++ b/ddl/indexmergetest/merge_test.go @@ -1,4 +1,4 @@ -// Copyright 2022 PingCAP, Inc. +// Copyright 2023 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ddl_test +package indexmergetest import ( "testing" @@ -21,15 +21,14 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/ddl/ingest" - "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/ddl/internal/callback" + "github.com/pingcap/tidb/ddl/testutil" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/logutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.uber.org/zap" ) func TestAddIndexMergeProcess(t *testing.T) { @@ -47,12 +46,12 @@ func TestAddIndexMergeProcess(t *testing.T) { var checkErr error var runDML, backfillDone bool originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: dom, } onJobUpdatedExportedFunc := func(job *model.Job) { if !runDML && job.Type == model.ActionAddIndex && job.SchemaState == model.StateWriteReorganization { - idx := findIdxInfo(dom, "test", "t", "idx") + idx := testutil.FindIdxInfo(dom, "test", "t", "idx") if idx == nil || idx.BackfillState != model.BackfillStateRunning { return } @@ -94,13 +93,13 @@ func TestAddPrimaryKeyMergeProcess(t *testing.T) { var checkErr error var runDML, backfillDone bool originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: nil, // We'll reload the schema manually. } onJobUpdatedExportedFunc := func(job *model.Job) { if !runDML && job.Type == model.ActionAddPrimaryKey && job.SchemaState == model.StateWriteReorganization { - idx := findIdxInfo(dom, "test", "t", "primary") + idx := testutil.FindIdxInfo(dom, "test", "t", "primary") if idx == nil || idx.BackfillState != model.BackfillStateRunning || job.SnapshotVer == 0 { return } @@ -143,12 +142,12 @@ func TestAddIndexMergeVersionIndexValue(t *testing.T) { var runDML bool var tblID, idxID int64 originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: dom, } onJobUpdatedExportedFunc := func(job *model.Job) { if !runDML && job.Type == model.ActionAddIndex && job.SchemaState == model.StateWriteReorganization { - idx := findIdxInfo(dom, "test", "t", "idx") + idx := testutil.FindIdxInfo(dom, "test", "t", "idx") if idx == nil || idx.BackfillState != model.BackfillStateReadyToMerge { return } @@ -198,14 +197,14 @@ func TestAddIndexMergeIndexUntouchedValue(t *testing.T) { var runInsert bool var runUpdate bool originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: dom, } onJobUpdatedExportedFunc := func(job *model.Job) { if job.Type != model.ActionAddIndex || job.SchemaState != model.StateWriteReorganization { return } - idx := findIdxInfo(dom, "test", "t", "idx") + idx := testutil.FindIdxInfo(dom, "test", "t", "idx") if idx == nil { return } @@ -243,15 +242,6 @@ func TestAddIndexMergeIndexUntouchedValue(t *testing.T) { tk.MustQuery("select * from t ignore index (idx);").Check(testkit.Rows("1 1 a a", "100 2 a a")) } -func findIdxInfo(dom *domain.Domain, dbName, tbName, idxName string) *model.IndexInfo { - tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(dbName), model.NewCIStr(tbName)) - if err != nil { - logutil.BgLogger().Warn("cannot find table", zap.String("dbName", dbName), zap.String("tbName", tbName)) - return nil - } - return tbl.Meta().FindIndexByName(idxName) -} - // TestCreateUniqueIndexKeyExist this case will test below things: // Create one unique index idx((a*b+1)); // insert (0, 6) and delete it; @@ -279,7 +269,7 @@ func TestCreateUniqueIndexKeyExist(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if t.Failed() { return @@ -346,7 +336,7 @@ func TestAddIndexMergeIndexUpdateOnDeleteOnly(t *testing.T) { var checkErrs []error originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: dom, } onJobUpdatedBefore := func(job *model.Job) { @@ -383,7 +373,7 @@ func TestAddIndexMergeDeleteUniqueOnWriteOnly(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if t.Failed() { return @@ -441,7 +431,7 @@ func TestAddIndexMergeDoubleDelete(t *testing.T) { d := dom.DDL() originalCallback := d.GetHook() defer d.SetHook(originalCallback) - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} onJobUpdatedExportedFunc := func(job *model.Job) { if t.Failed() { return @@ -485,7 +475,7 @@ func TestAddIndexMergeConflictWithPessimistic(t *testing.T) { tk.MustExec("set @@global.tidb_enable_metadata_lock = 0;") originHook := dom.DDL().GetHook() - callback := &ddl.TestDDLCallback{Do: dom} + callback := &callback.TestDDLCallback{Do: dom} runPessimisticTxn := false callback.OnJobRunBeforeExported = func(job *model.Job) { @@ -498,7 +488,7 @@ func TestAddIndexMergeConflictWithPessimistic(t *testing.T) { assert.NoError(t, err) } if !runPessimisticTxn && job.SchemaState == model.StateWriteReorganization { - idx := findIdxInfo(dom, "test", "t", "idx") + idx := testutil.FindIdxInfo(dom, "test", "t", "idx") if idx == nil { return } diff --git a/ddl/integration_test.go b/ddl/integration_test.go index 29e69aa855274..264e755889899 100644 --- a/ddl/integration_test.go +++ b/ddl/integration_test.go @@ -18,7 +18,7 @@ import ( "fmt" "testing" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" "github.com/stretchr/testify/require" @@ -86,7 +86,7 @@ func TestDDLStatementsBackFill(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test;") needReorg := false - callback := &ddl.TestDDLCallback{ + callback := &callback.TestDDLCallback{ Do: dom, } onJobUpdatedExportedFunc := func(job *model.Job) { diff --git a/ddl/internal/callback/BUILD.bazel b/ddl/internal/callback/BUILD.bazel new file mode 100644 index 0000000000000..e0bb07c3b2c21 --- /dev/null +++ b/ddl/internal/callback/BUILD.bazel @@ -0,0 +1,51 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "callback", + srcs = ["callback.go"], + importpath = "github.com/pingcap/tidb/ddl/internal/callback", + visibility = ["//ddl:__subpackages__"], + deps = [ + "//ddl", + "//infoschema", + "//parser/model", + "//sessionctx", + "//util/logutil", + "@org_uber_go_zap//:zap", + ], +) + +go_test( + name = "callback_test", + srcs = ["callback_test.go"], + embed = [":callback"], + deps = [ + "//ddl", + "@com_github_stretchr_testify//require", + ], +) + +go_library( + name = "ddlcallback", + srcs = ["callback.go"], + importpath = "github.com/pingcap/tidb/ddl/internal/ddlcallback", + visibility = ["//ddl:__subpackages__"], + deps = [ + "//ddl", + "//infoschema", + "//parser/model", + "//sessionctx", + "//util/logutil", + "@org_uber_go_zap//:zap", + ], +) + +go_test( + name = "ddlcallback_test", + srcs = ["callback_test.go"], + embed = [":ddlcallback"], + deps = [ + "//ddl", + "@com_github_stretchr_testify//require", + ], +) diff --git a/ddl/callback_test.go b/ddl/internal/callback/callback.go similarity index 93% rename from ddl/callback_test.go rename to ddl/internal/callback/callback.go index 5a97e8212689e..a3c84d774dd5c 100644 --- a/ddl/callback_test.go +++ b/ddl/internal/callback/callback.go @@ -1,4 +1,4 @@ -// Copyright 2015 PingCAP, Inc. +// Copyright 2023 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,27 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ddl +package callback import ( "context" "sync/atomic" - "testing" + "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/util/logutil" - "github.com/stretchr/testify/require" "go.uber.org/zap" ) +// TestInterceptor is a test interceptor in the ddl type TestInterceptor struct { - *BaseInterceptor + *ddl.BaseInterceptor OnGetInfoSchemaExported func(ctx sessionctx.Context, is infoschema.InfoSchema) infoschema.InfoSchema } +// OnGetInfoSchema is to run when to call GetInfoSchema func (ti *TestInterceptor) OnGetInfoSchema(ctx sessionctx.Context, is infoschema.InfoSchema) infoschema.InfoSchema { if ti.OnGetInfoSchemaExported != nil { return ti.OnGetInfoSchemaExported(ctx, is) @@ -43,10 +44,10 @@ func (ti *TestInterceptor) OnGetInfoSchema(ctx sessionctx.Context, is infoschema // TestDDLCallback is used to customize user callback themselves. type TestDDLCallback struct { - *BaseCallback + *ddl.BaseCallback // We recommended to pass the domain parameter to the test ddl callback, it will ensure // domain to reload schema before your ddl stepping into the next state change. - Do DomainReloader + Do ddl.DomainReloader onJobRunBefore func(*model.Job) OnJobRunBeforeExported func(*model.Job) @@ -149,11 +150,3 @@ func (tc *TestDDLCallback) OnGetJobAfter(jobType string, job *model.Job) { func (tc *TestDDLCallback) Clone() *TestDDLCallback { return &*tc } - -func TestCallback(t *testing.T) { - cb := &BaseCallback{} - require.Nil(t, cb.OnChanged(nil)) - cb.OnJobRunBefore(nil) - cb.OnJobUpdated(nil) - cb.OnWatched(context.TODO()) -} diff --git a/ddl/internal/callback/callback_test.go b/ddl/internal/callback/callback_test.go new file mode 100644 index 0000000000000..f611394909e48 --- /dev/null +++ b/ddl/internal/callback/callback_test.go @@ -0,0 +1,31 @@ +// Copyright 2023 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package callback + +import ( + "context" + "testing" + + "github.com/pingcap/tidb/ddl" + "github.com/stretchr/testify/require" +) + +func TestCallback(t *testing.T) { + cb := &ddl.BaseCallback{} + require.Nil(t, cb.OnChanged(nil)) + cb.OnJobRunBefore(nil) + cb.OnJobUpdated(nil) + cb.OnWatched(context.TODO()) +} diff --git a/ddl/job_table_test.go b/ddl/job_table_test.go index d869dcecc2c0e..9f1150241bbd1 100644 --- a/ddl/job_table_test.go +++ b/ddl/job_table_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" @@ -62,7 +63,7 @@ func TestDDLScheduling(t *testing.T) { "ALTER TABLE e EXCHANGE PARTITION p1 WITH TABLE e3;", } - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} var wg util.WaitGroupWrapper wg.Add(1) var once sync.Once diff --git a/ddl/modify_column_test.go b/ddl/modify_column_test.go index 6eb8e633be007..583c0a435b4ec 100644 --- a/ddl/modify_column_test.go +++ b/ddl/modify_column_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/parser/ast" @@ -72,7 +73,7 @@ func TestModifyColumnReorgInfo(t *testing.T) { tbl := external.GetTableByName(t, tk, "test", "t1") // Check insert null before job first update. - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error var currJob *model.Job var elements []*meta.Element @@ -198,7 +199,7 @@ func TestModifyColumnNullToNotNull(t *testing.T) { tbl := external.GetTableByName(t, tk1, "test", "t1") // Check insert null before job first update. - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} tk1.MustExec("delete from t1") once := sync.Once{} var checkErr error @@ -253,7 +254,7 @@ func TestModifyColumnNullToNotNullWithChangingVal(t *testing.T) { tbl := external.GetTableByName(t, tk1, "test", "t1") // Check insert null before job first update. - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} tk1.MustExec("delete from t1") once := sync.Once{} var checkErr error diff --git a/ddl/multi_schema_change_test.go b/ddl/multi_schema_change_test.go index d9facec4642cf..1f6a52bcce244 100644 --- a/ddl/multi_schema_change_test.go +++ b/ddl/multi_schema_change_test.go @@ -20,6 +20,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/kv" @@ -386,7 +387,7 @@ func TestMultiSchemaChangeRenameColumns(t *testing.T) { tk.MustExec("drop table if exists t") tk.MustExec("create table t (a int default 1, b int default 2)") tk.MustExec("insert into t values ()") - hook1 := &ddl.TestDDLCallback{Do: dom} + hook1 := &callback.TestDDLCallback{Do: dom} hook1.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionMultiSchemaChange, job.Type) if job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteReorganization { @@ -457,7 +458,7 @@ func TestMultiSchemaChangeAlterColumns(t *testing.T) { // Test dml stmts when do alter tk.MustExec("drop table if exists t") tk.MustExec("create table t (a int default 1, b int default 2)") - hook1 := &ddl.TestDDLCallback{Do: dom} + hook1 := &callback.TestDDLCallback{Do: dom} hook1.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionMultiSchemaChange, job.Type) if job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateWriteOnly { @@ -972,7 +973,7 @@ func TestMultiSchemaChangeAlterIndex(t *testing.T) { tk.MustExec("insert into t values (1, 2);") originHook := dom.DDL().GetHook() var checked bool - callback := &ddl.TestDDLCallback{Do: dom} + callback := &callback.TestDDLCallback{Do: dom} onJobUpdatedExportedFunc := func(job *model.Job) { assert.NotNil(t, job.MultiSchemaInfo) // "modify column a tinyint" in write-reorg. @@ -1042,7 +1043,7 @@ func TestMultiSchemaChangeAdminShowDDLJobs(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") originHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { assert.Equal(t, model.ActionMultiSchemaChange, job.Type) if job.MultiSchemaInfo.SubJobs[0].SchemaState == model.StateDeleteOnly { @@ -1129,7 +1130,7 @@ func TestMultiSchemaChangeWithExpressionIndex(t *testing.T) { tk.MustQuery("select * from t;").Check(testkit.Rows("1 2", "2 1")) originHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -1191,7 +1192,7 @@ func TestMultiSchemaChangeSchemaVersion(t *testing.T) { schemaVerMap := map[int64]struct{}{} originHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobSchemaStateChanged = func(schemaVer int64) { if schemaVer != 0 { // No same return schemaVer during multi-schema change @@ -1231,7 +1232,7 @@ func TestMultiSchemaChangeMixedWithUpdate(t *testing.T) { "'2020-01-01 10:00:00', 'wer', '10:00:00', 2.1, 12, 'qwer', 12, 'asdf');") originHook := dom.DDL().GetHook() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var checkErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if checkErr != nil { @@ -1266,7 +1267,7 @@ type cancelOnceHook struct { pred func(job *model.Job) bool s sessionctx.Context - ddl.TestDDLCallback + callback.TestDDLCallback } func (c *cancelOnceHook) OnJobUpdated(job *model.Job) { @@ -1299,7 +1300,7 @@ func newCancelJobHook(t *testing.T, store kv.Storage, dom *domain.Domain, return &cancelOnceHook{ store: store, pred: pred, - TestDDLCallback: ddl.TestDDLCallback{Do: dom}, + TestDDLCallback: callback.TestDDLCallback{Do: dom}, s: tk.Session(), } } diff --git a/ddl/mv_index_test.go b/ddl/mv_index_test.go index 964211ad76740..10fbe2971377a 100644 --- a/ddl/mv_index_test.go +++ b/ddl/mv_index_test.go @@ -19,7 +19,7 @@ import ( "strings" "testing" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" @@ -45,7 +45,7 @@ func TestMultiValuedIndexOnlineDDL(t *testing.T) { internalTK := testkit.NewTestKit(t, store) internalTK.MustExec("use test") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} n := 100 hook.OnJobRunBeforeExported = func(job *model.Job) { internalTK.MustExec(fmt.Sprintf("insert into t values (%d, '[%d, %d, %d]')", n, n, n+1, n+2)) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 559cc0ff59a46..327c7e02cf0b6 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/domain" @@ -135,7 +136,7 @@ func TestPlacementPolicy(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop placement policy if exists x") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var policyID int64 onJobUpdatedExportedFunc := func(job *model.Job) { if policyID != 0 { diff --git a/ddl/repair_table_test.go b/ddl/repair_table_test.go index 6881c6ce5f019..8b16f9bfbc69d 100644 --- a/ddl/repair_table_test.go +++ b/ddl/repair_table_test.go @@ -20,7 +20,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" @@ -101,7 +101,7 @@ func TestRepairTable(t *testing.T) { // Repaired tableInfo has been filtered by `domain.InfoSchema()`, so get it in repairInfo. originTableInfo, _ := domainutil.RepairInfo.GetRepairedTableInfoByTableName("test", "origin") - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var repairErr error hook.OnJobRunBeforeExported = func(job *model.Job) { if job.Type != model.ActionRepairTable { diff --git a/ddl/resource_group_test.go b/ddl/resource_group_test.go index 789e81f99f0fb..3bf33b04d9012 100644 --- a/ddl/resource_group_test.go +++ b/ddl/resource_group_test.go @@ -19,7 +19,7 @@ import ( "strconv" "testing" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/ddl/resourcegroup" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/domain/infosync" @@ -36,7 +36,7 @@ func TestResourceGroupBasic(t *testing.T) { tk.MustExec("use test") re := require.New(t) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var groupID int64 onJobUpdatedExportedFunc := func(job *model.Job) { // job.SchemaID will be assigned when the group is created. diff --git a/ddl/rollingback_test.go b/ddl/rollingback_test.go index f1850eb80dcbc..ee5894441175f 100644 --- a/ddl/rollingback_test.go +++ b/ddl/rollingback_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/external" @@ -50,7 +51,7 @@ func TestCancelAddIndexJobError(t *testing.T) { require.NotNil(t, tbl) d := dom.DDL() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var ( checkErr error jobID int64 diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 970f60a95ff96..668b675a0b185 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -445,7 +446,7 @@ func TestCancelAddIndexPanic(t *testing.T) { oldReorgWaitTimeout := ddl.ReorgWaitTimeout ddl.ReorgWaitTimeout = 50 * time.Millisecond defer func() { ddl.ReorgWaitTimeout = oldReorgWaitTimeout }() - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.Type == model.ActionAddIndex && job.State == model.JobStateRunning && job.SchemaState == model.StateWriteReorganization && job.SnapshotVer != 0 { tkCancel.MustQuery(fmt.Sprintf("admin cancel ddl jobs %d", job.ID)) @@ -684,7 +685,7 @@ func TestRecoverTableByJobIDFail(t *testing.T) { tk.MustExec(fmt.Sprintf(safePointSQL, timeBeforeDrop)) // set hook - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.Type == model.ActionRecoverTable { require.NoError(t, failpoint.Enable("tikvclient/mockCommitError", `return(true)`)) @@ -743,7 +744,7 @@ func TestRecoverTableByTableNameFail(t *testing.T) { tk.MustExec(fmt.Sprintf(safePointSQL, timeBeforeDrop)) // set hook - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} hook.OnJobRunBeforeExported = func(job *model.Job) { if job.Type == model.ActionRecoverTable { require.NoError(t, failpoint.Enable("tikvclient/mockCommitError", `return(true)`)) @@ -816,7 +817,7 @@ func TestCanceledJobTakeTime(t *testing.T) { tk.MustExec("use test") tk.MustExec("create table t_cjtt(a int)") - hook := &ddl.TestDDLCallback{} + hook := &callback.TestDDLCallback{} once := sync.Once{} hook.OnJobRunBeforeExported = func(job *model.Job) { once.Do(func() { diff --git a/ddl/table_modify_test.go b/ddl/table_modify_test.go index 590fea8ad973d..7f0b23e3fd894 100644 --- a/ddl/table_modify_test.go +++ b/ddl/table_modify_test.go @@ -20,6 +20,7 @@ import ( "time" "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/infoschema" @@ -205,7 +206,7 @@ func TestConcurrentLockTables(t *testing.T) { } func testParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain, sql1, sql2 string, se1, se2 session.Session, f func(t *testing.T, err1, err2 error)) { - callback := &ddl.TestDDLCallback{} + callback := &callback.TestDDLCallback{} times := 0 callback.OnJobRunBeforeExported = func(job *model.Job) { if times != 0 { diff --git a/ddl/testutil/BUILD.bazel b/ddl/testutil/BUILD.bazel index 3562ca3b34571..052a747f427ba 100644 --- a/ddl/testutil/BUILD.bazel +++ b/ddl/testutil/BUILD.bazel @@ -14,6 +14,8 @@ go_library( "//table", "//table/tables", "//types", + "//util/logutil", "@com_github_pingcap_errors//:errors", + "@org_uber_go_zap//:zap", ], ) diff --git a/ddl/testutil/testutil.go b/ddl/testutil/testutil.go index 642579ba00ea7..52adf6b750f73 100644 --- a/ddl/testutil/testutil.go +++ b/ddl/testutil/testutil.go @@ -26,6 +26,8 @@ import ( "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/util/logutil" + "go.uber.org/zap" ) // SessionExecInGoroutine export for testing. @@ -82,3 +84,13 @@ func ExtractAllTableHandles(se session.Session, dbName, tbName string) ([]int64, }) return allHandles, err } + +// FindIdxInfo is to get IndexInfo by index name. +func FindIdxInfo(dom *domain.Domain, dbName, tbName, idxName string) *model.IndexInfo { + tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(dbName), model.NewCIStr(tbName)) + if err != nil { + logutil.BgLogger().Warn("cannot find table", zap.String("dbName", dbName), zap.String("tbName", tbName)) + return nil + } + return tbl.Meta().FindIndexByName(idxName) +} diff --git a/ddl/tiflash_replica_test.go b/ddl/tiflash_replica_test.go index 874ab05359d3b..08a91c1086caa 100644 --- a/ddl/tiflash_replica_test.go +++ b/ddl/tiflash_replica_test.go @@ -26,7 +26,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/ddl" + "github.com/pingcap/tidb/ddl/internal/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/kv" @@ -286,7 +286,7 @@ func TestCreateTableWithLike2(t *testing.T) { tbl1 := external.GetTableByName(t, tk, "test", "t1") doneCh := make(chan error, 2) - hook := &ddl.TestDDLCallback{Do: dom} + hook := &callback.TestDDLCallback{Do: dom} var onceChecker sync.Map hook.OnJobRunBeforeExported = func(job *model.Job) { if job.Type != model.ActionAddColumn && job.Type != model.ActionDropColumn && diff --git a/tests/realtikvtest/addindextest/BUILD.bazel b/tests/realtikvtest/addindextest/BUILD.bazel index a2e9c9906380b..a79f2a15f8ca7 100644 --- a/tests/realtikvtest/addindextest/BUILD.bazel +++ b/tests/realtikvtest/addindextest/BUILD.bazel @@ -43,10 +43,8 @@ go_test( "//parser/model", "//testkit", "//tests/realtikvtest", - "//util/logutil", "@com_github_pingcap_failpoint//:failpoint", "@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//require", - "@org_uber_go_zap//:zap", ], ) diff --git a/tests/realtikvtest/addindextest/integration_test.go b/tests/realtikvtest/addindextest/integration_test.go index 07b54089395da..ed1e4e6c85dcd 100644 --- a/tests/realtikvtest/addindextest/integration_test.go +++ b/tests/realtikvtest/addindextest/integration_test.go @@ -31,10 +31,8 @@ import ( "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/tests/realtikvtest" - "github.com/pingcap/tidb/util/logutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.uber.org/zap" ) func TestAddIndexIngestMemoryUsage(t *testing.T) { @@ -422,7 +420,7 @@ func TestAddIndexIngestCancel(t *testing.T) { return } if job.Type == model.ActionAddIndex && job.SchemaState == model.StateWriteReorganization { - idx := findIdxInfo(dom, "addindexlit", "t", "idx") + idx := testutil.FindIdxInfo(dom, "addindexlit", "t", "idx") if idx == nil { return } @@ -460,12 +458,3 @@ func (c *testCallback) OnJobRunBefore(job *model.Job) { c.OnJobRunBeforeExported(job) } } - -func findIdxInfo(dom *domain.Domain, dbName, tbName, idxName string) *model.IndexInfo { - tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(dbName), model.NewCIStr(tbName)) - if err != nil { - logutil.BgLogger().Warn("cannot find table", zap.String("dbName", dbName), zap.String("tbName", tbName)) - return nil - } - return tbl.Meta().FindIndexByName(idxName) -}