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: update the variable name related to Instance Plan Cache #56068

Merged
merged 6 commits into from
Sep 18, 2024
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
6 changes: 3 additions & 3 deletions pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3110,9 +3110,9 @@ func (do *Domain) StopAutoAnalyze() {

// InitInstancePlanCache initializes the instance level plan cache for this Domain.
func (do *Domain) InitInstancePlanCache() {
softLimit := variable.InstancePlanCacheTargetMemSize.Load()
hardLimit := variable.InstancePlanCacheMaxMemSize.Load()
do.instancePlanCache = NewInstancePlanCache(softLimit, hardLimit)
softLimit := float64(hardLimit) * (1 - variable.InstancePlanCacheReservedPercentage.Load())
do.instancePlanCache = NewInstancePlanCache(int64(softLimit), hardLimit)
// use a separate goroutine to avoid the eviction blocking other operations.
do.wg.Run(do.planCacheEvictTrigger, "planCacheEvictTrigger")
do.wg.Run(do.planCacheMetricsAndVars, "planCacheMetricsAndVars")
Expand All @@ -3136,8 +3136,8 @@ func (do *Domain) planCacheMetricsAndVars() {
select {
case <-ticker.C:
// update limits
softLimit := variable.InstancePlanCacheTargetMemSize.Load()
hardLimit := variable.InstancePlanCacheMaxMemSize.Load()
softLimit := int64(float64(hardLimit) * (1 - variable.InstancePlanCacheReservedPercentage.Load()))
curSoft, curHard := do.instancePlanCache.GetLimits()
if curSoft != softLimit || curHard != hardLimit {
do.instancePlanCache.SetLimits(softLimit, hardLimit)
Expand Down
32 changes: 18 additions & 14 deletions pkg/executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,20 +768,24 @@ func TestSetVar(t *testing.T) {

// test for instance plan cache variables
tk.MustQuery("select @@global.tidb_enable_instance_plan_cache").Check(testkit.Rows("0")) // default 0
tk.MustQuery("select @@global.tidb_instance_plan_cache_target_mem_size").Check(testkit.Rows("104857600"))
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_mem_size").Check(testkit.Rows("125829120"))
tk.MustExecToErr("set global tidb_instance_plan_cache_target_mem_size = 125829121") // target <= max
tk.MustExecToErr("set global tidb_instance_plan_cache_max_mem_size = 104857599")
tk.MustExec("set global tidb_instance_plan_cache_target_mem_size = 114857600")
tk.MustQuery("select @@global.tidb_instance_plan_cache_target_mem_size").Check(testkit.Rows("114857600"))
tk.MustExec("set global tidb_instance_plan_cache_max_mem_size = 135829120")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_mem_size").Check(testkit.Rows("135829120"))
tk.MustExec("set global tidb_instance_plan_cache_max_mem_size = 1GiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_mem_size").Check(testkit.Rows("1073741824"))
tk.MustExec("set global tidb_instance_plan_cache_target_mem_size = 999MiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_target_mem_size").Check(testkit.Rows("1047527424"))
tk.MustExec("set global tidb_instance_plan_cache_target_mem_size = 998MiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_target_mem_size").Check(testkit.Rows("1046478848"))
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("104857600"))
tk.MustExec("set global tidb_instance_plan_cache_max_size = 135829120")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("135829120"))
tk.MustExec("set global tidb_instance_plan_cache_max_size = 999999999")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("999999999"))
tk.MustExec("set global tidb_instance_plan_cache_max_size = 1GiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("1073741824"))
tk.MustExec("set global tidb_instance_plan_cache_max_size = 2GiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("2147483648"))
tk.MustExecToErr("set global tidb_instance_plan_cache_max_size = 2.5GiB")
tk.MustQuery("select @@global.tidb_instance_plan_cache_max_size").Check(testkit.Rows("2147483648"))
tk.MustQuery("select @@global.tidb_instance_plan_cache_reserved_percentage").Check(testkit.Rows("0.1"))
tk.MustExec(`set global tidb_instance_plan_cache_reserved_percentage=1.1`)
tk.MustQuery("select @@global.tidb_instance_plan_cache_reserved_percentage").Check(testkit.Rows("1"))
tk.MustExec(`set global tidb_instance_plan_cache_reserved_percentage=-0.1`)
tk.MustQuery("select @@global.tidb_instance_plan_cache_reserved_percentage").Check(testkit.Rows("0"))
tk.MustExec(`set global tidb_instance_plan_cache_reserved_percentage=0.5`)
tk.MustQuery("select @@global.tidb_instance_plan_cache_reserved_percentage").Check(testkit.Rows("0.5"))

// test variables for cost model ver2
tk.MustQuery("select @@tidb_cost_model_version").Check(testkit.Rows(fmt.Sprintf("%v", variable.DefTiDBCostModelVer)))
Expand Down
22 changes: 9 additions & 13 deletions pkg/sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,19 +1348,15 @@ var defaultSysVars = []*SysVar{
EnableInstancePlanCache.Store(TiDBOptOn(val))
return nil
}},
{Scope: ScopeGlobal, Name: TiDBInstancePlanCacheTargetMemSize, Value: strconv.Itoa(int(DefTiDBInstancePlanCacheTargetMemSize)), Type: TypeStr,
{Scope: ScopeGlobal, Name: TiDBInstancePlanCacheReservedPercentage,
Value: strconv.FormatFloat(DefTiDBInstancePlanCacheReservedPercentage, 'f', -1, 64),
Type: TypeFloat, MinValue: 0, MaxValue: 1,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return strconv.FormatInt(InstancePlanCacheTargetMemSize.Load(), 10), nil
return strconv.FormatFloat(InstancePlanCacheReservedPercentage.Load(), 'f', -1, 64), nil
},
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
v, str := parseByteSize(val)
if str == "" {
v = uint64(TidbOptInt64(val, int64(DefTiDBInstancePlanCacheTargetMemSize)))
}
if v > uint64(InstancePlanCacheMaxMemSize.Load()) {
return errors.New("tidb_instance_plan_cache_target_mem_size must be less than tidb_instance_plan_cache_max_mem_size")
}
InstancePlanCacheTargetMemSize.Store(int64(v))
v := tidbOptFloat64(val, DefTiDBInstancePlanCacheReservedPercentage)
InstancePlanCacheReservedPercentage.Store(v)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBInstancePlanCacheMaxMemSize, Value: strconv.Itoa(int(DefTiDBInstancePlanCacheMaxMemSize)), Type: TypeStr,
Expand All @@ -1370,10 +1366,10 @@ var defaultSysVars = []*SysVar{
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
v, str := parseByteSize(val)
if str == "" {
v = uint64(TidbOptInt64(val, int64(DefTiDBInstancePlanCacheTargetMemSize)))
v = uint64(TidbOptInt64(val, int64(DefTiDBInstancePlanCacheMaxMemSize)))
}
if v < uint64(InstancePlanCacheTargetMemSize.Load()) {
return errors.New("tidb_instance_plan_cache_max_mem_size must be greater than tidb_instance_plan_cache_target_mem_size")
if v < 0 {
return errors.New("tidb_instance_plan_cache_max_mem_size must be a non-negative integer")
}
InstancePlanCacheMaxMemSize.Store(int64(v))
return nil
Expand Down
Loading