Skip to content

dispatcher: prevent premature checkpoint advancement - #7031

Open
wk989898 wants to merge 7 commits into
pingcap:masterfrom
wk989898:checkpoint-fix
Open

wk989898 wants to merge 7 commits into
pingcap:masterfrom
wk989898:checkpoint-fix

Conversation

@wk989898

@wk989898 wk989898 commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

Issue Number: close #7030

What is changed and how it works?

  • Read the cached watermark after collecting the status batch. Reuse it only when its Seq covers both the current dispatcher generation and the statuses in the batch; otherwise, perform a read-only aggregation of dispatcher progress. Preserve the exclusion of Preparing and MergeReady dispatchers.
  • Register new dispatchers in the manager’s map and assign their Seq before enabling event delivery. Apply this ordering to both default and redo dispatchers.
  • Read resolvedTs before reading tableProgress in GetCheckpointTs(). Together with the writer’s ordering of registering DML before publishing resolved progress, this prevents an old empty progress list from being combined with a newer resolved timestamp.
  • Advance the dispatcher generation when activating a merged dispatcher, before committing event delivery and retiring its source dispatchers. This distinguishes scans that excluded the merged dispatcher from its subsequent Working status.

Check List

Tests

  • Unit test
  • Integration test
image

Questions

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

Release note

Fix an issue that could cause premature checkpoint advancement and data loss during dispatcher creation, migration, or merging.

Summary by CodeRabbit

  • Bug Fixes
    • Checkpoint timestamps remain consistent when pending events, new dispatchers, or merges overlap with progress updates.
    • Status watermarks account for dispatchers registered or merged while heartbeat information is collected.
    • Removed dispatchers are no longer restored during merge activation, and stopped dispatchers are reported accurately.
    • Dispatcher status and progress reporting better reflect registration, removal, and merge activity.
  • Tests
    • Added coverage for checkpoint consistency, dispatcher registration, status watermarks, and merge activity during status collection.

Signed-off-by: wk989898 <nhsmwk@gmail.com>
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The change snapshots resolved timestamps during checkpoint calculation, records dispatcher membership and progress bounds, and updates heartbeat watermark collection. Merge activation, rollback, and removal now coordinate with the write path. Tests cover checkpoint reads, registration, watermark scans, and merge lifecycle cases.

Changes: Checkpoint and Watermark Consistency

Layer / File(s) Summary
Checkpoint timestamp snapshot
downstreamadapter/dispatcher/basic_dispatcher.go, downstreamadapter/dispatcher/basic_dispatcher_info_test.go
GetCheckpointTs snapshots resolvedTs before reading table progress. Tests check the checkpoint when pending DML and a newer resolved timestamp arrive during the read.
Dispatcher registration and map generations
downstreamadapter/dispatchermanager/helper.go, downstreamadapter/dispatchermanager/dispatcher_manager.go, downstreamadapter/dispatchermanager/dispatcher_manager_redo.go, downstreamadapter/dispatchermanager/dispatcher_manager_test.go, downstreamadapter/dispatchermanager/helper_test.go
Default and redo dispatchers enter their maps and receive a sequence before event delivery registration. DispatcherMap records membership generations and pending progress bounds. Tests cover registration timing and status collection.
Heartbeat watermark collection
downstreamadapter/dispatchermanager/dispatcher_manager.go, downstreamadapter/dispatchermanager/dispatcher_manager_helper.go, downstreamadapter/dispatchermanager/dispatcher_watermark.go, downstreamadapter/dispatchermanager/dispatcher_manager_watermark_test.go
The manager obtains watermarks from dispatcher maps and combines cached values with pending bounds and scan results. Tests cover scan races, status publication, and aggregation in default and redo modes.
Merge activation and removal
downstreamadapter/dispatchermanager/task.go, downstreamadapter/dispatchermanager/dispatcher_manager_helper.go, downstreamadapter/dispatchermanager/dispatcher_manager_test.go, downstreamadapter/dispatchermanager/dispatcher_manager_watermark_test.go
Merge activation and rollback coordinate with the write path. Removal intent is set under the write-path lock. Tests cover removal, rollback, and pre-commit abort cases.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant DispatcherManager
  participant DispatcherMap
  participant EventCollector
  participant WatermarkScan
  DispatcherManager->>DispatcherMap: Insert dispatcher and record sequence and progress bounds
  DispatcherManager->>EventCollector: Register dispatcher
  DispatcherManager->>WatermarkScan: Begin scan and collect dispatcher progress
  WatermarkScan->>DispatcherMap: Finish scan and merge pending bounds
  DispatcherManager->>DispatcherMap: Read published status watermark
Loading

