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

table: Add option DupKeyCheckLazy to check duplicated key lazily #55246

Merged
merged 3 commits into from
Aug 8, 2024

Conversation

lcwangchao
Copy link
Collaborator

@lcwangchao lcwangchao commented Aug 7, 2024

What problem does this PR solve?

Issue Number: ref #54397

What changed and how does it work?

  • Added a new option DupKeyCheckLazy for table.DupKeyMode to check duplicated keys lazily in table operations. After this, the key check mode is determined by outside code and the logic is easier to understand.
  • Remove DupKeyCheckDefault and add DupKeyCheckInPlace to replace it.
  • Adding IgnoreError to UpdateExec and InsertExec to indicate INSERT IGNORE ... or UPDATE IGNORE .... The condition e.IgnoreError will replace ec.ErrGroupLevel(errctx.ErrGroupDupKey) != errctx.LevelError which makes code more readable.
  • Remove LazyCheckKeyNotExists because the lazy check mode is determined by each scene now, this method is useless now. This PR also removed PresumeKeyNotExists in SessionVars.

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. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 7, 2024
Copy link

tiprow bot commented Aug 7, 2024

Hi @lcwangchao. 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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Aug 7, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@lcwangchao lcwangchao force-pushed the dupkeychecklazy branch 3 times, most recently from a93279b to b0381d4 Compare August 7, 2024 06:30
Copy link

codecov bot commented Aug 7, 2024

Codecov Report

Attention: Patch coverage is 93.81443% with 6 lines in your changes missing coverage. Please review.

Project coverage is 74.1474%. Comparing base (3dfa15c) to head (b1140a3).
Report is 19 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #55246        +/-   ##
================================================
+ Coverage   72.8486%   74.1474%   +1.2987%     
================================================
  Files          1570       1573         +3     
  Lines        439518     443407      +3889     
================================================
+ Hits         320183     328775      +8592     
+ Misses        99610      94604      -5006     
- Partials      19725      20028       +303     
Flag Coverage Δ
integration 47.0532% <82.4742%> (?)
unit 71.8007% <93.8144%> (-0.0872%) ⬇️

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

Components Coverage Δ
dumpling 52.9567% <ø> (ø)
parser ∅ <ø> (∅)
br 46.2527% <ø> (+0.3905%) ⬆️

// https://github.com/pingcap/tidb/issues/54492#issuecomment-2229941881
// Though it is still in an insert statement, but it seems some old tests still think it should
// check constraints in place, see test:
// https://github.com/pingcap/tidb/blob/3117d3fae50bbb5dabcde7b9589f92bfbbda5dc6/pkg/executor/test/writetest/write_test.go#L419-L426
Copy link
Collaborator Author

@lcwangchao lcwangchao Aug 7, 2024

Choose a reason for hiding this comment

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

There are same corner cases here, just check the test case:

tk.MustExec(`set tidb_constraint_check_in_place = 0;`)
tk.MustExec("replace into t values (1),(2)")
tk.MustExec("begin")
err = tk.ExecToErr("update t set i = 2 where i = 1")
require.Error(t, err)
err = tk.ExecToErr("insert into t values (1) on duplicate key update i = i + 1")
require.Error(t, err)
tk.MustExec("rollback")

And some comments:

#54492 (comment)

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Aug 7, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Aug 7, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

pkg/executor/insert.go Outdated Show resolved Hide resolved
// https://github.com/pingcap/tidb/issues/54492#issuecomment-2229941881
// Though it is still in an insert statement, but it seems some old tests still think it should
// check constraints in place, see test:
// https://github.com/pingcap/tidb/blob/3117d3fae50bbb5dabcde7b9589f92bfbbda5dc6/pkg/executor/test/writetest/write_test.go#L419-L426
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the tidb_constraint_check_in_place_pessimistic also be explained here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

tidb_constraint_check_in_place_pessimistic is not considerate yet. It will be handled in another PR to introduce a new option like PessimisticLazyCheckMode which only works for pessimistic txn and DupKeyCheckMode is DupKeyCheckLazy

@@ -236,14 +237,42 @@ func (e *InsertExec) batchUpdateDupRows(ctx context.Context, newRows [][]types.D
e.stats.Prefetch += time.Since(prefetchStart)
}

// If the current row has some conflicts, the operation will be changed to update.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to wrap L240-L260 to a txn util function like optimizeDupKeyCheckForNormalInsert? And duplications from L284 to L297 in update.go could be avoided.

lcwangchao and others added 2 commits August 8, 2024 12:07
Co-authored-by: cfzjywxk <cfzjywxk@gmail.com>
Copy link
Contributor

@cfzjywxk cfzjywxk left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -1265,9 +1264,6 @@ type SessionVars struct {
// EnableGlobalIndex indicates whether we could create an global index on a partition table or not.
EnableGlobalIndex bool

// PresumeKeyNotExists indicates lazy existence checking is enabled.
PresumeKeyNotExists bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Good to see the complexity is reduced by removing the unncessary states.

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Aug 8, 2024
@cfzjywxk
Copy link
Contributor

cfzjywxk commented Aug 8, 2024

@crazycs520 @ekexium
PTAL if you have time.

@easonn7
Copy link

easonn7 commented Aug 8, 2024

/approve

Copy link
Contributor

@qw4990 qw4990 left a comment

Choose a reason for hiding this comment

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

LGTM for the optimizer part

Copy link

ti-chi-bot bot commented Aug 8, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, easonn7, qw4990

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 approved lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 8, 2024
Copy link

ti-chi-bot bot commented Aug 8, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-08-08 08:38:17.989054514 +0000 UTC m=+514027.856153601: ☑️ agreed by cfzjywxk.
  • 2024-08-08 09:17:35.021049449 +0000 UTC m=+516384.888148537: ☑️ agreed by qw4990.

Copy link

tiprow bot commented Aug 8, 2024

@lcwangchao: 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 b1140a3 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 d4c26ca into pingcap:master Aug 8, 2024
22 of 23 checks passed
@lcwangchao lcwangchao deleted the dupkeychecklazy branch August 8, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants