Skip to content

server/cluster: avoid repeated region size scans (#11072) - #11158

Open
ti-chi-bot wants to merge 2 commits into
tikv:release-8.5from
ti-chi-bot:cherry-pick-11072-to-release-8.5
Open

server/cluster: avoid repeated region size scans (#11072)#11158
ti-chi-bot wants to merge 2 commits into
tikv:release-8.5from
ti-chi-bot:cherry-pick-11072-to-release-8.5

Conversation

@ti-chi-bot

Copy link
Copy Markdown
Member

This is an automated cherry-pick of #11072

What problem does this PR solve?

When multiple stores are in the Preparing state, PD recalculates the same
region sizes for every store during each checkStores round. With placement
rules enabled, calculateRange also calls GetRegionSizeByRange once for
every matching rule.

For non-empty key ranges, these repeated scans can significantly increase
Region tree lock contention and delay Region heartbeat processing.

Issue Number: ref #9574

What is changed and how does it work?

Cache region sizes by key range within one checkStores round so all Preparing
stores reuse the same calculation.

Load each range size before processing its matching placement rules instead of
scanning the range once per rule.

Discard the cache after every `checkStores` round so the next round reloads
region sizes from the current Region tree.

Limitations

This PR eliminates duplicate range scans across placement rules and Preparing
stores within one checkStores round. Each unique non-empty range is still
scanned in O(N) time once per round. Supporting O(log N) range queries or
incremental range-size aggregation will be handled separately.

Check List

Tests

  • Unit test

Related changes

Release note

Reduce repeated Region tree scans when multiple stores are preparing.

Summary by CodeRabbit

  • Bug Fixes

    • Improved store readiness and placement-rule threshold calculations by reusing region-size results within each calculation round.
    • Corrected placement-rule range evaluation to begin with the first rule matching the target store.
    • Preserved separate calculations across rounds to prevent stale sizing results.
  • Tests

    • Added coverage for cache reuse across stores and placement rules, independent range caching, and calculation-round isolation.

ref tikv#9574

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot ti-chi-bot added dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR. labels Aug 18, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

This cherry pick PR is for a release branch and has not yet been approved by triage owners.
Adding the do-not-merge/cherry-pick-not-approved label.

To merge this cherry pick:

  1. It must be LGTMed and approved by the reviewers firstly.
  2. For pull requests to TiDB-x branches, it must have no failed tests.
  3. AFTER it has lgtm and approved labels, please wait for the cherry-pick merging approval from triage owners.
Details

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

Copy link
Copy Markdown
Member Author

@lhy1024 This PR has conflicts, I have hold it.
Please resolve them or ask others to resolve them, then comment /unhold to remove the hold label.

@ti-chi-bot

ti-chi-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@ti-chi-bot: ## If you want to know how to resolve it, please read the guide in TiDB Dev Guide.

Details

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 ti-community-infra/tichi repository.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 98e9ae9d-e09b-4261-bea1-f75a90483292

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Signed-off-by: lhy1024 <19542290+lhy1024@users.noreply.github.com>
@ti-chi-bot

ti-chi-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from lhy1024. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

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

Details 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

Copy link
Copy Markdown
Member Author

Cherry-pick conflicts appear resolved; removing the do-not-merge/hold label.

@ti-chi-bot ti-chi-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 19, 2026
@lhy1024
lhy1024 requested review from bufferflies and rleungx August 19, 2026 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/cherry-pick-not-approved release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants