Skip to content

[fix][ml] Fix cursor reset wrongly persisting pre-reset ACKs that cause messages to be silently skipped after recovery - #26973

Merged
lhotari merged 8 commits into
apache:masterfrom
void-ptr974:fix/ml-reset-ack-state-persistence
Nov 25, 2026
Merged

lhotari merged 8 commits into
apache:masterfrom
void-ptr974:fix/ml-reset-ack-state-persistence

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

A durable cursor reset should recover with the ACK state supplied by the seek. The reset write can pair the new position with ACKs from before the seek, causing recovery to skip messages that should be replayed.

Modifications

  • Write the reset's ACK snapshot to BookKeeper and the Metadata Store fallback. Clear the old individual ACK ranges and persist the batch ACK bitmap supplied by the reset, subject to the existing persistence limits. Apply the same ACK state in memory after the reset write succeeds.
  • Keep queued ordinary ACKs out of the recovery checkpoint until submission. Use the same captured checkpoint for the first record of a replacement cursor ledger and its Metadata Store pointer.

Follow-ups

Verifying this change

  • Confirm CI passes on this revision.
  • Tests inspect the persisted BookKeeper and Metadata Store records before graceful close and verify the replacement ledger's seed against its Metadata Store checkpoint.
  • The full managed-ledger suite, three targeted broker tests, and quickCheck passed locally. One configuration-dependent case skipped in the full run passed when rerun with common-pool parallelism set to 1.
  • In a 15-case comparison on master (38b4615c72b), five cases went from failing to passing and nine continued to pass. The failed forward-reset rebuild case reproduced the same checkpoint error in both runs.

Does this pull request potentially affect one of the following parts?

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

…ring cursor reset

Persist the reset target ACK snapshot with the new cursor position in
BookKeeper and the Metadata Store fallback. Keep live ACK state unchanged
until persistence succeeds, and checkpoint live state when rebuilding a
cursor ledger after a failed reset.

Cover recovery, persistence failures, fallback, rollover, and batch
persistence configuration boundaries with deterministic regression tests.

Assisted-by: OpenAI Codex
A concurrent close can checkpoint the reset target with stale live ACKs
between the reset append and its completion callback. Wait asynchronously
for reset state updates to finish before entering Closing and persisting
the cursor. Resume close on reset failure as well.

Start close on the ledger executor to preserve the pending-operation and
cursor lock order. Cover the race, recovery, backward reset, batch ACKs,
metadata fallback, queued reads, and callback reentrancy.

Assisted-by: OpenAI Codex
Keep reset persistence state operation-local, publish successful reset checkpoints atomically, and preserve concurrent compaction cursor properties.

Assisted-by: Codex/GPT-5
@void-ptr974 void-ptr974 changed the title [fix][ml] Fix stale individual and batch ACK state being persisted during cursor reset [fix][ml] Fix cursor reset wrongly persisting pre-reset ACKs that cause messages to be silently skipped after recovery Oct 18, 2026
@codelipenghui codelipenghui self-assigned this Oct 21, 2026
@codelipenghui codelipenghui added this to the 5.1.0 milestone Oct 21, 2026
@codelipenghui codelipenghui removed their assignment Oct 23, 2026
Preserve reset queue ordering, reset-owned ACK snapshots and cursor close ordering.

Assisted-by: Codex (GPT-6)
Verify both close persistence backends preserve the reset position and batch bitmap when ordinary ACKs surround a queued reset.

Assisted-by: Codex (GPT-6)

@Denovo1998 Denovo1998 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.

The latest update addresses my previous concern about #26305. The reset ordering and persistence paths look good to me now, and the additional regression coverage is useful.

LGTM, pending CI validation.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tracking this down and for the thorough deterministic tests around reset, close and rebuild.

The core fix looks right to me. A successful reset now writes its record with the ACK state that belongs to the reset, on both the cursor-ledger path and the metadata-store path. A failed reset no longer publishes its target to lastMarkDeleteEntry, so a later dirty-cursor flush, ledger rebuild or close writes the previous, consistent state. I checked that the new tests pin this. Each of these changes, reverted on its own, makes the new tests fail:

  • ignoring the reset snapshot in serializePositionInfo fails testResetPersistsAcknowledgmentState (2 invocations)
  • dropping the reset-barrier wait in internalAsyncClose fails 7 ManagedCursorResetCloseRaceTest invocations
  • publishing reset entries to lastMarkDeleteEntry before they are persisted fails testRebuildingCursorLedgerAfterFailedResetPreservesAcknowledgments and testCloseWaitingForFailedResetPreservesPreviousAcknowledgments

