From 74500655353125c6dfb272c7d88f6e89acca0d25 Mon Sep 17 00:00:00 2001 From: YangKeao Date: Fri, 14 Feb 2025 01:30:02 +0800 Subject: [PATCH 1/4] This is an automated cherry-pick of #59511 Signed-off-by: ti-chi-bot --- .../snap_client/systable_restore_test.go | 4 + pkg/ddl/schematracker/checker.go | 1 + pkg/session/bootstrap.go | 57 ++++++++++ pkg/session/bootstrap_test.go | 103 ++++++++++++++++++ 4 files changed, 165 insertions(+) diff --git a/br/pkg/restore/snap_client/systable_restore_test.go b/br/pkg/restore/snap_client/systable_restore_test.go index 5729ee1dd4e7c..953cb1247449d 100644 --- a/br/pkg/restore/snap_client/systable_restore_test.go +++ b/br/pkg/restore/snap_client/systable_restore_test.go @@ -393,6 +393,7 @@ func TestCheckPrivilegeTableRowsCollateCompatibility(t *testing.T) { // // The above variables are in the file br/pkg/restore/systable_restore.go func TestMonitorTheSystemTableIncremental(t *testing.T) { +<<<<<<< HEAD require.Equal(t, int64(223), session.CurrentBootstrapVersion) } @@ -559,4 +560,7 @@ func TestNotifyUpdateAllUsersPrivilege(t *testing.T) { "mysql": {"test": {}, "user": {}, "db": {}}, }, notifier) require.Error(t, err) +======= + require.Equal(t, int64(242), session.CurrentBootstrapVersion) +>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) } diff --git a/pkg/ddl/schematracker/checker.go b/pkg/ddl/schematracker/checker.go index ca651b6d9673a..33b6b6f4a9da6 100644 --- a/pkg/ddl/schematracker/checker.go +++ b/pkg/ddl/schematracker/checker.go @@ -575,6 +575,7 @@ func (d *Checker) DoDDLJobWrapper(ctx sessionctx.Context, jobW *ddl.JobWrapper) type storageAndMore interface { kv.Storage + kv.StorageWithPD kv.EtcdBackend helper.Storage } diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index f68458ee18b3f..f88cbef638995 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -852,6 +852,8 @@ const ( tidbDefOOMAction = "default_oom_action" // The variable name in mysql.tidb table and it records the current DDLTableVersion tidbDDLTableVersion = "ddl_table_version" + // The variable name in mysql.tidb table and it records the cluster id of this cluster + tidbClusterID = "cluster_id" // Const for TiDB server version 2. version2 = 2 version3 = 3 @@ -1243,11 +1245,33 @@ const ( // ... // next version should start with 239 +<<<<<<< HEAD +======= + + // version 239 + // 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 + + // version 242 + // insert `cluster_id` into the `mysql.tidb` table. + version242 = 242 +>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) ) // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version +<<<<<<< HEAD var currentBootstrapVersion int64 = version223 +======= +var currentBootstrapVersion int64 = version242 +>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) // 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 @@ -1421,11 +1445,18 @@ var ( upgradeToVer216, upgradeToVer217, upgradeToVer218, +<<<<<<< HEAD upgradeToVer219, upgradeToVer220, upgradeToVer221, upgradeToVer222, upgradeToVer223, +======= + upgradeToVer239, + upgradeToVer240, + upgradeToVer241, + upgradeToVer242, +>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) } ) @@ -3302,6 +3333,30 @@ func upgradeToVer223(s sessiontypes.Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN modify_params json AFTER `error`;", infoschema.ErrColumnExists) } +// writeClusterID writes cluster id into mysql.tidb +func writeClusterID(s sessiontypes.Session) { + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second) + defer cancel() + + clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx) + + mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, + mysql.SystemDB, + mysql.TiDBTable, + tidbClusterID, + clusterID, + clusterID, + ) +} + +func upgradeToVer242(s sessiontypes.Session, ver int64) { + if ver >= version242 { + return + } + + writeClusterID(s) +} + // initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist. func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) @@ -3553,6 +3608,8 @@ func doDMLWorks(s sessiontypes.Session) { writeDDLTableVersion(s) + writeClusterID(s) + ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) _, err := s.ExecuteInternal(ctx, "COMMIT") if err != nil { diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index e5c269d95cea2..2f63f27139e0c 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -2591,3 +2591,106 @@ func TestTiDBUpgradeToVer219(t *testing.T) { 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") } +<<<<<<< HEAD +======= + +// testExampleAFunc is a example func for TestGetFuncName +func testExampleAFunc(s sessiontypes.Session, i int64) {} + +// testExampleBFunc is a example func for TestGetFuncName +func testExampleBFunc(s sessiontypes.Session, i int64) {} + +func TestGetFuncName(t *testing.T) { + // Test case 1: Pass a valid function + t.Run("Valid function", func(t *testing.T) { + name, err := getFunctionName(testExampleAFunc) + if err != nil { + t.Fatalf("Expected no error, got: %v", err) + } + if name != "testExampleAFunc" { + t.Errorf("Expected function name 'testExampleAFunc', got: %s", name) + } + }) + + // Test case 2: Pass another valid function + t.Run("Another valid function", func(t *testing.T) { + name, err := getFunctionName(testExampleBFunc) + if err != nil { + t.Fatalf("Expected no error, got: %v", err) + } + if name != "testExampleBFunc" { + t.Errorf("Expected function name 'testExampleBFunc', got: %s", name) + } + }) + + // Test case 3: Pass nil as the function + t.Run("Nil function", func(t *testing.T) { + name, err := getFunctionName(nil) + if err == nil { + t.Fatalf("Expected an error, got nil") + } + if name != "" { + t.Errorf("Expected empty function name, got: %s", name) + } + if err.Error() != "function is nil" { + t.Errorf("Expected error 'function is nil', got: %v", err) + } + }) +} + +func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo242(t *testing.T) { + ctx := context.Background() + store, dom := CreateStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // `cluster_id` is inserted for a new TiDB cluster. + se := CreateSessionAndSetID(t, store) + r := MustExecToRecodeSet(t, se, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`) + req := r.NewChunk(nil) + err := r.Next(ctx, req) + require.NoError(t, err) + require.Equal(t, 1, req.NumRows()) + require.NotEmpty(t, req.GetRow(0).GetBytes(0)) + require.NoError(t, r.Close()) + se.Close() + + // bootstrap as version241 + ver241 := version241 + seV241 := CreateSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMutator(txn) + err = m.FinishBootstrap(int64(ver241)) + require.NoError(t, err) + revertVersionAndVariables(t, seV241, ver241) + // remove the cluster_id entry from mysql.tidb table + MustExec(t, seV241, "delete from mysql.tidb where variable_name='cluster_id'") + err = txn.Commit(ctx) + require.NoError(t, err) + store.SetOption(StoreBootstrappedKey, nil) + ver, err := getBootstrapVersion(seV241) + require.NoError(t, err) + require.Equal(t, int64(ver241), ver) + seV241.Close() + + // upgrade to current version + 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) + + // check if the cluster_id has been set in the `mysql.tidb` table during upgrade + r = MustExecToRecodeSet(t, seCurVer, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`) + req = r.NewChunk(nil) + err = r.Next(ctx, req) + require.NoError(t, err) + require.Equal(t, 1, req.NumRows()) + require.NotEmpty(t, req.GetRow(0).GetBytes(0)) + require.NoError(t, r.Close()) + seCurVer.Close() +} +>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) From 8bd98c2cb4504f9bf2ce2884d6aea1ba94fadb32 Mon Sep 17 00:00:00 2001 From: Yang Keao Date: Wed, 11 Mar 2026 13:02:36 +0800 Subject: [PATCH 2/4] fix conflicts Signed-off-by: Yang Keao --- .../snap_client/systable_restore_test.go | 6 +-- pkg/session/bootstrap.go | 45 ++++-------------- pkg/session/bootstrap_test.go | 47 ------------------- 3 files changed, 9 insertions(+), 89 deletions(-) diff --git a/br/pkg/restore/snap_client/systable_restore_test.go b/br/pkg/restore/snap_client/systable_restore_test.go index 953cb1247449d..45d1063389b98 100644 --- a/br/pkg/restore/snap_client/systable_restore_test.go +++ b/br/pkg/restore/snap_client/systable_restore_test.go @@ -393,8 +393,7 @@ func TestCheckPrivilegeTableRowsCollateCompatibility(t *testing.T) { // // The above variables are in the file br/pkg/restore/systable_restore.go func TestMonitorTheSystemTableIncremental(t *testing.T) { -<<<<<<< HEAD - require.Equal(t, int64(223), session.CurrentBootstrapVersion) + require.Equal(t, int64(224), session.CurrentBootstrapVersion) } func TestIsStatsTemporaryTable(t *testing.T) { @@ -560,7 +559,4 @@ func TestNotifyUpdateAllUsersPrivilege(t *testing.T) { "mysql": {"test": {}, "user": {}, "db": {}}, }, notifier) require.Error(t, err) -======= - require.Equal(t, int64(242), session.CurrentBootstrapVersion) ->>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) } diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index f88cbef638995..8ee0d3210eac1 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -1240,38 +1240,20 @@ const ( // add modify_params to tidb_global_task and tidb_global_task_history. version223 = 223 + // version 224 + // insert `cluster_id` into the `mysql.tidb` table. + version224 = 224 + // ... // [version223, version238] is the version range reserved for patches of 8.5.x // ... // next version should start with 239 -<<<<<<< HEAD -======= - - // version 239 - // 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 - - // version 242 - // insert `cluster_id` into the `mysql.tidb` table. - version242 = 242 ->>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) ) // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version -<<<<<<< HEAD -var currentBootstrapVersion int64 = version223 -======= -var currentBootstrapVersion int64 = version242 ->>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) +var currentBootstrapVersion int64 = version224 // 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 @@ -1445,18 +1427,7 @@ var ( upgradeToVer216, upgradeToVer217, upgradeToVer218, -<<<<<<< HEAD - upgradeToVer219, - upgradeToVer220, - upgradeToVer221, - upgradeToVer222, - upgradeToVer223, -======= - upgradeToVer239, - upgradeToVer240, - upgradeToVer241, - upgradeToVer242, ->>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) + upgradeToVer224, } ) @@ -3349,8 +3320,8 @@ func writeClusterID(s sessiontypes.Session) { ) } -func upgradeToVer242(s sessiontypes.Session, ver int64) { - if ver >= version242 { +func upgradeToVer224(s sessiontypes.Session, ver int64) { + if ver >= version224 { return } diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index 2f63f27139e0c..39e4178b95361 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -2591,52 +2591,6 @@ func TestTiDBUpgradeToVer219(t *testing.T) { 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") } -<<<<<<< HEAD -======= - -// testExampleAFunc is a example func for TestGetFuncName -func testExampleAFunc(s sessiontypes.Session, i int64) {} - -// testExampleBFunc is a example func for TestGetFuncName -func testExampleBFunc(s sessiontypes.Session, i int64) {} - -func TestGetFuncName(t *testing.T) { - // Test case 1: Pass a valid function - t.Run("Valid function", func(t *testing.T) { - name, err := getFunctionName(testExampleAFunc) - if err != nil { - t.Fatalf("Expected no error, got: %v", err) - } - if name != "testExampleAFunc" { - t.Errorf("Expected function name 'testExampleAFunc', got: %s", name) - } - }) - - // Test case 2: Pass another valid function - t.Run("Another valid function", func(t *testing.T) { - name, err := getFunctionName(testExampleBFunc) - if err != nil { - t.Fatalf("Expected no error, got: %v", err) - } - if name != "testExampleBFunc" { - t.Errorf("Expected function name 'testExampleBFunc', got: %s", name) - } - }) - - // Test case 3: Pass nil as the function - t.Run("Nil function", func(t *testing.T) { - name, err := getFunctionName(nil) - if err == nil { - t.Fatalf("Expected an error, got nil") - } - if name != "" { - t.Errorf("Expected empty function name, got: %s", name) - } - if err.Error() != "function is nil" { - t.Errorf("Expected error 'function is nil', got: %v", err) - } - }) -} func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo242(t *testing.T) { ctx := context.Background() @@ -2693,4 +2647,3 @@ func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo242(t *testing.T) { require.NoError(t, r.Close()) seCurVer.Close() } ->>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511)) From f88160a636169162ed05b0637adc844741c5d91f Mon Sep 17 00:00:00 2001 From: Yang Keao Date: Wed, 11 Mar 2026 13:32:31 +0800 Subject: [PATCH 3/4] fix test Signed-off-by: Yang Keao --- pkg/session/bootstrap_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index a3a0bc4ce73dd..7647719c2e169 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -2628,6 +2628,9 @@ func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo225(t *testing.T) { // upgrade to current version dom.Close() + storeBootstrappedLock.Lock() + delete(storeBootstrapped, store.UUID()) + storeBootstrappedLock.Unlock() domCurVer, err := BootstrapSession(store) require.NoError(t, err) defer domCurVer.Close() From 84ae4647ac1940f8c3c566f1c11c3d70a00db92b Mon Sep 17 00:00:00 2001 From: Yang Keao Date: Wed, 11 Mar 2026 16:09:46 +0800 Subject: [PATCH 4/4] fix test --- 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 45d1063389b98..69471ff50f726 100644 --- a/br/pkg/restore/snap_client/systable_restore_test.go +++ b/br/pkg/restore/snap_client/systable_restore_test.go @@ -393,7 +393,7 @@ func TestCheckPrivilegeTableRowsCollateCompatibility(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(224), session.CurrentBootstrapVersion) + require.Equal(t, int64(225), session.CurrentBootstrapVersion) } func TestIsStatsTemporaryTable(t *testing.T) {