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: unify the management of transaction activation by TxnManager. #35679

Merged
merged 27 commits into from
Jun 30, 2022

Conversation

SpadeA-Tang
Copy link
Contributor

@SpadeA-Tang SpadeA-Tang commented Jun 23, 2022

What problem does this PR solve?

Issue Number: close #35705, #35686

Problem Summary:

Now, we can activate transaction by calling Txn as well as activateTxn by the relevant transaction context provider.
activateTxn calls Txn internally. This PR reverses this call relationship, which means it lets Txn call activateTxn internally. The reason is that we should let the txnManager to manage the transaction related initialization and modification. Currently, if someone calls Txn 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's ActivateTxn.
And it also transfers the transaction related intialization from Txn to ActivateTxn.

By the way, it solves the problem in #35686.

What is changed and how it works?

Check List

Tests

Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jun 23, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • lcwangchao
  • you06

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/needs-linked-issue do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 23, 2022
kv/kv.go Outdated Show resolved Hide resolved
kv/kv.go Outdated Show resolved Hide resolved
session/session.go Outdated Show resolved Hide resolved
session/session.go Show resolved Hide resolved
session/txn.go Show resolved Hide resolved
sessVars.SetInTxn(true)
}

sessVars.TxnCtx.CouldRetry = sessiontxn.IsTxnRetryable(sessVars)
Copy link
Collaborator

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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.

@@ -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
Copy link
Collaborator

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

Copy link
Contributor Author

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.

sessiontxn/txn.go Outdated Show resolved Hide resolved
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
SpadeA-Tang and others added 2 commits June 24, 2022 15:00
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 27, 2022
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 27, 2022
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 27, 2022
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@sre-bot
Copy link
Contributor

sre-bot commented Jun 27, 2022

Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@SpadeA-Tang SpadeA-Tang added the sig/transaction SIG:Transaction label Jun 28, 2022
require.NoError(t, err)
se.txn.changeInvalidToValid(txn)
txn, err = se.Txn(true)
txn, err := se.Txn(false)
Copy link
Collaborator

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)

Copy link
Contributor Author

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.

if err != nil {
return nil, err
}

sessVars := p.sctx.GetSessionVars()
sessVars.TxnCtx.StartTS = txn.StartTS()

if !sessVars.IsAutocommit() && sessVars.SnapshotTS == 0 {
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified it.

session/txnmanager.go Outdated Show resolved Hide resolved
@@ -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 {
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

// 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 {
Copy link
Collaborator

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
return p.txn, nil
}

err := p.OnInitialize(p.ctx, sessiontxn.EnterNewTxnDefault)
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

return nil, err
}

txnFuture := p.sctx.GetPreparedTxnFuture()
Copy link
Collaborator

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.

sessiontxn/staleread/staleread_test.go Outdated Show resolved Hide resolved
}

type wrapTxn struct {
kv.Transaction
}

func (txn *wrapTxn) Wait(_ context.Context, _ sessionctx.Context) (kv.Transaction, error) {
return txn.Transaction, nil
Copy link
Collaborator

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 ?

SpadeA-Tang and others added 5 commits June 28, 2022 15:53
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>

tk := testkit.NewTestKit(t, store)
// This query should not panic
tk.MustExec("select * from information_schema.ddl_jobs as of timestamp now()")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select should use MustQuery

Copy link
Contributor Author

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.

@@ -176,6 +176,12 @@ type Context interface {
ReleaseAllAdvisoryLocks() int
}

// TxnFuture todo: add comments
Copy link
Collaborator

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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... Done.

@@ -51,6 +52,7 @@ type baseTxnContextProvider struct {
infoSchema infoschema.InfoSchema
txn kv.Transaction
isTxnPrepared bool
tp *sessiontxn.EnterNewTxnType
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// 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 {
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

default:
return errors.Errorf("Unsupported type: %v", tp)
}
}

func (p *StalenessTxnContextProvider) enterNewStaleTxn(ctx context.Context) error {
Copy link
Collaborator

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

Copy link
Contributor Author

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>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@SpadeA-Tang SpadeA-Tang added the release-note-none Denotes a PR that doesn't merit a release note. label Jun 29, 2022
@ti-chi-bot ti-chi-bot removed the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Jun 29, 2022
Copy link
Collaborator

@lcwangchao lcwangchao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

sessiontxn/isolation/optimistic.go Outdated Show resolved Hide resolved
Co-authored-by: 王超 <cclcwangchao@hotmail.com>
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 29, 2022
@@ -27,9 +28,11 @@ import (

// StalenessTxnContextProvider implements sessiontxn.TxnContextProvider
type StalenessTxnContextProvider struct {
ctx context.Context
Copy link
Contributor

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?

Copy link
Contributor Author

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.

if err != nil {
return err
}
p.is = temptable.AttachLocalTemporaryTableInfoSchema(p.sctx, is)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 29, 2022
@SpadeA-Tang
Copy link
Contributor Author

/run-mysql-test

1 similar comment
@SpadeA-Tang
Copy link
Contributor Author

/run-mysql-test

Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 30, 2022
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 30, 2022
@xhebox
Copy link
Contributor

xhebox commented Jun 30, 2022

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 8789554

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 30, 2022
@ti-chi-bot ti-chi-bot merged commit 11f39cd into pingcap:master Jun 30, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Jun 30, 2022

TiDB MergeCI notify

🔴 Bad News! [1] CI still failing after this pr merged.
These failed integration tests don't seem to be introduced by the current PR.

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/integration-common-test 🔴 failed 1, success 10, total 11 26 min Existing failure
idc-jenkins-ci/integration-cdc-test 🟢 all 35 tests passed 30 min Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 12 tests passed 15 min Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 7 min 3 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 6 min 38 sec Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 6 min 11 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 44 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 2 min 50 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 2 min 39 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. sig/transaction SIG:Transaction size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify the entrance of new transaction.
6 participants