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

session: track LastCommitTS in SessionVars and check timestamps of later txns are larger #57305

Merged
merged 12 commits into from
Feb 13, 2025

Conversation

b6g
Copy link
Contributor

@b6g b6g commented Nov 12, 2024

What problem does this PR solve?

Issue Number: close #57165

Problem Summary:

This PR checks the invariant that timestamps of transactions in a session should increase monotonically.

It saves the timestamp of the last transaction in SessionVars, after the transaction is committed. The saved timestamp is compared with the start_ts and the commit_ts of the next transaction.

This PR depends on the TiKV client-go PR, tikv/client-go#1489, which exports commit_ts of the transaction.

What changed and how does it work?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-tests-checked labels Nov 12, 2024
Copy link

ti-chi-bot bot commented Nov 12, 2024

Hi @b6g. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Nov 12, 2024
Copy link

tiprow bot commented Nov 12, 2024

Hi @b6g. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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-sigs/prow repository.

@@ -929,6 +929,21 @@ func (s *session) CommitTxn(ctx context.Context) error {
s.sessionVars.StmtCtx.MergeExecDetails(nil, commitDetail)
}

if err == nil {
// save CommitTS in sessionVars for invariant check
// TODO: enable LastCommitTS with a session variable
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 will add a session variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in a separate PR #57313, to make review easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per discussion in #57313 (comment), we don't need a session variable.

@b6g b6g force-pushed the b6g/tsinvariant1 branch from e8df58f to e053b6f Compare November 12, 2024 07:04
@b6g b6g force-pushed the b6g/tsinvariant1 branch 2 times, most recently from 5cc54f3 to 8297ca5 Compare November 17, 2024 08:39
@dveeden
Copy link
Contributor

dveeden commented Nov 18, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added ok-to-test Indicates a PR is ready to be tested. and removed needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Nov 18, 2024
@b6g b6g force-pushed the b6g/tsinvariant1 branch 2 times, most recently from 4e39c69 to 31193ac Compare November 19, 2024 03:38
Copy link

codecov bot commented Nov 25, 2024

Codecov Report

Attention: Patch coverage is 70.96774% with 9 lines in your changes missing coverage. Please review.

Project coverage is 73.4547%. Comparing base (b7e9769) to head (85e0179).
Report is 1 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #57305        +/-   ##
================================================
+ Coverage   72.9819%   73.4547%   +0.4727%     
================================================
  Files          1693       1693                
  Lines        467972     468042        +70     
================================================
+ Hits         341535     343799      +2264     
+ Misses       105410     103227      -2183     
+ Partials      21027      21016        -11     
Flag Coverage Δ
integration 42.9857% <29.0322%> (?)
unit 72.2302% <70.9677%> (+0.0718%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 45.4740% <ø> (+0.0056%) ⬆️

@b6g b6g force-pushed the b6g/tsinvariant1 branch 3 times, most recently from fbfaf0f to 3c7fb77 Compare November 27, 2024 08:17
@b6g
Copy link
Contributor Author

b6g commented Nov 27, 2024

The client-go PR is merged and go.mod is updated. The PR is ready for review. PTAL Thanks!

go.mod Outdated
@@ -109,7 +109,7 @@ require (
github.com/tdakkota/asciicheck v0.2.0
Copy link
Member

Choose a reason for hiding this comment

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

Please run the make bazel_preapre and upload the dependencies.

@b6g
Copy link
Contributor Author

b6g commented Nov 27, 2024

Some tests failed with

Was it because the client-go PR was just merged and it takes time to create http://bazel-cache.pingcap.net:8080/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip?

@b6g b6g force-pushed the b6g/tsinvariant1 branch from 1af9227 to ef63538 Compare November 27, 2024 18:50
@b6g
Copy link
Contributor Author

b6g commented Nov 27, 2024

Some tests failed with

Was it because the client-go PR was just merged and it takes time to create http://bazel-cache.pingcap.net:8080/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip?

The error was,

Error in download_and_extract: java.io.IOException: Error downloading [http://bazel-cache.pingcap.net:8080/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip, http://ats.apps.svc/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip, https://cache.hawkingrei.com/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip, https://storage.googleapis.com/pingcapmirror/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip] to /root/.cache/bazel/_bazel_root/37ff7b0eddea115f60ab9f853788d7f5/external/com_github_tikv_client_go_v2/temp12921244511850196136/com_github_tikv_client_go_v2-v2.0.8-0.20241127071526-89643b0e8c9e.zip: GET returned 404 Not Found

@b6g
Copy link
Contributor Author

b6g commented Nov 28, 2024

/test build

@ti-chi-bot ti-chi-bot bot removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Feb 4, 2025
Copy link

ti-chi-bot bot commented Feb 4, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-01-08 04:38:11.286280732 +0000 UTC m=+328434.575112437: ☑️ agreed by ekexium.
  • 2025-02-04 02:39:32.636618077 +0000 UTC m=+153044.169030075: ☑️ agreed by cfzjywxk.

zap.Uint64("txnLastCommitTS", s.txn.lastCommitTS),
zap.String("sql", redact.String(s.sessionVars.EnableRedactLog, s.sessionVars.StmtCtx.OriginalSQL)),
)
return kv.ErrAssertionFailed
Copy link
Contributor

Choose a reason for hiding this comment

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

kv.ErrAssertionFailed is an error contains key info.

Without any argument, it'll be confusing.

[tikv:8141]assertion failed: key: %s, assertion: %s, start_ts: %v, existing start ts: %v, existing commit ts: %v

Consider just create error by errors.Errorf(...)

Copy link
Contributor

Choose a reason for hiding this comment

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

@b6g PTAL

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int)")
ts := oracle.GoTimeToTS(time.Now().Add(time.Minute))
Copy link
Contributor

Choose a reason for hiding this comment

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

Better using a ts from timestamp oracle.

Suggested change
ts := oracle.GoTimeToTS(time.Now().Add(time.Minute))
currentTS, err := store.GetOracle().GetTimestamp(context.Background(), &oracle.Option{TxnScope: oracle.GlobalTxnScope})
ts := oracle.GoTimeToTS(oracle.GetTimeFromTS(currentTS).Add(time.Minute))

zap.Uint64("startTS", sessVars.TxnCtx.StartTS),
zap.String("sql", redact.String(sessVars.EnableRedactLog, sessVars.StmtCtx.OriginalSQL)),
)
return nil, kv.ErrAssertionFailed
Copy link
Contributor

Choose a reason for hiding this comment

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

Same to the checking of commitTS, use errors.Errorf(...)

@@ -268,6 +270,10 @@ func (p *baseTxnContextProvider) getTxnStartTS() (uint64, error) {
return txn.StartTS(), nil
}

func (p *baseTxnContextProvider) usePresetStartTS() bool {
return p.constStartTS != 0 || p.sctx.GetSessionVars().SnapshotTS != 0
Copy link
Contributor

Choose a reason for hiding this comment

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

usePresetStartTS looks fine for me. But better leave a comment that we may have a flag which indicate the start_ts is from PD.

I took a look at the code and feel like we can add StartTSFromPD here, and set it to true if the start_ts is from a ts future here.

@b6g b6g force-pushed the b6g/tsinvariant1 branch from ddd6f66 to ecaeea3 Compare February 9, 2025 19:12
@b6g
Copy link
Contributor Author

b6g commented Feb 9, 2025

Updated. PTAL. Need another approval to merge. Thanks!

@cfzjywxk cfzjywxk requested a review from you06 February 11, 2025 00:43
@b6g b6g force-pushed the b6g/tsinvariant1 branch from 88102cb to 85e0179 Compare February 12, 2025 23:44
@you06
Copy link
Contributor

you06 commented Feb 13, 2025

/retest

2 similar comments
@you06
Copy link
Contributor

you06 commented Feb 13, 2025

/retest

@cfzjywxk
Copy link
Contributor

/retest

Copy link

ti-chi-bot bot commented Feb 13, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, ekexium, you06, yudongusa

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the approved label Feb 13, 2025
@hawkingrei
Copy link
Member

/retest

Copy link

tiprow bot commented Feb 13, 2025

@b6g: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
fast_test_tiprow 85e0179 link true /test fast_test_tiprow

Full PR test history. Your PR dashboard.

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-sigs/prow repository. I understand the commands that are listed here.

@ti-chi-bot ti-chi-bot bot merged commit 0c99ccb into pingcap:master Feb 13, 2025
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved component/dumpling This is related to Dumpling of TiDB. lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. require-LGT3 Indicates that the PR requires three LGTM. sig/planner SIG: Planner 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.

server: enhance the ts check considering commit timestamp
8 participants