Skip to content

Commit

Permalink
planner: update the variable name related to Instance Plan Cache (#56068
Browse files Browse the repository at this point in the history
)

ref #54057
  • Loading branch information
qw4990 committed Sep 18, 2024
1 parent 12e5b31 commit 3ae6470
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 134 deletions.
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

0 comments on commit 3ae6470

Please sign in to comment.