GeezerVison is showing github.com — [fix][broker] Release pending acks when persisting an expiry mark-delete fails by lhotari · Pull Request #27416 · apache/pulsar
Conversation
lhotari
commented
Nov 6, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation
When a message expiry mark-delete can't be persisted, the expired entries can stay in the consumers' pendingAcks.
ManagedCursorImpl moves its mark-delete position in memory before persisting it, and keeps that position when persisting fails. PersistentMessageExpiryMonitor only calls Dispatcher#markDeletePositionMoveForward when the mark-delete succeeds, and a later expiry check for the same position returns early because the position has already been reached.
The entries therefore stay until the consumer acks them, disconnects or gets a redelivery, or until a later call to the hook moves past them. Until then, the consumer's unacked count stays inflated and, on Key_Shared, its draining hashes can stay held.
Before #25592, the pending acks sweep in readMoreEntries released these entries the next time it ran. This is a follow-up to that change, like #27415.
Modifications
- Call
markDeletePositionMoveForward() from the expiry monitor's markDeleteFailed callback as well.
- Run that cleanup on the topic's ordered executor without waiting for it. The cursor can invoke the failure callback while holding its
pendingMarkDeleteOps lock, for example when creating its metadata ledger fails. The cleanup takes the dispatcher's lock, and dispatch holds that lock while acknowledging entries rejected by an entry filter on the cursor, so running the cleanup inside the callback could deadlock. The success path is unchanged, since the cursor doesn't complete a successful mark-delete while holding that lock.
- The cleanup reads the cursor's current mark-delete position when it runs and only releases entries at or before it, so it's also safe for failures where the position didn't move.
Verifying this change
- Make sure that the change passes the CI checks.
This change added tests and can be verified as follows:
- Added
ConsumerStatsTest#testPendingAcksReleasedWhenPersistingExpiryMarkDeleteFails, which runs with Shared and Key_Shared subscriptions and makes the cursor's metadata ledger creation fail during an expiry mark-delete. Without the fix, all 10 pending acks stay after the mark-delete position has moved past 9 of them, and the test fails. It also asserts that the success callback didn't run.
- Added
ConsumerStatsTest#testExpiryFailureCleanupDoesNotWaitForDispatcherInsideCursorCallback for Shared and Key_Shared. It holds the dispatcher's lock while the failed mark-delete runs, then checks that the thread waiting for that lock isn't inside a ManagedCursorImpl callback and doesn't hold the cursor's pendingMarkDeleteOps monitor. With the cleanup run synchronously in the callback, both variants fail.
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
- 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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ete fails
The cursor moves its mark-delete position in memory before persisting it and
keeps that position when persisting fails. The expiry monitor only invoked
markDeletePositionMoveForward on success, and a later expiry check for the same
position returns early, so expired entries stayed in the consumers' pending
acks until they acked or disconnected, or the position moved again.
Invoke the hook from the failure callback too.
Assisted-by: Claude Code (claude-opus-5-5)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested approach for 5ec6f5b78d: keep the failure-path cleanup, but hand it off to the topic’s existing ordered executor rather than invoking the dispatcher synchronously from markDeleteFailed.
The cleanup is necessary because the cursor can advance in memory even when persistence fails, and a subsequent expiry check skips that already-covered position. However, metadata-ledger creation failure can invoke the callback while holding pendingMarkDeleteOps. Calling the dispatcher there creates the reverse lock order of dispatch-time entry-filter rejection, which holds the dispatcher monitor and calls into cursor acknowledgement bookkeeping.
For example, the failure callback could enqueue:
topic.getOrderedExecutor().execute(this::notifyMarkDeletePositionMoveForward);
Do not wait for the queued task. Let the existing hook read the current cursor position when it executes, rather than capturing the failed request’s target position. Keep the failure reporting and expiry-check bookkeeping intact.
This retains selective, position-bounded cleanup without restoring scans on every dispatch read. The tradeoff is a short scheduling delay; it does not require changing cursor persistence semantics. Moving storage callbacks outside their locks is another option, but would be a broader change.
Please cover the handoff with a deterministic test of metadata-ledger creation failure concurrent with entry-filter rejection, alongside the Shared/Key_Shared pending-ack cleanup cases.
This is a source-based suggestion; I have not implemented or runtime-validated this alternative.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up to the earlier suggestion: the executor handoff has now been implemented and runtime-tested locally on top of 5ec6f5b78df2256934831e322e512c6fa3f3462a.
The production change replaces the synchronous notification in markDeleteFailed with:
topic.getOrderedExecutor().execute(this::notifyMarkDeletePositionMoveForward);
There is no wait for the queued task. The existing hook reads the current cursor position when it executes; success handling and persistence semantics remain unchanged.
A local regression test, PersistentMessageExpiryMonitorTest#testExpiryFailureDoesNotWaitForDispatcherDuringEntryFilterRejection, covers Shared and Key_Shared using a running broker, real cursor/dispatcher, and injected BookKeeper ledger-creation failure. It enables subscription-thread dispatch, pauses an entry filter while it holds the dispatcher monitor, and observes whether the cursor failure callback returns or blocks on that exact monitor while holding the cursor queue lock. On the broken path it safely unwinds instead of leaving the JVM permanently deadlocked. With the handoff, the filter actually rejects/acknowledges its entry; expired pending acks are released, the unexpired pending ack remains, and subsequent delivery succeeds.
Results (test retries disabled):
- Before the handoff: the new Shared regression failed at the intended lock-order assertion. Key_Shared was skipped by the default fail-fast listener in that baseline run.
- After the handoff: 14 focused tests passed, none skipped, including both concurrency variants, persistence-failure cleanup, and non-batch/batch/partial-batch expiry accounting.
- Repeat: both concurrency variants passed again (2/2).
- Broker-module
checkstyleMain, checkstyleTest, spotlessCheck, and git diff --check passed.
Validation was on Corretto 21.0.11/macOS arm64, using the project's mock BookKeeper for storage-failure injection—not a production cluster, full-project suite, or prolonged stress test. These changes remain local; nothing has been committed or pushed.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The cursor can invoke the mark-delete failure callback while holding its
pending mark-delete lock, for example when creating its metadata ledger fails.
The cleanup takes the dispatcher's lock, while dispatch holds that lock when it
acknowledges entries rejected by an entry filter on the cursor, so running the
cleanup inside the callback could deadlock.
Hand the cleanup off to the topic's ordered executor without waiting for it.
It reads the current mark-delete position when it runs. Add a test that holds
the dispatcher's lock and checks that the thread waiting for it isn't inside a
cursor callback holding the cursor's lock.
Assisted-by: Claude Code (claude-opus-5-5)
|
@codelipenghui Thanks for the suggestion and for validating it. I applied the executor handoff in 759a820, as you described: markDeleteFailed queues the cleanup on topic.getOrderedExecutor() without waiting, and the success path and the cursor's persistence semantics are unchanged.
For the regression coverage, the new test doesn't pause an entry filter. Instead, it holds the dispatcher's lock itself, which stands in for the dispatch side of the lock cycle. It then asserts that the thread waiting for that lock isn't inside the cursor's failure callback holding pendingMarkDeleteOps. This keeps the test deterministic, and it can't leave the shared broker deadlocked if it regresses. It fails on the previous revision for both Shared and Key_Shared, and the existing pending-ack cleanup test still covers the cleanup itself. I can also add your full entry-filter reproduction if you'd like.
I also checked whether the success path has the same problem, and it doesn't. The cursor never completes a mark-delete successfully while holding pendingMarkDeleteOps. Success completes from the BookKeeper add callback or from the metadata store callback, which MetaStoreImpl always runs on its executor. Mark-deletes skipped for a newer position are moved to the managed ledger executor. The rate-limited path completes in the calling thread, after the cursor's lock is released. A probe run of the new test without the injected failure showed the success-path cleanup holding no monitors while it waited for the dispatcher.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|