Skip to content

br: set scan region limiter (#66822) - #67142

Merged
ti-chi-bot[bot] merged 5 commits into
pingcap:release-8.5from
ti-chi-bot:cherry-pick-66822-to-release-8.5
Jun 3, 2026
Merged

ti-chi-bot[bot] merged 5 commits into
pingcap:release-8.5from
ti-chi-bot:cherry-pick-66822-to-release-8.5

Conversation

@ti-chi-bot

@ti-chi-bot ti-chi-bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

This is an automated cherry-pick of #66822

What problem does this PR solve?

Issue Number: close #66821 ref #67623

Problem Summary:
When there are many scan region requests sent to PD, PD will be stuck.

What changed and how does it work?

set region scan limiter to avoid too many scan region requests sent to PD at the same time.

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.

Introduce a new parameter `--region-scan-concurrency` to limit the number of scan region requests sent to PD at the same time.

Summary by CodeRabbit

  • New Features

    • Added restore option --region-scan-concurrency (default: 256) to limit concurrent PD region-scan requests during restore; applied to snapshot and log restore flows.
  • Chores

    • Added defaults and hard limits for worker token channel sizes with warnings when values are clamped.
  • Tests

    • Added flow-control tests validating the region-scan concurrency behavior under concurrent restore workloads.

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot ti-chi-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. ok-to-test Indicates a PR is ready to be tested. 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 Mar 19, 2026
@ti-chi-bot

Copy link
Copy Markdown
Member Author

@Leavrth 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 Mar 19, 2026

Copy link
Copy Markdown

@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 Mar 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a configurable region-scan concurrency limiter wired through restore clients and snap importer; enforces the limit with token-based flow control in snap importer and extends tests/scripts to validate and accommodate related behavior and test harness changes.

Changes

Region Scan Concurrency & Tests

Layer / File(s) Summary
Snap client test shard update
br/pkg/restore/snap_client/BUILD.bazel
snap_client_test Bazel target shard_count changed from 44 to 45.
Flow-control test imports
br/pkg/restore/snap_client/import_test.go
Add sync and sync/atomic imports required by the flow-control test helper.
Flow-control split client & concurrency test
br/pkg/restore/snap_client/import_test.go
Add flowControlSplitClient wrapper that enforces a max in-flight ScanRegions counter and TestSnapImporterPDScanRequestFlowControl that runs 200 concurrent PaginateScanRegionForTest calls to validate the PD scan concurrency bound.

GCS credential & endpoint script updates

Layer / File(s) Summary
Credential JSON fields in tests
br/compatibility/credentials/application_default_credentials.json, br/tests/br_gcs/run.sh, br/tests/docker_compatible_gcs/_run.sh
Add project_id and private_key_id fields to generated service-account credential JSON used by tests/scripts.
GCS endpoint simplification & legacy flow
br/tests/br_gcs/run.sh, br/tests/docker_compatible_gcs/_run.sh, br/tests/docker_compatible_gcs/prepare.sh
Change --gcs.endpoint/endpoint query parameter to use base host:port (drop /storage/v1/) and add an optional legacy v4.0.8 backup+restore verification block gated by TEST_LEGACY_BR_GCS.

Sequence Diagram(s)

sequenceDiagram
    participant Test as TestHarness
    participant Importer as SnapFileImporter
    participant PDMock as flowControlSplitClient

    Test->>Importer: start 200 concurrent PaginateScanRegionForTest
    Importer->>PDMock: ScanRegions requests (concurrent)
    PDMock-->>Importer: region info (enforcing maxFlight)
    Importer-->>Test: return results
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • pingcap/tidb#66822: Implements identical region-scan concurrency limiter feature in parallel—both add regionScanConcurrency wiring and snap importer throttling.

Suggested reviewers

  • RidRisR
  • YuJuncen
  • 0xPoe

Poem

🐰 I counted tokens, one by one,
PD's calm when scans are done,
Tests raced fast, but never spilled,
Tokens held till all were filled,
Restore hops home — hooray, well-run!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.65% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Out of Scope Changes check ❓ Inconclusive Changes to GCS test scripts and credentials files appear unrelated to the region scan limiter feature, suggesting potential scope drift or test infrastructure updates. Clarify whether GCS credential and test changes are intentional scope additions or unrelated modifications that should be reverted.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a scan region limiter for BR to control concurrent region scan requests to PD.
Description check ✅ Passed The description includes issue numbers, problem summary, implementation details, test coverage (unit and manual tests), and a release note, satisfying all required template sections.
Linked Issues check ✅ Passed The PR fully implements the objective from #66821: introduces a region scan limiter via the --region-scan-concurrency parameter to prevent excessive concurrent scan-region requests to PD.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
br/pkg/restore/snap_client/export_test.go (1)

164-177: Delegate this test helper to the real constructor.

This is now a second manual initializer for SnapFileImporterOptions. Reusing NewSnapFileImporterOptions(..., 0, nil, nil) here would keep future option defaults in sync and avoid another drift point when the struct grows.

♻️ Suggested cleanup
func NewSnapFileImporterOptionsForTest(
	splitClient split.SplitClient,
	importClient importclient.ImporterClient,
	tikvStores []*metapb.Store,
	rewriteMode RewriteMode,
	concurrencyPerStore uint,
) *SnapFileImporterOptions {
-	return &SnapFileImporterOptions{
-		metaClient:          splitClient,
-		importClient:        importClient,
-		tikvStores:          tikvStores,
-		rewriteMode:         rewriteMode,
-		concurrencyPerStore: concurrencyPerStore,
-	}
+	return NewSnapFileImporterOptions(
+		nil,
+		splitClient,
+		importClient,
+		nil,
+		rewriteMode,
+		tikvStores,
+		concurrencyPerStore,
+		0,
+		nil,
+		nil,
+	)
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@br/pkg/restore/snap_client/export_test.go` around lines 164 - 177, The
NewSnapFileImporterOptionsForTest function duplicates the
SnapFileImporterOptions initializer; replace the manual struct literal with a
call to the canonical constructor NewSnapFileImporterOptions and pass the
test-specific values plus the defaults (e.g. 0, nil, nil) so defaults stay in
sync with the real constructor; update NewSnapFileImporterOptionsForTest to
delegate to NewSnapFileImporterOptions(splitClient, importClient, tikvStores,
rewriteMode, 0, nil, nil) and remove the inline struct literal.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@br/pkg/restore/snap_client/import_test.go`:
- Around line 224-233: The test currently calls require.LessOrEqual and
require.NoError from a worker goroutine inside
flowControlSplitClient.ScanRegions (and the other worker at lines ~264-269);
move those assertions out of spawned goroutines by returning error/status values
to the caller via channels (e.g. an error channel or result struct channel)
instead of invoking require.* in the goroutine, and perform
require.LessOrEqual/require.NoError in the main test goroutine after receiving
from the channel; specifically modify ScanRegions and the other worker closure
to send any violation or error back, then assert on that received value in the
test harness.

---

Nitpick comments:
In `@br/pkg/restore/snap_client/export_test.go`:
- Around line 164-177: The NewSnapFileImporterOptionsForTest function duplicates
the SnapFileImporterOptions initializer; replace the manual struct literal with
a call to the canonical constructor NewSnapFileImporterOptions and pass the
test-specific values plus the defaults (e.g. 0, nil, nil) so defaults stay in
sync with the real constructor; update NewSnapFileImporterOptionsForTest to
delegate to NewSnapFileImporterOptions(splitClient, importClient, tikvStores,
rewriteMode, 0, nil, nil) and remove the inline struct literal.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e6041ffc-f142-4683-9927-04a017abb594

📥 Commits

Reviewing files that changed from the base of the PR and between 99a2ff1 and a446a96.

📒 Files selected for processing (12)
  • br/pkg/restore/log_client/client.go
  • br/pkg/restore/snap_client/BUILD.bazel
  • br/pkg/restore/snap_client/client.go
  • br/pkg/restore/snap_client/export_test.go
  • br/pkg/restore/snap_client/import.go
  • br/pkg/restore/snap_client/import_test.go
  • br/pkg/task/common_test.go
  • br/pkg/task/config_test.go
  • br/pkg/task/restore.go
  • br/pkg/task/stream.go
  • br/pkg/utils/worker.go
  • br/tests/br_pitr_table_filter/run.sh

Comment thread br/pkg/task/restore.go Resolved
@codecov

codecov Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.23077% with 16 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release-8.5@99a2ff1). Learn more about missing BASE report.

Additional details and impacted files
@@               Coverage Diff                @@
##             release-8.5     #67142   +/-   ##
================================================
  Coverage               ?   55.7259%           
================================================
  Files                  ?       1824           
  Lines                  ?     657879           
  Branches               ?          0           
================================================
  Hits                   ?     366609           
  Misses                 ?     264201           
  Partials               ?      27069           
Flag Coverage Δ
integration 39.3178% <65.3846%> (?)
unit 65.2220% <63.4615%> (?)

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

Components Coverage Δ
dumpling 55.3108% <0.0000%> (?)
parser ∅ <0.0000%> (?)
br 62.9820% <0.0000%> (?)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. cherry-pick-approved Cherry pick PR approved by release team. and removed do-not-merge/cherry-pick-not-approved labels Jun 1, 2026
@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jun 2, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jun 2, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-06-01 07:24:40.680191206 +0000 UTC m=+167181.750508596: ☑️ agreed by YuJuncen.
  • 2026-06-02 06:23:05.151173308 +0000 UTC m=+249886.221490688: ☑️ agreed by RidRisR.

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@Leavrth

Leavrth commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

/unhold

@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 2, 2026
@Leavrth

Leavrth commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

/retest

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot Bot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 2, 2026
@ti-chi-bot ti-chi-bot Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed approved labels Jun 2, 2026
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot

ti-chi-bot Bot commented Jun 2, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RidRisR, YuJuncen

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:

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 Jun 2, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@br/tests/br_gcs/run.sh`:
- Line 111: The restore command invocation contains an unnecessary trailing '?'
in the storage URL ("gcs://$BUCKET/$DB?") which should be removed to avoid
confusing URL parsing; update the run_br call that executes "run_br restore full
-s \"gcs://$BUCKET/$DB?\" --pd $PD_ADDR --gcs.endpoint=...
--check-requirements=false" to use "gcs://$BUCKET/$DB" (no '?') while keeping
the --gcs.endpoint flag unchanged so the endpoint is still provided via
--gcs.endpoint.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d7073377-21dc-49c3-87d9-31a8dd6fd795

📥 Commits

Reviewing files that changed from the base of the PR and between 79b7d77 and c9e6e76.

📒 Files selected for processing (4)
  • br/compatibility/credentials/application_default_credentials.json
  • br/tests/br_gcs/run.sh
  • br/tests/docker_compatible_gcs/_run.sh
  • br/tests/docker_compatible_gcs/prepare.sh
✅ Files skipped from review due to trivial changes (1)
  • br/tests/docker_compatible_gcs/prepare.sh

Comment thread br/tests/br_gcs/run.sh Resolved
@Leavrth

Leavrth commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

/retest

2 similar comments
@Leavrth

Leavrth commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

/retest

@Leavrth

Leavrth commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

/retest

@ti-chi-bot
ti-chi-bot Bot merged commit 0f820cb into pingcap:release-8.5 Jun 3, 2026
30 checks passed
@ti-chi-bot
ti-chi-bot Bot deleted the cherry-pick-66822-to-release-8.5 branch June 3, 2026 05:21
@Leavrth Leavrth mentioned this pull request Sep 28, 2026
1 of 13 tasks
GMHDBJD added a commit to GMHDBJD/tidb that referenced this pull request Oct 15, 2026
Backport only br/tests/br_gcs/run.sh from 0f820cb (pingcap#67142). Include the missing fake credential fields, current TiKV endpoint format, and upstream opt-in legacy BR coverage. Do not include the source commit production changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved cherry-pick-approved Cherry pick PR approved by release team. lgtm ok-to-test Indicates a PR is ready to be tested. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 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.

4 participants