Skip to content

[fix][ml] Reconcile cursor ledger publication after metadata errors - #27385

Merged
codelipenghui merged 6 commits into
apache:masterfrom
codelipenghui:penghui/fix-cursor-publication-20261105
Nov 25, 2026
Merged

codelipenghui merged 6 commits into
apache:masterfrom
codelipenghui:penghui/fix-cursor-publication-20261105

Conversation

@codelipenghui

@codelipenghui codelipenghui commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

A cursor metadata update can commit while its response fails. The cursor currently treats that failure as proof that its candidate ledger is an orphan and deletes a ledger still referenced by the persisted cursor. Reopening can then lose the retained ACK state or fail recovery.

Modifications

Read and reconcile native cursor metadata before adopting or deleting a candidate ledger. While the store has not reported the cursor node's creation timestamp (ZooKeeper does not on create), resolve it before publishing: use the native Stat when the node still holds the cursor's version and content, or refresh to it after an ownership check; otherwise fail the publication before writing. Accept a committed update only from the same node, an advanced version and the complete attempted bytes. Allow deletion only with affirmative versioned evidence that the candidate is not referenced. Retain an uncertain publication's candidate, logging its ID at WARN, and delete it once the cursor node moves past the version that its CAS expected without referencing it, or when the cursor is deleted. A version conflict still refreshes the cursor's version after an ownership check, whatever the node's creation timestamp, and every refresh adopts the stored cursor properties along with the version, so that a later write cannot erase them. Publish through a dedicated method with explicit published, rejected and uncertain outcomes, and keep ownership, closing-state and executor-failure handling bounded to this publication path.

Verifying this change

On Apache master a72adf733ead20ba4b19a76222b698f315073ecf, the reproducing baseline had 2 failing cases out of 2 selected cases, with no compilation failure. The two direct baseline regressions observe the published test BookKeeper ledger being deleted and individual ACK state failing recovery after a lost commit response. Additional tests cover a late server commit, failed reconciliation reads, complete record identity, close and executor rejection. Regression tests cover a newly created cursor whose first publication response is lost, also after a failed cursor node read and after a lost cursor-properties response: subsequent ACKs succeed without retaining candidate ledgers. Further tests cover deleting a retained candidate once the node moves past it, keeping one while a late commit references it, deleting it with the cursor, recovery after the node's creation timestamp changes, cursor properties kept across all three refresh paths, an executor rejection after the read proved an orphan, and a failed ownership check during a concurrent cursor update.

With the fix, 436 cases passed, with no failures/errors/skips and retries disabled. quickCheck and git diff --check passed.

Validated classes:

  • ManagedCursorTest
  • ManagedLedgerTest
  • MetaStoreImplTest
./gradlew :managed-ledger:test --tests org.apache.bookkeeper.mledger.impl.ManagedCursorTest --tests org.apache.bookkeeper.mledger.impl.ManagedLedgerTest --tests org.apache.bookkeeper.mledger.impl.MetaStoreImplTest quickCheck --no-daemon --max-workers=2 -PtestRetryCount=0 -PtestMaxParallelForks=1 -PtestMaxHeapSize=1536m -PtestFailFast=false -PtestJavaVersion=21

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

Threading: repair ordering and lifecycle ownership on the existing executors/locks; no new pool is introduced.

A cursor metadata update can commit while its response fails. The cursor currently treats that failure as proof that its candidate ledger is an orphan and deletes a ledger still referenced by the persisted cursor. Reopening can then lose the retained ACK state or fail recovery.

Read and reconcile native cursor metadata before adopting or deleting a candidate ledger. Accept a committed update only from the same node, an advanced version and the complete attempted bytes. Retain uncertain publications; allow deletion only with affirmative versioned evidence that the candidate is not referenced. Keep ownership, closing-state and executor-failure handling bounded to this publication path.

Baseline regressions fail before the fix. All 424 scoped cases and quickCheck pass with retries disabled.

Assisted-by: OpenAI Codex
…ublication

ZooKeeper reports a zero creation timestamp for a node it has just
created. A new cursor therefore held a Stat that publication
reconciliation could not match to the native node: after a lost response
to its first cursor ledger publication, the committed update was neither
adopted nor used to refresh the version. Every later ACK then failed with
BadVersionException and retained another candidate ledger.

After creating the cursor node, read it back and use the native Stat when
the node still holds the created version and content. The evidence
required to adopt or delete a candidate ledger is unchanged.

Assisted-by: Claude Code (Claude Opus 5.5)
…tion

The previous change read the cursor node back once after creating it. If
that read failed, the cursor still opened without the node's creation
timestamp, and a lost response to its first publication again left every
later ACK failing with BadVersionException. Failing initialization
instead would leave a created node that a retried open cannot create
again.

