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

*: fix ctrl+c can not kill tidb during statistics init #54594

Merged
merged 7 commits into from
Jul 15, 2024

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #54589

Problem Summary:

When there a a lot of tables, statistics initialize takes a long time.
For example, 1M tables takes 13-14min with infoschema v2 currently.

When TiDB start up and initialize statistics, it does not handle any interrupt signal.
So if I start tidb and then Ctrl+C, domain.Close will wait the background worker to exit.
But the statistics load worker are handling the statistics initialization, it never response to the exit signal.
So tidb can't exit.

What changed and how does it work?

Add a context.Context to the related statistics code and handle context.Cancel signal.

Check List

Tests

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

Before this commit, the log is this:

[2024/07/11 16:25:27.006 +08:00] [INFO] [domain.go:1164] ["domain closed"] ["take time"=12m54.727120057s]

And after the fix, tidb can exit quickly:

[2024/07/12 10:42:00.562 +08:00] [INFO] [wait_group_wrapper.go:140] ["background process exited"] [source=domain] [process=loadStatsWorker]
[2024/07/12 10:42:02.552 +08:00] [INFO] [manager.go:326] ["revoke session"] ["owner info"="[bindinfo] /tidb/bindinfo/owner ownerManager 906be1c7-194e-4bb4-a742-09a9a4db15de"] [error="rpc error: code = Canceled desc = grpc: the client connection is closing"]
[2024/07/12 10:42:02.552 +08:00] [INFO] [domain.go:1984] ["globalBindHandleWorkerLoop exited."]
[2024/07/12 10:42:02.552 +08:00] [INFO] [wait_group_wrapper.go:140] ["background process exited"] [source=domain] [process=globalBindHandleWorkerLoop]
[2024/07/12 10:42:02.574 +08:00] [INFO] [manager.go:326] ["revoke session"] ["owner info"="[stats] /tidb/stats/owner ownerManager 906be1c7-194e-4bb4-a742-09a9a4db15de"] [error="rpc error: code = Canceled desc = grpc: the client connection is closing"]
[2024/07/12 10:42:02.574 +08:00] [INFO] [domain.go:2509] ["updateStatsWorker exit preprocessing finished"]
[2024/07/12 10:42:02.574 +08:00] [INFO] [domain.go:2552] ["updateStatsWorker exited."]
[2024/07/12 10:42:02.574 +08:00] [INFO] [wait_group_wrapper.go:140] ["background process exited"] [source=domain] [process=updateStatsWorker]

[2024/07/12 10:42:02.575 +08:00] [INFO] [domain.go:1168] ["domain closed"] ["take time"=2.55484294s]

[2024/07/12 10:42:02.575 +08:00] [INFO] [gc_worker.go:223] [quit] [category="gc worker"] [uuid=6429345da3c0005]
[2024/07/12 10:42:02.575 +08:00] [INFO] [pd_service_discovery.go:550] ["[pd] exit member loop due to context canceled"]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_client.go:147] ["[tso] exit tso dispatcher check loop"]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_dispatcher.go:137] ["[tso] exit tso deadline watcher"] [dc-location=global]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_dispatcher.go:210] ["[tso] stop fetching the pending tso requests due to context canceled"] [dc-location=global]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_dispatcher.go:350] ["[tso] exit tso connection contexts updater"] [dc-location=global]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_dispatcher.go:171] ["[tso] exit tso dispatcher"] [dc-location=global]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_batch_controller.go:160] ["[pd] clear the tso batch controller"] [max-batch-size=10000] [best-batch-size=1] [collected-request-count=0] [pending-request-count=0]
[2024/07/12 10:42:02.575 +08:00] [INFO] [resource_manager_client.go:290] ["[resource manager] exit resource token dispatcher"]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_client.go:157] ["[tso] closing tso client"]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_client.go:162] ["[tso] close tso client"]
[2024/07/12 10:42:02.575 +08:00] [INFO] [tso_batch_controller.go:160] ["[pd] clear the tso batch controller"] [max-batch-size=10000] [best-batch-size=1] [collected-request-count=0] [pending-request-count=0]
  • 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 do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner labels Jul 12, 2024
@ti-chi-bot ti-chi-bot bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 12, 2024
@tiancaiamao tiancaiamao requested a review from qw4990 July 12, 2024 06:20
Copy link

tiprow bot commented Jul 12, 2024

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

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 12, 2024
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 12, 2024
Copy link
Contributor

@lance6716 lance6716 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

// Detect the context cancel signal, since it may take a long time for the loop.
// TODO: add context to TableInfoByID and remove this code block?
select {
case <-ctx.Done():
Copy link
Contributor

Choose a reason for hiding this comment

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

if ctx.Err() != nil { return }

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

Copy link
Contributor

@lance6716 lance6716 Jul 15, 2024

Choose a reason for hiding this comment

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

No I mean replace the whole select {case <- ctx.Done(): ..., default: ...}.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think @lance6716 means we can use if ctx.Err() != nil { return } instead of line63-69.

pkg/statistics/handle/cache/statscache.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 12, 2024
Copy link

ti-chi-bot bot commented Jul 12, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-07-12 06:23:02.378094948 +0000 UTC m=+597879.613329046: ☑️ agreed by hawkingrei.
  • 2024-07-12 07:42:14.41667926 +0000 UTC m=+602631.651913371: ☑️ agreed by lance6716.

Copy link

codecov bot commented Jul 12, 2024

Codecov Report

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

Project coverage is 73.2545%. Comparing base (05c8119) to head (f8c0095).
Report is 11 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #54594        +/-   ##
================================================
+ Coverage   72.7895%   73.2545%   +0.4649%     
================================================
  Files          1549       1549                
  Lines        436285     437999      +1714     
================================================
+ Hits         317570     320854      +3284     
+ Misses        99067      97478      -1589     
- Partials      19648      19667        +19     
Flag Coverage Δ
integration 43.5413% <49.0196%> (?)

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

Components Coverage Δ
dumpling 52.9656% <ø> (ø)
parser ∅ <ø> (∅)
br 45.8706% <ø> (+0.0100%) ⬆️

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 12, 2024
@ti-chi-bot ti-chi-bot bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 15, 2024
Copy link

ti-chi-bot bot commented Jul 15, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hawkingrei, lance6716, zimulala

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 Jul 15, 2024
@hawkingrei
Copy link
Member

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Jul 15, 2024
@tiancaiamao
Copy link
Contributor Author

/retest

@ti-chi-bot ti-chi-bot bot merged commit 06e0e17 into pingcap:master Jul 15, 2024
12 checks passed
@tiancaiamao tiancaiamao deleted the ctrlc-exit branch July 15, 2024 10:11
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. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ctrl+C can't make tidb exit with 1M tables using infoschema v2
4 participants