Skip to content

Commit

Permalink
sessionctx: refactor set to be in struct (#23383)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored Mar 25, 2021
1 parent 9b4ae3a commit 1c83b14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,9 @@ func (s *SessionVars) ClearStmtVars() {
s.stmtVars = make(map[string]string)
}

// SetSystemVar sets the value of a system variable.
// SetSystemVar sets the value of a system variable for session scope.
// Validation has already been performed, and the values have been normalized.
// i.e. oN / on / 1 => ON
func (s *SessionVars) SetSystemVar(name string, val string) error {
switch name {
case TxnIsolationOneShot:
Expand Down Expand Up @@ -1750,8 +1752,6 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.EnableIndexMergeJoin = TiDBOptOn(val)
case TiDBTrackAggregateMemoryUsage:
s.TrackAggregateMemoryUsage = TiDBOptOn(val)
case TiDBMultiStatementMode:
s.MultiStatementMode = TiDBOptMultiStmt(val)
case TiDBEnableExchangePartition:
s.TiDBEnableExchangePartition = TiDBOptOn(val)
case TiDBAllowFallbackToTiKV:
Expand All @@ -1764,6 +1764,11 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
}
case TiDBIntPrimaryKeyDefaultAsClustered:
s.IntPrimaryKeyDefaultAsClustered = TiDBOptOn(val)
default:
sv := GetSysVar(name)
if err := sv.SetSessionFromHook(s, val); err != nil {
return err
}
}
s.systems[name] = val
return nil
Expand Down
15 changes: 14 additions & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,20 @@ type SysVar struct {
AllowAutoValue bool
// Validation is a callback after the type validation has been performed
Validation func(*SessionVars, string, string, ScopeFlag) (string, error)
// SetSession is called after validation
SetSession func(*SessionVars, string) error
// IsHintUpdatable indicate whether it's updatable via SET_VAR() hint (optional)
IsHintUpdatable bool
}

// SetSessionFromHook calls the SetSession func if it exists.
func (sv *SysVar) SetSessionFromHook(s *SessionVars, val string) error {
if sv.SetSession != nil {
return sv.SetSession(s, val)
}
return nil
}

// ValidateFromType provides automatic validation based on the SysVar's type
func (sv *SysVar) ValidateFromType(vars *SessionVars, value string, scope ScopeFlag) (string, error) {
// Some sysvars are read-only. Attempting to set should always fail.
Expand Down Expand Up @@ -804,7 +814,10 @@ var defaultSysVars = []*SysVar{
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableIndexMergeJoin, Value: BoolToOnOff(DefTiDBEnableIndexMergeJoin), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBTrackAggregateMemoryUsage, Value: BoolToOnOff(DefTiDBTrackAggregateMemoryUsage), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBMultiStatementMode, Value: Off, Type: TypeEnum, PossibleValues: []string{Off, On, Warn}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBMultiStatementMode, Value: Off, Type: TypeEnum, PossibleValues: []string{Off, On, Warn}, SetSession: func(s *SessionVars, val string) error {
s.MultiStatementMode = TiDBOptMultiStmt(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableExchangePartition, Value: BoolToOnOff(DefTiDBEnableExchangePartition), Type: TypeBool},

/* tikv gc metrics */
Expand Down

0 comments on commit 1c83b14

Please sign in to comment.