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

*: move config file options prepared-plan-cache.* to sysvars #34752

Closed
wants to merge 0 commits into from

Conversation

qw4990
Copy link
Contributor

@qw4990 qw4990 commented May 17, 2022

What problem does this PR solve?

Issue Number: ref #33769, close #30168

Previous closing PR: #33836

Problem Summary:

The options prepared-plan-cache.* have historically been config options. But based on requirements from cloud & PM they should instead be sysvars.

What is changed and how it works?

Remove them from the config list and add them to global sysvars.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

The TiDB configuration file options `prepared-plan-cache-*` has been replaced by the system variables `tidb_enable_prepared_plan_cache`, `tidb_prepared_plan_cache_size`, and `tidb_prepared_plan_cache_memory_guard_ratio`. This change makes it easier to modify this setting on a cluster wide basis.

@qw4990 qw4990 requested a review from a team as a code owner May 17, 2022 14:19
@ti-chi-bot
Copy link
Member

ti-chi-bot commented May 17, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • tiancaiamao

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 17, 2022
@qw4990 qw4990 changed the title [WIP] *: move config file options prepared-plan-cache.* to sysvars *: move config file options prepared-plan-cache.* to sysvars May 17, 2022
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2022
@sre-bot
Copy link
Contributor

sre-bot commented May 17, 2022

@morgo morgo self-requested a review May 17, 2022 19:04
@morgo
Copy link
Contributor

morgo commented May 17, 2022

Looking good. Please run go fmt though ;-)

config/config.go Outdated Show resolved Hide resolved
@qw4990
Copy link
Contributor Author

qw4990 commented May 18, 2022

Looking good. Please run go fmt though ;-)

Updated, PTAL

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label May 18, 2022
@bb7133
Copy link
Member

bb7133 commented May 18, 2022

Hi @qw4990 , please consider introducing an upgrade function to make sure that the 'old' configuration values can be inherited when users upgrade the their clusters to v6.1, just like:

https://github.com/pingcap/tidb/blob/381e870c5ca018d9b5b6e89233d2cbd9a3b41cac/session/bootstrap.go/#L1843-L1849

@bb7133 bb7133 added the compatibility-breaker Violation of forwards/backwards compatibility in a design-time piece. label May 18, 2022
Comment on lines 665 to 672
variable.EnablePreparedPlanCache.Store(config.CheckTableBeforeDrop)
// use server-memory-quota as max-plan-cache-memory
variable.PreparedPlanCacheMaxMemory.Store(cfg.Performance.ServerMemoryQuota)
total, err := memory.MemTotal()
terror.MustNil(err)
// if server-memory-quota is larger than max-system-memory or not set, use max-system-memory as max-plan-cache-memory
if variable.PreparedPlanCacheMaxMemory.Load() > total || variable.PreparedPlanCacheMaxMemory.Load() <= 0 {
variable.PreparedPlanCacheMaxMemory.Store(total)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's best to remove this code. The reason being that these values will get overwritten by the SetGlobal funcs anyway when the sysvar cache is populated. So if there are behaviors that are required here they should be in SetGlobal.

The SetGlobal func is also called periodically when the cache is reloaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, but actually PreparedPlanCacheMaxMemory is not a system variable, it's just a pure Golang variable to store the max memory usage of the plan cache and needs to be initialized when starting. I've moved it from variable package back to planner/core package.

Comment on lines 747 to 761
{Scope: ScopeGlobal, Name: TiDBEnablePrepPlanCache, Value: BoolToOnOff(DefTiDBEnablePrepPlanCache), Type: TypeBool, SetGlobal: func(s *SessionVars, val string) error {
EnablePreparedPlanCache.Store(TiDBOptOn(val))
return nil
}, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(EnablePreparedPlanCache.Load()), nil
}},
{Scope: ScopeGlobal, Name: TiDBPrepPlanCacheSize, Value: strconv.FormatUint(uint64(DefTiDBPrepPlanCacheSize), 10), Type: TypeUnsigned, MinValue: 1, MaxValue: 100000, SetGlobal: func(s *SessionVars, val string) error {
uVal, err := strconv.ParseUint(val, 10, 64)
if err == nil {
PreparedPlanCacheSize.Store(uVal)
}
return err
}, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatUint(PreparedPlanCacheSize.Load(), 10), nil
}},
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to combine these two sysvars so that CacheSize > 0 means enabled?

} else {
atomic.StoreInt32(&preparedPlanCacheEnabledValue, preparedPlanCacheUnable)
}
variable.EnablePreparedPlanCache.Store(isEnabled)
Copy link
Contributor

Choose a reason for hiding this comment

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

Be aware that this is not permanent. When the sysvar cache is refreshed, it will call the SetGlobal func with whatever is the value of the setting in the mysql.global_variables table.

So if this is used by tests, it is better to use tk.MustExec("SET sysvar = x").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Actually, I've tried to use tk.MustExec("set ...") in tests, but it'll cause lots of code changes. So for safety and to make it easier to review, I finally use this way and plan to refactor all plan cache tests to use tk.MustExec(set session var...) in the next Sprint after we implement session-level variables for plan-cache.

@qw4990
Copy link
Contributor Author

qw4990 commented May 18, 2022

Hi @qw4990 , please consider introducing an upgrade function to make sure that the 'old' configuration values can be inherited when users upgrade the their clusters to v6.1, just like:

https://github.com/pingcap/tidb/blob/381e870c5ca018d9b5b6e89233d2cbd9a3b41cac/session/bootstrap.go/#L1843-L1849

Updated, PTAL

@qw4990 qw4990 closed this May 18, 2022
@ti-chi-bot ti-chi-bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility-breaker Violation of forwards/backwards compatibility in a design-time piece. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce a new global variable as the switch for Plan Cache
6 participants