Skip to content

Commit 7450065

Browse files
YangKeaoti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#59511
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent b5f2c76 commit 7450065

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

br/pkg/restore/snap_client/systable_restore_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +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+
<<<<<<< HEAD
396397
require.Equal(t, int64(223), session.CurrentBootstrapVersion)
397398
}
398399

@@ -559,4 +560,7 @@ func TestNotifyUpdateAllUsersPrivilege(t *testing.T) {
559560
"mysql": {"test": {}, "user": {}, "db": {}},
560561
}, notifier)
561562
require.Error(t, err)
563+
=======
564+
require.Equal(t, int64(242), session.CurrentBootstrapVersion)
565+
>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511))
562566
}

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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,8 @@ const (
852852
tidbDefOOMAction = "default_oom_action"
853853
// The variable name in mysql.tidb table and it records the current DDLTableVersion
854854
tidbDDLTableVersion = "ddl_table_version"
855+
// The variable name in mysql.tidb table and it records the cluster id of this cluster
856+
tidbClusterID = "cluster_id"
855857
// Const for TiDB server version 2.
856858
version2 = 2
857859
version3 = 3
@@ -1243,11 +1245,33 @@ const (
12431245
// ...
12441246

12451247
// next version should start with 239
1248+
<<<<<<< HEAD
1249+
=======
1250+
1251+
// version 239
1252+
// add modify_params to tidb_global_task and tidb_global_task_history.
1253+
version239 = 239
1254+
1255+
// version 240
1256+
// Add indexes to mysql.analyze_jobs to speed up the query.
1257+
version240 = 240
1258+
1259+
// Add index on user field for some mysql tables.
1260+
version241 = 241
1261+
1262+
// version 242
1263+
// insert `cluster_id` into the `mysql.tidb` table.
1264+
version242 = 242
1265+
>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511))
12461266
)
12471267
12481268
// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
12491269
// please make sure this is the largest version
1270+
<<<<<<< HEAD
12501271
var currentBootstrapVersion int64 = version223
1272+
=======
1273+
var currentBootstrapVersion int64 = version242
1274+
>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511))
12511275
12521276
// 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.
12531277
var internalSQLTimeout = owner.ManagerSessionTTL + 15
@@ -1421,11 +1445,18 @@ var (
14211445
upgradeToVer216,
14221446
upgradeToVer217,
14231447
upgradeToVer218,
1448+
<<<<<<< HEAD
14241449
upgradeToVer219,
14251450
upgradeToVer220,
14261451
upgradeToVer221,
14271452
upgradeToVer222,
14281453
upgradeToVer223,
1454+
=======
1455+
upgradeToVer239,
1456+
upgradeToVer240,
1457+
upgradeToVer241,
1458+
upgradeToVer242,
1459+
>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511))
14291460
}
14301461
)
14311462
@@ -3302,6 +3333,30 @@ func upgradeToVer223(s sessiontypes.Session, ver int64) {
33023333
doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN modify_params json AFTER `error`;", infoschema.ErrColumnExists)
33033334
}
33043335

