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

txn: remove legacy.SimpleTxnContextProvider #35667

Merged
merged 12 commits into from
Jun 29, 2022
1 change: 0 additions & 1 deletion executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ go_library(
"//sessionctx/stmtctx",
"//sessionctx/variable",
"//sessiontxn",
"//sessiontxn/legacy",
"//sessiontxn/staleread",
"//statistics",
"//statistics/handle",
Expand Down
72 changes: 6 additions & 66 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/sessiontxn/legacy"
"github.com/pingcap/tidb/sessiontxn/staleread"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/store/helper"
Expand Down Expand Up @@ -88,14 +87,11 @@ var (
// executorBuilder builds an Executor from a Plan.
// The InfoSchema must not change during execution.
type executorBuilder struct {
ctx sessionctx.Context
is infoschema.InfoSchema
snapshotTS uint64 // The ts for snapshot-read. A select statement without for update will use this ts
forUpdateTS uint64 // The ts should be used by insert/update/delete/select-for-update statement
snapshotTSCached bool
err error // err is set when there is error happened during Executor building process.
hasLock bool
Ti *TelemetryInfo
ctx sessionctx.Context
is infoschema.InfoSchema
err error // err is set when there is error happened during Executor building process.
hasLock bool
Ti *TelemetryInfo
// isStaleness means whether this statement use stale read.
isStaleness bool
readReplicaScope string
Expand All @@ -121,26 +117,13 @@ type CTEStorages struct {
}

func newExecutorBuilder(ctx sessionctx.Context, is infoschema.InfoSchema, ti *TelemetryInfo, replicaReadScope string) *executorBuilder {
b := &executorBuilder{
return &executorBuilder{
ctx: ctx,
is: is,
Ti: ti,
isStaleness: staleread.IsStmtStaleness(ctx),
readReplicaScope: replicaReadScope,
}

txnManager := sessiontxn.GetTxnManager(ctx)
if provider, ok := txnManager.GetContextProvider().(*legacy.SimpleTxnContextProvider); ok {
provider.GetReadTSFunc = b.getReadTS
provider.GetForUpdateTSFunc = func() (uint64, error) {
if b.forUpdateTS != 0 {
return b.forUpdateTS, nil
}
return b.getReadTS()
}
}

return b
}

// MockPhysicalPlan is used to return a specified executor in when build.
Expand Down Expand Up @@ -660,8 +643,6 @@ func (b *executorBuilder) buildSelectLock(v *plannercore.PhysicalLock) Executor
if b.err = b.updateForUpdateTSIfNeeded(v.Children()[0]); b.err != nil {
return nil
}
// Build 'select for update' using the 'for update' ts.
b.forUpdateTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()

src := b.build(v.Children()[0])
if b.err != nil {
Expand Down Expand Up @@ -872,7 +853,6 @@ func (b *executorBuilder) buildInsert(v *plannercore.Insert) Executor {
return nil
}
}
b.forUpdateTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()
selectExec := b.build(v.SelectPlan)
if b.err != nil {
return nil
Expand Down Expand Up @@ -1578,44 +1558,6 @@ func (b *executorBuilder) getSnapshotTS() (uint64, error) {
return txnManager.GetStmtReadTS()
}

// getReadTS returns the ts used by select (without for-update clause). The return value is affected by the isolation level
// and some stale/historical read contexts. For example, it will return txn.StartTS in RR and return
// the current timestamp in RC isolation
func (b *executorBuilder) getReadTS() (uint64, error) {
failpoint.Inject("assertNotStaleReadForExecutorGetReadTS", func() {
// after refactoring stale read will use its own context provider
staleread.AssertStmtStaleness(b.ctx, false)
})

if b.snapshotTSCached {
return b.snapshotTS, nil
}

if snapshotTS := b.ctx.GetSessionVars().SnapshotTS; snapshotTS != 0 {
b.snapshotTS = snapshotTS
b.snapshotTSCached = true
return snapshotTS, nil
}

if b.snapshotTS != 0 {
b.snapshotTSCached = true
// Return the cached value.
return b.snapshotTS, nil
}

txn, err := b.ctx.Txn(true)
if err != nil {
return 0, err
}

b.snapshotTS = txn.StartTS()
if b.snapshotTS == 0 {
return 0, errors.Trace(ErrGetStartTS)
}
b.snapshotTSCached = true
return b.snapshotTS, nil
}

func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executor {
switch v.DBName.L {
case util.MetricSchemaName.L:
Expand Down Expand Up @@ -2117,7 +2059,6 @@ func (b *executorBuilder) buildUpdate(v *plannercore.Update) Executor {
if b.err = b.updateForUpdateTSIfNeeded(v.SelectPlan); b.err != nil {
return nil
}
b.forUpdateTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()
selExec := b.build(v.SelectPlan)
if b.err != nil {
return nil
Expand Down Expand Up @@ -2174,7 +2115,6 @@ func (b *executorBuilder) buildDelete(v *plannercore.Delete) Executor {
if b.err = b.updateForUpdateTSIfNeeded(v.SelectPlan); b.err != nil {
return nil
}
b.forUpdateTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()
selExec := b.build(v.SelectPlan)
if b.err != nil {
return nil
Expand Down
2 changes: 0 additions & 2 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ import (
func enableStaleReadCommonFailPoint(t *testing.T) func() {
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/assertStaleReadValuesSameWithExecuteAndBuilder", "return"))
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/planner/core/assertStaleReadForOptimizePreparedPlan", "return"))
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/assertNotStaleReadForExecutorGetReadTS", "return"))
return func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/assertStaleReadValuesSameWithExecuteAndBuilder"))
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/assertNotStaleReadForExecutorGetReadTS"))
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/planner/core/assertStaleReadForOptimizePreparedPlan"))
}
}
Expand Down
1 change: 0 additions & 1 deletion session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ go_library(
"//sessionctx/variable",
"//sessiontxn",
"//sessiontxn/isolation",
"//sessiontxn/legacy",
"//sessiontxn/staleread",
"//statistics",
"//statistics/handle",
Expand Down
10 changes: 1 addition & 9 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx/sessionstates"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/sessiontxn/legacy"
"github.com/pingcap/tidb/sessiontxn/staleread"
"github.com/pingcap/tidb/store/driver/txn"
"github.com/pingcap/tidb/store/helper"
Expand Down Expand Up @@ -2385,7 +2384,6 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
return nil, errors.Errorf("invalid CachedPrepareStmt type")
}

var is infoschema.InfoSchema
var snapshotTS uint64
replicaReadScope := oracle.GlobalTxnScope

Expand All @@ -2397,7 +2395,7 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
txnManager := sessiontxn.GetTxnManager(s)
if staleReadProcessor.IsStaleness() {
snapshotTS = staleReadProcessor.GetStalenessReadTS()
is = staleReadProcessor.GetStalenessInfoSchema()
is := staleReadProcessor.GetStalenessInfoSchema()
replicaReadScope = config.GetTxnScopeFromConfig()
err = txnManager.EnterNewTxn(ctx, &sessiontxn.EnterNewTxnRequest{
Type: sessiontxn.EnterNewTxnWithReplaceProvider,
Expand All @@ -2407,8 +2405,6 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
if err != nil {
return nil, err
}
} else {
is = s.GetInfoSchema().(infoschema.InfoSchema)
}

staleness := snapshotTS > 0
Expand All @@ -2424,10 +2420,6 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
return nil, err
}

if p, isOK := txnManager.GetContextProvider().(*legacy.SimpleTxnContextProvider); isOK {
p.InfoSchema = is
}

if ok {
rs, ok, err := s.cachedPointPlanExec(ctx, txnManager.GetTxnInfoSchema(), stmtID, preparedStmt, replicaReadScope, args)
if err != nil {
Expand Down
41 changes: 0 additions & 41 deletions sessiontxn/legacy/BUILD.bazel

This file was deleted.

Loading