From d9b15e26c03fda705207e2ff42e75ca42c2f14aa Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 15 Feb 2023 13:18:48 +0800 Subject: [PATCH 1/7] session: change the global variable foreign_key_checks/tidb_enable_foreign_key value from off to on when upgrade Signed-off-by: crazycs520 --- session/bootstrap.go | 16 +++++++++- session/bootstrap_test.go | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index 69a5345e40791..263fb490fe308 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -794,11 +794,15 @@ const ( version111 = 111 // version112 modifies the view tidb_mdl_view version112 = 112 + // version113 modifies the following global variables default value: + // - foreign_key_checks: off -> on + // - tidb_enable_foreign_key: off -> on + 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 +922,7 @@ var ( upgradeToVer110, upgradeToVer111, upgradeToVer112, + upgradeToVer113, } ) @@ -2285,6 +2290,15 @@ func upgradeToVer112(s Session, ver int64) { doReentrantDDL(s, CreateMDLView) } +func upgradeToVer113(s Session, ver int64) { + if ver >= version113 { + return + } + + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.ForeignKeyChecks, variable.On) + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On) +} + 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..cb95647b7205d 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1417,3 +1417,64 @@ func TestTiDBGCAwareUpgradeFrom630To650(t *testing.T) { require.Equal(t, 2, row.Len()) require.Equal(t, "0", row.GetString(1)) } + +func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { + ctx := context.Background() + store, _ := createStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // upgrade from 6.3.0 to 6.6.0. + ver630 := version93 + seV630 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver630)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV630, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver630)) + mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableForeignKey)) + mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.ForeignKeyChecks)) + mustExec(t, seV630, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV630) + require.NoError(t, err) + require.Equal(t, int64(ver630), ver) + + // We are now in 6.3.0. + upgradeVars := []string{variable.TiDBEnableForeignKey, variable.ForeignKeyChecks} + varsValueList := []string{"OFF", "OFF"} + for i := range upgradeVars { + res := mustExecToRecodeSet(t, seV630, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i])) + 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, varsValueList[i], row.GetString(1)) + } + + // Upgrade to 6.6.0. + domCurVer, err := BootstrapSession(store) + require.NoError(t, err) + defer domCurVer.Close() + seV660 := createSessionAndSetID(t, store) + ver, err = getBootstrapVersion(seV660) + require.NoError(t, err) + require.Equal(t, currentBootstrapVersion, ver) + + // We are now in 6.6.0. + varsValueList = []string{"ON", "ON"} + for i := range upgradeVars { + res := mustExecToRecodeSet(t, seV660, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i])) + 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, varsValueList[i], row.GetString(1)) + } +} From a9f6e41f64f11155b63964917836c6a115930d36 Mon Sep 17 00:00:00 2001 From: yisaer Date: Wed, 15 Feb 2023 15:06:01 +0800 Subject: [PATCH 2/7] add variable --- session/bootstrap.go | 2 ++ session/bootstrap_test.go | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index 263fb490fe308..c937e1748b659 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -2297,6 +2297,8 @@ func upgradeToVer113(s Session, ver int64) { mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.ForeignKeyChecks, variable.On) mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On) + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableHistoricalStats, variable.On) + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnablePlanReplayerCapture, variable.On) } func writeOOMAction(s Session) { diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index cb95647b7205d..f20530c46bff4 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1436,6 +1436,8 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { mustExec(t, seV630, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver630)) mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableForeignKey)) mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.ForeignKeyChecks)) + mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableHistoricalStats)) + mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnablePlanReplayerCapture)) mustExec(t, seV630, "commit") unsetStoreBootstrapped(store.UUID()) ver, err := getBootstrapVersion(seV630) @@ -1443,8 +1445,8 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { require.Equal(t, int64(ver630), ver) // We are now in 6.3.0. - upgradeVars := []string{variable.TiDBEnableForeignKey, variable.ForeignKeyChecks} - varsValueList := []string{"OFF", "OFF"} + upgradeVars := []string{variable.TiDBEnableForeignKey, variable.ForeignKeyChecks, variable.TiDBEnableHistoricalStats, variable.TiDBEnablePlanReplayerCapture} + varsValueList := []string{"OFF", "OFF", "OFF", "OFF"} for i := range upgradeVars { res := mustExecToRecodeSet(t, seV630, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i])) chk := res.NewChunk(nil) @@ -1466,7 +1468,7 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { require.Equal(t, currentBootstrapVersion, ver) // We are now in 6.6.0. - varsValueList = []string{"ON", "ON"} + varsValueList = []string{"ON", "ON", "ON", "ON"} for i := range upgradeVars { res := mustExecToRecodeSet(t, seV660, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i])) chk := res.NewChunk(nil) @@ -1478,3 +1480,7 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { require.Equal(t, varsValueList[i], row.GetString(1)) } } + +func TestTiDBGlobalVariablesDefaultValueUpgradeFrom650To660(t *testing.T) { + +} From efc8cd2b661f187912bf9a280acd5193dcc26751 Mon Sep 17 00:00:00 2001 From: crazycs Date: Wed, 15 Feb 2023 15:08:58 +0800 Subject: [PATCH 3/7] Update session/bootstrap_test.go --- session/bootstrap_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index f20530c46bff4..e69561e6858b6 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1481,6 +1481,3 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { } } -func TestTiDBGlobalVariablesDefaultValueUpgradeFrom650To660(t *testing.T) { - -} From 6ec9072c7347b32664ff38e63d096850a55a70d5 Mon Sep 17 00:00:00 2001 From: yisaer Date: Wed, 15 Feb 2023 15:10:08 +0800 Subject: [PATCH 4/7] add variable --- session/bootstrap_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index f20530c46bff4..c623041ea00cf 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1480,7 +1480,3 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { require.Equal(t, varsValueList[i], row.GetString(1)) } } - -func TestTiDBGlobalVariablesDefaultValueUpgradeFrom650To660(t *testing.T) { - -} From 3941f4e02d14b74f53daa7b32298f8c5e6f9785e Mon Sep 17 00:00:00 2001 From: you06 Date: Wed, 15 Feb 2023 15:00:46 +0800 Subject: [PATCH 5/7] session: change the global variable tidb_store_batch_size value from 0 to 4 when upgrade Signed-off-by: you06 --- session/bootstrap.go | 16 +++++++++ session/bootstrap_test.go | 72 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index c937e1748b659..0a4e3f4f70711 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -797,6 +797,7 @@ const ( // version113 modifies the following global variables default value: // - foreign_key_checks: off -> on // - tidb_enable_foreign_key: off -> on + // - tidb_store_batch_size: 0 -> 4 version113 = 113 ) @@ -2299,6 +2300,21 @@ func upgradeToVer113(s Session, ver int64) { mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On) mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableHistoricalStats, variable.On) mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnablePlanReplayerCapture, variable.On) + + ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) + rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBStoreBatchSize) + terror.MustNil(err) + chk := rs.NewChunk(nil) + err = rs.Next(ctx, chk) + terror.MustNil(err) + if chk.NumRows() == 1 { + row := chk.GetRow(0) + if row.GetString(0) == "0" { + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBStoreBatchSize, "4") + } + } } func writeOOMAction(s Session) { diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index c623041ea00cf..6ddbf9e2f17a3 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1231,7 +1231,7 @@ func TestTiDBCostModelInNewCluster(t *testing.T) { defer dom.Close() se := createSessionAndSetID(t, store) - // In a new created cluster(above 6.5+), tidb_cost_model_version is 2 by default. + // In a new created cluster(above 6.5+), tidb_ost_model_version is 2 by default. mustExec(t, se, "use test;") r := mustExecToRecodeSet(t, se, "select @@tidb_cost_model_version;") require.NotNil(t, r) @@ -1480,3 +1480,73 @@ func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) { require.Equal(t, varsValueList[i], row.GetString(1)) } } + +func TestTiDBStoreBatchSizeUpgradeFrom650To660(t *testing.T) { + for i := 0; i < 2; i++ { + func() { + ctx := context.Background() + store, dom := createStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // upgrade from 6.5 to 6.6. + ver65 := version112 + seV65 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver65)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV65, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver65)) + mustExec(t, seV65, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBStoreBatchSize)) + mustExec(t, seV65, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV65) + require.NoError(t, err) + require.Equal(t, int64(ver65), ver) + + // We are now in 6.5, tidb_store_batch_size is 0. + res := mustExecToRecodeSet(t, seV65, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBStoreBatchSize)) + 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)) + res.Close() + + if i == 0 { + // For the first time, We set tidb_store_batch_size to 1. + // And after upgrade to 6.6, tidb_store_batch_size should be 1. + // For the second it should be the latest default value. + mustExec(t, seV65, "set global tidb_store_batch_size = 1") + } + dom.Close() + // Upgrade to 6.6. + 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.6. + res = mustExecToRecodeSet(t, seCurVer, "select @@tidb_store_batch_size") + 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, 1, row.Len()) + if i == 0 { + require.Equal(t, "1", row.GetString(0)) + } else { + require.Equal(t, "4", row.GetString(0)) + } + res.Close() + }() + } +} From f6120bed35f54ff9ab8f2aadf87a41cd3ced04b1 Mon Sep 17 00:00:00 2001 From: you06 Date: Wed, 15 Feb 2023 15:10:46 +0800 Subject: [PATCH 6/7] restore unexpected change Signed-off-by: you06 --- session/bootstrap_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index 6ddbf9e2f17a3..832ba478dfed3 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1231,7 +1231,7 @@ func TestTiDBCostModelInNewCluster(t *testing.T) { defer dom.Close() se := createSessionAndSetID(t, store) - // In a new created cluster(above 6.5+), tidb_ost_model_version is 2 by default. + // In a new created cluster(above 6.5+), tidb_cost_model_version is 2 by default. mustExec(t, se, "use test;") r := mustExecToRecodeSet(t, se, "select @@tidb_cost_model_version;") require.NotNil(t, r) From 6d1064084bb7498daac70183f3dbe42c216914b7 Mon Sep 17 00:00:00 2001 From: you06 Date: Wed, 15 Feb 2023 15:23:57 +0800 Subject: [PATCH 7/7] use update statement Signed-off-by: you06 --- session/bootstrap.go | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index 0a4e3f4f70711..e2ef6f854e594 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -2300,21 +2300,7 @@ func upgradeToVer113(s Session, ver int64) { mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On) mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableHistoricalStats, variable.On) mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnablePlanReplayerCapture, variable.On) - - ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) - rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;", - mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBStoreBatchSize) - terror.MustNil(err) - chk := rs.NewChunk(nil) - err = rs.Next(ctx, chk) - terror.MustNil(err) - if chk.NumRows() == 1 { - row := chk.GetRow(0) - if row.GetString(0) == "0" { - mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", - mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBStoreBatchSize, "4") - } - } + mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n SET VARIABLE_VALUE = %? WHERE VARIABLE_NAME = %? AND VARIABLE_VALUE = %?;", mysql.SystemDB, mysql.GlobalVariablesTable, "4", variable.TiDBStoreBatchSize, "0") } func writeOOMAction(s Session) {