3336+
// writeClusterID writes cluster id into mysql.tidb
3337+
func writeClusterID(s sessiontypes.Session) {
3338+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second)
3339+
defer cancel()
3340+
3341+
clusterID := s.GetDomain().(*domain.Domain).GetPDClient().GetClusterID(ctx)
3342+
3343+
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Cluster ID.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
3344+
mysql.SystemDB,
3345+
mysql.TiDBTable,
3346+
tidbClusterID,
3347+
clusterID,
3348+
clusterID,
3349+
)
3350+
}
3351+
3352+
func upgradeToVer242(s sessiontypes.Session, ver int64) {
3353+
if ver >= version242 {
3354+
return
3355+
}
3356+
3357+
writeClusterID(s)
3358+
}
3359+
33053360
// initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist.
33063361
func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) {
33073362
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
@@ -3553,6 +3608,8 @@ func doDMLWorks(s sessiontypes.Session) {
35533608

35543609
writeDDLTableVersion(s)
35553610

3611+
writeClusterID(s)
3612+
35563613
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
35573614
_, err := s.ExecuteInternal(ctx, "COMMIT")
35583615
if err != nil {

pkg/session/bootstrap_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,3 +2591,106 @@ 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+
<<<<<<< HEAD
2595+
=======
2596+
2597+
// testExampleAFunc is a example func for TestGetFuncName
2598+
func testExampleAFunc(s sessiontypes.Session, i int64) {}
2599+
2600+
// testExampleBFunc is a example func for TestGetFuncName
2601+
func testExampleBFunc(s sessiontypes.Session, i int64) {}
2602+
2603+
func TestGetFuncName(t *testing.T) {
2604+
// Test case 1: Pass a valid function
2605+
t.Run("Valid function", func(t *testing.T) {
2606+
name, err := getFunctionName(testExampleAFunc)
2607+
if err != nil {
2608+
t.Fatalf("Expected no error, got: %v", err)
2609+
}
2610+
if name != "testExampleAFunc" {
2611+
t.Errorf("Expected function name 'testExampleAFunc', got: %s", name)
2612+
}
2613+
})
2614+
2615+
// Test case 2: Pass another valid function
2616+
t.Run("Another valid function", func(t *testing.T) {
2617+
name, err := getFunctionName(testExampleBFunc)
2618+
if err != nil {
2619+
t.Fatalf("Expected no error, got: %v", err)
2620+
}
2621+
if name != "testExampleBFunc" {
2622+
t.Errorf("Expected function name 'testExampleBFunc', got: %s", name)
2623+
}
2624+
})
2625+
2626+
// Test case 3: Pass nil as the function
2627+
t.Run("Nil function", func(t *testing.T) {
2628+
name, err := getFunctionName(nil)
2629+
if err == nil {
2630+
t.Fatalf("Expected an error, got nil")
2631+
}
2632+
if name != "" {
2633+
t.Errorf("Expected empty function name, got: %s", name)
2634+
}
2635+
if err.Error() != "function is nil" {
2636+
t.Errorf("Expected error 'function is nil', got: %v", err)
2637+
}
2638+
})
2639+
}
2640+
2641+
func TestWriteClusterIDToMySQLTiDBWhenUpgradingTo242(t *testing.T) {
2642+
ctx := context.Background()
2643+
store, dom := CreateStoreAndBootstrap(t)
2644+
defer func() { require.NoError(t, store.Close()) }()
2645+
2646+
// `cluster_id` is inserted for a new TiDB cluster.
2647+
se := CreateSessionAndSetID(t, store)
2648+
r := MustExecToRecodeSet(t, se, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2649+
req := r.NewChunk(nil)
2650+
err := r.Next(ctx, req)
2651+
require.NoError(t, err)
2652+
require.Equal(t, 1, req.NumRows())
2653+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2654+
require.NoError(t, r.Close())
2655+
se.Close()
2656+
2657+
// bootstrap as version241
2658+
ver241 := version241
2659+
seV241 := CreateSessionAndSetID(t, store)
2660+
txn, err := store.Begin()
2661+
require.NoError(t, err)
2662+
m := meta.NewMutator(txn)
2663+
err = m.FinishBootstrap(int64(ver241))
2664+
require.NoError(t, err)
2665+
revertVersionAndVariables(t, seV241, ver241)
2666+
// remove the cluster_id entry from mysql.tidb table
2667+
MustExec(t, seV241, "delete from mysql.tidb where variable_name='cluster_id'")
2668+
err = txn.Commit(ctx)
2669+
require.NoError(t, err)
2670+
store.SetOption(StoreBootstrappedKey, nil)
2671+
ver, err := getBootstrapVersion(seV241)
2672+
require.NoError(t, err)
2673+
require.Equal(t, int64(ver241), ver)
2674+
seV241.Close()
2675+
2676+
// upgrade to current version
2677+
dom.Close()
2678+
domCurVer, err := BootstrapSession(store)
2679+
require.NoError(t, err)
2680+
defer domCurVer.Close()
2681+
seCurVer := CreateSessionAndSetID(t, store)
2682+
ver, err = getBootstrapVersion(seCurVer)
2683+
require.NoError(t, err)
2684+
require.Equal(t, currentBootstrapVersion, ver)
2685+
2686+
// check if the cluster_id has been set in the `mysql.tidb` table during upgrade
2687+
r = MustExecToRecodeSet(t, seCurVer, `select VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME='cluster_id'`)
2688+
req = r.NewChunk(nil)
2689+
err = r.Next(ctx, req)
2690+
require.NoError(t, err)
2691+
require.Equal(t, 1, req.NumRows())
2692+
require.NotEmpty(t, req.GetRow(0).GetBytes(0))
2693+
require.NoError(t, r.Close())
2694+
seCurVer.Close()
2695+
}
2696+
>>>>>>> f6bf8e8bec5 (bootstrap: add `cluster_id` to the `mysql.tidb` table (#59511))

0 commit comments

Comments
 (0)