-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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: unify the management of transaction activation by TxnManager. #35679
Conversation
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
sessiontxn/isolation/base.go
Outdated
sessVars.SetInTxn(true) | ||
} | ||
|
||
sessVars.TxnCtx.CouldRetry = sessiontxn.IsTxnRetryable(sessVars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TxnCtx.CouldRetry can only be true when it is a optimistic txn, so it's better to move set it in OptimisticTxnContextProvider.onTxnActive
, for other providers, keep it always false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, session.getSnapshotInterceptor can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion. I will modify it.
sessiontxn/staleread/provider.go
Outdated
@@ -90,6 +91,11 @@ func (p *StalenessTxnContextProvider) OnStmtStart(_ context.Context) error { | |||
return nil | |||
} | |||
|
|||
// ActivateTxn activates the transaction. | |||
func (p *StalenessTxnContextProvider) ActivateTxn() (kv.Transaction, error) { | |||
return nil, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also support ActivateTxn
for StalenessTxnContextProvider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I have added some code for this.
Code Coverage Details: https://codecov.io/github/pingcap/tidb/commit/8ffb6eb2b70805b43f865d66412d6c0e07fe0cf4 |
require.NoError(t, err) | ||
se.txn.changeInvalidToValid(txn) | ||
txn, err = se.Txn(true) | ||
txn, err := se.Txn(false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Txn(true) => Txn(false)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The txn now is already actiavated in sessiontxn.NewTxn(ctx, se), so whether true or false here has no difference.
sessiontxn/isolation/base.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sessVars := p.sctx.GetSessionVars() | ||
sessVars.TxnCtx.StartTS = txn.StartTS() | ||
|
||
if !sessVars.IsAutocommit() && sessVars.SnapshotTS == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When EnterNewTxnDefault
and autocommit=0
it will also enter an explicit txn? We should keep the behavior the same with the old implement. And we need to add more tests for this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have modified it.
@@ -326,6 +326,10 @@ func canSkipSchemaCheckerDDL(tp model.ActionType) bool { | |||
|
|||
// InfoSchema gets the latest information schema from domain. | |||
func (do *Domain) InfoSchema() infoschema.InfoSchema { | |||
if do.infoCache == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If mockContext can return a valid infoschema, can we revert this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is still needed as some tests does not init domain propoerly.
sessiontxn/isolation/optimistic.go
Outdated
// If the session is already in transaction, enable retry or internal SQL could retry. | ||
// If not, the transaction could always retry, because it should be auto committed transaction. | ||
// Anyway the retry limit is 0, the transaction could not retry. | ||
func isTxnRetryable(sessVars *variable.SessionVars, tp *sessiontxn.EnterNewTxnType) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isTxnRetryable => isOptimisticTxnRetryable may be better because it is only used by optimistic mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sessiontxn/staleread/provider.go
Outdated
return p.txn, nil | ||
} | ||
|
||
err := p.OnInitialize(p.ctx, sessiontxn.EnterNewTxnDefault) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why call p.OnInitialize
here? p.OnInitialize
should have been called already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I have adjusted the strcuture of this.
sessiontxn/staleread/provider.go
Outdated
return nil, err | ||
} | ||
|
||
txnFuture := p.sctx.GetPreparedTxnFuture() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should call NewStaleTxnWithStartTS
because stale read has some it's own states to set.
util/mock/context.go
Outdated
} | ||
|
||
type wrapTxn struct { | ||
kv.Transaction | ||
} | ||
|
||
func (txn *wrapTxn) Wait(_ context.Context, _ sessionctx.Context) (kv.Transaction, error) { | ||
return txn.Transaction, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return txn.Transaction
not txn
?
executor/stale_txn_test.go
Outdated
|
||
tk := testkit.NewTestKit(t, store) | ||
// This query should not panic | ||
tk.MustExec("select * from information_schema.ddl_jobs as of timestamp now()") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
select should use MustQuery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are the same in this case. Anyway, I will change it.
sessionctx/context.go
Outdated
@@ -176,6 +176,12 @@ type Context interface { | |||
ReleaseAllAdvisoryLocks() int | |||
} | |||
|
|||
// TxnFuture todo: add comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add comments now...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... Done.
sessiontxn/isolation/base.go
Outdated
@@ -51,6 +52,7 @@ type baseTxnContextProvider struct { | |||
infoSchema infoschema.InfoSchema | |||
txn kv.Transaction | |||
isTxnPrepared bool | |||
tp *sessiontxn.EnterNewTxnType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why it is a pointer? And the name enterNewTxnType
may be better because it is not provider's type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sessiontxn/isolation/optimistic.go
Outdated
// If the session is already in transaction, enable retry or internal SQL could retry. | ||
// If not, the transaction could always retry, because it should be auto committed transaction. | ||
// Anyway the retry limit is 0, the transaction could not retry. | ||
func isOptimisticTxnRetryable(sessVars *variable.SessionVars, tp *sessiontxn.EnterNewTxnType) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto A pointer is not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
sessiontxn/staleread/provider.go
Outdated
default: | ||
return errors.Errorf("Unsupported type: %v", tp) | ||
} | ||
} | ||
|
||
func (p *StalenessTxnContextProvider) enterNewStaleTxn(ctx context.Context) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's name can be activateTxn
or activateStaleTxn
or just merge the codes to ActivateTxn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest LGTM
Co-authored-by: 王超 <cclcwangchao@hotmail.com>
@@ -27,9 +28,11 @@ import ( | |||
|
|||
// StalenessTxnContextProvider implements sessiontxn.TxnContextProvider | |||
type StalenessTxnContextProvider struct { | |||
ctx context.Context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit weird to store the go context in a struct and update it in other methods that accept the latest context(I also find the same usage in other places), the go context may be modified before the next usage like ActivateTxn
, but we still use the old one. Will making the function signature like ActivateTxn(context.Context) (kv.Transaction, error)
better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx
will be updated in provider when OnInitialize/OnStmtStart/OnStmtRetry is called which guarantees that the ctx is the same with the current statment context. On the other hand, if we choose to convey ctx in parameter, we will make many modifications as GetStmtReadTS/GetStmtForUpdateTS... all need to add this parameter.
sessiontxn/staleread/provider.go
Outdated
if err != nil { | ||
return err | ||
} | ||
p.is = temptable.AttachLocalTemporaryTableInfoSchema(p.sctx, is) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so.
/run-mysql-test |
1 similar comment
/run-mysql-test |
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
/merge |
This pull request has been accepted and is ready to merge. Commit hash: 8789554
|
TiDB MergeCI notify🔴 Bad News! [1] CI still failing after this pr merged.
|
What problem does this PR solve?
Issue Number: close #35705, #35686
Problem Summary:
Now, we can activate transaction by calling
Txn
as well asactivateTxn
by the relevant transaction context provider.activateTxn
callsTxn
internally. This PR reverses this call relationship, which means it letsTxn
callactivateTxn
internally. The reason is that we should let the txnManager to manage the transaction related initialization and modification. Currently, if someone callsTxn
directly, txnManager will not be involved with it.This PR adds a new method for the interface
TxnManager
:ActivateTxn
, which calls the relevant transaction context provider'sActivateTxn
.And it also transfers the transaction related intialization from
Txn
toActivateTxn
.By the way, it solves the problem in #35686.
What is changed and how it works?
Check List
Tests