Skip to content

[fix][ml] Serialize individual ACK admission with durable cursor resets - #27693

Open
void-ptr974 wants to merge 12 commits into
apache:masterfrom
void-ptr974:fix/issue-27668
Open

void-ptr974 wants to merge 12 commits into
apache:masterfrom
void-ptr974:fix/issue-27668

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27668

Motivation

A backward seek on a durable subscription can succeed and still skip messages after the cursor reopens. For example, seeking from mark-delete position 3:4 to read position 3:2 writes a checkpoint at 3:1. An overlapping individual ACK can append a later record at 3:4. Recovery from that record skips entries 3:2 through 3:4.

If a read is pending, an individual ACK can instead queue behind the reset and lose its callback. A transaction commit waiting for that ACK can then remain incomplete after seek disconnects the consumers.

Modifications

  • Order durable reset admission, ACK application, and checkpoint submission under the existing mark-delete queue lock. Earlier ACKs that share the reset's write receive its storage result.
  • Reject ordinary individual ACKs that overlap a reset before applying them to the cursor or broker's batch ACK state. A transaction commit waits for the reset to settle and retries only its cursor ACK, using the original COMMIT record.
  • After a successful seek, clear ordinary batch ACK state and preserve outstanding transaction reservations.
  • Reject ordinary individual ACKs already routed to a disconnected consumer after seek. Refresh Failover consumers that remain connected before accepting their ACKs in the new generation.
  • For the compaction cursor, restore the previous horizon when a shared checkpoint write fails or cursor-ledger publication fails with the compaction ACK still queued. A retried seek then persists the horizon with the matching compacted ledger ID.
  • During close, cancel a queued reset and settle the ACKs that preceded it. When the reset write has already been submitted, wait for its storage result before choosing the closing checkpoint.

Related work

Verifying this change

  • Make sure that the change passes the CI checks.
  • On 69c4b15d8e1 (including master at a602f161344), all 333 selected Managed Ledger tests and 171 selected broker tests passed locally, along with repository-wide quickCheck. Coverage includes cursor recovery and takeover, close during a submitted reset, retained Failover consumers, transaction commits that overlap seek, and cursor-ledger publication failure during compaction.
  • A separate run repeated 12 of those timestamp seek, lookup, and expiry cases, including concurrent seeks, partitioned topics, trimmed ledgers, and individual ACKs during a failed timestamp lookup. Both runs used zero automatic retries and reported zero skipped tests.

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

Order ACK admission and checkpoint submission with durable cursor resets. An overlapping individual ACK can overwrite a backward reset checkpoint or lose its callback when a read is pending.

Assisted-by: OpenAI Codex (GPT-6)
@void-ptr974
void-ptr974 marked this pull request as draft November 22, 2026 07:30
Resume individual ACKs after reset and keep their callbacks owned through cursor closure. Settle transaction reservations by their original ownership so a later seek cannot leave stale pending ACKs or remove a replacement reservation.

Assisted-by: OpenAI Codex (GPT-6)
Keep normal ACK application and pending-ACK cleanup on the existing paths. Reconcile ordinary ACKs against the reset outcome and retry only the cursor step of a transaction commit.

Assisted-by: OpenAI Codex (GPT-6)

@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 the careful work on this race and for the detailed tests, @void-ptr974. Putting reset admission, ACK application and checkpoint submission under the pendingMarkDeleteOps monitor is a reasonable fix for #27668. The lock order (queue monitor, then cursor write lock) looks consistent with the other paths I checked.

The inline comments are mostly small. Two need attention before this leaves draft:

  • The new PersistentSubscription entry points skip the PulsarCompactorSubscription override.
  • The reset checkpoint still carries individual ACKs made before the seek. That leaves the restart scenario from the motivation open for those ACKs.

The rest are suggestions for the close and retry paths and for test robustness.

Two related issues already exist on master and are out of scope here. They might be worth follow-ups:

  • A forward reset whose write fails leaves its target in lastMarkDeleteEntry and marks the cursor dirty. A later flush() can then advance mark-delete to the target of a reset that was reported as failed.
  • Closing a cursor while a read is in flight never runs the callbacks still queued in pendingMarkDeleteOps, because readOperationCompleted() only flushes in State.Open. closeSettlesResetCompletionWhileReadIsPending checks the conflict future but not the reset future itself.

Keep the cursor-ledger switch barrier and order the close checkpoint after a submitted reset.

Assisted-by: Codex (GPT-6)
@void-ptr974
void-ptr974 marked this pull request as ready for review November 25, 2026 14:05

@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 the thorough follow-up, @void-ptr974. Commit 028faf2 covers every point from the last round:

  • The reset checkpoint no longer carries ranges or batch masks from before the seek. resetCheckpointMatchesAcknowledgmentsAfterSeek checks this for the ledger, long-array and metadata-store forms, and also recovers from that checkpoint.
  • PulsarCompactorSubscription now overrides the batch-state entry point.
  • Removing the retry-after-failed-reset path also removes the wrapper default and its batch-mask concern.
  • A cursor closing during the commit retry now maps to ServiceUnitNotReadyException.
  • testResetCursorDrainsQueuedAcknowledgmentAfterReadCountReachesZero covers the zero-pending-read branch.
  • The broker tests gate only the cursor ledger.

Keeping ordinary batch ACKs apart from transaction reservations, with a generation that a successful reset bumps, is a cleaner model than the previous retry.

I have one remaining comment, on the robustness of a test in ManagedCursorResetAcknowledgmentTest.

… reset

Keep the upstream reset checkpoint snapshot when serializing ACK submission and preserve range-based counting. Hold the new cursor ledger's reset response until close has entered Closing so the test exercises both operations in flight.

Assisted-by: Codex (GPT-6)
Fence new ACKs when close is requested. Settle queued compaction ACKs against the shared checkpoint write and restore the previous horizon on failure. Preserve property changes while a reset waits for submission or persistence.

Assisted-by: Codex (GPT-6)
Bind ACK admission to the consumer generation and refresh retained failover consumers on their connection threads. Keep ACKs available during timestamp lookup and release the reset fence on submission and disconnect failures.

Assisted-by: Codex (GPT-6)
…covery

Exercise receipt-less grouped ACKs after redelivery and an ACK queued on a retained Failover connection. Hold one real bookie during reset and recover the cursor through fresh BookKeeper and metadata clients.

Assisted-by: Codex (GPT-6)
Keep backward seek with an explicit empty batch mask and recovery before writer close. Rely on the reset durability suite for alternate encodings and metadata fallback. Verify standby ACK ordering separately from client replay and receipt, keeping the replayed message ID assertion in the end-to-end test.

Assisted-by: Codex (GPT-6)
Restore the live mark-delete position and consumed count before failing a queued compaction ACK. Otherwise a later seek can persist the advanced position alongside the previous compacted ledger after cursor ledger publication fails.

Assisted-by: OpenAI Codex

@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 the update, @void-ptr974, and for filing #27786 for the double failure callback.

closeWaitsForResetSubmittedAfterCursorLedgerSwitch now holds the reset's add response on the new cursor ledger (ManagedCursorResetAcknowledgmentTest.java:788-815). It then drains the ledger executor and checks Closing while close and reset are both pending, so the test exercises close waiting for a submitted reset.

I also went through the commits since my last review: the ACK admission tied to the consumer generation in PersistentSubscription, the close fence and the compaction checkpoint restore in ManagedCursorImpl, and the master merge, which keeps the reset checkpoint snapshot from #26973. I have no further comments on them.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Individual ACKs racing with a durable cursor reset can overwrite its checkpoint or lose their callback

3 participants