Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao committed Apr 16, 2022
1 parent 93f5e67 commit e631e65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
39 changes: 23 additions & 16 deletions session/txnmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,26 @@ func (m *txnManager) GetTxnInfoSchema() infoschema.InfoSchema {
return m.ctxProvider.GetTxnInfoSchema()
}

func (m *txnManager) GetReadTS() (uint64, error) {
func (m *txnManager) GetReadTS() (ts uint64, err error) {
if m.ctxProvider == nil {
return 0, errors.New("context provider not set")
}
defer m.handleAutoCommit()
return m.ctxProvider.GetReadTS()
if ts, err = m.ctxProvider.GetReadTS(); err != nil {
return 0, err
}
m.handleAutoCommit()
return
}

func (m *txnManager) GetForUpdateTS() (uint64, error) {
func (m *txnManager) GetForUpdateTS() (ts uint64, err error) {
if m.ctxProvider == nil {
return 0, errors.New("context provider not set")
}
defer m.handleAutoCommit()
return m.ctxProvider.GetForUpdateTS()
if ts, err = m.ctxProvider.GetForUpdateTS(); err != nil {
return 0, err
}
m.handleAutoCommit()
return
}

func (m *txnManager) GetContextProvider() sessiontxn.TxnContextProvider {
Expand Down Expand Up @@ -123,17 +129,23 @@ func (m *txnManager) EnterNewTxn(ctx context.Context, r *sessiontxn.NewTxnReques
} else {
sessVars.SetInTxn(false)
}

return nil
}

func (m *txnManager) ActiveTxn(ctx context.Context) (kv.Transaction, error) {
func (m *txnManager) ActiveTxn(ctx context.Context) (txn kv.Transaction, err error) {
if m.ctxProvider == nil {
return nil, errors.New("context provider not set")
}
if txn, err = m.ctxProvider.ActiveTxn(ctx); err != nil {
return nil, err
}
m.handleAutoCommit()
return txn, err
}

defer m.handleAutoCommit()
return m.ctxProvider.ActiveTxn(ctx)
func (m *txnManager) IsTxnActive() bool {
txn, _ := m.sctx.Txn(false)
return txn.Valid()
}

func (m *txnManager) OnStmtStart(ctx context.Context) error {
Expand All @@ -160,12 +172,7 @@ func (m *txnManager) Advise(opt sessiontxn.AdviceOption, val ...interface{}) err

func (m *txnManager) handleAutoCommit() {
sessVars := m.sctx.GetSessionVars()
if sessVars.IsAutocommit() {
return
}

tx, _ := m.sctx.Txn(false)
if tx.Valid() {
if sessVars.IsAutocommit() && m.IsTxnActive() {
sessVars.SetInTxn(true)
}
}
14 changes: 8 additions & 6 deletions sessiontxn/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,13 @@ type TxnManager interface {
GetReadTS() (uint64, error)
// GetForUpdateTS returns the read timestamp used by update/insert/delete or select ... for update
GetForUpdateTS() (uint64, error)

// IsTxnActive returns whether the current txn is active
IsTxnActive() bool
// ActiveTxn actives the txn
ActiveTxn(ctx context.Context) (kv.Transaction, error)
// GetContextProvider returns the current TxnContextProvider
GetContextProvider() TxnContextProvider
// ReplaceContextProvider replaces the context provider
ReplaceContextProvider(provider TxnContextProvider) error

// EnterNewTxn enters a new txn
EnterNewTxn(ctx context.Context, request *NewTxnRequest) error

// OnStmtStart is the hook that should be called when a new statement started
OnStmtStart(ctx context.Context) error
// OnStmtRetry is the hook that should be called when a statement is retrying
Expand All @@ -177,6 +174,11 @@ type TxnManager interface {
// For example, we can give `AdviceWarmUpNow` to advice provider prefetch tso.
// Give or not give an advice should not affect the correctness.
Advise(opt AdviceOption, val ...interface{}) error

// GetContextProvider returns the current TxnContextProvider
GetContextProvider() TxnContextProvider
// ReplaceContextProvider replaces the context provider
ReplaceContextProvider(provider TxnContextProvider) error
}

func AdviseTxnWarmUp(sctx sessionctx.Context) error {
Expand Down

0 comments on commit e631e65

Please sign in to comment.