Skip to content

Commit

Permalink
*: add variables and support external timestamp read (#38276)
Browse files Browse the repository at this point in the history
close #38274
  • Loading branch information
YangKeao authored Nov 1, 2022
1 parent be75669 commit ba6ae45
Show file tree
Hide file tree
Showing 19 changed files with 405 additions and 27 deletions.
12 changes: 6 additions & 6 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2825,8 +2825,8 @@ def go_deps():
name = "com_github_pingcap_kvproto",
build_file_proto_mode = "disable_global",
importpath = "github.com/pingcap/kvproto",
sum = "h1:McYxPhA8SHqfUtLfQHHN0fQl4dy93IkhlX4Pp2MKIFA=",
version = "v0.0.0-20221014081430-26e28e6a281a",
sum = "h1:FYgKV9znRQmzVrrJDZ0gUfMIvKLAMU1tu1UKJib8bEQ=",
version = "v0.0.0-20221026112947-f8d61344b172",
)
go_repository(
name = "com_github_pingcap_log",
Expand Down Expand Up @@ -3429,15 +3429,15 @@ def go_deps():
name = "com_github_tikv_client_go_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/client-go/v2",
sum = "h1:s8eJEGI4p/fxFwMBkoJ+4FAEQNQhHR47TZmVW+EEtOE=",
version = "v2.0.1-0.20221026083454-6c9c7c7c5815",
sum = "h1:NvQHWk0GeXSLEBbmGMPnDMc0to0a3ogzgIRbTKw8MHI=",
version = "v2.0.1-0.20221031063202-30e803b7082c",
)
go_repository(
name = "com_github_tikv_pd_client",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/pd/client",
sum = "h1:REQOR1XraH1fT9BCoNBPZs1CAe+w7VPLU+d+si7DLYo=",
version = "v0.0.0-20221010134149-d50e5fe43f14",
sum = "h1:ckPpxKcl75mO2N6a4cJXiZH43hvcHPpqc9dh1TmH1nc=",
version = "v0.0.0-20221031025758-80f0d8ca4d07",
)
go_repository(
name = "com_github_timakin_bodyclose",
Expand Down
12 changes: 12 additions & 0 deletions domain/domain_sysvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package domain

import (
"context"
"strconv"
"time"

Expand All @@ -33,6 +34,9 @@ func (do *Domain) initDomainSysVars() {
variable.SetStatsCacheCapacity.Store(do.setStatsCacheCapacity)
pdClientDynamicOptionFunc := do.setPDClientDynamicOption
variable.SetPDClientDynamicOption.Store(&pdClientDynamicOptionFunc)

variable.SetExternalTimestamp = do.setExternalTimestamp
variable.GetExternalTimestamp = do.getExternalTimestamp
}

// setStatsCacheCapacity sets statsCache cap
Expand Down Expand Up @@ -75,3 +79,11 @@ func (do *Domain) updatePDClient(option pd.DynamicOption, val interface{}) error
}
return pdClient.UpdateOption(option, val)
}

func (do *Domain) setExternalTimestamp(ctx context.Context, ts uint64) error {
return do.store.GetOracle().SetExternalTimestamp(ctx, ts)
}

