Skip to content

Commit 43a9ed8

Browse files
committed
add cluster_id to the mysql.tidb table
Signed-off-by: Yang Keao <[email protected]>
1 parent 397b0f2 commit 43a9ed8

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
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
@@ -116,5 +116,5 @@ func TestCheckSysTableCompatibility(t *testing.T) {
116116
//
117117
// The above variables are in the file br/pkg/restore/systable_restore.go
118118
func TestMonitorTheSystemTableIncremental(t *testing.T) {
119-
require.Equal(t, int64(241), session.CurrentBootstrapVersion)
119+
require.Equal(t, int64(242), session.CurrentBootstrapVersion)
120120
}

pkg/ddl/schematracker/checker.go

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

566566
type storageAndMore interface {
567567
kv.Storage
568+
kv.StorageWithPD
568569
kv.EtcdBackend
569570
helper.Storage
570571
}

pkg/session/bootstrap.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,8 @@ const (
859859
tidbDefOOMAction = "default_oom_action"
860860
// The variable name in mysql.tidb table and it records the current DDLTableVersion
861861
tidbDDLTableVersion = "ddl_table_version"
862+
// The variable name in mysql.tidb table and it records the cluster id of this cluster
863+
tidbClusterID = "cluster_id"
862864
// Const for TiDB server version 2.
863865
version2 = 2
864866
version3 = 3
@@ -1242,11 +1244,15 @@ const (
12421244

12431245
// Add index on user field for some mysql tables.
12441246
version241 = 241
1247+
1248+
// version 242
1249+
// insert `cluster_id` into the `mysql.tidb` table.
1250+
version242 = 242
12451251
)
12461252

12471253
// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
12481254
// please make sure this is the largest version
1249-
var currentBootstrapVersion int64 = version241
1255+
var currentBootstrapVersion int64 = version242
12501256

12511257
// 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.
12521258
var internalSQLTimeout = owner.ManagerSessionTTL + 15
@@ -1423,6 +1429,7 @@ var (
14231429
upgradeToVer239,
14241430
upgradeToVer240,
14251431
upgradeToVer241,
1432+
upgradeToVer242,
14261433
}
14271434
)
14281435

@@ -3318,6 +3325,30 @@ func upgradeToVer241(s sessiontypes.Session, ver int64) {
33183325
doReentrantDDL(s, "ALTER TABLE mysql.default_roles ADD INDEX i_user (user)", dbterror.ErrDupKeyName)
33193326
}
33203327

3328+
// writeClusterID writes cluster id into mysql.tidb
3329+
func writeClusterID(s sessiontypes.Session) {
3330+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second)
3331+
defer cancel()
3332+
3333+
clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx)
3334+
3335+
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
3336+
mysql.SystemDB,
3337+
mysql.TiDBTable,
3338+
tidbClusterID,
3339+
clusterID,
3340+
clusterID,
3341+
)
3342+
}
3343+
3344+
func upgradeToVer242(s sessiontypes.Session, ver int64) {
3345+
if ver >= version242 {
3346+
return
3347+
}
3348+
3349+
writeClusterID(s)
3350+
}
3351+
33213352
// initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist.
33223353
func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) {
33233354
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
@@ -3571,6 +3602,8 @@ func doDMLWorks(s sessiontypes.Session) {
35713602

35723603
writeDDLTableVersion(s)
35733604

3605+
writeClusterID(s)
3606+
35743607
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
35753608
_, err := s.ExecuteInternal(ctx, "COMMIT")
35763609
if err != nil {

pkg/session/bootstrap_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,3 +2704,59 @@ func TestGetFuncName(t *testing.T) {
27042704
}
27052705
})
27062706
}
2707+
2708+
func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo242(t *testing.T) {
2709+
ctx := context.Background()
2710+
store, dom := CreateStoreAndBootstrap(t)
2711+
defer func() { require.NoError(t, store.Close()) }()
2712+
2713+
// `cluster_id` is inserted for a new TiDB cluster.
2714+
se := CreateSessionAndSetID(t, store)
2715+
r := MustExecToRecodeSet(t, se, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2716+
req := r.NewChunk(nil)
2717+
err := r.Next(ctx, req)
2718+
require.NoError(t, err)
2719+
require.Equal(t, 1, req.NumRows())
2720+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2721+
require.NoError(t, r.Close())
2722+
se.Close()
2723+
2724+
// bootstrap as version241
2725+
ver241 := version241
2726+
seV241 := CreateSessionAndSetID(t, store)
2727+
txn, err := store.Begin()
2728+
require.NoError(t, err)
2729+
m := meta.NewMutator(txn)
2730+
err = m.FinishBootstrap(int64(ver241))
2731+
require.NoError(t, err)
2732+
revertVersionAndVariables(t, seV241, ver241)
2733+
// remove the cluster_id entry from mysql.tidb table
2734+
MustExec(t, seV241, "delete from mysql.tidb where variable_name='cluster_id'")
2735+
err = txn.Commit(ctx)
2736+
require.NoError(t, err)
2737+
store.SetOption(StoreBootstrappedKey, nil)
2738+
ver, err := getBootstrapVersion(seV241)
2739+
require.NoError(t, err)
2740+
require.Equal(t, int64(ver241), ver)
2741+
seV241.Close()
2742+
2743+
// upgrade to current version
2744+
dom.Close()
2745+
domCurVer, err := BootstrapSession(store)
2746+
require.NoError(t, err)
2747+
defer domCurVer.Close()
2748+
seCurVer := CreateSessionAndSetID(t, store)
2749+
ver, err = getBootstrapVersion(seCurVer)
2750+
require.NoError(t, err)
2751+
require.Equal(t, currentBootstrapVersion, ver)
2752+
2753+
// check if the cluster_id has been set in the `mysql.tidb` table during upgrade
2754+
r = MustExecToRecodeSet(t, seCurVer, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2755+
req = r.NewChunk(nil)
2756+
err = r.Next(ctx, req)
2757+
require.NoError(t, err)
2758+
require.Equal(t, 1, req.NumRows())
2759+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2760+
require.NoError(t, r.Close())
2761+
seCurVer.Close()
2762+
}

0 commit comments

Comments
 (0)