Skip to content

Conversation

hongyunyan
Copy link
Collaborator

@hongyunyan hongyunyan commented Jul 8, 2025

What problem does this PR solve?

Issue Number: close #12221

What is changed and how it works?

use LoadRegionsInKeyRange instead of ListRegionIDsInKeyRange to speed up the region scanning performance

Check List

Tests

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

We make a tidb cluster with nearly 5w regions(region size is 5MB)
After optimization, the time cost of scanning regions reduce to 450ms from 10s.

  • No code

Questions

Will it cause performance regression or break compatibility?
Do you need to update user documentation, design documentation or monitoring documentation?

Release note

Please refer to [Release Notes Language Style Guide](https://pingcap.github.io/tidb-dev-guide/contribute-to-tidb/release-notes-style-guide.html) to write a quality release note.

If you don't think this PR needs a release note then fill it with `None`.

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-triage-completed release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 8, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @hongyunyan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily focuses on refactoring the RegionCache interface and its mock implementation within the keyspan package. The goal is to align the mock interface more closely with the actual tikv.RegionCache behavior, allowing for direct retrieval of tikv.Region objects. This change simplifies the logic in the regionCountSplitter by removing redundant region lookups, ultimately improving code clarity and efficiency in how key spans are managed and split based on region information.

Highlights

  • RegionCache Interface Update: The RegionCache interface in cdc/scheduler/internal/v3/keyspan/mock.go has been refactored. The ListRegionIDsInKeyRange and LocateRegionByID methods were removed, and a new LoadRegionsInKeyRange method was introduced. This new method now directly returns a slice of *tikv.Region objects, simplifying subsequent data access.
  • Mock Implementation Refinement: The mockCache implementation in mock.go was updated to conform to the new RegionCache interface. It now constructs tikv.Region objects, utilizing the unsafe package to populate the unexported meta field for testing purposes.
  • Simplified Region Splitting Logic: The splitter_region_count.go file was updated to leverage the new RegionCache interface. This change eliminates the need for separate LocateRegionByID calls, as the tikv.Region objects are now directly available, leading to more concise and efficient code for determining span boundaries.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@hongyunyan hongyunyan changed the title update scheduler: Speed ​​up region scanning before table splitting Jul 8, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the keyspan splitter to reduce RPCs by using LoadRegionsInKeyRange instead of fetching region information one by one. This is a solid performance improvement.

}
return false

// meta.id is not exported, so we use unsafe to access it more easier for test.

Choose a reason for hiding this comment

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

medium

The comment on this line is inaccurate. It's the region.meta field that is unexported, not meta.id. metapb.Region.Id is an exported field.

Suggested change
// meta.id is not exported, so we use unsafe to access it more easier for test.
// region.meta is not exported, so we use unsafe to set it for testing.

Comment on lines 84 to 85
zap.Any("startKey", startKey),
zap.Any("endKey", endKey))

Choose a reason for hiding this comment

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

medium

For consistency and better readability, it's preferable to continue using hex encoding for the keys in the logs.

Suggested change
zap.Any("startKey", startKey),
zap.Any("endKey", endKey))
zap.Stringer("startKey", tablepb.Key(startKey)),
zap.Stringer("endKey", tablepb.Key(endKey)))

Copy link

codecov bot commented Jul 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.0802%. Comparing base (8f20364) to head (d4b13ea).
Report is 13 commits behind head on master.

Additional details and impacted files
Components Coverage Δ
cdc 59.3246% <100.0000%> (+0.0430%) ⬆️
dm 50.1179% <ø> (-0.0020%) ⬇️
engine 53.2336% <ø> (+0.0112%) ⬆️
Flag Coverage Δ
unit 55.0802% <100.0000%> (+0.0050%) ⬆️

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

@@               Coverage Diff                @@
##             master     #12222        +/-   ##
================================================
+ Coverage   55.0752%   55.0802%   +0.0050%     
================================================
  Files          1030       1030                
  Lines        143235     143545       +310     
================================================
+ Hits          78887      79065       +178     
- Misses        58553      58667       +114     
- Partials       5795       5813        +18     
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 8, 2025
Copy link
Contributor

ti-chi-bot bot commented Jul 8, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: asddongmen, lidezhu

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 and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 8, 2025
Copy link
Contributor

ti-chi-bot bot commented Jul 8, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-07-08 03:26:44.769249448 +0000 UTC m=+1970257.492428428: ☑️ agreed by lidezhu.
  • 2025-07-08 03:46:20.279103875 +0000 UTC m=+1971433.002282857: ☑️ agreed by asddongmen.

@hongyunyan
Copy link
Collaborator Author

/retest

@ti-chi-bot ti-chi-bot bot merged commit 4b2c47d into master Jul 8, 2025
28 checks passed
@ti-chi-bot ti-chi-bot bot deleted the scanRegions branch July 8, 2025 07:28
@hongyunyan
Copy link
Collaborator Author

/cherry-pick release-7.5

@ti-chi-bot
Copy link
Member

@hongyunyan: new pull request created to branch release-7.5: #12223.

In response to this:

/cherry-pick release-7.5

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.

@hongyunyan
Copy link
Collaborator Author

/cherry-pick release-8.1

@hongyunyan
Copy link
Collaborator Author

/cherry-pick release-8.5

@ti-chi-bot
Copy link
Member

@hongyunyan: new pull request created to branch release-8.1: #12224.

In response to this:

/cherry-pick release-8.1

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.

@ti-chi-bot
Copy link
Member

@hongyunyan: new pull request created to branch release-8.5: #12225.

In response to this:

/cherry-pick release-8.5

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance the speed of scan regions before split the big table
4 participants