Skip to content

Commit

Permalink
*: remove non dry sysvar code (#38182)
Browse files Browse the repository at this point in the history
close #33896
  • Loading branch information
morgo authored Oct 9, 2022
1 parent 780af97 commit b0e0cc2
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 99 deletions.
1 change: 1 addition & 0 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "domain",
srcs = [
"domain.go",
"domain_sysvars.go",
"domainctx.go",
"optimize_trace.go",
"plan_replayer.go",
Expand Down
2 changes: 1 addition & 1 deletion domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio
do.expensiveQueryHandle = expensivequery.NewExpensiveQueryHandle(do.exit)
do.serverMemoryLimitHandle = servermemorylimit.NewServerMemoryLimitHandle(do.exit)
do.sysProcesses = SysProcesses{mu: &sync.RWMutex{}, procMap: make(map[uint64]sessionctx.Context)}
variable.SetStatsCacheCapacity.Store(do.SetStatsCacheCapacity)
do.initDomainSysVars()
return do
}

Expand Down
76 changes: 76 additions & 0 deletions domain/domain_sysvars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package domain

import (
"strconv"
"time"

"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/logutil"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
)

// initDomainSysVars() is called when a domain is initialized.
// These are special system variables which require the current domain.
// They can not be SetGlobal functions in sessionctx/variable directly
// because the domain is not available. Instead a noop func is specified,
// which is overwritten here.
func (do *Domain) initDomainSysVars() {
variable.SetStatsCacheCapacity.Store(do.setStatsCacheCapacity)
variable.SetPDClientDynamicOption = do.setPDClientDynamicOption
}

// setStatsCacheCapacity sets statsCache cap
func (do *Domain) setStatsCacheCapacity(c int64) {
do.StatsHandle().SetStatsCacheCapacity(c)
logutil.BgLogger().Info("update stats cache capacity successfully", zap.Int64("capacity", c))
}

func (do *Domain) setPDClientDynamicOption(name, sVal string) {
switch name {
case variable.TiDBTSOClientBatchMaxWaitTime:
val, err := strconv.ParseFloat(sVal, 64)
if err != nil {
break
}
err = do.updatePDClient(pd.MaxTSOBatchWaitInterval, time.Duration(float64(time.Millisecond)*val))
if err != nil {
break
}
variable.MaxTSOBatchWaitInterval.Store(val)
case variable.TiDBEnableTSOFollowerProxy:
val := variable.TiDBOptOn(sVal)
err := do.updatePDClient(pd.EnableTSOFollowerProxy, val)
if err != nil {
break
}
variable.EnableTSOFollowerProxy.Store(val)
}
}

// updatePDClient is used to set the dynamic option into the PD client.
func (do *Domain) updatePDClient(option pd.DynamicOption, val interface{}) error {
store, ok := do.store.(interface{ GetPDClient() pd.Client })
if !ok {
return nil
}
pdClient := store.GetPDClient()
if pdClient == nil {
return nil
}
return pdClient.UpdateOption(option, val)
}
49 changes: 0 additions & 49 deletions domain/sysvar_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ package domain
import (
"context"
"fmt"
"strconv"
"sync"
"time"

"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sqlexec"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -153,11 +150,6 @@ func (do *Domain) rebuildSysVarCache(ctx sessionctx.Context) error {
}
}
}

// Some PD options need to be checked outside of the SetGlobal func.
// This is also done for the SET GLOBAL caller in executor/set.go,
// but here we check for other tidb instances.
do.checkPDClientDynamicOption(sv.Name, sVal)
}

logutil.BgLogger().Debug("rebuilding sysvar cache")
Expand All @@ -168,44 +160,3 @@ func (do *Domain) rebuildSysVarCache(ctx sessionctx.Context) error {
do.sysVarCache.global = newGlobalCache
return nil
}

func (do *Domain) checkPDClientDynamicOption(name, sVal string) {
switch name {
case variable.TiDBTSOClientBatchMaxWaitTime:
val, err := strconv.ParseFloat(sVal, 64)
if err != nil {
break
}
err = do.SetPDClientDynamicOption(pd.MaxTSOBatchWaitInterval, time.Duration(float64(time.Millisecond)*val))
if err != nil {
break
}
variable.MaxTSOBatchWaitInterval.Store(val)
case variable.TiDBEnableTSOFollowerProxy:
val := variable.TiDBOptOn(sVal)
err := do.SetPDClientDynamicOption(pd.EnableTSOFollowerProxy, val)
if err != nil {
break
}
variable.EnableTSOFollowerProxy.Store(val)
}
}

