Skip to content

[connect/tidb] Split key ranges by TiKV region via FLIP-27 enumerator - #4508

Open
lifulong wants to merge 1 commit into
apache:masterfrom
lifulong:split_key_ranges_by_tike_region_to_avoid_auto_increament_skew
Open

[connect/tidb] Split key ranges by TiKV region via FLIP-27 enumerator#4508
lifulong wants to merge 1 commit into
apache:masterfrom
lifulong:split_key_ranges_by_tike_region_to_avoid_auto_increament_skew

Conversation

@lifulong

@lifulong lifulong commented Aug 17, 2026

Copy link
Copy Markdown

What is the purpose of this pull request?

TiDB CDC used to split a table by evenly partitioning the entire long handle space (Long.MIN_VALUE ~ Long.MAX_VALUE).
For clustered AUTO_INCREMENT primary keys, real handles only occupy a tiny slice of that space. With parallelism 8, almost all rows land on a single subtask, which then OOMs during KVClient.scan.

This change assigns each reader a contiguous chunk of real TiKV regions covering the table, so snapshot and incremental CDC scale with region count instead of the theoretical long domain.

Region topology is discovered once by a FLIP-27 enumerator. Each parallel SourceFunction.open() used to query PD independently; if region split/merge happened while tasks started minutes apart, assigned key ranges could overlap (duplicate CDC) or leave gaps (lost data). The enumerator snapshots region boundaries, assigns one contiguous KeyRange per reader, and restore reuses checkpointed splits instead of rediscovering PD.

Runtime region split/merge is still handled by CDCClient inside the assigned key range. That only remaps physical regions onto a frozen key-space ownership and does not re-partition work across readers.

Brief change log

  • Add a thin FLIP-27 TiKVSource (TiKVSourceEnumerator + TiKVSourceReader)
  • Enumerator queries PD once via RangeSplitter.splitRangeByRegion, clips/sorts/merges contiguous region chunks, and assigns one TiKVKeyRangeSplit per subtask
  • Reader scans snapshot then streams CDC only for its assigned key range; resolvedTs is stored on the split
  • Enumerator checkpoint stores unassigned splits; restore never rediscovers regions
  • Fail the job if region meta cannot be fetched (no silent fallback to task0-owns-all-table)
  • Table API uses SourceProvider; DataStream TiDBSource.builder().build() now returns TiKVSource (use env.fromSource)
  • Deprecate TiKVRichParallelSourceFunction / buildSourceFunction()
  • Keep the old handle-space helpers as deprecated APIs
  • Update DataStream docs (en/zh)

Verifying this change

  • Added/Updated unit tests in TableKeyRangeUtilsTest and TiKVKeyRangeSplitSerializerTest
  • Manually tested by our produce env job (region-based split)

Documentation

  • Does this pull request introduce a new feature? no (bugfix / correctness of parallel split)
  • If yes, how is the feature documented? docs / JavaDocs (DataStream example switched to fromSource)

Was generative AI tooling used to co-author this PR?
  • Yes (Cursor)

@lifulong lifulong changed the title Split snapshot key ranges by TiKV region to avoid AUTO_INCREMENT … [connect/tidb] Split key ranges by TiKV region via FLIP-27 enumerator Aug 17, 2026
@lifulong
lifulong force-pushed the split_key_ranges_by_tike_region_to_avoid_auto_increament_skew branch 2 times, most recently from 7002d48 to 07990aa Compare August 17, 2026 11:11
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Aug 17, 2026
@lifulong
lifulong force-pushed the split_key_ranges_by_tike_region_to_avoid_auto_increament_skew branch from 07990aa to 0632314 Compare August 18, 2026 07:04
@lifulong

Copy link
Copy Markdown
Author

@leonardBang @GOODBOY008 @lvyanquan can you help review this change?

@lifulong
lifulong force-pushed the split_key_ranges_by_tike_region_to_avoid_auto_increament_skew branch from 0632314 to 10d3206 Compare August 18, 2026 08:09
@lifulong
lifulong force-pushed the split_key_ranges_by_tike_region_to_avoid_auto_increament_skew branch from 10d3206 to f5895fa Compare August 18, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation tidb-cdc-connector

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants