Skip to content

Commit b6c77e4

Browse files
authored
bootstrap: add cluster_id to the mysql.tidb table (#59511) (#65564)
close #59476
1 parent d7a5796 commit b6c77e4

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

br/pkg/restore/snap_client/systable_restore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func TestCheckPrivilegeTableRowsCollateCompatibility(t *testing.T) {
393393
//
394394
// The above variables are in the file br/pkg/restore/systable_restore.go
395395
func TestMonitorTheSystemTableIncremental(t *testing.T) {
396-
require.Equal(t, int64(225), session.CurrentBootstrapVersion)
396+
require.Equal(t, int64(226), session.CurrentBootstrapVersion)
397397
}
398398

399399
func TestIsStatsTemporaryTable(t *testing.T) {

pkg/ddl/schematracker/checker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ func (d *Checker) DoDDLJobWrapper(ctx sessionctx.Context, jobW *ddl.JobWrapper)
575575

576576
type storageAndMore interface {
577577
kv.Storage
578+
kv.StorageWithPD
578579
kv.EtcdBackend
579580
helper.Storage
580581
}

pkg/session/bootstrap.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,8 @@ const (
860860
tidbDefOOMAction = "default_oom_action"
861861
// The variable name in mysql.tidb table and it records the current DDLTableVersion
862862
tidbDDLTableVersion = "ddl_table_version"
863+
// The variable name in mysql.tidb table and it records the cluster id of this cluster
864+
tidbClusterID = "cluster_id"
863865
// Const for TiDB server version 2.
864866
version2 = 2
865867
version3 = 3
@@ -1254,16 +1256,19 @@ const (
12541256
// Add index on user field for some mysql tables.
12551257
version225 = 225
12561258

1259+
// insert `cluster_id` into the `mysql.tidb` table.
1260+
version226 = 226
1261+
12571262
// ...
1258-
// [version226, version238] is the version range reserved for patches of 8.5.x
1263+
// [version227, version238] is the version range reserved for patches of 8.5.x
12591264
// ...
12601265
// next version should start with 239
12611266

12621267
)
12631268

12641269
// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
12651270
// please make sure this is the largest version
1266-
var currentBootstrapVersion int64 = version225
1271+
var currentBootstrapVersion int64 = version226
12671272

12681273
// 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.
12691274
var internalSQLTimeout = owner.ManagerSessionTTL + 15
@@ -1444,6 +1449,7 @@ var (
14441449
upgradeToVer223,
14451450
upgradeToVer224,
14461451
upgradeToVer225,
1452+
upgradeToVer226,
14471453
}
14481454
)
14491455

@@ -3332,6 +3338,7 @@ func upgradeToVer225(s sessiontypes.Session, ver int64) {
33323338
if ver >= version225 {
33333339
return
33343340
}
3341+
33353342
doReentrantDDL(s, "ALTER TABLE mysql.user ADD INDEX i_user (user)", dbterror.ErrDupKeyName)
33363343
doReentrantDDL(s, "ALTER TABLE mysql.global_priv ADD INDEX i_user (user)", dbterror.ErrDupKeyName)
33373344
doReentrantDDL(s, "ALTER TABLE mysql.db ADD INDEX i_user (user)", dbterror.ErrDupKeyName)
@@ -3341,6 +3348,30 @@ func upgradeToVer225(s sessiontypes.Session, ver int64) {
33413348
doReentrantDDL(s, "ALTER TABLE mysql.default_roles ADD INDEX i_user (user)", dbterror.ErrDupKeyName)
33423349
}
33433350

3351+
// writeClusterID writes cluster id into mysql.tidb
3352+
func writeClusterID(s sessiontypes.Session) {
3353+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second)
3354+
defer cancel()
3355+
3356+
clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx)
3357+
3358+
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
3359+
mysql.SystemDB,
3360+
mysql.TiDBTable,
3361+
tidbClusterID,
3362+
clusterID,
3363+
clusterID,
3364+
)
3365+
}
3366+
3367+
func upgradeToVer226(s sessiontypes.Session, ver int64) {
3368+
if ver >= version226 {
3369+
return
3370+
}
3371+
3372+
writeClusterID(s)
3373+
}
3374+
33443375
// initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist.
33453376
func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) {
33463377
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
@@ -3592,6 +3623,8 @@ func doDMLWorks(s sessiontypes.Session) {
35923623

35933624
writeDDLTableVersion(s)
35943625

3626+
writeClusterID(s)
3627+
35953628
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
35963629
_, err := s.ExecuteInternal(ctx, "COMMIT")
35973630
if err != nil {

pkg/session/bootstrap_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,3 +2591,61 @@ func TestTiDBUpgradeToVer219(t *testing.T) {
25912591
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state")
25922592
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state")
25932593
}
2594+
2595+
func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo225(t *testing.T) {
2596+
ctx := context.Background()
2597+
store, dom := CreateStoreAndBootstrap(t)
2598+
defer func() { require.NoError(t, store.Close()) }()
2599+
2600+
// `cluster_id` is inserted for a new TiDB cluster.
2601+
se := CreateSessionAndSetID(t, store)
2602+
r := MustExecToRecodeSet(t, se, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2603+
req := r.NewChunk(nil)
2604+
err := r.Next(ctx, req)
2605+
require.NoError(t, err)
2606+
require.Equal(t, 1, req.NumRows())
2607+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2608+
require.NoError(t, r.Close())
2609+
se.Close()
2610+
2611+
// bootstrap as version224
2612+
ver224 := version224
2613+
seV224 := CreateSessionAndSetID(t, store)
2614+
txn, err := store.Begin()
2615+
require.NoError(t, err)
2616+
m := meta.NewMutator(txn)
2617+
err = m.FinishBootstrap(int64(ver224))
2618+
require.NoError(t, err)
2619+
revertVersionAndVariables(t, seV224, ver224)
2620+
// remove the cluster_id entry from mysql.tidb table
2621+
MustExec(t, seV224, "delete from mysql.tidb where variable_name='cluster_id'")
2622+
err = txn.Commit(ctx)
2623+
require.NoError(t, err)
2624+
ver, err := getBootstrapVersion(seV224)
2625+
require.NoError(t, err)
2626+
require.Equal(t, int64(ver224), ver)
2627+
seV224.Close()
2628+
2629+
// upgrade to current version
2630+
dom.Close()
2631+
storeBootstrappedLock.Lock()
2632+
delete(storeBootstrapped, store.UUID())
2633+
storeBootstrappedLock.Unlock()
2634+
domCurVer, err := BootstrapSession(store)
2635+
require.NoError(t, err)
2636+
defer domCurVer.Close()
2637+
seCurVer := CreateSessionAndSetID(t, store)
2638+
ver, err = getBootstrapVersion(seCurVer)
2639+
require.NoError(t, err)
2640+
require.Equal(t, currentBootstrapVersion, ver)
2641+
2642+
// check if the cluster_id has been set in the `mysql.tidb` table during upgrade
2643+
r = MustExecToRecodeSet(t, seCurVer, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2644+
req = r.NewChunk(nil)
2645+
err = r.Next(ctx, req)
2646+
require.NoError(t, err)
2647+
require.Equal(t, 1, req.NumRows())
2648+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2649+
require.NoError(t, r.Close())
2650+
seCurVer.Close()
2651+
}

0 commit comments

Comments
 (0)