Skip to content

fix(logservice): tolerate duplicate WAL records during replay - #27552

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
aptend:fix/wal-replay-late-duplicate
Aug 26, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
aptend:fix/wal-replay-late-duplicate

Conversation

@aptend

@aptend aptend commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27410

What this PR does / why we need it:

WAL replay uses PSN as physical order and DSN as logical order. A record may appear again at a later PSN, for example when an append is committed but times out and the caller retries it. If that record's complete DSN range has already been scheduled, the later physical copy contains no new logical work; the previous code treated it as an impossible ordering and panicked, preventing TN restart.

This PR:

  • ignores a late physical copy only when its complete DSN range is already scheduled;
  • keeps the first physical copy when the same DSN range is still pending;
  • rejects partial overlaps and conflicting ranges instead of hiding corruption;
  • removes skipped records from both the logical mapping and read cache so the DSN can be reused safely;
  • logs ignored duplicates as Wal-Replay-Duplicate-Entry and invalid overlaps as Wal-Replay-DSN-Overlap.

A genuinely missing next DSN is not discarded: the scheduling watermark cannot advance past it, so normal out-of-order records continue to be cached and replayed in DSN order. The behavior is therefore independent of replay read-batch boundaries without reading or spilling the complete WAL.

Regression tests cover same-batch and cross-batch duplicates, continuous replay after EOF, multi-entry DSN ranges, partial and pending range conflicts, and DSN reuse after a skip command.

@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

Choose a reason for hiding this comment

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

Deep-reviewed exact head 81d7817eeee1bab5faea3291f5a652429304a07d against the replay state machine, physical/logical ordering model, skip records, batch boundaries, continuous replay, and failure cleanup. One recovery-correctness blocker remains:

[P1] Reject overlaps against all pending ranges, not only the scheduled watermark or an identical start DSN. In readNextBatch, the new checks cover (a) a range overlapping dsnScheduled, and (b) a pending entry with the same start DSN. Two pending ranges with different starts are not compared. This makes correctness depend on whether scheduling occurs between the two reads.

Deterministic counterexample on this head, using the existing mock replayer:

PSN 1: DSN range [1,3]
PSN 2: DSN range [3,5]
readBatchSize = 30

Both records enter dsn2PSNMap before either is scheduled. Replay schedules [1,3], advances dsnScheduled to 3, then looks for start DSN 4; the remaining [3,5] entry is now unreachable under key 3. The exact result is err=nil, applied DSNs [1,2,3], pending map map[3:2]—DSNs 4 and 5 are silently omitted while recovery reports success. With readBatchSize=1, the existing scheduled-overlap check rejects the same logical corruption, so the outcome is explicitly batch-size dependent.

Please maintain/check a non-overlapping interval invariant for every pending record (including different-start partial and contained overlaps) before accepting it; exact retry duplicates may still be coalesced under the intended identity rule. Add same-batch and cross-batch tests for partial overlaps in both directions and containment, asserting fail-closed behavior and no successful incomplete replay. The check also needs to remain sub-quadratic for the million-record/18-GiB recovery shape in #27410.

Validation: the new PR tests pass as a package and under focused -race -count=5; the counterexample above fails before any test-only assumptions are introduced. Q1–Q3 audit found no new resource leak, double cleanup, wait cycle, or unbounded duplicate retention beyond this logical-range ownership hole.

@aptend

aptend commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the P1 blocker in 4f8c2301ae.

  • Replaced the pending start-DSN map with a single B-tree index keyed by start DSN. Pending ranges now maintain a non-overlapping closed-interval invariant; insertion checks only the predecessor and successor, so lookup/insert/delete remain O(log n). No second long-lived index is maintained.
  • Exact same-range retries still coalesce to the first PSN. Any different-start partial overlap, same-start conflict, or containment fails closed before the new record enters pending state.
  • Scheduling, skip removal, and EOF skip-command generation now all consume the same ordered index.

The new matrix covers both read batch sizes 1 and 30 for [1,3]/[3,5], the reverse direction, and containment in both directions. All 8 cells returned err=nil with incomplete replay on the reviewed head before the fix; they now return an overlap error with no entries applied. The exact-duplicate pending/scheduled controls and skip-then-reuse control still pass.

Local validation on the final diff:

  • owning-package build and vet: pass
  • owning-package tests: pass (10.868s)
  • 7 focused behavior tests under -race -count=100: pass
  • full owning package under -race -count=1: pass (11.499s)

@aptend
aptend requested a review from XuPeng-SH August 25, 2026 02:48

@jiangxinmeng1 jiangxinmeng1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed the updated head 4f8c230. The previous P1 blocker is resolved.

The new B-tree-based pending-range index correctly detects overlaps through predecessor/successor checks, covering
partial overlaps and containment in both directions with O(log n) operations. Exact duplicate ranges retain the
first physical copy, while scheduling and skip processing consistently remove records from both the pending index
and read cache. The WAL and skip-command formats remain unchanged.

I also checked the scheduling, duplicate, skip/reuse, EOF, and continuous-replay paths. No new correctness,
resource-lifecycle, wait-cycle, or unbounded-growth blockers were found.

Code review LGTM. The latest build, UT, coverage, and SCA checks were still running at the time of this review.

@aptend

aptend commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the remaining legacy-skip P1 in 28d6cef. Legacy commands now accept a pending DSN only when its current PSN is present in the command PSN set, preserving compatibility with historically permuted pairs while rejecting delayed duplicates after DSN reuse. Added a serialized legacy regression for readBatchSize=1 and 30; before the fix it reproduced applied DSNs [1,2], and after the fix it applies [1,2,3]. Validation passed: owning-package normal and full race, focused race count=100, controlled CGo build/vet, and make static-check (0 issues).

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Deep re-review of exact head e77a07ee7705405fd2c45ec08c30992a1562798a completed. The ordered B-tree now enforces disjoint pending DSN ranges in O(log n), exact duplicate ranges keep the first physical owner, and scheduled duplicates are retired from cache without changing logical order. Skip replay is now idempotent and identity-safe in both formats: v1 uses exact DSN/PSN pairs, while legacy v0 uses the preserved PSN set to tolerate historical pair permutation without deleting a later record that reused the DSN. Static serialized legacy fixtures cover compatibility and delayed duplicates across batch sizes. Non-sentinel scheduling/append errors terminate once instead of looping/logging, and Replay owns apply-channel closure on normal, error, and panic unwind paths. The original same/cross-batch, continuous replay, range overlap/containment, skip/reuse, EOF, and failure cases are all represented. I found no remaining scoped recovery-correctness, asymptotic-performance, lifecycle, or log-storm blocker; exact-head CI is green.

@mergify mergify Bot added the queued label Aug 26, 2026
@mergify

mergify Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-26 13:07 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-26 13:17 UTC · at 27c91479cff10ff6e2bd7ed8a4c4084f917aac0f

This pull request spent 10 minutes 12 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #27552 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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 size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TN cannot restart when WAL DSN disorder crosses replay read-batch boundaries

4 participants