Resolve the identity right before publishing while the creation
timestamp is unknown. Use the native Stat when the node still holds the
cursor's version and content; otherwise refresh to it after an ownership
check, as a version conflict does. When the node cannot be read or the
refresh is not allowed, fail the publication before writing, so the
candidate ledger is safe to delete and a later ACK retries.

Assisted-by: Claude Code (Claude Opus 5.5)
@codelipenghui codelipenghui self-assigned this Nov 7, 2026
@codelipenghui codelipenghui added this to the 5.1.0 milestone Nov 7, 2026
@codelipenghui codelipenghui added type/bug The PR fixed a bug or issue reported a bug release/4.2.6 release/4.0.15 release/5.0.2 labels Nov 7, 2026
@codelipenghui
codelipenghui marked this pull request as ready for review November 7, 2026 07:39

@void-ptr974 void-ptr974 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.

Thanks for the fix! This prevents deleting a cursor ledger when the metadata update succeeds but its response is lost, so the subscription can recover its ACK state correctly. LGTM.

@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. Deleting a cursor ledger that the durable cursor might still reference is clearly worse than keeping it, and the tests that reproduce a lost commit response are a good addition.

Most of my comments are about the other side of that trade-off. A candidate that is kept after an uncertain publication is never revisited. While the failures are specific to the cursor node (a timeout or lost response on the cursor write while BookKeeper can still create ledgers), each later ACK can create and keep one more candidate. There is also a smaller liveness concern in the new creation-timestamp check, plus a few nits.

…roperties

A candidate ledger kept after an uncertain publication was never
revisited, so each one stayed in BookKeeper for good. Record it with the
version that its CAS expected and log its ID at WARN. Delete it once the
cursor node moves past that version without referencing it, which a
successful write, a properties update or a refresh read shows, or when
the cursor is deleted.

A version conflict skipped the version refresh when the node's creation
timestamp did not match, e.g. after a metadata store migration that
keeps versions, so every later publication conflicted again. Refresh
after the ownership check, as before reconciliation, while still keeping
the candidate.

Refreshing to a stored version kept the in-memory cursor properties, so
the next write at that version could erase a property that the cursor
had not seen. Adopt the properties of the record whose version is
adopted, also in the existing refresh of persistPositionMetaStore.

When the executor rejects the reconciliation completion, report what the
read established, so a proven orphan is deleted. Check the ownership
result before the shortcut for a concurrently changed Stat.

Publish through publishCursorLedger with explicit published, rejected
and uncertain outcomes, instead of selecting behavior in
persistPositionMetaStore by the callback type.

The new tests fail before this change. ManagedCursorTest,
ManagedLedgerTest and MetaStoreImplTest (436 cases) and quickCheck pass
with retries disabled.

Assisted-by: Claude Code (Claude Opus 5.5)
Resolve the conflicts in ManagedCursorImpl and adapt the publication path to
the cursor changes on master:

- apache#27703 rebuilds the cursor properties in updateCursorLedgerStat, so drop
  adoptStoredCursorInfo and keep only the deletion of retained ledgers there.
  Sample the version and the properties as one pair in publishCursorLedger as
  well as in persistPositionMetaStore, and pass them to buildManagedCursorInfo.
- apache#27652 assigns a publication ID in MetaStoreImpl during the write. Assign it
  in publishCursorLedger before taking the attempted bytes, so that
  reconciliation still recognizes a committed publication.
- apache#27671 makes a closing cursor wait for an in-flight ledger switch, so allow
  that switch to publish while the cursor is Closing.
- apache#26817 closes the read-only cursor ledger in asyncDeleteCursorLedger; keep
  that next to the deletion of retained ledgers.

MetaStoreImpl now calls the put overload with options, so the fault-injection
stubs in ManagedCursorTest target that overload.

Assisted-by: Claude Code (Claude Opus 5.5)

@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 thorough follow-up. Every point from my last review is handled in 1ab7c12, each with a test. Candidates kept after an uncertain publication are now reclaimed once the cursor node moves past the version their CAS expected, and they are logged at WARN. A BadVersion refreshes the Stat again whatever the creation timestamps. Executor rejection keeps what the read proved. The ownership result is checked first. The publication has its own publishCursorLedger path. The remaining leak window you describe (broker stop, or a close through the ledger path before any later node write) is narrow, and the ledger ID is in the WARN log, so that trade-off looks reasonable to me.

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)
@codelipenghui
codelipenghui merged commit 3e3a4a9 into apache:master Nov 25, 2026
43 checks passed
lhotari pushed a commit that referenced this pull request Nov 26, 2026
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.

5 participants