From a585ba752e2e0d0f8ff0e72b1771bc9360631ffb Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Wed, 15 Feb 2023 15:49:57 +0800 Subject: [PATCH 1/2] session: set tidb_server_memory_limit to 80% during bootstrap --- 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 69a5345e40791..ad23c02168bd6 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -794,11 +794,13 @@ const ( version111 = 111 // version112 modifies the view tidb_mdl_view version112 = 112 + // version113 sets tidb_server_memory_limit to "80%" + version113 = 113 ) // 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 = version112 +var currentBootstrapVersion int64 = version113 // 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 @@ -918,6 +920,7 @@ var ( upgradeToVer110, upgradeToVer111, upgradeToVer112, + upgradeToVer113, } ) @@ -2285,6 +2288,14 @@ func upgradeToVer112(s Session, ver int64) { doReentrantDDL(s, CreateMDLView) } +func upgradeToVer113(s Session, ver int64) { + if ver >= version113 { + return + } + mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n set VARIABLE_VALUE = %? where VARIABLE_NAME = %? and VARIABLE_VALUE = %?;", + mysql.SystemDB, mysql.GlobalVariablesTable, "80%", 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 d126c1f7549f9..2e63ad92873c4 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1417,3 +1417,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+. + ver112 := version112 + seV112 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver112)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV112, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver112)) + mustExec(t, seV112, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBServerMemoryLimit)) + mustExec(t, seV112, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV112) + require.NoError(t, err) + require.Equal(t, int64(ver112), ver) + + // We are now in 6.5.0, tidb_server_memory_limit is 0. + res := mustExecToRecodeSet(t, seV112, 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 = mustExecToRecodeSet(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, "80%", 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+. + ver112 := version112 + seV112 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver112)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV112, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver112)) + mustExec(t, seV112, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "70%", variable.TiDBServerMemoryLimit)) + mustExec(t, seV112, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV112) + require.NoError(t, err) + require.Equal(t, int64(ver112), ver) + + // We are now in 6.5.0, tidb_server_memory_limit is "70%". + res := mustExecToRecodeSet(t, seV112, 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 = mustExecToRecodeSet(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)) +} From 306fa5dc1f2ea555dbcbd05276e3ae43cce713a5 Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Wed, 15 Feb 2023 16:01:51 +0800 Subject: [PATCH 2/2] address comment --- session/bootstrap.go | 2 +- session/bootstrap_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index ad23c02168bd6..0aa613f5edcfb 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -2293,7 +2293,7 @@ func upgradeToVer113(s Session, ver int64) { return } mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n set VARIABLE_VALUE = %? where VARIABLE_NAME = %? and VARIABLE_VALUE = %?;", - mysql.SystemDB, mysql.GlobalVariablesTable, "80%", variable.TiDBServerMemoryLimit, "0") + mysql.SystemDB, mysql.GlobalVariablesTable, variable.DefTiDBServerMemoryLimit, variable.TiDBServerMemoryLimit, "0") } func writeOOMAction(s Session) { diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index 2e63ad92873c4..9c3178713ccf9 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1468,7 +1468,7 @@ func TestTiDBServerMemoryLimitUpgradeTo651_1(t *testing.T) { require.Equal(t, 1, chk.NumRows()) row = chk.GetRow(0) require.Equal(t, 2, row.Len()) - require.Equal(t, "80%", row.GetString(1)) + require.Equal(t, variable.DefTiDBServerMemoryLimit, row.GetString(1)) } func TestTiDBServerMemoryLimitUpgradeTo651_2(t *testing.T) {