func (do *Domain) getExternalTimestamp(ctx context.Context) (uint64, error) {
return do.store.GetOracle().GetExternalTimestamp(ctx)
}
1 change: 1 addition & 0 deletions executor/fktest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_test(
deps = [
"//config",
"//executor",
"//kv",
"//meta/autoid",
"//parser/model",
"//planner/core",
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/pingcap/errors v0.11.5-0.20220729040631-518f63d66278
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20221014081430-26e28e6a281a
github.com/pingcap/kvproto v0.0.0-20221026112947-f8d61344b172
github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4
github.com/pingcap/tidb/parser v0.0.0-20221013075951-a5c7c039c6c3
Expand All @@ -85,8 +85,8 @@ require (
github.com/stretchr/testify v1.8.0
github.com/tdakkota/asciicheck v0.1.1
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.1-0.20221026083454-6c9c7c7c5815
github.com/tikv/pd/client v0.0.0-20221010134149-d50e5fe43f14
github.com/tikv/client-go/v2 v2.0.1-0.20221031063202-30e803b7082c
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
github.com/twmb/murmur3 v1.1.3
github.com/uber/jaeger-client-go v2.30.0+incompatible
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,11 @@ github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059/go.mod h1:fMRU1BA1y+r89
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20220818063303-5c20f55db5ad/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20221014081430-26e28e6a281a h1:McYxPhA8SHqfUtLfQHHN0fQl4dy93IkhlX4Pp2MKIFA=
github.com/pingcap/kvproto v0.0.0-20221014081430-26e28e6a281a/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20221026112947-f8d61344b172 h1:FYgKV9znRQmzVrrJDZ0gUfMIvKLAMU1tu1UKJib8bEQ=
github.com/pingcap/kvproto v0.0.0-20221026112947-f8d61344b172/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81 h1:URLoJ61DmmY++Sa/yyPEQHG2s/ZBeV1FbIswHEMrdoY=
github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4 h1:HYbcxtnkN3s5tqrZ/z3eJS4j3Db8wMphEm1q10lY/TM=
Expand Down Expand Up @@ -930,10 +928,10 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tikv/client-go/v2 v2.0.1-0.20221026083454-6c9c7c7c5815 h1:s8eJEGI4p/fxFwMBkoJ+4FAEQNQhHR47TZmVW+EEtOE=
github.com/tikv/client-go/v2 v2.0.1-0.20221026083454-6c9c7c7c5815/go.mod h1:9hmGJFrWdehClHg0lv2cYgzvCUEhwLZkH67/PHl75tg=
github.com/tikv/pd/client v0.0.0-20221010134149-d50e5fe43f14 h1:REQOR1XraH1fT9BCoNBPZs1CAe+w7VPLU+d+si7DLYo=
github.com/tikv/pd/client v0.0.0-20221010134149-d50e5fe43f14/go.mod h1:E/7+Fkqzwsrp4duzJ2gLPqFl6awU7QG+5yFRXaQwimM=
github.com/tikv/client-go/v2 v2.0.1-0.20221031063202-30e803b7082c h1:NvQHWk0GeXSLEBbmGMPnDMc0to0a3ogzgIRbTKw8MHI=
github.com/tikv/client-go/v2 v2.0.1-0.20221031063202-30e803b7082c/go.mod h1:X9s4ct/MLk1sFqe5mU79KClKegLFDTa/FCx3hzexGtk=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07 h1:ckPpxKcl75mO2N6a4cJXiZH43hvcHPpqc9dh1TmH1nc=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07/go.mod h1:CipBxPfxPUME+BImx9MUYXCnAVLS3VJUr3mnSJwh40A=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
Expand Down
10 changes: 10 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,16 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
p.StaleTxnStartTS = readTS
// consume read ts here
b.ctx.GetSessionVars().TxnReadTS.UseTxnReadTS()
} else if b.ctx.GetSessionVars().EnableExternalTSRead && !b.ctx.GetSessionVars().InRestrictedSQL {
// try to get the stale ts from external timestamp
startTS, err := staleread.GetExternalTimestamp(ctx, b.ctx)
if err != nil {
return nil, err
}
if err := sessionctx.ValidateStaleReadTS(ctx, b.ctx, startTS); err != nil {
return nil, err
}
p.StaleTxnStartTS = startTS
}
}
return p, nil
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,7 @@ func (s *session) onTxnManagerStmtStartOrRetry(ctx context.Context, node ast.Stm

func (s *session) validateStatementReadOnlyInStaleness(stmtNode ast.StmtNode) error {
vars := s.GetSessionVars()
if !vars.TxnCtx.IsStaleness && vars.TxnReadTS.PeakTxnReadTS() == 0 {
if !vars.TxnCtx.IsStaleness && vars.TxnReadTS.PeakTxnReadTS() == 0 && !vars.EnableExternalTSRead {
return nil
}
errMsg := "only support read-only statement during read-only staleness transactions"
Expand Down
19 changes: 19 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ const (
StmtNowTsCacheKey StmtCacheKey = iota
// StmtSafeTSCacheKey is a variable for safeTS calculation/cache of one stmt.
StmtSafeTSCacheKey
// StmtExternalTSCacheKey is a variable for externalTS calculation/cache of one stmt.
StmtExternalTSCacheKey
)

// GetOrStoreStmtCache gets the cached value of the given key if it exists, otherwise stores the value.
Expand All @@ -397,6 +399,23 @@ func (sc *StatementContext) GetOrStoreStmtCache(key StmtCacheKey, value interfac
return sc.stmtCache.data[key]
}

// GetOrEvaluateStmtCache gets the cached value of the given key if it exists, otherwise calculate the value.
func (sc *StatementContext) GetOrEvaluateStmtCache(key StmtCacheKey, valueEvaluator func() (interface{}, error)) (interface{}, error) {
sc.stmtCache.mu.Lock()
defer sc.stmtCache.mu.Unlock()
if sc.stmtCache.data == nil {
sc.stmtCache.data = make(map[StmtCacheKey]interface{})
}
if _, ok := sc.stmtCache.data[key]; !ok {
value, err := valueEvaluator()
if err != nil {
return nil, err
}
sc.stmtCache.data[key] = value
}
return sc.stmtCache.data[key], nil
}

// ResetInStmtCache resets the cache of given key.
func (sc *StatementContext) ResetInStmtCache(key StmtCacheKey) {
sc.stmtCache.mu.Lock()
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,9 @@ type SessionVars struct {
// AnalyzePartitionMergeConcurrency indicates concurrency for merging partition stats
AnalyzePartitionMergeConcurrency int

// EnableExternalTSRead indicates whether to enable read through external ts
EnableExternalTSRead bool

HookContext

// OptPrefixIndexSingleScan indicates whether to do some optimizations to avoid double scan for prefix index.
Expand Down
17 changes: 17 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,23 @@ var defaultSysVars = []*SysVar{
s.OptPrefixIndexSingleScan = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBExternalTS, Value: strconv.FormatInt(DefTiDBExternalTS, 10), SetGlobal: func(ctx context.Context, s *SessionVars, val string) error {
ts, err := parseTSFromNumberOrTime(s, val)
if err != nil {
return err
}
return SetExternalTimestamp(ctx, ts)
}, GetGlobal: func(ctx context.Context, s *SessionVars) (string, error) {
ts, err := GetExternalTimestamp(ctx)
if err != nil {
return "", err
}
return strconv.Itoa(int(ts)), err
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableExternalTSRead, Value: BoolToOnOff(false), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.EnableExternalTSRead = TiDBOptOn(val)
return nil
}},
}

// FeedbackProbability points to the FeedbackProbability in statistics package.
Expand Down
12 changes: 12 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package variable

import (
"context"
"math"

"github.com/pingcap/tidb/config"
Expand Down Expand Up @@ -759,6 +760,9 @@ const (
// TiDBOptPrefixIndexSingleScan indicates whether to do some optimizations to avoid double scan for prefix index.
// When set to true, `col is (not) null`(`col` is index prefix column) is regarded as index filter rather than table filter.
TiDBOptPrefixIndexSingleScan = "tidb_opt_prefix_index_single_scan"

// TiDBEnableExternalTSRead indicates whether to enable read through an external ts
TiDBEnableExternalTSRead = "tidb_enable_external_ts_read"
)

// TiDB vars that have only global scope
Expand Down Expand Up @@ -837,6 +841,8 @@ const (
TiDBEnableGOGCTuner = "tidb_enable_gogc_tuner"
// TiDBGOGCTunerThreshold is to control the threshold of GOGC tuner.
TiDBGOGCTunerThreshold = "tidb_gogc_tuner_threshold"
// TiDBExternalTS is the ts to read through when the `TiDBEnableExternalTsRead` is on
TiDBExternalTS = "tidb_external_ts"
)

// TiDB intentional limits
Expand Down Expand Up @@ -1072,6 +1078,8 @@ const (
// DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold.
DefTiDBGOGCTunerThreshold float64 = 0.6
DefTiDBOptPrefixIndexSingleScan = true
DefTiDBExternalTS = 0
DefTiDBEnableExternalTSRead = false
)

// Process global variables.
Expand Down Expand Up @@ -1149,4 +1157,8 @@ var (
EnableDDL func() error = nil
// DisableDDL is the func registered by ddl to disable running ddl in this instance.
DisableDDL func() error = nil
// SetExternalTimestamp is the func registered by staleread to set externaltimestamp in pd
SetExternalTimestamp func(ctx context.Context, ts uint64) error
// GetExternalTimestamp is the func registered by staleread to get externaltimestamp from pd
GetExternalTimestamp func(ctx context.Context) (uint64, error)
)
2 changes: 1 addition & 1 deletion sessionctx/variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (sv *SysVar) SkipInit() bool {
func (sv *SysVar) SkipSysvarCache() bool {
switch sv.Name {
case TiDBGCEnable, TiDBGCRunInterval, TiDBGCLifetime,
TiDBGCConcurrency, TiDBGCScanLockMode:
TiDBGCConcurrency, TiDBGCScanLockMode, TiDBExternalTS:
return true
}
return false
Expand Down
18 changes: 11 additions & 7 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,25 @@ func setSnapshotTS(s *SessionVars, sVal string) error {
return fmt.Errorf("tidb_read_staleness should be clear before setting tidb_snapshot")
}

tso, err := parseTSFromNumberOrTime(s, sVal)
s.SnapshotTS = tso
// tx_read_ts should be mutual exclusive with tidb_snapshot
s.TxnReadTS = NewTxnReadTS(0)
return err
}

func parseTSFromNumberOrTime(s *SessionVars, sVal string) (uint64, error) {
if tso, err := strconv.ParseUint(sVal, 10, 64); err == nil {
s.SnapshotTS = tso
return nil
return tso, nil
}

t, err := types.ParseTime(s.StmtCtx, sVal, mysql.TypeTimestamp, types.MaxFsp)
if err != nil {
return err
return 0, err
}

t1, err := t.GoTime(s.Location())
s.SnapshotTS = oracle.GoTimeToTS(t1)
// tx_read_ts should be mutual exclusive with tidb_snapshot
s.TxnReadTS = NewTxnReadTS(0)
return err
return oracle.GoTimeToTS(t1), err
}

func setTxnReadTS(s *SessionVars, sVal string) error {
Expand Down
3 changes: 3 additions & 0 deletions sessiontxn/staleread/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_library(
"//parser/ast",
"//parser/mysql",
"//sessionctx",
"//sessionctx/stmtctx",
"//sessionctx/variable",
"//sessiontxn",
"//sessiontxn/internal",
Expand All @@ -36,6 +37,7 @@ go_test(
name = "staleread_test",
timeout = "short",
srcs = [
"externalts_test.go",
"main_test.go",
"processor_test.go",
"provider_test.go",
Expand All @@ -48,6 +50,7 @@ go_test(
"//kv",
"//parser",
"//parser/ast",
"//parser/auth",
"//sessionctx",
"//sessiontxn",
"//table/temptable",
Expand Down
Loading

0 comments on commit ba6ae45

Please sign in to comment.