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 tidb_general_plan_cache_size for general plan cache #37087

Merged
merged 15 commits into from
Aug 16, 2022
30 changes: 30 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,36 @@ func TestSetVar(t *testing.T) {
tk.MustGetErrCode("set global init_connect = '-1'", mysql.ErrWrongTypeForVar)
tk.MustGetErrCode("set global init_connect = 'invalidstring'", mysql.ErrWrongTypeForVar)
tk.MustExec("set global init_connect = 'select now(); select timestamp()'")

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

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

func TestGetSetNoopVars(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,12 @@ type SessionVars struct {

// EnablePreparedPlanCache indicates whether to enable prepared plan cache.
EnablePreparedPlanCache bool

// EnableGeneralPlanCache indicates whether to enable general plan cache.
EnableGeneralPlanCache bool

// GeneralPlanCacheSize controls the size of general plan cache.
GeneralPlanCacheSize uint64
}

// GetPreparedStmtByName returns the prepared statement specified by stmtName.
Expand Down
11 changes: 11 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,17 @@ var defaultSysVars = []*SysVar{
}, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatFloat(PreparedPlanCacheMemoryGuardRatio.Load(), 'f', -1, 64), nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableGeneralPlanCache, Value: BoolToOnOff(DefTiDBEnableGeneralPlanCache), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.EnableGeneralPlanCache = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBGeneralPlanCacheSize, Value: strconv.FormatUint(uint64(DefTiDBGeneralPlanCacheSize), 10), Type: TypeUnsigned, MinValue: 0, MaxValue: 100000, SetSession: func(s *SessionVars, val string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the minValue for TiDBPrepPlanCacheSize is 1. See line 781. Should we keep them the same?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can change this in the next PR or maybe not changed. Both is OK for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can change this in the next PR or maybe not changed. Both is OK for me.

Got it. Before, minValue = 0 is used to let the general plan cache OFF, I forget to revised back. I 'll fix it in next pr.

uVal, err := strconv.ParseUint(val, 10, 64)
if err == nil {
s.GeneralPlanCacheSize = uVal
}
return err
}},
{Scope: ScopeGlobal, Name: TiDBMemOOMAction, Value: DefTiDBMemOOMAction, PossibleValues: []string{"CANCEL", "LOG"}, Type: TypeEnum,
GetGlobal: func(s *SessionVars) (string, error) {
return OOMAction.Load(), nil
Expand Down
7 changes: 7 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,11 @@ const (
// When set to (0, 1], Selectivity() will use the value of this variable as the default selectivity of those
// functions instead of the selectionFactor (0.8).
TiDBDefaultStrMatchSelectivity = "tidb_default_string_match_selectivity"

// TiDBEnableGeneralPlanCache indicates whether to enable general plan cache.
TiDBEnableGeneralPlanCache = "tidb_enable_general_plan_cache"
// TiDBGeneralPlanCacheSize controls the size of general plan cache.
TiDBGeneralPlanCacheSize = "tidb_general_plan_cache_size"
)

// TiDB vars that have only global scope
Expand Down Expand Up @@ -1007,6 +1012,8 @@ const (
DefTiDBEnableFastReorg = false
DefTiDBDDLDiskQuota = 100 * 1024 * 1024 * 1024 // 100GB
DefExecutorConcurrency = 5
DefTiDBEnableGeneralPlanCache = false
DefTiDBGeneralPlanCacheSize = 100
// MaxDDLReorgBatchSize is exported for testing.
MaxDDLReorgBatchSize int32 = 10240
MinDDLReorgBatchSize int32 = 32
Expand Down