From dd5f95c22f2d4216cf6a70e17f0e30ffca9c8b2d Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 30 Dec 2024 14:30:03 +0800 Subject: [PATCH 1/3] session: skip creating indexes on the analyze_jobs table for older clusters Signed-off-by: Rustin170506 --- pkg/session/bootstrap.go | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index 61a207c347667..d1925e24b30b8 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -1236,17 +1236,13 @@ const ( // add modify_params to tidb_global_task and tidb_global_task_history. version239 = 239 - // version 240 - // Add indexes to mysql.analyze_jobs to speed up the query. - version240 = 240 - // Add index on user field for some mysql tables. - version241 = 241 + version240 = 240 ) // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version -var currentBootstrapVersion int64 = version241 +var currentBootstrapVersion int64 = version240 // DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it. var internalSQLTimeout = owner.ManagerSessionTTL + 15 @@ -1422,7 +1418,6 @@ var ( upgradeToVer218, upgradeToVer239, upgradeToVer240, - upgradeToVer241, } ) @@ -3334,28 +3329,10 @@ func upgradeToVer239(s sessiontypes.Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN modify_params json AFTER `error`;", infoschema.ErrColumnExists) } -const ( - // addAnalyzeJobsSchemaTableStateIndex is a DDL statement that adds an index on (table_schema, table_name, state) - // columns to mysql.analyze_jobs table. This index is currently unused since queries filter on partition_name='', - // even for non-partitioned tables. It is kept for potential future optimization where queries could use this - // simpler index directly for non-partitioned tables. - addAnalyzeJobsSchemaTableStateIndex = "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_state (table_schema, table_name, state)" - // addAnalyzeJobsSchemaTablePartitionStateIndex adds an index on (table_schema, table_name, partition_name, state) to mysql.analyze_jobs - addAnalyzeJobsSchemaTablePartitionStateIndex = "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_partition_state (table_schema, table_name, partition_name, state)" -) - func upgradeToVer240(s sessiontypes.Session, ver int64) { if ver >= version240 { return } - doReentrantDDL(s, addAnalyzeJobsSchemaTableStateIndex, dbterror.ErrDupKeyName) - doReentrantDDL(s, addAnalyzeJobsSchemaTablePartitionStateIndex, dbterror.ErrDupKeyName) -} - -func upgradeToVer241(s sessiontypes.Session, ver int64) { - if ver >= version241 { - return - } doReentrantDDL(s, "ALTER TABLE mysql.user ADD INDEX i_user (user)", dbterror.ErrDupKeyName) doReentrantDDL(s, "ALTER TABLE mysql.global_priv ADD INDEX i_user (user)", dbterror.ErrDupKeyName) doReentrantDDL(s, "ALTER TABLE mysql.db ADD INDEX i_user (user)", dbterror.ErrDupKeyName) From 6e70b5b5feb165ff05b9625d1a7c23ada228ce39 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 30 Dec 2024 14:38:06 +0800 Subject: [PATCH 2/3] test: simplify the cases Signed-off-by: Rustin170506 --- pkg/session/bootstrap_test.go | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index 7bf76aebf7b31..af2459ea2fc09 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -2615,10 +2615,13 @@ func (mebd *mockEtcdBackend) TLSConfig() *tls.Config { return nil } func (mebd *mockEtcdBackend) StartGCWorker() error { return nil } -func TestTiDBUpgradeToVer240(t *testing.T) { +func TestAnalyzeJobsIndexes(t *testing.T) { ctx := context.Background() store, dom := CreateStoreAndBootstrap(t) - defer func() { require.NoError(t, store.Close()) }() + defer func() { + dom.Close() + require.NoError(t, store.Close()) + }() ver239 := version239 seV239 := CreateSessionAndSetID(t, store) @@ -2640,24 +2643,6 @@ func TestTiDBUpgradeToVer240(t *testing.T) { require.Equal(t, 1, chk.NumRows()) require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state") require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state") - - // Check that the indexes still exist after upgrading to the new version and that no errors occurred during the upgrade. - dom.Close() - domCurVer, err := BootstrapSession(store) - require.NoError(t, err) - defer domCurVer.Close() - seCurVer := CreateSessionAndSetID(t, store) - ver, err := getBootstrapVersion(seCurVer) - require.NoError(t, err) - require.Equal(t, currentBootstrapVersion, ver) - - res = MustExecToRecodeSet(t, seCurVer, "show create table mysql.analyze_jobs") - chk = res.NewChunk(nil) - err = res.Next(ctx, chk) - require.NoError(t, err) - require.Equal(t, 1, chk.NumRows()) - require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state") - require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state") } // testExampleAFunc is a example func for TestGetFuncName From 7a52e88e5fb94f7f275845a3ef2791ad3dc49e9a Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Mon, 30 Dec 2024 14:51:31 +0800 Subject: [PATCH 3/3] test: update the broken test case Signed-off-by: Rustin170506 --- br/pkg/restore/snap_client/systable_restore_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/br/pkg/restore/snap_client/systable_restore_test.go b/br/pkg/restore/snap_client/systable_restore_test.go index cc95160482d45..6142a270745db 100644 --- a/br/pkg/restore/snap_client/systable_restore_test.go +++ b/br/pkg/restore/snap_client/systable_restore_test.go @@ -116,5 +116,5 @@ func TestCheckSysTableCompatibility(t *testing.T) { // // The above variables are in the file br/pkg/restore/systable_restore.go func TestMonitorTheSystemTableIncremental(t *testing.T) { - require.Equal(t, int64(241), session.CurrentBootstrapVersion) + require.Equal(t, int64(240), session.CurrentBootstrapVersion) }