Skip to content

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

Merged
ti-chi-bot[bot] merged 5 commits into
tikv:masterfrom
lhy1024:fix/preparing-range-size-cache
Aug 4, 2026
Merged

server/cluster: avoid repeated region size scans#11072
ti-chi-bot[bot] merged 5 commits into
tikv:masterfrom
lhy1024:fix/preparing-range-size-cache

Conversation

@lhy1024

@lhy1024 lhy1024 commented Jul 31, 2026

Copy link
Copy Markdown
Member

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.

Cache each key range size for one checkStores round so preparing stores share the same calculation. Move the range size lookup out of the placement rule processing loop.

Ref tikv#9574

Signed-off-by: lhy1024 <19542290+lhy1024@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has signed the dco. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 08f14df7-6ea9-4289-92b9-747b53c05073

📥 Commits

Reviewing files that changed from the base of the PR and between ccc20b6 and 8473529.

📒 Files selected for processing (1)
  • server/cluster/cluster.go

📝 Walkthrough

Walkthrough

The cluster now caches region sizes by key range for each checkStores round. Store readiness and placement-rule threshold calculations reuse cached values. Tests verify reuse across stores and rules, separate range caching, and isolation between calculation rounds.

Changes

Region-size threshold calculations

Layer / File(s) Summary
Per-round cache wiring
server/cluster/cluster.go, server/cluster/cluster_test.go
checkStores creates a range-keyed cache and passes it through store and threshold checks. Tests verify shared cache behavior and isolation between calculation rounds.
Cached threshold and rule evaluation
server/cluster/cluster.go, server/cluster/cluster_test.go
Threshold and weighted range calculations use cached region sizes. Placement-rule evaluation loads a range after the first matching rule and reuses its size for later matching rules.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change by stating that repeated region size scans are avoided.
Description check ✅ Passed The description covers the problem, issue reference, implementation, limitations, tests, related changes, and release note.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Pass the store snapshot collected by checkStores into each store state check instead of rebuilding the complete store slice for every preparing store.

Ref tikv#9574

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

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.33%. Comparing base (070828c) to head (8473529).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11072      +/-   ##
==========================================
+ Coverage   79.23%   79.33%   +0.10%     
==========================================
  Files         540      541       +1     
  Lines       76010    76675     +665     
==========================================
+ Hits        60226    60832     +606     
- Misses      11533    11560      +27     
- Partials     4251     4283      +32     
Flag Coverage Δ
unittests 79.33% <94.11%> (+0.10%) ⬆️

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

lhy1024 added 2 commits July 31, 2026 14:59
This reverts commit 1bacbb0.

Signed-off-by: lhy1024 <19542290+lhy1024@users.noreply.github.com>
Signed-off-by: lhy1024 <19542290+lhy1024@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Aug 3, 2026
Comment thread server/cluster/cluster.go Outdated
Comment thread server/cluster/cluster.go Outdated
Signed-off-by: lhy1024 <19542290+lhy1024@users.noreply.github.com>
@ti-chi-bot

ti-chi-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bufferflies, rleungx

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

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [bufferflies,rleungx]

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 and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 4, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

[LGTM Timeline notifier]

Timeline:

  • 2026-08-03 04:00:59.205592306 +0000 UTC m=+2414245.241687372: ☑️ agreed by rleungx.
  • 2026-08-04 02:19:34.70777377 +0000 UTC m=+2494560.743868826: ☑️ agreed by bufferflies.

@lhy1024

lhy1024 commented Aug 4, 2026

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@lhy1024

lhy1024 commented Aug 4, 2026

Copy link
Copy Markdown
Member Author

/retest

@ti-chi-bot
ti-chi-bot Bot merged commit ba23e72 into tikv:master Aug 4, 2026
41 of 43 checks passed
@lhy1024 lhy1024 added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Aug 18, 2026
@ti-chi-bot

Copy link
Copy Markdown
Member

In response to a cherrypick label: new pull request created to branch release-8.5: #11158.
But this PR has conflicts, please resolve them!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants