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 4 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
20 changes: 7 additions & 13 deletions pkg/sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,19 +1348,13 @@ 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: "0.1", Type: TypeFloat, MinValue: 0, MaxValue: 1,
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
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, 0.1)
InstancePlanCacheReservedPercentage.Store(v)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBInstancePlanCacheMaxMemSize, Value: strconv.Itoa(int(DefTiDBInstancePlanCacheMaxMemSize)), Type: TypeStr,
Expand All @@ -1370,10 +1364,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
31 changes: 15 additions & 16 deletions pkg/sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,10 @@ const (
// TiDBEnableInstancePlanCache indicates whether to enable instance plan cache.
// If this variable is false, session-level plan cache will be used.
TiDBEnableInstancePlanCache = "tidb_enable_instance_plan_cache"
// TiDBInstancePlanCacheTargetMemSize indicates the target memory size of instance plan cache.
TiDBInstancePlanCacheTargetMemSize = "tidb_instance_plan_cache_target_mem_size"
// TiDBInstancePlanCacheReservedPercentage indicates the percentage memory to evict.
TiDBInstancePlanCacheReservedPercentage = "tidb_instance_plan_cache_reserved_percentage"
// TiDBInstancePlanCacheMaxMemSize indicates the maximum memory size of instance plan cache.
TiDBInstancePlanCacheMaxMemSize = "tidb_instance_plan_cache_max_mem_size"
TiDBInstancePlanCacheMaxMemSize = "tidb_instance_plan_cache_max_size"

// TiDBConstraintCheckInPlacePessimistic controls whether to skip certain kinds of pessimistic locks.
TiDBConstraintCheckInPlacePessimistic = "tidb_constraint_check_in_place_pessimistic"
Expand Down Expand Up @@ -1440,8 +1440,7 @@ const (
DefTiDBEnableNonPreparedPlanCacheForDML = false
DefTiDBNonPreparedPlanCacheSize = 100
DefTiDBPlanCacheMaxPlanSize = 2 * size.MB
DefTiDBInstancePlanCacheTargetMemSize = 100 * size.MB
DefTiDBInstancePlanCacheMaxMemSize = 120 * size.MB
DefTiDBInstancePlanCacheMaxMemSize = 100 * size.MB
// MaxDDLReorgBatchSize is exported for testing.
MaxDDLReorgBatchSize int32 = 10240
MinDDLReorgBatchSize int32 = 32
Expand Down Expand Up @@ -1588,17 +1587,17 @@ var (
OOMAction = atomic.NewString(DefTiDBMemOOMAction)
MaxAutoAnalyzeTime = atomic.NewInt64(DefTiDBMaxAutoAnalyzeTime)
// variables for plan cache
PreparedPlanCacheMemoryGuardRatio = atomic.NewFloat64(DefTiDBPrepPlanCacheMemoryGuardRatio)
EnableInstancePlanCache = atomic.NewBool(false)
InstancePlanCacheTargetMemSize = atomic.NewInt64(int64(DefTiDBInstancePlanCacheTargetMemSize))
InstancePlanCacheMaxMemSize = atomic.NewInt64(int64(DefTiDBInstancePlanCacheMaxMemSize))
EnableDistTask = atomic.NewBool(DefTiDBEnableDistTask)
EnableFastCreateTable = atomic.NewBool(DefTiDBEnableFastCreateTable)
DDLForce2Queue = atomic.NewBool(false)
EnableNoopVariables = atomic.NewBool(DefTiDBEnableNoopVariables)
EnableMDL = atomic.NewBool(false)
AutoAnalyzePartitionBatchSize = atomic.NewInt64(DefTiDBAutoAnalyzePartitionBatchSize)
AutoAnalyzeConcurrency = atomic.NewInt32(DefTiDBAutoAnalyzeConcurrency)
PreparedPlanCacheMemoryGuardRatio = atomic.NewFloat64(DefTiDBPrepPlanCacheMemoryGuardRatio)
EnableInstancePlanCache = atomic.NewBool(false)
InstancePlanCacheReservedPercentage = atomic.NewFloat64(0.1)
InstancePlanCacheMaxMemSize = atomic.NewInt64(int64(DefTiDBInstancePlanCacheMaxMemSize))
EnableDistTask = atomic.NewBool(DefTiDBEnableDistTask)
EnableFastCreateTable = atomic.NewBool(DefTiDBEnableFastCreateTable)
DDLForce2Queue = atomic.NewBool(false)
EnableNoopVariables = atomic.NewBool(DefTiDBEnableNoopVariables)
EnableMDL = atomic.NewBool(false)
AutoAnalyzePartitionBatchSize = atomic.NewInt64(DefTiDBAutoAnalyzePartitionBatchSize)
AutoAnalyzeConcurrency = atomic.NewInt32(DefTiDBAutoAnalyzeConcurrency)
// EnableFastReorg indicates whether to use lightning to enhance DDL reorg performance.
EnableFastReorg = atomic.NewBool(DefTiDBEnableFastReorg)
// DDLDiskQuota is the temporary variable for set disk quota for lightning
Expand Down