// SetPDClientDynamicOption is used to set the dynamic option into the PD client.
func (do *Domain) SetPDClientDynamicOption(option pd.DynamicOption, val interface{}) error {
store, ok := do.store.(interface{ GetPDClient() pd.Client })
if !ok {
return nil
}
pdClient := store.GetPDClient()
if pdClient == nil {
return nil
}
return pdClient.UpdateOption(option, val)
}

// SetStatsCacheCapacity sets statsCache cap
func (do *Domain) SetStatsCacheCapacity(c int64) {
do.StatsHandle().SetStatsCacheCapacity(c)
logutil.BgLogger().Info("update stats cache capacity successfully", zap.Int64("capacity", c))
}
47 changes: 0 additions & 47 deletions executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ package executor

import (
"context"
"strconv"
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/domain"
Expand All @@ -34,7 +32,6 @@ import (
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/gcutil"
"github.com/pingcap/tidb/util/logutil"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -134,11 +131,6 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
if err != nil {
return err
}
// Some PD client dynamic options need to be checked first and set here.
err = e.checkPDClientDynamicOption(name, sessionVars)
if err != nil {
return err
}
err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error {
auditPlugin := plugin.DeclareAuditManifest(p.Manifest)
if auditPlugin.OnGlobalVariableEvent != nil {
Expand Down Expand Up @@ -214,45 +206,6 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
return nil
}

func (e *SetExecutor) checkPDClientDynamicOption(name string, sessionVars *variable.SessionVars) error {
if name != variable.TiDBTSOClientBatchMaxWaitTime &&
name != variable.TiDBEnableTSOFollowerProxy {
return nil
}
var (
err error
valStr string
)
valStr, err = sessionVars.GlobalVarsAccessor.GetGlobalSysVar(name)
if err != nil {
return err
}
switch name {
case variable.TiDBTSOClientBatchMaxWaitTime:
var val float64
val, err = strconv.ParseFloat(valStr, 64)
if err != nil {
return err
}
err = domain.GetDomain(e.ctx).SetPDClientDynamicOption(
pd.MaxTSOBatchWaitInterval,
time.Duration(float64(time.Millisecond)*val),
)
if err != nil {
return err
}
logutil.BgLogger().Info("set pd client dynamic option", zap.Uint64("conn", sessionVars.ConnectionID), zap.String("name", name), zap.String("val", valStr))
case variable.TiDBEnableTSOFollowerProxy:
val := variable.TiDBOptOn(valStr)
err = domain.GetDomain(e.ctx).SetPDClientDynamicOption(pd.EnableTSOFollowerProxy, val)
if err != nil {
return err
}
logutil.BgLogger().Info("set pd client dynamic option", zap.Uint64("conn", sessionVars.ConnectionID), zap.String("name", name), zap.String("val", valStr))
}
return nil
}

func (e *SetExecutor) setCharset(cs, co string, isSetName bool) error {
var err error
sessionVars := e.ctx.GetSessionVars()
Expand Down
4 changes: 2 additions & 2 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,13 @@ var defaultSysVars = []*SysVar{
return strconv.FormatFloat(MaxTSOBatchWaitInterval.Load(), 'f', -1, 64), nil
},
SetGlobal: func(s *SessionVars, val string) error {
MaxTSOBatchWaitInterval.Store(tidbOptFloat64(val, DefTiDBTSOClientBatchMaxWaitTime))
SetPDClientDynamicOption(TiDBTSOClientBatchMaxWaitTime, val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableTSOFollowerProxy, Value: BoolToOnOff(DefTiDBEnableTSOFollowerProxy), Type: TypeBool, GetGlobal: func(sv *SessionVars) (string, error) {
return BoolToOnOff(EnableTSOFollowerProxy.Load()), nil
}, SetGlobal: func(s *SessionVars, val string) error {
EnableTSOFollowerProxy.Store(TiDBOptOn(val))
SetPDClientDynamicOption(TiDBEnableTSOFollowerProxy, val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableLocalTxn, Value: BoolToOnOff(DefTiDBEnableLocalTxn), Hidden: true, Type: TypeBool, GetGlobal: func(sv *SessionVars) (string, error) {
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,8 @@ var (
GetMemQuotaAnalyze func() int64 = nil
// SetStatsCacheCapacity is the func registered by domain to set statsCache memory quota.
SetStatsCacheCapacity atomic.Value
// SetPDClientDynamicOption is the func registered by domain
SetPDClientDynamicOption func(string, string) = nil
// SwitchConcurrentDDL is the func registered by DDL to switch concurrent DDL.
SwitchConcurrentDDL func(bool) error = nil
// SwitchMDL is the func registered by DDL to switch MDL.
Expand Down

0 comments on commit b0e0cc2

Please sign in to comment.