Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: introduce a new variable tidb_session_plan_cache_size #43140

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 19 additions & 39 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,35 +766,15 @@ func TestSetVar(t *testing.T) {
tk.MustGetErrCode("set global init_connect = 'invalidstring'", mysql.ErrWrongTypeForVar)
tk.MustExec("set global init_connect = 'select now(); select timestamp()'")

// test variable 'tidb_enable_non_prepared_plan_cache'
// test variable 'tidb_session_plan_cache_size'
// global scope
tk.MustQuery("select @@global.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("0")) // default value
tk.MustExec("set global tidb_enable_non_prepared_plan_cache = 1")
tk.MustQuery("select @@global.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("1"))
tk.MustExec("set global tidb_enable_non_prepared_plan_cache = 0")
tk.MustQuery("select @@global.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("0"))
tk.MustQuery("select @@global.tidb_session_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("set global tidb_session_plan_cache_size = 1")
tk.MustQuery("select @@global.tidb_session_plan_cache_size").Check(testkit.Rows("1"))
// session scope
tk.MustQuery("select @@session.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("0")) // default value
tk.MustExec("set session tidb_enable_non_prepared_plan_cache = 1")
tk.MustQuery("select @@session.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("1"))
tk.MustExec("set session tidb_enable_non_prepared_plan_cache = 0")
tk.MustQuery("select @@session.tidb_enable_non_prepared_plan_cache").Check(testkit.Rows("0"))

// test variable 'tidb_non_prepared_plan_cache-size'
// global scope
tk.MustQuery("select @@global.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("set global tidb_non_prepared_plan_cache_size = 200")
tk.MustQuery("select @@global.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("200"))
tk.MustExec("set global tidb_non_prepared_plan_cache_size = 200000000") // overflow
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_non_prepared_plan_cache_size value: '200000000'"))
tk.MustQuery("select @@global.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("100000"))
// session scope
tk.MustQuery("select @@session.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("set session tidb_non_prepared_plan_cache_size = 300")
tk.MustQuery("select @@session.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("300"))
tk.MustExec("set session tidb_non_prepared_plan_cache_size = -1") // underflow
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_non_prepared_plan_cache_size value: '-1'"))
tk.MustQuery("select @@session.tidb_non_prepared_plan_cache_size").Check(testkit.Rows("1"))
tk.MustQuery("select @@session.tidb_session_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("set session tidb_session_plan_cache_size = 1")
tk.MustQuery("select @@session.tidb_session_plan_cache_size").Check(testkit.Rows("1"))

// test variable 'foreign_key_checks'
// global scope
Expand Down Expand Up @@ -1912,21 +1892,21 @@ func TestPreparePlanCacheValid(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
// global scope
tk.MustQuery("select @@global.tidb_prepared_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("SET GLOBAL tidb_prepared_plan_cache_size = 0")
tk.MustQuery("select @@global.tidb_session_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("SET GLOBAL tidb_session_plan_cache_size = 0")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1292 Truncated incorrect tidb_prepared_plan_cache_size value: '0'"))
tk.MustQuery("select @@global.tidb_prepared_plan_cache_size").Check(testkit.Rows("1"))
tk.MustExec("SET GLOBAL tidb_prepared_plan_cache_size = 2")
tk.MustQuery("select @@global.tidb_prepared_plan_cache_size").Check(testkit.Rows("2"))
"Warning 1292 Truncated incorrect tidb_session_plan_cache_size value: '0'"))
tk.MustQuery("select @@global.tidb_session_plan_cache_size").Check(testkit.Rows("1"))
tk.MustExec("SET GLOBAL tidb_session_plan_cache_size = 2")
tk.MustQuery("select @@global.tidb_session_plan_cache_size").Check(testkit.Rows("2"))
// session scope
tk.MustQuery("select @@session.tidb_prepared_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("SET SESSION tidb_prepared_plan_cache_size = 0")
tk.MustQuery("select @@session.tidb_session_plan_cache_size").Check(testkit.Rows("100")) // default value
tk.MustExec("SET SESSION tidb_session_plan_cache_size = 0")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1292 Truncated incorrect tidb_prepared_plan_cache_size value: '0'"))
tk.MustQuery("select @@session.tidb_prepared_plan_cache_size").Check(testkit.Rows("1"))
tk.MustExec("SET SESSION tidb_prepared_plan_cache_size = 2")
tk.MustQuery("select @@session.tidb_prepared_plan_cache_size").Check(testkit.Rows("2"))
"Warning 1292 Truncated incorrect tidb_session_plan_cache_size value: '0'"))
tk.MustQuery("select @@session.tidb_session_plan_cache_size").Check(testkit.Rows("1"))
tk.MustExec("SET SESSION tidb_session_plan_cache_size = 2")
tk.MustQuery("select @@session.tidb_session_plan_cache_size").Check(testkit.Rows("2"))

tk.MustExec("SET GLOBAL tidb_prepared_plan_cache_memory_guard_ratio = -0.1")
tk.MustQuery("show warnings").Check(testkit.Rows(
Expand Down
28 changes: 28 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +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`.
// This will only happens when we upgrade a cluster before 7.1.
version141 = 141
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
Expand Down Expand Up @@ -987,6 +990,7 @@ var (
upgradeToVer138,
upgradeToVer139,
upgradeToVer140,
upgradeToVer141,
}
)

Expand Down Expand Up @@ -2439,6 +2443,30 @@ 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.
func upgradeToVer141(s Session, ver int64) {
if ver >= version141 {
return
}
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.TiDBPrepPlanCacheSize)
terror.MustNil(err)
req := rs.NewChunk(nil)
err = rs.Next(ctx, req)
if err != nil || req.NumRows() == 0 {
return
}
row := req.GetRow(0)
if row.IsNull(0) {
return
}
val := row.GetString(0)

mustExecute(s, "INSERT HIGH_PRIORITY IGNORE INTO %n.%n VALUES (%?, %?);",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBSessionPlanCacheSize, val)
}

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= %?`,
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (s *session) GetSessionPlanCache() sessionctx.PlanCache {
return nil
}
if s.sessionPlanCache == nil { // lazy construction
s.sessionPlanCache = plannercore.NewLRUPlanCache(uint(s.GetSessionVars().PreparedPlanCacheSize),
s.sessionPlanCache = plannercore.NewLRUPlanCache(uint(s.GetSessionVars().SessionPlanCacheSize),
variable.PreparedPlanCacheMemoryGuardRatio.Load(), plannercore.PreparedPlanCacheMaxMemory.Load(), s, false)
}
return s.sessionPlanCache
Expand Down
5 changes: 4 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,9 @@ type SessionVars struct {
// PlanCacheMaxPlanSize controls the maximum size of a plan that can be cached.
PlanCacheMaxPlanSize uint64

// SessionPlanCacheSize controls the size of session plan cache.
SessionPlanCacheSize uint64

// ConstraintCheckInPlacePessimistic controls whether to skip the locking of some keys in pessimistic transactions.
// Postpone the conflict check and constraint check to prewrite or later pessimistic locking requests.
ConstraintCheckInPlacePessimistic bool
Expand Down Expand Up @@ -2228,7 +2231,7 @@ func (k planCacheStmtKey) Hash() []byte {
// AddNonPreparedPlanCacheStmt adds this PlanCacheStmt into non-preapred plan-cache stmt cache
func (s *SessionVars) AddNonPreparedPlanCacheStmt(sql string, stmt interface{}) {
if s.nonPreparedPlanCacheStmts == nil {
s.nonPreparedPlanCacheStmts = kvcache.NewSimpleLRUCache(uint(s.NonPreparedPlanCacheSize), 0, 0)
s.nonPreparedPlanCacheStmts = kvcache.NewSimpleLRUCache(uint(s.SessionPlanCacheSize), 0, 0)
}
s.nonPreparedPlanCacheStmts.Put(planCacheStmtKey(sql), stmt)
}
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func TestTransactionContextSavepoint(t *testing.T) {

func TestNonPreparedPlanCacheStmt(t *testing.T) {
sessVars := variable.NewSessionVars(nil)
sessVars.NonPreparedPlanCacheSize = 100
sessVars.SessionPlanCacheSize = 100
sql1 := "select * from t where a>?"
sql2 := "select * from t where a<?"
require.Nil(t, sessVars.GetNonPreparedPlanCacheStmt(sql1))
Expand Down
13 changes: 13 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,9 @@ var defaultSysVars = []*SysVar{
s.PreparedPlanCacheSize = uVal
}
return err
}, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
appendDeprecationWarning(vars, TiDBPrepPlanCacheSize, TiDBSessionPlanCacheSize)
return normalizedValue, nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnablePrepPlanCacheMemoryMonitor, Value: BoolToOnOff(DefTiDBEnablePrepPlanCacheMemoryMonitor), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.EnablePreparedPlanCacheMemoryMonitor = TiDBOptOn(val)
Expand All @@ -1127,6 +1130,9 @@ var defaultSysVars = []*SysVar{
s.NonPreparedPlanCacheSize = uVal
}
return err
}, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
appendDeprecationWarning(vars, TiDBNonPreparedPlanCacheSize, TiDBSessionPlanCacheSize)
return normalizedValue, nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBPlanCacheMaxPlanSize, Value: strconv.FormatUint(DefTiDBPlanCacheMaxPlanSize, 10), Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
uVal, err := strconv.ParseUint(val, 10, 64)
Expand All @@ -1135,6 +1141,13 @@ var defaultSysVars = []*SysVar{
}
return err
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBSessionPlanCacheSize, Value: strconv.FormatUint(uint64(DefTiDBSessionPlanCacheSize), 10), Type: TypeUnsigned, MinValue: 1, MaxValue: 100000, SetSession: func(s *SessionVars, val string) error {
uVal, err := strconv.ParseUint(val, 10, 64)
if err == nil {
s.SessionPlanCacheSize = uVal
}
return err
}},
{Scope: ScopeGlobal, Name: TiDBMemOOMAction, Value: DefTiDBMemOOMAction, PossibleValues: []string{"CANCEL", "LOG"}, Type: TypeEnum,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return OOMAction.Load(), nil
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,16 +773,20 @@ const (
// TiDBEnablePrepPlanCache indicates whether to enable prepared plan cache
TiDBEnablePrepPlanCache = "tidb_enable_prepared_plan_cache"
// TiDBPrepPlanCacheSize indicates the number of cached statements.
// This variable is deprecated, use tidb_session_plan_cache_size instead.
TiDBPrepPlanCacheSize = "tidb_prepared_plan_cache_size"
// TiDBEnablePrepPlanCacheMemoryMonitor indicates whether to enable prepared plan cache monitor
TiDBEnablePrepPlanCacheMemoryMonitor = "tidb_enable_prepared_plan_cache_memory_monitor"

// TiDBEnableNonPreparedPlanCache indicates whether to enable non-prepared plan cache.
TiDBEnableNonPreparedPlanCache = "tidb_enable_non_prepared_plan_cache"
// TiDBNonPreparedPlanCacheSize controls the size of non-prepared plan cache.
// This variable is deprecated, use tidb_session_plan_cache_size instead.
TiDBNonPreparedPlanCacheSize = "tidb_non_prepared_plan_cache_size"
// TiDBPlanCacheMaxPlanSize controls the maximum size of a plan that can be cached.
TiDBPlanCacheMaxPlanSize = "tidb_plan_cache_max_plan_size"
// TiDBSessionPlanCacheSize controls the size of session plan cache.
TiDBSessionPlanCacheSize = "tidb_session_plan_cache_size"

// TiDBConstraintCheckInPlacePessimistic controls whether to skip certain kinds of pessimistic locks.
TiDBConstraintCheckInPlacePessimistic = "tidb_constraint_check_in_place_pessimistic"
Expand Down Expand Up @@ -1174,6 +1178,7 @@ const (
DefTiDBMaxAutoAnalyzeTime = 12 * 60 * 60
DefTiDBEnablePrepPlanCache = true
DefTiDBPrepPlanCacheSize = 100
DefTiDBSessionPlanCacheSize = 100
DefTiDBEnablePrepPlanCacheMemoryMonitor = true
DefTiDBPrepPlanCacheMemoryGuardRatio = 0.1
DefTiDBEnableDistTask = disttask.TiDBEnableDistTask
Expand Down