diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index fb048aa3ab..23c1cc8229 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -800,7 +800,7 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeGlobal, Name: InitConnect, Value: ""}, /* TiDB specific variables */ - {Scope: ScopeGlobal, Name: TiDBEnableLocalTxn, Value: BoolToOnOff(DefTiDBEnableLocalTxn), Hidden: true, Type: TypeBool, GetSession: func(sv *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBEnableLocalTxn, Value: BoolToOnOff(DefTiDBEnableLocalTxn), Hidden: true, Type: TypeBool, GetGlobal: func(sv *SessionVars) (string, error) { return BoolToOnOff(EnableLocalTxn.Load()), nil }, SetGlobal: func(s *SessionVars, val string) error { oldVal := EnableLocalTxn.Load() @@ -1692,7 +1692,7 @@ var defaultSysVars = []*SysVar{ return nil }}, // variable for top SQL feature. - {Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) { return BoolToOnOff(TopSQLVariable.Enable.Load()), nil }, SetGlobal: func(vars *SessionVars, s string) error { TopSQLVariable.Enable.Store(TiDBOptOn(s)) @@ -1705,7 +1705,7 @@ var defaultSysVars = []*SysVar{ TopSQLVariable.AgentAddress.Store(s) return nil }}, - {Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) { return strconv.FormatInt(TopSQLVariable.PrecisionSeconds.Load(), 10), nil }, SetGlobal: func(vars *SessionVars, s string) error { val, err := strconv.ParseInt(s, 10, 64) @@ -1715,7 +1715,7 @@ var defaultSysVars = []*SysVar{ TopSQLVariable.PrecisionSeconds.Store(val) return nil }}, - {Scope: ScopeGlobal, Name: TiDBTopSQLMaxStatementCount, Value: strconv.Itoa(DefTiDBTopSQLMaxStatementCount), Type: TypeInt, Hidden: true, MinValue: 0, MaxValue: 5000, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBTopSQLMaxStatementCount, Value: strconv.Itoa(DefTiDBTopSQLMaxStatementCount), Type: TypeInt, Hidden: true, MinValue: 0, MaxValue: 5000, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) { return strconv.FormatInt(TopSQLVariable.MaxStatementCount.Load(), 10), nil }, SetGlobal: func(vars *SessionVars, s string) error { val, err := strconv.ParseInt(s, 10, 64) @@ -1725,7 +1725,7 @@ var defaultSysVars = []*SysVar{ TopSQLVariable.MaxStatementCount.Store(val) return nil }}, - {Scope: ScopeGlobal, Name: TiDBTopSQLMaxCollect, Value: strconv.Itoa(DefTiDBTopSQLMaxCollect), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 500000, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBTopSQLMaxCollect, Value: strconv.Itoa(DefTiDBTopSQLMaxCollect), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 500000, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) { return strconv.FormatInt(TopSQLVariable.MaxCollect.Load(), 10), nil }, SetGlobal: func(vars *SessionVars, s string) error { val, err := strconv.ParseInt(s, 10, 64) @@ -1735,7 +1735,7 @@ var defaultSysVars = []*SysVar{ TopSQLVariable.MaxCollect.Store(val) return nil }}, - {Scope: ScopeGlobal, Name: TiDBTopSQLReportIntervalSeconds, Value: strconv.Itoa(DefTiDBTopSQLReportIntervalSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 1 * 60 * 60, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeGlobal, Name: TiDBTopSQLReportIntervalSeconds, Value: strconv.Itoa(DefTiDBTopSQLReportIntervalSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 1 * 60 * 60, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) { return strconv.FormatInt(TopSQLVariable.ReportIntervalSeconds.Load(), 10), nil }, SetGlobal: func(vars *SessionVars, s string) error { val, err := strconv.ParseInt(s, 10, 64) diff --git a/sessionctx/variable/sysvar_test.go b/sessionctx/variable/sysvar_test.go index 85723ccc11..ecea3c494a 100644 --- a/sessionctx/variable/sysvar_test.go +++ b/sessionctx/variable/sysvar_test.go @@ -620,6 +620,30 @@ func (*testSysVarSuite) TestDefaultValuesAreSettable(c *C) { } } +// This tests that sysvars are logically correct with getter and setter functions. +// i.e. it doesn't make sense to have a SetSession function on a variable that is only globally scoped. +func (*testSysVarSuite) TestSettersandGetters(c *C) { + for _, sv := range GetSysVars() { + if !sv.HasSessionScope() { + // There are some historial exceptions where global variables are loaded into the session. + // Please don't add to this list, the behavior is not MySQL compatible. + switch sv.Name { + case TiDBEnableChangeMultiSchema, TiDBDDLReorgBatchSize, TiDBEnableAlterPlacement, + TiDBMaxDeltaSchemaCount, InitConnect, MaxPreparedStmtCount, + TiDBDDLReorgWorkerCount, TiDBDDLErrorCountLimit, TiDBRowFormatVersion, + TiDBEnableTelemetry, TiDBEnablePointGetCache: + continue + } + c.Assert(sv.SetSession, IsNil) + c.Assert(sv.GetSession, IsNil) + } + if !sv.HasGlobalScope() { + c.Assert(sv.SetGlobal, IsNil) + c.Assert(sv.GetGlobal, IsNil) + } + } +} + func (*testSysVarSuite) TestSecureAuth(c *C) { sv := GetSysVar(SecureAuth) vars := NewSessionVars()