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

*: add context for hook of getting/setting variables #38379

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ddl/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func ValidateFlashbackTS(ctx context.Context, sctx sessionctx.Context, flashBack
}

func setTiDBSuperReadOnly(sess sessionctx.Context, value string) error {
return sess.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBSuperReadOnly, value)
return sess.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.TiDBSuperReadOnly, value)
}

func getTiDBSuperReadOnly(sess sessionctx.Context) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (d *ddl) CreateSchema(ctx sessionctx.Context, stmt *ast.CreateDatabaseStmt)
// If no charset and/or collation is specified use collation_server and character_set_server
charsetOpt := ast.CharsetOpt{}
if sessionVars.GlobalVarsAccessor != nil {
charsetOpt.Col, err = sessionVars.GetSessionOrGlobalSystemVar(variable.CollationServer)
charsetOpt.Col, err = sessionVars.GetSessionOrGlobalSystemVar(context.Background(), variable.CollationServer)
if err != nil {
return err
}
charsetOpt.Chs, err = sessionVars.GetSessionOrGlobalSystemVar(variable.CharacterSetServer)
charsetOpt.Chs, err = sessionVars.GetSessionOrGlobalSystemVar(context.Background(), variable.CharacterSetServer)
if err != nil {
return err
}
Expand Down Expand Up @@ -2636,7 +2636,7 @@ func (d *ddl) preSplitAndScatter(ctx sessionctx.Context, tbInfo *model.TableInfo
preSplit func()
scatterRegion bool
)
val, err := ctx.GetSessionVars().GetGlobalSystemVar(variable.TiDBScatterRegion)
val, err := ctx.GetSessionVars().GetGlobalSystemVar(context.Background(), variable.TiDBScatterRegion)
if err != nil {
logutil.BgLogger().Warn("[ddl] won't scatter region", zap.Error(err))
} else {
Expand Down
2 changes: 1 addition & 1 deletion domain/sysvar_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (do *Domain) rebuildSysVarCache(ctx sessionctx.Context) error {
// This does not apply to INSTANCE scoped vars (HasGlobalScope() is false)
if sv.SetGlobal != nil && !sv.SkipSysvarCache() {
sVal = sv.ValidateWithRelaxedValidation(ctx.GetSessionVars(), sVal, variable.ScopeGlobal)
err = sv.SetGlobal(ctx.GetSessionVars(), sVal)
err = sv.SetGlobal(context.Background(), ctx.GetSessionVars(), sVal)
if err != nil {
logutil.BgLogger().Error(fmt.Sprintf("load global variable %s error", sv.Name), zap.Error(err))
}
Expand Down
3 changes: 2 additions & 1 deletion executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package aggfuncs

import (
"context"
"fmt"
"strconv"

Expand Down Expand Up @@ -462,7 +463,7 @@ func buildGroupConcat(ctx sessionctx.Context, aggFuncDesc *aggregation.AggFuncDe
panic(fmt.Sprintf("Error happened when buildGroupConcat: %s", err.Error()))
}
var s string
s, err = ctx.GetSessionVars().GetSessionOrGlobalSystemVar(variable.GroupConcatMaxLen)
s, err = ctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), variable.GroupConcatMaxLen)
if err != nil {
panic(fmt.Sprintf("Error happened when buildGroupConcat: no system variable named '%s'", variable.GroupConcatMaxLen))
}
Expand Down
3 changes: 2 additions & 1 deletion executor/analyze_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package executor

import (
"context"
"strconv"
"sync"

Expand All @@ -28,7 +29,7 @@ import (

func getBuildStatsConcurrency(ctx sessionctx.Context) (int, error) {
sessionVars := ctx.GetSessionVars()
concurrency, err := sessionVars.GetSessionOrGlobalSystemVar(variable.TiDBBuildStatsConcurrency)
concurrency, err := sessionVars.GetSessionOrGlobalSystemVar(context.Background(), variable.TiDBBuildStatsConcurrency)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (c *checksumContext) HandleResponse(update *tipb.ChecksumResponse) {

func getChecksumTableConcurrency(ctx sessionctx.Context) (int, error) {
sessionVars := ctx.GetSessionVars()
concurrency, err := sessionVars.GetSessionOrGlobalSystemVar(variable.TiDBChecksumTableConcurrency)
concurrency, err := sessionVars.GetSessionOrGlobalSystemVar(context.Background(), variable.TiDBChecksumTableConcurrency)
if err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,9 +1630,9 @@ func (e *TableScanExec) nextChunk4InfoSchema(ctx context.Context, chk *chunk.Chu
}
mutableRow := chunk.MutRowFromTypes(retTypes(e))
type tableIter interface {
IterRecords(sessionctx.Context, []*table.Column, table.RecordIterFunc) error
IterRecords(ctx context.Context, sctx sessionctx.Context, cols []*table.Column, fn table.RecordIterFunc) error
}
err := (e.t.(tableIter)).IterRecords(e.ctx, columns, func(_ kv.Handle, rec []types.Datum, cols []*table.Column) (bool, error) {
err := (e.t.(tableIter)).IterRecords(ctx, e.ctx, columns, func(_ kv.Handle, rec []types.Datum, cols []*table.Column) (bool, error) {
mutableRow.SetDatums(rec...)
e.virtualTableChunkList.AppendRow(mutableRow.ToRow())
return true, nil
Expand Down
4 changes: 2 additions & 2 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
case infoschema.TableConstraints:
e.setDataFromTableConstraints(sctx, dbs)
case infoschema.TableSessionVar:
e.rows, err = infoschema.GetDataFromSessionVariables(sctx)
e.rows, err = infoschema.GetDataFromSessionVariables(ctx, sctx)
case infoschema.TableTiDBServersInfo:
err = e.setDataForServersInfo(sctx)
case infoschema.TableTiFlashReplica:
Expand Down Expand Up @@ -402,7 +402,7 @@ func (e *memtableRetriever) setDataForVariablesInfo(ctx sessionctx.Context) erro
if infoschema.SysVarHiddenForSem(ctx, sv.Name) {
continue
}
currentVal, err := ctx.GetSessionVars().GetSessionOrGlobalSystemVar(sv.Name)
currentVal, err := ctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), sv.Name)
if err != nil {
currentVal = ""
}
Expand Down
10 changes: 5 additions & 5 deletions executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
}

if v.IsGlobal {
valStr, err := e.getVarValue(v, sysVar)
valStr, err := e.getVarValue(ctx, v, sysVar)
if err != nil {
return err
}
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(name, valStr)
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(ctx, name, valStr)
if err != nil {
return err
}
Expand All @@ -142,7 +142,7 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
return err
}
// Set session variable
valStr, err := e.getVarValue(v, nil)
valStr, err := e.getVarValue(ctx, v, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,15 +251,15 @@ func (e *SetExecutor) setCharset(cs, co string, isSetName bool) error {
return errors.Trace(sessionVars.SetSystemVar(variable.CollationConnection, coDb))
}

func (e *SetExecutor) getVarValue(v *expression.VarAssignment, sysVar *variable.SysVar) (value string, err error) {
func (e *SetExecutor) getVarValue(ctx context.Context, v *expression.VarAssignment, sysVar *variable.SysVar) (value string, err error) {
if v.IsDefault {
// To set a SESSION variable to the GLOBAL value or a GLOBAL value
// to the compiled-in MySQL default value, use the DEFAULT keyword.
// See http://dev.mysql.com/doc/refman/5.7/en/set-statement.html
if sysVar != nil {
return sysVar.Value, nil
}
return e.ctx.GetSessionVars().GetGlobalSystemVar(v.Name)
return e.ctx.GetSessionVars().GetGlobalSystemVar(ctx, v.Name)
}
nativeVal, err := v.Expr.Eval(chunk.Row{})
if err != nil || nativeVal.IsNull() {
Expand Down
2 changes: 1 addition & 1 deletion executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func TestSetCharset(t *testing.T) {

check := func(args ...string) {
for i, v := range characterSetVariables {
sVar, err := sessionVars.GetSessionOrGlobalSystemVar(v)
sVar, err := sessionVars.GetSessionOrGlobalSystemVar(context.Background(), v)
require.NoError(t, err)
require.Equal(t, args[i], sVar, fmt.Sprintf("%d: %s", i, characterSetVariables[i]))
}
Expand Down
8 changes: 4 additions & 4 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
case ast.ShowTriggers:
return e.fetchShowTriggers()
case ast.ShowVariables:
return e.fetchShowVariables()
return e.fetchShowVariables(ctx)
case ast.ShowWarnings:
return e.fetchShowWarnings(false)
case ast.ShowErrors:
Expand Down Expand Up @@ -817,7 +817,7 @@ func (e *ShowExec) fetchShowMasterStatus() error {
return nil
}

func (e *ShowExec) fetchShowVariables() (err error) {
func (e *ShowExec) fetchShowVariables(ctx context.Context) (err error) {
var (
value string
sessionVars = e.ctx.GetSessionVars()
Expand Down Expand Up @@ -849,7 +849,7 @@ func (e *ShowExec) fetchShowVariables() (err error) {
if infoschema.SysVarHiddenForSem(e.ctx, v.Name) {
continue
}
value, err = sessionVars.GetGlobalSystemVar(v.Name)
value, err = sessionVars.GetGlobalSystemVar(ctx, v.Name)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -874,7 +874,7 @@ func (e *ShowExec) fetchShowVariables() (err error) {
if infoschema.SysVarHiddenForSem(e.ctx, v.Name) {
continue
}
value, err = sessionVars.GetSessionOrGlobalSystemVar(v.Name)
value, err = sessionVars.GetSessionOrGlobalSystemVar(context.Background(), v.Name)
if err != nil {
return errors.Trace(err)
}
Expand Down
3 changes: 2 additions & 1 deletion expression/aggregation/agg_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package aggregation

import (
"context"
"strconv"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -130,7 +131,7 @@ func AggFuncToPBExpr(sctx sessionctx.Context, client kv.Client, aggFunc *AggFunc
orderBy = append(orderBy, pbArg)
}
// encode GroupConcatMaxLen
GCMaxLen, err := sctx.GetSessionVars().GetSessionOrGlobalSystemVar(variable.GroupConcatMaxLen)
GCMaxLen, err := sctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), variable.GroupConcatMaxLen)
if err != nil {
return nil, errors.Errorf("Error happened when buildGroupConcat: no system variable named '%s'", variable.GroupConcatMaxLen)
}
Expand Down
3 changes: 2 additions & 1 deletion expression/aggregation/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package aggregation

import (
"bytes"
"context"
"fmt"
"math"
"strconv"
Expand Down Expand Up @@ -229,7 +230,7 @@ func (a *AggFuncDesc) GetAggFunc(ctx sessionctx.Context) Aggregation {
var s string
var err error
var maxLen uint64
s, err = ctx.GetSessionVars().GetSessionOrGlobalSystemVar(variable.GroupConcatMaxLen)
s, err = ctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), variable.GroupConcatMaxLen)
if err != nil {
panic(fmt.Sprintf("Error happened when GetAggFunc: no system variable named '%s'", variable.GroupConcatMaxLen))
}
Expand Down
3 changes: 2 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package expression

import (
"context"
"fmt"
"math"
"regexp"
Expand Down Expand Up @@ -6643,7 +6644,7 @@ func (b *builtinTiDBCurrentTsoSig) Clone() builtinFunc {

// evalInt evals currentTSO().
func (b *builtinTiDBCurrentTsoSig) evalInt(row chunk.Row) (d int64, isNull bool, err error) {
tso, _ := b.ctx.GetSessionVars().GetSessionOrGlobalSystemVar("tidb_current_ts")
tso, _ := b.ctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), "tidb_current_ts")
itso, _ := strconv.ParseInt(tso, 10, 64)
return itso, false, nil
}
3 changes: 2 additions & 1 deletion expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package expression

import (
"context"
"fmt"
"math"
"strconv"
Expand Down Expand Up @@ -3112,7 +3113,7 @@ func TestCurrentTso(t *testing.T) {
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
n := v.GetInt64()
tso, _ := ctx.GetSessionVars().GetSessionOrGlobalSystemVar("tidb_current_ts")
tso, _ := ctx.GetSessionVars().GetSessionOrGlobalSystemVar(context.Background(), "tidb_current_ts")
itso, _ := strconv.ParseInt(tso, 10, 64)
require.Equal(t, itso, n, v.Kind())
}
3 changes: 2 additions & 1 deletion expression/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package expression

import (
"context"
"math"
"strings"
"time"
Expand Down Expand Up @@ -164,7 +165,7 @@ func getStmtTimestamp(ctx sessionctx.Context) (time.Time, error) {
}

sessionVars := ctx.GetSessionVars()
timestampStr, err := sessionVars.GetSessionOrGlobalSystemVar("timestamp")
timestampStr, err := sessionVars.GetSessionOrGlobalSystemVar(context.Background(), "timestamp")
if err != nil {
return now, err
}
Expand Down
24 changes: 12 additions & 12 deletions infoschema/perfschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package perfschema

import (
"context"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -219,7 +220,7 @@ func initTableIndices(t *perfSchemaTable) error {
return nil
}

func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column) (fullRows [][]types.Datum, err error) {
func (vt *perfSchemaTable) getRows(ctx context.Context, sctx sessionctx.Context, cols []*table.Column) (fullRows [][]types.Datum, err error) {
switch vt.meta.Name.O {
case tableNameTiDBProfileCPU:
fullRows, err = (&profile.Collector{}).ProfileGraph("cpu")
Expand All @@ -235,22 +236,22 @@ func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows, err = (&profile.Collector{}).ProfileGraph("goroutine")
case tableNameTiKVProfileCPU:
interval := fmt.Sprintf("%d", profile.CPUProfileInterval/time.Second)
fullRows, err = dataForRemoteProfile(ctx, "tikv", "/debug/pprof/profile?seconds="+interval, false)
fullRows, err = dataForRemoteProfile(sctx, "tikv", "/debug/pprof/profile?seconds="+interval, false)
case tableNamePDProfileCPU:
interval := fmt.Sprintf("%d", profile.CPUProfileInterval/time.Second)
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/profile?seconds="+interval, false)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/profile?seconds="+interval, false)
case tableNamePDProfileMemory:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/heap", false)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/heap", false)
case tableNamePDProfileMutex:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/mutex", false)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/mutex", false)
case tableNamePDProfileAllocs:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/allocs", false)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/allocs", false)
case tableNamePDProfileBlock:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/block", false)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/block", false)
case tableNamePDProfileGoroutines:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/goroutine?debug=2", true)
fullRows, err = dataForRemoteProfile(sctx, "pd", "/pd/api/v1/debug/pprof/goroutine?debug=2", true)
case tableNameSessionVariables:
fullRows, err = infoschema.GetDataFromSessionVariables(ctx)
fullRows, err = infoschema.GetDataFromSessionVariables(ctx, sctx)
}
if err != nil {
return
Expand All @@ -270,9 +271,8 @@ func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
}

// IterRecords implements table.Table IterRecords interface.
func (vt *perfSchemaTable) IterRecords(ctx sessionctx.Context, cols []*table.Column,
fn table.RecordIterFunc) error {
rows, err := vt.getRows(ctx, cols)
func (vt *perfSchemaTable) IterRecords(ctx context.Context, sctx sessionctx.Context, cols []*table.Column, fn table.RecordIterFunc) error {
rows, err := vt.getRows(ctx, sctx, cols)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1903,16 +1903,16 @@ func SysVarHiddenForSem(ctx sessionctx.Context, sysVarNameInLower string) bool {
}

// GetDataFromSessionVariables return the [name, value] of all session variables
func GetDataFromSessionVariables(ctx sessionctx.Context) ([][]types.Datum, error) {
sessionVars := ctx.GetSessionVars()
func GetDataFromSessionVariables(ctx context.Context, sctx sessionctx.Context) ([][]types.Datum, error) {
sessionVars := sctx.GetSessionVars()
sysVars := variable.GetSysVars()
rows := make([][]types.Datum, 0, len(sysVars))
for _, v := range sysVars {
if SysVarHiddenForSem(ctx, v.Name) {
if SysVarHiddenForSem(sctx, v.Name) {
continue
}
var value string
value, err := sessionVars.GetSessionOrGlobalSystemVar(v.Name)
value, err := sessionVars.GetSessionOrGlobalSystemVar(ctx, v.Name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2016,8 +2016,7 @@ type infoschemaTable struct {
}

// IterRecords implements table.Table IterRecords interface.
func (*infoschemaTable) IterRecords(_ sessionctx.Context, _ []*table.Column,
_ table.RecordIterFunc) error {
func (*infoschemaTable) IterRecords(ctx context.Context, sctx sessionctx.Context, cols []*table.Column, fn table.RecordIterFunc) error {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,9 +1391,9 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
if sysVar.HasNoneScope() {
val = sysVar.Value
} else if v.IsGlobal {
val, err = sessionVars.GetGlobalSystemVar(name)
val, err = sessionVars.GetGlobalSystemVar(er.ctx, name)
} else {
val, err = sessionVars.GetSessionOrGlobalSystemVar(name)
val, err = sessionVars.GetSessionOrGlobalSystemVar(er.ctx, name)
}
if err != nil {
er.err = err
Expand Down
2 changes: 1 addition & 1 deletion plugin/conn_ip_example/conn_ip_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func OnInit(ctx context.Context, manifest *plugin.Manifest) error {
// (Optional) the SetGlobal function is called when a global variable is changed.
// This will only be called on the TiDB server that the change is made on,
// and not on the tidb-server peers which will also update their global variable eventually.
SetGlobal: func(vars *variable.SessionVars, value string) error {
SetGlobal: func(_ context.Context, vars *variable.SessionVars, value string) error {
fmt.Println("The set global function was called")
return nil
},
Expand Down
Loading