From d6dfb1607c8bd44a00a4e8558ed7eb60c081088b Mon Sep 17 00:00:00 2001 From: you06 Date: Thu, 20 Apr 2023 01:01:19 +0800 Subject: [PATCH] copr: set the default value of load-based replica read threshold to 1s (#43149) close pingcap/tidb#43148 --- session/bootstrap.go | 8 +++-- session/bootstrap_test.go | 53 ++++++++++++++++++++++++++++++++ sessionctx/variable/sysvar.go | 2 +- sessionctx/variable/tidb_vars.go | 2 +- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index e736731a4b..e031aba5b0 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -854,7 +854,9 @@ const ( version139 = 139 // version 140 add column task_key to mysql.tidb_global_task version140 = 140 - // version 141 set the value of `tidb_session_plan_cache_size` to "tidb_prepared_plan_cache_size" if there is no `tidb_session_plan_cache_size`. + // version 141 + // set the value of `tidb_session_plan_cache_size` to "tidb_prepared_plan_cache_size" if there is no `tidb_session_plan_cache_size`. + // update tidb_load_based_replica_read_threshold from 0 to 4 // This will only happens when we upgrade a cluster before 7.1. version141 = 141 // version 142 insert "tidb_enable_non_prepared_plan_cache|0" to mysql.GLOBAL_VARIABLES if there is no tidb_enable_non_prepared_plan_cache. @@ -2447,7 +2449,8 @@ func upgradeToVer140(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task ADD UNIQUE KEY task_key(task_key)", dbterror.ErrDupKeyName) } -// upgradeToVer141 sets the value of `tidb_session_plan_cache_size` as `tidb_prepared_plan_cache_size` for compatibility. +// upgradeToVer141 sets the value of `tidb_session_plan_cache_size` as `tidb_prepared_plan_cache_size` for compatibility, +// and update tidb_load_based_replica_read_threshold from 0 to 4. func upgradeToVer141(s Session, ver int64) { if ver >= version141 { return @@ -2469,6 +2472,7 @@ func upgradeToVer141(s Session, ver int64) { mustExecute(s, "INSERT HIGH_PRIORITY IGNORE INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBSessionPlanCacheSize, val) + mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBLoadBasedReplicaReadThreshold, variable.DefTiDBLoadBasedReplicaReadThreshold.String()) } func upgradeToVer142(s Session, ver int64) { diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index 06e86e67fa..71cf4d4d37 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -2048,3 +2048,56 @@ func TestTiDBTiDBOptTiDBOptimizerEnableNAAJWhenUpgradingToVer138(t *testing.T) { require.Equal(t, 2, row.Len()) require.Equal(t, "ON", row.GetString(1)) } + +func TestTiDBLoadBasedReplicaReadThresholdUpgradingToVer141(t *testing.T) { + ctx := context.Background() + store, _ := CreateStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // upgrade from 7.0 to 7.1. + ver70 := version139 + seV70 := CreateSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver70)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV70, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver70)) + mustExec(t, seV70, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBLoadBasedReplicaReadThreshold)) + mustExec(t, seV70, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV70) + require.NoError(t, err) + require.Equal(t, int64(ver70), ver) + + // We are now in 7.0, tidb_load_based_replica_read_threshold is 0. + res := mustExecToRecodeSet(t, seV70, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBLoadBasedReplicaReadThreshold)) + 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 7.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 7.1. + res = mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBLoadBasedReplicaReadThreshold)) + 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, "1s", row.GetString(1)) +} diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 8b2e86d86d..8871cce5ac 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -2439,7 +2439,7 @@ var defaultSysVars = []*SysVar{ s.EnableLateMaterialization = TiDBOptOn(val) return nil }}, - {Scope: ScopeGlobal | ScopeSession, Name: TiDBLoadBasedReplicaReadThreshold, Value: time.Duration(DefTiDBLoadBasedReplicaReadThreshold).String(), Type: TypeDuration, MaxValue: uint64(time.Hour), SetSession: func(s *SessionVars, val string) error { + {Scope: ScopeGlobal | ScopeSession, Name: TiDBLoadBasedReplicaReadThreshold, Value: DefTiDBLoadBasedReplicaReadThreshold.String(), Type: TypeDuration, MaxValue: uint64(time.Hour), SetSession: func(s *SessionVars, val string) error { d, err := time.ParseDuration(val) if err != nil { return err diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 84b6b24fb6..b4e9186924 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -1253,7 +1253,7 @@ const ( DefTiDBEnablePlanCacheForParamLimit = true DefTiFlashComputeDispatchPolicy = tiflashcompute.DispatchPolicyConsistentHashStr DefTiDBEnablePlanCacheForSubquery = true - DefTiDBLoadBasedReplicaReadThreshold = 0 + DefTiDBLoadBasedReplicaReadThreshold = time.Second DefTiDBOptEnableLateMaterialization = true DefTiDBOptOrderingIdxSelThresh = 0.0 )