Skip to content

fix(test): wait for CDC takeover checkpoint visibility - #29376

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-cdc-takeover-ut
Sep 25, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-cdc-takeover-ut

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Test and CI

Which issue(s) this PR fixes:

Fixes #29375

What this PR does / why we need it:

TestCDCNoFullPublicTakeoverLifecycle must compare each replacement reader with the final durable checkpoint of the previous owner. Cross-CN catalog reads can briefly lag, and a guarded watermark write can finish after the previous owner's cancellation returns. Either race could make a valid takeover fail the test's strict first CollectChanges assertion.

For A→B and B→C, the test now confirms old callbacks and readers stopped, drains previously admitted updater writes with ForceFlush, reads the final owner generation and checkpoint through CN A (the updater's internal executor), and holds the new reader before owner claim until its CN sees that exact tuple. The original strict first-collection, generation, delayed-cleanup, and sink-delivery assertions remain. A deterministic updater test holds a guarded write after its owner check and verifies a later ForceFlush cannot complete until that write is durable.

The C admission hook no longer has an unreleasable extra wait. Failure cleanup releases C and joins its explicitly started detector scan with a bound. The scenario owns a fresh embedded fixture generation and resets the process-local updater only after the cluster closes; C starts only one new CN. Production behavior is unchanged except for the inert test hook argument and exported test reset entry point.

A later deep review reproduced a second false failure: B can legitimately advance its durable checkpoint while A's delayed cleanup is held, so a post-cleanup equality comparison to an earlier B sample is invalid. The test now waits for B to advance beyond that sample, then requires the same owner generation and no regression below the advanced checkpoint after A cleanup and at B→C handoff. B's and C's first CollectChanges boundaries remain strict equality checks.

Validation

  • Root-cause evidence: #29350 UT, #29345 UT.

  • Base/merge-base 426cf10b000b; prior head 14264bccf5389f5737739b1e2d3b19ca7babca82. Go 1.26.4, Linux/amd64, CPU libmo built in task worktree with make cgo; all Go tests use repository CGo wrapper and isolated TMPDIR.

  • TestCDCNoFullPublicTakeoverLifecycle -count=2: PASS, 197.187s total, both iterations passed in one process (121.31s, 75.14s).

  • Focused takeover -race -count=1: PASS, 134.852s.

  • pkg/cdc -count=1: PASS, 11.845s; pkg/cdc -race -count=1: PASS, 13.364s.

  • Guarded-progress regression -race -count=100: PASS, 3.442s.

  • pkg/tests/sqlintegration -count=1: PASS, 253.862s. Full package -race -count=1: PASS, 234.887s.

  • pkg/frontend -count=1: PASS, 97.941s.

  • Incremental golangci-lint v2.6.2 with repository CGo flags: 0 issues (includes govet); gofmt and git diff --check: clean.
    Follow-up validation for current head 5a54ee63d7 on the same base (Go 1.26.4, Linux/amd64, fresh CPU make cgo):

  • TestCDCNoFullPublicTakeoverLifecycle -count=1: PASS, 137.88s; it observes B advance past the earlier durable sample before releasing A.

  • Exact test -race -count=1: PASS, 121.12s test execution, 122.798s package.

  • Entire pkg/tests/sqlintegration -race -count=1: PASS, 313.845s.

  • Incremental go vet on pkg/cdc, pkg/frontend, pkg/tests/sqlintegration: PASS.

  • Incremental golangci-lint v2.6.2 (Go 1.26.4), --new-from-rev 426cf10b000b, same three packages: PASS, 0 issues; gofmt and git diff --check: PASS.

  • Independent whole-change and final evidence review: PASS, no unresolved blocker.

The previously reported equality failure was reproduced on the prior head with a controlled live-B-progress schedule, and the same schedule passed with monotonic comparison. CI for the new head is pending.

Risk and rollback

The added synchronization runs only in tests. Updater drains and visibility waits have 30-second bounds, and failure cleanup releases all admission barriers before fixture teardown. Revert the CDC test-fix commits to restore the previous test behavior.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review of e698b7c against declared base e914d98.

Recommendation: fix the P2 below before merging. This addresses cross-CN visibility lag, but does not yet establish that the sampled checkpoint is final. Submitting COMMENT because the authenticated reviewer is also the PR author.

Reviewed both changed files and traced reader shutdown, the cancellation hook, buffered watermark persistence, durable owner claiming, and the takeover assertions. This is a small test fix; no separate feature design is needed.

Area Result
Correctness / repeatability Strict checkpoint, generation, delivery and fresh-reader assertions remain intact, but an in-flight old-owner flush can invalidate the new sampled oracle; see inline P2.
Cleanup / unhappy paths Query results retain deferred Close; barrier releases retain idempotent deferred cleanup. The proxy deferred cancel correctly covers the early Fatal path. Reader completion does not imply updater completion.
Bounds / performance No production path changes or extra cluster fixtures. The new visibility poll adds queries at 200 ms intervals with a 30 s polling timeout; it is not a fixed 30 s delay. Its SQL still uses the existing outer test context.
Engineering / compatibility The executor-parameter helper is small and appropriate; no API, catalog, configuration or compatibility changes, and no overengineering.

Evidence reused from exact-head CI run 36076276702: Linux UT, SCA and CI Required passed. The UT artifact records TestCDCNoFullPublicTakeoverLifecycle PASS in 52.76 s and the sqlintegration package PASS in 199.46 s; the embedded package build uses -race. BVT was skipped for the test-only change. I did not rerun the package locally or claim a runtime reproduction of the remaining schedule: the finding is established by the traced ordering, and the green run does not exercise a controlled delayed flush.

Minimal next step: establish a test-only updater completion barrier after actual producer termination and before taking the final checkpoint sample, while retaining the delayed old-runner cleanup and strict equality assertions. Add a deterministic delayed-flush schedule to verify this boundary; additional broad retries or longer waits do not close it.

Comment thread pkg/tests/sqlintegration/ddl_test.go Resolved

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review of head 47571c7e847064742193bbed30a793edfd54d7f2 against declared base 47ec0605c1deaeec3eda1a7ffcba2cb79327fa7e. I am submitting COMMENT because this account is the PR author.

Recommendation: fix the P2 cleanup gap below before merging. The A-to-B checkpoint synchronization added by this PR is sound for the reported flake; the finding concerns a pre-existing failure path in the same lifecycle test.

[P2] Make C's admission barrier releasable when B cancellation fails or times out (pkg/tests/sqlintegration/ddl_test.go:798-805, pre-existing in the base). Once freshEntered is signaled, the C admission hook waits on <-bCancelDone before it can read freshRelease. If B's cancel completion never arrives, the main test goroutine reaches its 240-second context deadline and calls t.Fatal at lines 1183-1190. Deferred releaseFresh() cannot free C's hook because it is blocked on the earlier channel. RunTableDetectorScanForTest uses context.Background() for the callback, so its scan goroutine and detector's handling state remain stuck beyond the test failure; fixture teardown can then wait on work that the test cannot release. Give this first wait an abort/release path on test failure and join the scan. This is already present in the base, but the test's cleanup comment says every phase barrier is released on assertion failure, which is not true for C.

The changed A-to-B path closes its relevant races: the cancel hook now requires callback and reader drains; ForceFlush is a FIFO updater queue/persistence barrier after those producers stop; A samples its own catalog; B is held before owner claim until it observes the same generation and checkpoint. The exact first CollectChanges boundary, newer owner generation, post-takeover delivery, and delayed A cleanup assertions remain intact. The B visibility query and flush have 30-second contexts, and the poll stays on the test goroutine. No production CDC behavior, public SQL contract, schema, or additional fixture was added.

Evidence: current-head focused -count=3 PASS in 127.079s (same-process repetition), prior current-head focused -count=1 PASS in 47.198s, and full pkg/tests/sqlintegration -count=1 PASS in 111.230s. Focused -race -count=1 passed in 51.903s on the same CDC patch immediately before the unrelated base rebase. Current-head vet/lint and diff checks passed. Exact-head CI preflight and shared build are green; Linux UT, SCA, coverage, and two BVT jobs were still running at review time. The added test-only flush is one bounded updater operation; three repeated runs averaged about 42 seconds, with no evidence of a material test-cost regression. No product hot-path work was added.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up review of fa9d1e6ffe93b9c7ebf07056ac3b0abe60ced429 (COMMENT because I am the PR author): the P2 failure-path wait reported in my prior review is fixed. C admission now has one test-owned release channel; the main goroutine still observes B cancellation and samples its checkpoint before releasing C on success. On an assertion failure, the deferred release can reach that wait, and the explicitly started C scan is joined with a 30-second cleanup bound. The strict CDC takeover oracles remain unchanged.

The repeated-run investigation also found that this scenario left extra CNs in the shared fixture, and a closed fixture left the process-local watermark updater bound to the old CN executor. The scenario now restarts and closes its fixture generation, resets the updater after shutdown, and starts only one C service. No production execution path calls the new test-only reset helper.

Validation on this exact code: focused test -count=2 PASS (93.787s), focused -race -count=1 PASS (57.065s), full pkg/tests/sqlintegration PASS (134.622s), full pkg/cdc PASS (12.199s), incremental vet PASS, incremental lint 0 issues, format/diff checks clean. I found no remaining code blocker in the changed closure. Exact-head CI has only started, and this branch is currently behind main, so this comment does not claim CI completion or merge readiness.

@matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/S Denotes a PR that changes [10,99] lines labels Sep 25, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review of 14264bccf5389f5737739b1e2d3b19ca7babca82 against base/merge-base 426cf10b000b6a828e8c69f1af26082f26036ed3.

Recommendation: request changes for one reproduced P2 test flake. Submitting COMMENT because the authenticated account is also the PR author. This finding was missed in my earlier review; it is pre-existing in the base and remains in the lifecycle test this PR is stabilizing.

[P2] Allow the live B owner to advance after its checkpoint is sampled — ddl_test.go:1158–1161.

After checkpointB is sampled, B remains active while the test releases and observes A's cancellation. B can legitimately flush W1 > the sampled W0 before the first post-cleanup read. Empty collection rounds also advance watermarks (DataProcessor.processNoMoreData), so no extra source writes are needed. The predicate got == checkpointB then waits 30 seconds for a durable value that will not return, even though A preserved B's generation and progress correctly. This is another timing-dependent false failure of the same integration test.

I reproduced it using a review-only Go overlay on this exact head, with no production changes and no fixed sleep. Immediately before releaseCancel(), the overlay only waits until readWatermark() observes the same generationAfterB and got.GT(&checkpointB). Observed witness:

owner=1790323565922280
sampled=1790323573422963008-1
advanced=1790323576421257345-1

The original post-cleanup require.Eventually then failed with Condition never satisfied; the selected test terminated normally with FAIL in 77.55s. Failure cleanup completed rather than hanging. With the identical schedule and only this comparator changed to got.GE(&checkpointB), the entire test passed in 56.29s, including C takeover and the unchanged strict first-collection assertions.

Use the actual delayed-cleanup invariant here: the row exists, the owner generation is unchanged, and got.GE(&checkpointB). Retain the strict equality checks for B's and C's first CollectChanges; those compare frozen admission boundaries and serve a different contract. Exercise the live-B-progress schedule in the retained regression coverage.

The rest of the four-file change map is sound:

Closure Result
A→B / B→C sampling and persistence Stopped producers → completed updater queue barrier → authoritative final tuple → receiving CN visibility closes the previously reported sampling races. The first collection assertions remain strict.
Failure cleanup and fixture generations C's releasable admission precedes its bounded scan join. Gates release before SQL cleanup. The shared fixture closes before updater reset; same-process repetition and full-package race cover reuse.
Ownership / waits / growth Query results and test hooks have cleanup owners; added channels and scan concurrency are bounded. New flush and visibility waits are bounded. The counterexample run also validates the assertion-failure cleanup path.
Engineering / performance / compatibility Small shared helpers and existing synchronization seams; one unnecessary CN removed. Fixture restart has a concrete topology/executor-generation need. No production hot-path, SQL contract, wire, schema, or platform behavior change. Ordinary synchronization fix; separate feature design gate is exempt.

Evidence reused and verified for this exact head: CDC/frontend/SQL integration normal packages, CDC and SQL integration package race, takeover twice and focused race, guarded-progress race ×100, incremental lint (0 issues), and clean diff/status. These green unforced runs do not cover the explicit counterexample above. The new checks were only the controlled schedule and its comparator control; no broad suite rerun. Current-head CI SCA and coverage passed; Linux UT and BVT jobs were still running at inspection. CI completion was not awaited.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review of exact head 5a54ee63d71425e72a4bcc3e70983006cceeb9bc against declared main merge base 426cf10b000b6a828e8c69f1af26082f26036ed3. I reviewed all four changed files and traced the CDC reader/callback shutdown, updater queue and persistence barrier, cross-CN visibility, A→B and B→C admissions, fixture reset, and failure cleanup. Recommendation: no remaining blocker on this head. I am submitting COMMENT because the authenticated account is also the PR author.

The original flaky comparison is closed at its owner: after A's producers stop, ForceFlush drains admitted writes, A samples its authoritative tuple, and B waits to see that exact generation and checkpoint before claiming. The B→C handoff applies the same sequence after B's cancellation. A previously reproduced second failure was the equality check against an earlier sample while B was still live. This head waits for actual later B progress and requires the durable value to remain at least that far after A's delayed cleanup. The first CollectChanges checks at both replacement admissions remain strict equality, so the relaxed live-owner comparison does not weaken the recovery oracle.

Unhappy paths and reuse: C's admission has one releasable barrier; deferred failure cleanup releases all phase gates before SQL teardown and joins the explicitly started C scan with a bound. The fixture closes before the process-local updater is reset, and same-process repetition exercises the next generation. Reader/callback drains and updater waits are bounded; the added state is test-only, with no new production queue, retry, or retained resource. The extra B-progress poll uses existing heartbeat progress and a 30-second bound. This change does add a wait and catalog reads to an already expensive three-CN integration test; it adds no production hot-path work or extra CN.

Evidence reused for this exact code and base: focused takeover normal PASS (137.88s) and race PASS (121.12s), full pkg/tests/sqlintegration -race -count=1 PASS (313.845s), incremental go vet PASS for pkg/cdc, pkg/frontend, and pkg/tests/sqlintegration, golangci-lint 0 issues, clean format/diff, and a clean review worktree. The previous head's pkg/cdc normal/race, pkg/frontend, repeated takeover, guarded-write race ×100, and full SQL integration results remain semantically valid for the unchanged closures. The prior-head controlled live-B-progress schedule failed the old equality check and passed with the monotonic comparator; this head incorporates that comparator plus a positive progress witness. Current-head CI UT/SCA/BVT were still in progress at review time and are not counted as passed. This is an ordinary test synchronization fix, so the feature-design gate does not apply. No independent SQL BVT is needed for a test-only change with no public behavior change.

@XuPeng-SH
XuPeng-SH merged commit ce7f84d into matrixorigin:main Sep 25, 2026
6 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/fix-cdc-takeover-ut branch September 25, 2026 09:14

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TestCDCNoFullPublicTakeoverLifecycle flakes on stale cross-CN checkpoint read

2 participants