From 215048410fa7419f1d5ff3c596ae7f802b04683e Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 21 Feb 2023 15:01:05 +0800 Subject: [PATCH] session: set tidb_server_memory_limit to defValue during bootstrap (#41438) (#41497) close pingcap/tidb#41434 --- session/bootstrap.go | 13 ++++- session/bootstrap_test.go | 106 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index a83086676bfc7..5246ea320af0a 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -736,11 +736,13 @@ const ( version108 = 108 // version109 sets tidb_enable_gc_aware_memory_track to off when a cluster upgrades from some version lower than v6.5.0. version109 = 109 + // version110 sets tidb_server_memory_limit to "80%" + version110 = 110 ) // 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 = version109 +var currentBootstrapVersion int64 = version110 // 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 @@ -857,6 +859,7 @@ var ( upgradeToVer107, upgradeToVer108, upgradeToVer109, + upgradeToVer110, } ) @@ -2208,6 +2211,14 @@ func upgradeToVer109(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableGCAwareMemoryTrack, 0) } +func upgradeToVer110(s Session, ver int64) { + if ver >= version110 { + return + } + mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n set VARIABLE_VALUE = %? where VARIABLE_NAME = %? and VARIABLE_VALUE = %?;", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.DefTiDBServerMemoryLimit, variable.TiDBServerMemoryLimit, "0") +} + func writeOOMAction(s Session) { comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+" mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index 6cdde0447cf8f..99ad8518a52c5 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1384,3 +1384,109 @@ func TestTiDBGCAwareUpgradeFrom630To650(t *testing.T) { require.Equal(t, 2, row.Len()) require.Equal(t, "0", row.GetString(1)) } + +func TestTiDBServerMemoryLimitUpgradeTo651_1(t *testing.T) { + ctx := context.Background() + store, _ := createStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // upgrade from 6.5.0 to 6.5.1+. + ver109 := version109 + seV109 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver109)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV109, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver109)) + mustExec(t, seV109, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBServerMemoryLimit)) + mustExec(t, seV109, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV109) + require.NoError(t, err) + require.Equal(t, int64(ver109), ver) + + // We are now in 6.5.0, tidb_server_memory_limit is 0. + res := mustExec(t, seV109, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit)) + chk := res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + row := chk.GetRow(0) + require.Equal(t, 2, row.Len()) + require.Equal(t, "0", row.GetString(1)) + + // Upgrade to 6.5.1+. + 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) + + // We are now in 6.5.1+. + res = mustExec(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit)) + chk = res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + row = chk.GetRow(0) + require.Equal(t, 2, row.Len()) + require.Equal(t, variable.DefTiDBServerMemoryLimit, row.GetString(1)) +} + +func TestTiDBServerMemoryLimitUpgradeTo651_2(t *testing.T) { + ctx := context.Background() + store, _ := createStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // upgrade from 6.5.0 to 6.5.1+. + ver109 := version109 + seV109 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver109)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV109, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver109)) + mustExec(t, seV109, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "70%", variable.TiDBServerMemoryLimit)) + mustExec(t, seV109, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV109) + require.NoError(t, err) + require.Equal(t, int64(ver109), ver) + + // We are now in 6.5.0, tidb_server_memory_limit is "70%". + res := mustExec(t, seV109, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit)) + chk := res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + row := chk.GetRow(0) + require.Equal(t, 2, row.Len()) + require.Equal(t, "70%", row.GetString(1)) + + // Upgrade to 6.5.1+. + 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) + + // We are now in 6.5.1+. + res = mustExec(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit)) + chk = res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + row = chk.GetRow(0) + require.Equal(t, 2, row.Len()) + require.Equal(t, "70%", row.GetString(1)) +}