One thing blocks merging: CI - Unit - Other is red because of this change. ManagedCursorTest.failDuringRecoveryWithEmptyLedger and recoverCursorPropertiesWhenCursorLedgerIsEmpty fail every time on this head and pass with the base ManagedCursorImpl.java. CI only reports the first one. See the first inline comment.

The second inline comment covers a gap this PR leaves open, which is fine to handle in a follow-up.

@codelipenghui codelipenghui 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.

Request changes at unchanged head '92a21be7db'. I reproduced both empty-ledger recovery failures: publishing an ordinary ACK before the replacement cursor ledger is created changes the rollback checkpoint. This is no longer just unexplained red CI. The reset-snapshot, failed-reset, and close-ordering fixes remain sound. The separately reported in-flight individual-ACK race predates this PR and can be tracked as a follow-up.

Restore ordinary ACK checkpoint timing and reuse the captured checkpoint during cursor-ledger switches. Coordinate close with submitted resets and ledger switches, cancel queued resets when the owning ledger closes, and preserve explicit compaction property edits.

Assisted-by: OpenAI Codex (GPT-6)
Carry the reset ACK snapshot only through its original write and fallback. Keep cursor close and checkpoint publication timing together for separate review, and retain the captured checkpoint used for both replacement ledger initialization and its metadata pointer.

Assisted-by: OpenAI Codex (GPT-6)
@void-ptr974
void-ptr974 marked this pull request as draft November 19, 2026 07:34

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks for the quick turnaround on the empty-ledger recovery regression.

The blocker from my previous review is resolved. Ordinary ACKs publish to lastMarkDeleteEntry only when they are submitted again, as on master, so ManagedCursorTest.failDuringRecoveryWithEmptyLedger and recoverCursorPropertiesWhenCursorLedgerIsEmpty pass on this head. The ledger switch now writes the seed entry and the metadata record from the same captured entry (ManagedCursorImpl.java:3869-3875), and the new cursorLedgerSeedAndMetadataUseTheSameCheckpoint test fails if switchToNewLedger re-reads lastMarkDeleteEntry instead.

I ran ManagedCursorTest, the ManagedCursorReset* classes and ManagedLedgerFactoryTest locally on this head with retries disabled: no failures. I have not seen CI on this head yet, so please confirm CI - Unit - Other is green. The in-flight asyncDelete gap in the second thread stays a follow-up, as the description says.

@void-ptr974

Copy link
Copy Markdown
Contributor Author

@lhotari @codelipenghui Thanks for the earlier reviews. #26973 is ready for another look with a narrower scope. It persists the ACK snapshot supplied by the reset, keeps ordinary ACKs out of the recovery checkpoint until submission, and uses the same checkpoint for the replacement ledger's first record and its Metadata Store pointer. I’ll handle failed-reset checkpoint publication, compaction properties, and reset ordering during close and unload together in a follow-up PR. Cursor ledger creation failure handling will get its own PR. The two in-flight individual ACK cases, including the dropped callback, are tracked together in #27668. I’d appreciate a review of the narrowed diff.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. The narrowed scope is easier to reason about. The reset write and its metadata-store fallback now persist the reset's own ACK snapshot: no individual ranges, and only the batch ACK set carried by the reset position (ManagedCursorImpl.java:2792-2801, ManagedCursorImpl.java:3904-3922, ManagedCursorImpl.java:3393-3401). Ordinary writes keep using the live state. A rollover triggered by the reset append still waits until the reset's alignment has run, so the replacement ledger is seeded with the post-reset state. The seed and the Metadata Store pointer also share the captured checkpoint (ManagedCursorImpl.java:3696-3702). Thanks for opening #27668 and for listing the remaining close, compaction and ledger-creation cases as follow-ups.

@lhotari
lhotari merged commit d5500ef into apache:master Nov 25, 2026
43 checks passed
codelipenghui added a commit to codelipenghui/incubator-pulsar that referenced this pull request Nov 25, 2026
Resolve the conflicts in ManagedCursorImpl with apache#26973:

- persistPositionMetaStore now takes a reset ACK-state snapshot. Pass it to
  buildManagedCursorInfo, which skips the individual ACK ranges and takes the
  batch indexes from the snapshot, as master does. A publication persists no
  ACK ranges, so it passes none.
- switchToNewLedger now receives the checkpoint written to the new ledger.
  Publish that checkpoint instead of lastMarkDeleteEntry.

Assisted-by: Claude Code (Claude Opus 5.5)
lhotari pushed a commit that referenced this pull request Nov 26, 2026
…se messages to be silently skipped after recovery (#26973)

(cherry picked from commit d5500ef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants