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

Refactor txn context management in session #30535

Open
23 of 25 tasks
lcwangchao opened this issue Dec 8, 2021 · 1 comment
Open
23 of 25 tasks

Refactor txn context management in session #30535

lcwangchao opened this issue Dec 8, 2021 · 1 comment
Assignees
Labels
sig/sql-infra SIG: SQL Infra sig/transaction SIG:Transaction type/enhancement The issue or PR belongs to an enhancement.

Comments

@lcwangchao
Copy link
Collaborator

lcwangchao commented Dec 8, 2021

Introduction

We have some problems for the current transaction context implementation:

No single truth for txn context

Though we have sessionVars.TxnCtx to store the startTS and InfoSchema for current txn, we still need to obtain them from other places in some cases. For example, when we are using stale read in implicit transaction to read data, TxnCtx.InfoSchema and TxnCtx.StartTS is not set with right values. Instead, the real values are computed in session and passed to planner and executors by method arguments.

It seems that sessionctx.Context.GetInfoSchema provides the "current" infoschema. But it is not true, it provides the latest infoschema in implicit transaction. This behavior misleads developers a lot.

The codes maintaining txn context in different scenarios are serious coupling

We have several scenarios (i.e. RR/RC isolation, stale/historical read ...) providing txn context. These codes are mixed up not have not been modularized. So it is hard to read these codes.

Design

To solve the above problems. We'll introduce a new interface TxnManager which provides some methods to manage transaction context in session. The interface is like this:

pacakge sessiontxn

type TxnManager interface {
  GetTxnInfoSchema(ctx context.Context) (infoschema.InfoSchema, error)
  GetStmtReadTS(ctx context.Context) (uint64, error)
  ...
}

The get method, for example, GetTxnInfoSchema should be a single truth to return the real txn infoschema considering all scenarios.

For maintaining the context for different scenarios, we can also introduce a new interface TxnContextProvider :

type TxnContextProvider interface {
  Initialize(sctx sessionctx.Context) error;
  OnStartStmt() error;
  Txn(active bool) (kv.Transaction, error)
  GetTxnInfoSchema(ctx context.Context) (infoschema.InfoSchema, error)
  GetStmtReadTS(ctx context.Context) (uint64, error)
  ...
}

It provides two kind of methods. The first methods provide some lifecycle hooks, they should be called by TxnManager at the corresponding stage. The second methods provide context for their own scenario. After this, we can have RRIsolationContextProvider , RCIsolationContextProvider , StaleReadContextProvider ...

TxnManager should provide some methods to get/set the current provider:

type TxnContextProvider interface {
  SetContextProvider(provider *TxnContextProvider) error
  GetContextProvider() TxnContextProvider
  ...
}

We can obtain TxnManager from session like this:

txnManager := sessiontxn.GetTxnManager(sctx)

GetTxnManager is defined as a factory method:

func GetTxnManager(sctx sessionctx.Context) TxnManager {
  // Get txnManager from session
}

Refactor path

  1. At first step of refactoring, we can implement the "read" methods of the TxnManager which only "wraps" the current implements. At this time, SimpleTxnContextProvider can be provided to do the wrap works. We should also add some unit tests to ensure that the context get from TxnManager and the old interface equals.

  2. After step 1, the codes are still using the old interface to get the txn context. Then we should migrate these codes to reading the new interface for TxnManager . If we did well in step1, this step should be safe.

  3. After all interfaces are migrated to TxnManager . We can introduce more context providers to replace SimpleTxnContextProvider for each scenario. For example, we can introduce StaleReadTxnContextProvider to provide stale read context.

Tasks

@lcwangchao lcwangchao added the type/enhancement The issue or PR belongs to an enhancement. label Dec 8, 2021
@lcwangchao lcwangchao self-assigned this Dec 8, 2021
@lcwangchao lcwangchao added sig/transaction SIG:Transaction sig/sql-infra SIG: SQL Infra labels Dec 8, 2021
@cfzjywxk
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/sql-infra SIG: SQL Infra sig/transaction SIG:Transaction type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

No branches or pull requests

2 participants