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

store/copr: optimize copIterator by avoid start new goroutine #57522

Merged
merged 66 commits into from
Dec 17, 2024

Conversation

crazycs520
Copy link
Contributor

@crazycs520 crazycs520 commented Nov 19, 2024

What problem does this PR solve?

Issue Number: ref #56649

Problem Summary: optimize cop iterator by avoid start new goroutine

What changed and how does it work?

Before this PR, copIterator always start at least 1 copIteratorWorker goroutine and 1 copIteratorTaskSender goroutine. That's too wasteful for small queries.

This PR avoid start new goroutine for small queries, for better performance, following is the sysbench oltp_index_scan test, and the range size is 2:

sysbench --config-file=sysbench.conf oltp_index_scan --tables=16 --table-size=1000000 --threads=100 --range-size=2 run

The example query is like this:

SELECT
  k
FROM
  sbtest1
WHERE
  k BETWEEN ?
  AND ? [arguments: (503925, 503926)]

Which Plan is:


| id                 | estRows | estCost | actRows | task      | access object               | execution info                                                                                                                                                                                                                                                                                         | operator info                           | memory    | disk  |
| IndexReader_6      | 2.07    | 31.20   | 2       | root      |                             | time:1.31ms, loops:2, cop_task: {num: 1, max: 1.15ms, proc_keys: 2, tot_proc: 81.2µs, tot_wait: 575.8µs, copr_cache: disabled, build_task_duration: 12.2µs, max_distsql_concurrency: 1}, rpc_info:{Cop:{num_rpc:1, total_time:1.12ms}}                                                                 | index:IndexRangeScan_5                  | 278 Bytes | N/A   |
| └─IndexRangeScan_5 | 2.07    | 336.86  | 2       | cop[tikv] | table:sbtest1, index:k_1(k) | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 2, total_process_keys_size: 92, total_keys: 3, get_snapshot_time: 539.6µs, rocksdb: {key_skipped_count: 2, block: {cache_hit_count: 6}}}, time_detail: {total_process_time: 81.2µs, total_wait_time: 575.8µs, tikv_wall_time: 751.3µs} | range:[503925,503926], keep order:false | N/A       | N/A   |
version workload thread QPS QPS Increase
master oltp_index_scan 10 9224  
This PR oltp_index_scan 10 9471 2.6%
master oltp_index_scan 50 36072  
This PR oltp_index_scan 50 37392 3.6%
master oltp_index_scan 100 41977  
This PR oltp_index_scan 100 43554 3.7%
master oltp_index_scan 150 42785  
This PR oltp_index_scan 150 46430 8.5%

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test

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

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
…-iter0

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@ti-chi-bot ti-chi-bot bot added do-not-merge/invalid-title do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 19, 2024
Copy link

tiprow bot commented Nov 19, 2024

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

Copy link

codecov bot commented Nov 19, 2024

Codecov Report

Attention: Patch coverage is 88.18182% with 26 lines in your changes missing coverage. Please review.

Project coverage is 73.6562%. Comparing base (aec0fc5) to head (fdeff5b).
Report is 26 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #57522        +/-   ##
================================================
+ Coverage   73.2097%   73.6562%   +0.4465%     
================================================
  Files          1679       1679                
  Lines        462339     462466       +127     
================================================
+ Hits         338477     340635      +2158     
+ Misses       103082     101054      -2028     
+ Partials      20780      20777         -3     
Flag Coverage Δ
integration 43.3960% <75.9090%> (?)
unit 72.3521% <88.1818%> (+0.0077%) ⬆️

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

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

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@crazycs520
Copy link
Contributor Author

/retest-required

Copy link

tiprow bot commented Nov 20, 2024

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

In response to this:

/retest-required

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.

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@crazycs520
Copy link
Contributor Author

/retest-required

Copy link

tiprow bot commented Nov 20, 2024

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

In response to this:

/retest-required

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.

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@@ -85,6 +85,9 @@ type DistSQLContext struct {
SessionAlias string

ExecDetails *execdetails.SyncExecDetails

// Only one cop-reader can use lite worker. Using lite-worker in multiple readers will affect the concurrent execution of readers.
TryCopLiteWorker uint32
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm inclined that the TryCopLiteWorker is a request level option.

Like an index lookup with 1 row result set. If TryCopLiteWorker is a session level option, it's only enabled once in index scan, and in table lookup, the atomic.CompareAndSwapUint32(tryCopLiteWorker, 0, 1) will fail and execute with multi-goroutine model.

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'm afraid that make TryCopLiteWorker in request level option may have similar problem in this comment, there is a known case and I have added test for it. And I'm not sure if there are other cases like this, so I prefer keep TryCopLiteWorker to be statement level.

As index lookup with 1 row result set, you are right, but because of the above problems, I don't have a good way to fix this.

Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@cfzjywxk cfzjywxk requested review from you06 and zyguan December 12, 2024 09:33
@crazycs520
Copy link
Contributor Author

/retest-required

@pingcap pingcap deleted a comment from ti-chi-bot bot Dec 12, 2024
@pingcap pingcap deleted a comment from tiprow bot Dec 12, 2024
@pingcap pingcap deleted a comment from tiprow bot Dec 13, 2024
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 16, 2024
Copy link
Contributor

@you06 you06 left a comment

Choose a reason for hiding this comment

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

LGTM, once we have async kv interface, we can remove TryCopLiteWorker and make more coprocessor tasks benefit from this opimization.
Wait for @cfzjywxk's approve.

Copy link

ti-chi-bot bot commented Dec 17, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, zyguan

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

ti-chi-bot bot commented Dec 17, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-12-16 04:16:23.878897798 +0000 UTC m=+843973.967700342: ☑️ agreed by zyguan.
  • 2024-12-17 02:05:29.661253228 +0000 UTC m=+922519.750055771: ☑️ agreed by cfzjywxk.

@cfzjywxk
Copy link
Contributor

/retest

Copy link

tiprow bot commented Dec 17, 2024

@cfzjywxk: 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.

@cfzjywxk
Copy link
Contributor

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Dec 17, 2024
@cfzjywxk
Copy link
Contributor

/retest

@crazycs520
Copy link
Contributor Author

/retest-required

@ti-chi-bot ti-chi-bot bot merged commit 0ccee0e into pingcap:master Dec 17, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved 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. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants