Skip to content

Commit

Permalink
sysvar, config: mapping config settings to system variables. (#33279)
Browse files Browse the repository at this point in the history
ref #32887
  • Loading branch information
CbcWestwolf authored Apr 29, 2022
1 parent d42cf6a commit 0d5ac6f
Show file tree
Hide file tree
Showing 30 changed files with 495 additions and 202 deletions.
2 changes: 1 addition & 1 deletion br/cmd/br/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Init(cmd *cobra.Command) (err error) {
tidbLogCfg.File.Filename = timestampLogFileName()
} else {
// Don't print slow log in br
config.GetGlobalConfig().Log.EnableSlowLog.Store(false)
config.GetGlobalConfig().Instance.EnableSlowLog.Store(false)
}
e = logutil.InitLogger(&tidbLogCfg)
if e != nil {
Expand Down
161 changes: 158 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ const (
DefStatsLoadQueueSizeLimit = 1
// DefMaxOfStatsLoadQueueSizeLimit is maximum limitation of the size of stats-load request queue
DefMaxOfStatsLoadQueueSizeLimit = 100000
// DefDDLSlowOprThreshold sets log DDL operations whose execution time exceeds the threshold value.
DefDDLSlowOprThreshold = 300
// DefExpensiveQueryTimeThreshold indicates the time threshold of expensive query.
DefExpensiveQueryTimeThreshold = 60
// DefMemoryUsageAlarmRatio is the threshold triggering an alarm which the memory usage of tidb-server instance exceeds.
DefMemoryUsageAlarmRatio = 0.8
)

// Valid config maps
Expand All @@ -91,6 +97,51 @@ var (
tempStorageDirName = encodeDefTempStorageDir(os.TempDir(), DefHost, DefStatusHost, DefPort, DefStatusPort)
)

// InstanceConfigSection indicates a config session that has options moved to [instance] session.
type InstanceConfigSection struct {
// SectionName indicates the origin section name.
SectionName string
// NameMappings maps the origin name to the name in [instance].
NameMappings map[string]string
}

var (
// sectionMovedToInstance records all config section and options that should be moved to [instance].
sectionMovedToInstance = []InstanceConfigSection{
{
"",
map[string]string{
"check-mb4-value-in-utf8": "tidb_check_mb4_value_in_utf8",
"enable-collect-execution-info": "tidb_enable_collect_execution_info",
"plugin.load": "plugin_load",
"plugin.dir": "plugin_dir",
},
},
{
"log",
map[string]string{
"enable-slow-log": "tidb_enable_slow_log",
"slow-threshold": "tidb_slow_log_threshold",
"record-plan-in-slow-log": "tidb_record_plan_in_slow_log",
},
},
{
"performance",
map[string]string{
"force-priority": "tidb_force_priority",
"memory-usage-alarm-ratio": "tidb_memory_usage_alarm_ratio",
},
},
}

// ConflictOptions indicates the conflict config options existing in both [instance] and other sections in config file.
ConflictOptions []InstanceConfigSection

// DeprecatedOptions indicates the config options existing in some other sections in config file.
// They should be moved to [instance] section.
DeprecatedOptions []InstanceConfigSection
)

// Config contains configuration options.
type Config struct {
Host string `toml:"host" json:"host"`
Expand All @@ -117,6 +168,7 @@ type Config struct {
TiDBEdition string `toml:"tidb-edition" json:"tidb-edition"`
TiDBReleaseVersion string `toml:"tidb-release-version" json:"tidb-release-version"`
Log Log `toml:"log" json:"log"`
Instance Instance `toml:"instance" json:"instance"`
Security Security `toml:"security" json:"security"`
Status Status `toml:"status" json:"status"`
Performance Performance `toml:"performance" json:"performance"`
Expand Down Expand Up @@ -367,6 +419,34 @@ type Log struct {
RecordPlanInSlowLog uint32 `toml:"record-plan-in-slow-log" json:"record-plan-in-slow-log"`
}

// Instance is the section of instance scope system variables.
type Instance struct {
// These variables only exist in [instance] section.

// TiDBGeneralLog is used to log every query in the server in info level.
TiDBGeneralLog bool `toml:"tidb_general_log" json:"tidb_general_log"`
// EnablePProfSQLCPU is used to add label sql label to pprof result.
EnablePProfSQLCPU bool `toml:"tidb_pprof_sql_cpu" json:"tidb_pprof_sql_cpu"`
// DDLSlowOprThreshold sets log DDL operations whose execution time exceeds the threshold value.
DDLSlowOprThreshold uint32 `toml:"ddl_slow_threshold" json:"ddl_slow_threshold"`
// ExpensiveQueryTimeThreshold indicates the time threshold of expensive query.
ExpensiveQueryTimeThreshold uint64 `toml:"tidb_expensive_query_time_threshold" json:"tidb_expensive_query_time_threshold"`

// These variables exist in both 'instance' section and another place.
// The configuration in 'instance' section takes precedence.

EnableSlowLog AtomicBool `toml:"tidb_enable_slow_log" json:"tidb_enable_slow_log"`
SlowThreshold uint64 `toml:"tidb_slow_log_threshold" json:"tidb_slow_log_threshold"`
RecordPlanInSlowLog uint32 `toml:"tidb_record_plan_in_slow_log" json:"tidb_record_plan_in_slow_log"`
CheckMb4ValueInUTF8 AtomicBool `toml:"tidb_check_mb4_value_in_utf8" json:"tidb_check_mb4_value_in_utf8"`
ForcePriority string `toml:"tidb_force_priority" json:"tidb_force_priority"`
MemoryUsageAlarmRatio float64 `toml:"tidb_memory_usage_alarm_ratio" json:"tidb_memory_usage_alarm_ratio"`
// EnableCollectExecutionInfo enables the TiDB to collect execution info.
EnableCollectExecutionInfo bool `toml:"tidb_enable_collect_execution_info" json:"tidb_enable_collect_execution_info"`
PluginDir string `toml:"plugin_dir" json:"plugin_dir"`
PluginLoad string `toml:"plugin_load" json:"plugin_load"`
}

func (l *Log) getDisableTimestamp() bool {
if l.EnableTimestamp == nbUnset && l.DisableTimestamp == nbUnset {
return false
Expand Down Expand Up @@ -435,6 +515,38 @@ func (e *ErrConfigValidationFailed) Error() string {
e.UndecodedItems, ", "))
}

// ErrConfigInstanceSection error is used to warning the user
// which config options should be moved to 'instance'.
type ErrConfigInstanceSection struct {
confFile string
configSections *[]InstanceConfigSection
deprecatedSections *[]InstanceConfigSection
}

func (e *ErrConfigInstanceSection) Error() string {
var builder strings.Builder
if len(*e.configSections) > 0 {
builder.WriteString("Conflict configuration options exists on both [instance] section and some other sections. ")
}
if len(*e.deprecatedSections) > 0 {
builder.WriteString("Some configuration options should be moved to [instance] section. ")
}
builder.WriteString("Please use the latter config options in [instance] instead: ")
for _, configSection := range *e.configSections {
for oldName, newName := range configSection.NameMappings {
builder.WriteString(fmt.Sprintf(" (%s, %s)", oldName, newName))
}
}
for _, configSection := range *e.deprecatedSections {
for oldName, newName := range configSection.NameMappings {
builder.WriteString(fmt.Sprintf(" (%s, %s)", oldName, newName))
}
}
builder.WriteString(".")

return builder.String()
}

// ClusterSecurity returns Security info for cluster
func (s *Security) ClusterSecurity() tikvcfg.Security {
return tikvcfg.NewSecurity(s.ClusterSSLCA, s.ClusterSSLCert, s.ClusterSSLKey, s.ClusterVerifyCN)
Expand Down Expand Up @@ -663,6 +775,21 @@ var defaultConf = Config{
RecordPlanInSlowLog: logutil.DefaultRecordPlanInSlowLog,
EnableSlowLog: *NewAtomicBool(logutil.DefaultTiDBEnableSlowLog),
},
Instance: Instance{
TiDBGeneralLog: false,
EnablePProfSQLCPU: false,
DDLSlowOprThreshold: DefDDLSlowOprThreshold,
ExpensiveQueryTimeThreshold: DefExpensiveQueryTimeThreshold,
EnableSlowLog: *NewAtomicBool(logutil.DefaultTiDBEnableSlowLog),
SlowThreshold: logutil.DefaultSlowThreshold,
RecordPlanInSlowLog: logutil.DefaultRecordPlanInSlowLog,
CheckMb4ValueInUTF8: *NewAtomicBool(true),
ForcePriority: "NO_PRIORITY",
MemoryUsageAlarmRatio: DefMemoryUsageAlarmRatio,
EnableCollectExecutionInfo: true,
PluginDir: "/data/deploy/plugin",
PluginLoad: "",
},
Status: Status{
ReportStatus: true,
StatusHost: DefStatusHost,
Expand All @@ -678,7 +805,7 @@ var defaultConf = Config{
Performance: Performance{
MaxMemory: 0,
ServerMemoryQuota: 0,
MemoryUsageAlarmRatio: 0.8,
MemoryUsageAlarmRatio: DefMemoryUsageAlarmRatio,
TCPKeepAlive: true,
TCPNoDelay: true,
CrossJoin: true,
Expand Down Expand Up @@ -838,6 +965,9 @@ func InitializeConfig(confPath string, configCheck, configStrict bool, enforceCm
fmt.Fprintln(os.Stderr, err.Error())
err = nil
}
} else if tmp, ok := err.(*ErrConfigInstanceSection); ok {
logutil.BgLogger().Warn(tmp.Error())
err = nil
}
}

Expand Down Expand Up @@ -888,6 +1018,31 @@ func (c *Config) Load(confFile string) error {
err = &ErrConfigValidationFailed{confFile, undecodedItems}
}

for _, section := range sectionMovedToInstance {
newConflictSection := InstanceConfigSection{SectionName: section.SectionName, NameMappings: map[string]string{}}
newDeprecatedSection := InstanceConfigSection{SectionName: section.SectionName, NameMappings: map[string]string{}}
for oldName, newName := range section.NameMappings {
if section.SectionName == "" && metaData.IsDefined(oldName) ||
section.SectionName != "" && metaData.IsDefined(section.SectionName, oldName) {
if metaData.IsDefined("instance", newName) {
newConflictSection.NameMappings[oldName] = newName
} else {
newDeprecatedSection.NameMappings[oldName] = newName
}
}
}
if len(newConflictSection.NameMappings) > 0 {
ConflictOptions = append(ConflictOptions, newConflictSection)
}
if len(newDeprecatedSection.NameMappings) > 0 {
DeprecatedOptions = append(DeprecatedOptions, newDeprecatedSection)
}
}
if len(ConflictOptions) > 0 || len(DeprecatedOptions) > 0 {
// Give a warning that the 'instance' section should be used.
err = &ErrConfigInstanceSection{confFile, &ConflictOptions, &DeprecatedOptions}
}

return err
}

Expand Down Expand Up @@ -949,8 +1104,8 @@ func (c *Config) Valid() error {
return fmt.Errorf("txn-total-size-limit should be less than %d", 1<<40)
}

if c.Performance.MemoryUsageAlarmRatio > 1 || c.Performance.MemoryUsageAlarmRatio < 0 {
return fmt.Errorf("memory-usage-alarm-ratio in [Performance] must be greater than or equal to 0 and less than or equal to 1")
if c.Instance.MemoryUsageAlarmRatio > 1 || c.Instance.MemoryUsageAlarmRatio < 0 {
return fmt.Errorf("tidb_memory_usage_alarm_ratio in [Instance] must be greater than or equal to 0 and less than or equal to 1")
}

if c.PreparedPlanCache.Capacity < 1 {
Expand Down
71 changes: 43 additions & 28 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ compatible-kill-query = false
# The health check will fail immediately but the server will not start shutting down until the time has elapsed.
graceful-wait-before-shutdown = 0

# check mb4 value in utf8 is used to control whether to check the mb4 characters when the charset is utf8.
check-mb4-value-in-utf8 = true

# treat-old-version-utf8-as-utf8mb4 use for upgrade compatibility. Set to true will treat old version table/column UTF8 charset as UTF8MB4.
treat-old-version-utf8-as-utf8mb4 = true

Expand Down Expand Up @@ -139,25 +136,12 @@ format = "text"
# Enable annotating logs with the full stack error message, if not set, it will be defaulted to false.
# enable-error-stack = false

# Whether to enable slow query log.
enable-slow-log = true

# Stores slow query log into separated files.
slow-query-file = "tidb-slow.log"

# Queries with execution time greater than this value will be logged. (Milliseconds)
slow-threshold = 300

# record-plan-in-slow-log is used to enable record query plan in slow log.
# 0 is disable. 1 is enable.
record-plan-in-slow-log = 1

# Queries with internal result greater than this value will be logged.
expensive-threshold = 10000

# Maximum query length recorded in log.
query-log-max-len = 4096

# File logging.
[log.file]
# Log file name.
Expand Down Expand Up @@ -239,14 +223,6 @@ max-procs = 0
# Memory size quota for tidb server, 0 means unlimited
server-memory-quota = 0

# The alarm threshold when memory usage of the tidb-server exceeds. The valid value range is greater than or equal to 0
# and less than or equal to 1. The default value is 0.8.
# If this configuration is set to 0 or 1, it'll disable the alarm.
# Otherwise, related information will be recorded in the directory `tmp-storage-path/record`.
# Note: If the configuration `server-memory-quota` is set and larger than 0, the alarm threshold will be
# `memory-usage-alarm-ratio * server-memory-quota`; otherwise, it'll be `memory-usage-alarm-ratio * system memory size`.
memory-usage-alarm-ratio = 0.8

# StmtCountLimit limits the max count of statement inside a transaction.
stmt-count-limit = 5000

Expand All @@ -272,10 +248,6 @@ query-feedback-limit = 512
# row count in statistics of a table is greater than it.
pseudo-estimate-ratio = 0.8

# Force the priority of all statements in a specified priority.
# The value could be "NO_PRIORITY", "LOW_PRIORITY", "HIGH_PRIORITY" or "DELAYED".
force-priority = "NO_PRIORITY"

# Bind info lease duration, which influences the duration of loading bind info and handling invalid bind.
bind-info-lease = "3s"

Expand Down Expand Up @@ -467,3 +439,46 @@ allow-expression-index = false
[isolation-read]
# engines means allow the tidb server read data from which types of engines. options: "tikv", "tiflash", "tidb".
engines = ["tikv", "tiflash", "tidb"]

# instance scope variables
# These options are also available as a system variable for online configuration
# changes to the system variable do not persist to the cluster. You must make changes
# in this configuration file on each tidb-server.
[instance]

# tidb_general_log is used to log every query in the server in info level.
tidb_general_log = false

# tidb_pprof_sql_cpu is used to add label sql label to pprof result.
tidb_pprof_sql_cpu = false

# ddl_slow_threshold sets log DDL operations whose execution time exceeds the threshold value.
ddl_slow_threshold = 300

# tidb_expensive_query_time_threshold indicates the time threshold of expensive query.
tidb_expensive_query_time_threshold = 60

# Force the priority of all statements in a specified priority.
# The value could be "NO_PRIORITY", "LOW_PRIORITY", "HIGH_PRIORITY" or "DELAYED".
tidb_force_priority = "NO_PRIORITY"

# The alarm threshold when memory usage of the tidb-server exceeds. The valid value range is greater than or equal to 0
# and less than or equal to 1. The default value is 0.8.
# If this configuration is set to 0 or 1, it'll disable the alarm.
# Otherwise, related information will be recorded in the directory `tmp-storage-path/record`.
# Note: If the configuration `server-memory-quota` is set and larger than 0, the alarm threshold will be
# `tidb_memory_usage_alarm_ratio * server-memory-quota`; otherwise, it'll be `tidb_memory_usage_alarm_ratio * system memory size`.
tidb_memory_usage_alarm_ratio = 0.8

# check mb4 value in utf8 is used to control whether to check the mb4 characters when the charset is utf8.
tidb_check_mb4_value_in_utf8 = true

# Whether to enable slow query log.
tidb_enable_slow_log = true

# Queries with execution time greater than this value will be logged. (Milliseconds)
tidb_slow_log_threshold = 300

# tidb_record_plan_in_slow_log is used to enable record query plan in slow log.
# 0 is disable. 1 is enable.
tidb_record_plan_in_slow_log = 1
Loading

0 comments on commit 0d5ac6f

Please sign in to comment.