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

*: avoid pass session ID by context & fix no schemaChecker error log #24696

Closed
wants to merge 2 commits into from

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #22783

Problem Summary:

Pass important argument by context.WithValue is a bad idea.

The callee need the data in the context.Context, but the caller maybe passing a context.Bacground()
#22783 is caused by such kind of refactoring.

In the beginning, we just use the session ID for logging.
Now I review the code and find we're using sessionID == 0 to check whether it's an internal transaction or a user's transaction.

What is changed and how it works?

Introduce a txn.SetOption(kv.SessionID, id) to replace the old way of passing session ID by context.Context.

What's Changed:

Use txn.SetOption(kv.SessionID, id) instead of txn.Commit(tikvutil.SetSessionID(ctx, s.GetSessionVars().ConnectionID)) to pass the session ID.

How it Works:

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • No code

Release note

  • No release note

@tiancaiamao tiancaiamao requested a review from a team as a code owner May 17, 2021 14:06
@tiancaiamao tiancaiamao requested review from lzmhhh123 and removed request for a team May 17, 2021 14:06
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

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 writing /lgtm in a comment.
Reviewer can cancel approval by writing /lgtm cancel in a comment.

@ti-chi-bot ti-chi-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 17, 2021
@tiancaiamao tiancaiamao requested review from cfzjywxk and lysu and removed request for lzmhhh123 May 17, 2021 14:06
@tiancaiamao tiancaiamao added the sig/transaction SIG:Transaction label May 17, 2021
@github-actions github-actions bot added sig/execution SIG execution sig/sql-infra SIG: SQL Infra labels May 17, 2021
@ichn-hu ichn-hu mentioned this pull request May 17, 2021
@@ -369,9 +368,7 @@ func (txn *KVTxn) Commit(ctx context.Context) error {
// pessimistic transaction should also bypass latch.
if txn.store.txnLatches == nil || txn.IsPessimistic() {
err = committer.execute(ctx)
if val == nil || sessionID > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this make the following logic executed unexpected for the inner session statements whose session id is 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For internal queries, val is always nil; for external queries sessionID > 0 always hold,
so val == nil || sessionID > 0 is basically always true? @MyonKeminta

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I've change it to:
sessionID > 0 => onCommit
sessionID == 0 => nothing

The original author means to call onCommit for external queries (although val == nil may be confusing in the old code).
@cfzjywxk

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it's hard to distinguish external from internal queries form sessionID?

InRestrictedSQL also can call doCommit

s.txn.SetOption(kv.SessionID, s.GetSessionVars().ConnectionID)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we take a session from the session pool to execute the SQL, sessionID is not set.
However, if we direct use the session to execute a internal SQL, that might be a problem.

@@ -306,7 +306,6 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
}()

sctx := a.Ctx
ctx = util.SetSessionID(ctx, sctx.GetSessionVars().ConnectionID)
Copy link
Contributor

@lysu lysu May 20, 2021

Choose a reason for hiding this comment

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

it seems some other test case

if v := bo.GetCtx().Value(util.SessionID); v != nil {
need this ..

@MyonKeminta PTAL

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 updated the test, you can see the _test.go file changes in this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

it seems the previous

inject = false

will be used in some test case that out of tidb repo
https://github.com/pingcap/automated-tests/blob/cc44a56fd59cfdb71eaf37210e0861ba7acdc925/ticases/transaction/asynccommit/linearizability.go#L445

I'm not sure whether it works 😞

Copy link
Contributor

@cfzjywxk cfzjywxk May 21, 2021

Choose a reason for hiding this comment

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

Seems we may change the session id related logic too or abstract a isInnerExecution accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes that's a problem. Some failpoints that's used by tests outside the repo checks the session id and works only when session id != 0. These kind of usages also occurs in 2pc.go, prewrite.go, etc.

@cfzjywxk cfzjywxk requested a review from MyonKeminta May 21, 2021 02:18
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 29, 2021
@ti-chi-bot
Copy link
Member

@tiancaiamao: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tiancaiamao
Copy link
Contributor Author

Close for now, it's not a very easy fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. sig/execution SIG execution sig/sql-infra SIG: SQL Infra sig/transaction SIG:Transaction size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unexpected warning when inserting, schemaLeaseChecker is not set for this transaction
5 participants