Suggested reviewers: hongyunyan

Merge Risk: Minimal · up to 82ff1

The change orders dispatcher registration, watermark bounds, and merge activation so the checkpoint cannot advance before row changes are available. No concrete defect remains in the reviewed changes, and the added tests cover the key race and ordering cases. The remaining risk is low and ordinary for a concurrency-sensitive change.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "dispatcher: prevent premature checkpoint advancement" matches the main objective of the PR. The changes reorder dispatcher registration, watermark aggregation, GetCheckpointTs reads, and me…
Linked Issues check Passed [#7030] requires that a consumer which observes checkpoint C can read all row changes with commitTs <= C, including after a whole-table move.

The PR changes the checkpoint paths that can advance too …
Out of Scope Changes check Passed All changed areas relate to the checkpoint and dispatcher lifecycle in [#7030] and the PR description:
- GetCheckpointTs read ordering and its failpoint.
- Status watermark caching, pending bounds, Se…
Description check Passed The description follows the repository template and covers the problem, the four changes, and the release note. It links issue #7030 with "close", so the Issue Number line is present. It names the uni…
Full details: Title check

Explanation

The title "dispatcher: prevent premature checkpoint advancement" matches the main objective of the PR. The changes reorder dispatcher registration, watermark aggregation, GetCheckpointTs reads, and merge activation so that checkpoints do not advance before row changes are delivered. The title is short, specific, and follows the repository's package-prefix style. It does not name the individual sub-changes, which is acceptable.

Full details: Linked Issues check

Explanation

[#7030] requires that a consumer which observes checkpoint C can read all row changes with commitTs <= C, including after a whole-table move.

The PR changes the checkpoint paths that can advance too early:

  • BasicDispatcher.GetCheckpointTs() reads resolvedTs before tableProgress. An old empty progress list cannot combine with a newer resolvedTs. TestCheckpointTsDoesNotCombineOldProgressAndNewResolved covers this case.
  • Default and redo dispatcher creation add the dispatcher to the map and assign Seq before event delivery starts.
  • doMerge advances the dispatcher generation before event delivery is committed and source dispatchers are retired. abortMerge now rechecks sources under writePathMu.
  • Status watermark publication now combines the cached periodic watermark with pending bounds recorded by DispatcherMap.Set and Activate. Bounds from dispatchers that are neither Preparing nor MergeReady lower the pending watermark. Scans fold in bounds that arrive during the scan. This keeps the watermark conservative for dispatchers added or activated after the last scan.
  • Provisional MergeReady and Preparing dispatchers are excluded from status reporting, which preserves the exclusion stated in the PR.
  • Tests in dispatcher_manager_test.go, dispatcher_manager_watermark_test.go, and dispatcher_watermark_test.go check pending DML holding the checkpoint, dispatcher registration before status reporting, and merge activation and abort behavior.

The mapping to [#7030] holds from the whole-PR summary. Uncertainty remains. No test run result is reported. No end-to-end ddl_with_random_move_table result is reported. I did not inspect the source to confirm the new watermark helpers and the DispatcherMap pending-watermark logic.

Full details: Out of Scope Changes check

Explanation

All changed areas relate to the checkpoint and dispatcher lifecycle in [#7030] and the PR description:

  • GetCheckpointTs read ordering and its failpoint.
  • Status watermark caching, pending bounds, Seq coverage, and scan folding in dispatcher_watermark.go and DispatcherMap. The later commit replaces O(N) status-time aggregation with bounds recorded at add and activate time. This addresses the performance concern raised in review on the same watermark path. It does not add unrelated behavior.
  • Dispatcher map registration and Seq ordering for default and redo dispatchers.
  • doMerge generation advance, activation, and abortMerge guards in task.go.
  • removeDispatcher locking under writePathMu, which shares the lock with merge activation and supports the removal ordering.
  • Failpoints and tests that exercise these paths.
  • The helper_test.go change, which fixes a duplicate-Create test to use a fully initialized dispatcher. It supports the map registration change.
    The release note matches the checkpoint fix. No unrelated change was identified in the whole-PR summary. Scope uncertainty is limited to the summary. I did not read the changed hunks directly.
Full details: Description check

Explanation

The description follows the repository template and covers the problem, the four changes, and the release note. It links issue #7030 with "close", so the Issue Number line is present. It names the unit and integration test categories, and it includes a screenshot. The PR states the ordering and watermark changes in enough detail for a reviewer to follow the intent. Two gaps remain. The performance and compatibility question is blank, even though the linked review comment raises an O(N) scan concern. The documentation question is also blank. The description does not name the specific tests added, although the objectives say it does not need to.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the checkpoint time,
While map and heartbeat bounds align.
New dispatchers join in their place,
Merge steps pause for a safer pace.
Pending rows then move along,
And watermarks keep time in song.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 385f9a07-6c5b-4d3e-94f4-664cf2829fdc
Commits

Reviewing files that changed from the base of the PR and between 3235b2c and 82ff16d.

Files selected for processing: 9

  • downstreamadapter/dispatchermanager/dispatcher_manager.go
  • downstreamadapter/dispatchermanager/dispatcher_manager_helper.go
  • downstreamadapter/dispatchermanager/dispatcher_manager_test.go
  • downstreamadapter/dispatchermanager/dispatcher_manager_watermark_test.go
  • downstreamadapter/dispatchermanager/dispatcher_watermark.go
  • downstreamadapter/dispatchermanager/dispatcher_watermark_test.go
  • downstreamadapter/dispatchermanager/helper.go
  • downstreamadapter/dispatchermanager/helper_test.go
  • downstreamadapter/dispatchermanager/task.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@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. needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. labels Dec 1, 2026
@ti-chi-bot

ti-chi-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown

In response to a cherrypick label: @wk989898 once the present PR merges, I will cherry-pick it on top of release-nextgen-202609 in a new PR and assign it to you.

@ti-chi-bot ti-chi-bot Bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Dec 1, 2026
@wk989898 wk989898 added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Dec 1, 2026
@ti-chi-bot

ti-chi-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown

In response to a cherrypick label: @wk989898 once the present PR merges, I will cherry-pick it on top of release-8.5 in a new PR and assign it to you.

Recompute periodic heartbeat watermarks when the dispatcher generation
changes during a scan so Working statuses cannot complete operators with
a stale watermark.

Advance merge generations without storing the target again. Serialize
removal intent with final activation checks and avoid removing a target
that is already being removed.

Add default and redo regressions for full scans and both removal phases.
@ti-chi-bot ti-chi-bot Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 1, 2026
Report Stopped and clean removed Preparing or MergeReady dispatchers
without including their provisional timestamps in node watermarks.

Hold the lifecycle lock across rollback source checks and registration,
and skip restoration after the write path is fenced.

Add default and redo regressions for early terminal cleanup, concurrent
source removal, and fencing during merge rollback.
Publish Initializing under writePathMu only after the write-path fence
and final source and target checks pass. Bootstrap treats Initializing
as the merge commit point, so pre-commit aborts must preserve MergeReady.

Add default and redo regressions for missing or removing sources and a
fenced write path at the final activation boundary.
@wk989898

wk989898 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

/test all

@wk989898

wk989898 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

/test all

@lidezhu

lidezhu commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

getStatusWatermark introduces a full O(N) dispatcher-map scan whenever the cached watermark does not cover the current generation. A single dispatcher creation or merge activation can advance Seq beyond the periodic cache and make the first corresponding status heartbeat scan every existing dispatcher. This is not acceptable for the intended large-dispatcher scale.
Please redesign this path so dispatcher scheduling and status publication can produce a safe generation-covered watermark without work proportional to the total dispatcher count. The periodic aggregation may remain O(N), but create/move/merge handling should be independent of N.

Record checkpoint and resolved bounds when adding or activating dispatchers,
and combine them with the periodic aggregate for constant-time status
watermark publication. Fold existing bounds into the cache before a scan and
merge concurrent additions before publishing it, avoiding generation retries.

Keep provisional merge timestamps excluded and preserve activation/removal
ordering. Retire conservative bounds through the next periodic refresh.

Validate with four-package race/failpoint tests, 20 repeated classic runs,
10 repeated NextGen runs, a watermark lookup benchmark, formatting and build.
Allocate generations and assign dispatcher sequences inside DispatcherMap,
before event delivery. Reject status batches that require an unallocated
generation and report an internal invariant error.

Serialize periodic scans through watermark publication and stopped-dispatcher
cleanup. Test that final progress remains cached after removal and is released
by the next scan without requiring another generation.

Validate with four-package race/failpoint tests, 20 repeated classic runs,
10 repeated NextGen runs, the watermark benchmark, formatting and build.
@ti-chi-bot ti-chi-bot Bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 4, 2026
@ti-chi-bot

ti-chi-bot Bot commented Dec 4, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: asddongmen

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 commented Dec 4, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-12-04 03:01:38.253626366 +0000 UTC m=+599002.245878929: ☑️ agreed by asddongmen.

@ti-chi-bot ti-chi-bot Bot added the approved label Dec 4, 2026
Signed-off-by: wk989898 <nhsmwk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Storage consumer skips 247 row changes after move_table with table splitting disabled

3 participants