Repository navigation
[fix][broker] Manage in-flight bucket work during clear - #26401
Conversation
|
Ping @void-ptr974 @Denovo1998 |
|
Thanks for the fix. I think there are two edge cases that may still need attention.
For example, suppose buckets A later
For example, suppose a stale delete is deleting bucket key The retry is only for |
33d7c2c to
7f22e56
Compare
The BucketNotExist branch in handleRecoverBucketSnapshotEntry never matched: dependent future stages wrap the checked exception in a CompletionException, so a stale cursor property pointing to a deleted snapshot failed the whole recovery instead of being cleaned up. Unwrap before the check so the bucket is deleted and the property is removed, like the path was designed to do. Also make MockBucketSnapshotStorage re-readable (parse from a slice, same as the metadata read) and add recreate-tracker tests for both cleanup-failure shapes: consistent snapshot+property residue loads normally, property-only residue is removed on recovery.
A failed merged-bucket creation completes with the INVALID_BUCKET_ID sentinel, so the source-delete chain still ran and deleted the source snapshots although their ledger ranges were already removed from immutableBuckets - after a restart the only durable delayed-index state for those ranges was gone. Skip the source deletes when the merged creation failed, so the source buckets stay recoverable from their cursor properties.
lhotari
left a comment
There was a problem hiding this comment.
Thanks for working on this fix. Waiting for in-flight loads and deletes in clear() is the right direction, and the gated-storage tests show the waits deterministically.
One production point and two small test points are below.
On the #26420 question: this change makes clear() wait for the load that is in flight when it is called, including the state it publishes and its terminal snapshot delete. It also waits for the trim/merge chain and for tracked deletes. A load that starts after the captured one completes, and before the cleanup takes the tracker lock, is still not covered. That is the open generation/fencing discussion on clear(), so I am not repeating it here.
codelipenghui
left a comment
There was a problem hiding this comment.
Based on 97817549a80, I'd suggest:
- Keep
pendingLoadlimited to index loading/publication. Track deletes separately by bucket identity, retaining failed cleanup for retry. - During
clear(), fence new bucket work and preserve incoming delayed positions. Await existing load/create/merge callbacks, then drain the cleanup they scheduled. - Reset old state and resume only after cleanup succeeds; otherwise fail
clear()and remain fenced until retry—an explicit availability tradeoff.
This avoids blocking ready deliveries on terminal deletion while establishing a clear boundary for old tracker work.
…bucket-clear-inflight # Conflicts: # pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketDelayedDeliveryTracker.java
…y failed cleanup in clear() - The terminal-segment snapshot delete no longer holds pendingLoad, so getScheduledMessages() keeps delivering while it runs; clear() still waits for it through pendingDeletes. - A failed tracked delete, and the source buckets of a failed merge, are kept and deleted by the next clear(). clear() fails while that cleanup fails, instead of reporting success. - Tests: deterministic failing-delete test, delivery not blocked by the terminal delete, clear() after a failed merge, == 0 bucket assertion, storage closed in finally.
|
Thanks @nodece. I picked this up as part of the open-PR backlog triage, merged master (no rebase) and pushed one commit on top:
Fencing work that starts after |
Co-authored-by: Matteo Merli <[email protected]> (cherry picked from commit 865e9be)
resetCursorInternal() reset the cursor without touching the delayed delivery tracker, although a reset moves the consumption baseline the tracker state was derived from: bucket snapshots, index bits and queued entries from before the reset survived into the replay, and in-flight trims/loads/deletes could race the replayed state. Clear the delayed messages and wait for the clear to settle after disconnecting consumers and before asyncResetCursor, with the whole reset flow chained into a single error path. Without a dispatcher, clean the residual bucket snapshots like the unsubscribe path does, so a later consumer reconnect cannot recover pre-reset buckets from the cursor properties. Review-feedback follow-ups folded in: restore the disconnect-failure branch (SubscriptionBusyException / HTTP 412, resetCloseFuture on both outcomes), unwrap CompletionException in the terminal error path so typed exceptions pass through unwrapped, and await the clear via thenCompose so the reset only starts after the clear settles (a handle() stage does not flatten a returned future, which let asyncResetCursor race the bucket cursor-property writes and fail with BadVersion). Rebased onto master: preserves the nonDurableCursorCleanupStarted guard, lastCursorResetFuture publication, and the compaction fallback to forceReset=false from intervening commits; inherits apache#26401, which made BucketDelayedDeliveryTracker.clear() also wait for in-flight segment loads and deletes. Assisted-by: Claude Code
Fix delayed-delivery bucket trim and segment-loading races that could stall progress or break cleanup: - In getScheduledMessages(), process the snapshot-segment boundary transition before dropping an orphaned tail entry, while still suppressing delivery of that orphaned entry. Previously the orphan check ran before the cutoff check, so a segment whose tail entry lived below the first live ledger never advanced its boundary and the next snapshot segment was never loaded. Cutoff gating is kept: the next segment is not loaded before the cutoff and is loaded once the cutoff is reached. - Remove the INVALID_BUCKET_ID sentinel: a failed snapshot creation now propagates through the create future's exceptional completion, and ImmutableBucket.resolveBucketIdForDelete() converts it into "nothing to delete" so the sequential trim chain keeps going instead of failing on a failed create. The no-create-future path resolves the bucket id through a failed CompletableFuture instead of throwing synchronously, and the merged-bucket terminal keeps the source snapshots (keepUncleaned) when the merged creation fails. - deleteBucketSnapshot() revalidates range-to-bucket ownership before starting the delete and again before removing tracker state, so a bucket replaced while the delete is in flight is neither deleted nor scrubbed twice. The reset-cursor changes previously carried in this PR moved to apache#26420, which owns the reset path and its delayed-state cleanup semantics. Rebased onto master: clear() waiting for in-flight segment loads and pending deletes, and the tracked snapshot deletes on every path, are now provided by apache#26401 and are not duplicated here. Assisted-by: Claude Code
resetCursorInternal() reset the cursor without touching the delayed delivery tracker, although a reset moves the consumption baseline the tracker state was derived from: bucket snapshots, index bits and queued entries from before the reset survived into the replay, and in-flight trims/loads/deletes could race the replayed state. Clear the delayed messages and wait for the clear to settle after disconnecting consumers and before asyncResetCursor, with the whole reset flow chained into a single error path. Without a dispatcher, clean the residual bucket snapshots like the unsubscribe path does, so a later consumer reconnect cannot recover pre-reset buckets from the cursor properties. Review-feedback follow-ups folded in: restore the disconnect-failure branch (SubscriptionBusyException / HTTP 412, resetCloseFuture on both outcomes), unwrap CompletionException in the terminal error path so typed exceptions pass through unwrapped, and await the clear via thenCompose so the reset only starts after the clear settles (a handle() stage does not flatten a returned future, which let asyncResetCursor race the bucket cursor-property writes and fail with BadVersion). Rebased onto master: preserves the nonDurableCursorCleanupStarted guard, lastCursorResetFuture publication, and the compaction fallback to forceReset=false from intervening commits; inherits apache#26401, which made BucketDelayedDeliveryTracker.clear() also wait for in-flight segment loads and deletes. Assisted-by: Claude Code
resetCursorInternal() reset the cursor without touching the delayed delivery tracker, although a reset moves the consumption baseline the tracker state was derived from: bucket snapshots, index bits and queued entries from before the reset survived into the replay, and in-flight trims/loads/deletes could race the replayed state. Clear the delayed messages and wait for the clear to settle after disconnecting consumers and before asyncResetCursor, with the whole reset flow chained into a single error path. Without a dispatcher, clean the residual bucket snapshots like the unsubscribe path does, so a later consumer reconnect cannot recover pre-reset buckets from the cursor properties. Review-feedback follow-ups folded in: restore the disconnect-failure branch (SubscriptionBusyException / HTTP 412, resetCloseFuture on both outcomes), unwrap CompletionException in the terminal error path so typed exceptions pass through unwrapped, and await the clear via thenCompose so the reset only starts after the clear settles (a handle() stage does not flatten a returned future, which let asyncResetCursor race the bucket cursor-property writes and fail with BadVersion). Rebased onto master: preserves the nonDurableCursorCleanupStarted guard, lastCursorResetFuture publication, and the compaction fallback to forceReset=false from intervening commits; inherits apache#26401, which made BucketDelayedDeliveryTracker.clear() also wait for in-flight segment loads and deletes. Assisted-by: Claude Code
resetCursorInternal() reset the cursor without touching the delayed delivery tracker, although a reset moves the consumption baseline the tracker state was derived from: bucket snapshots, index bits and queued entries from before the reset survived into the replay, and in-flight trims/loads/deletes could race the replayed state. Clear the delayed messages and wait for the clear to settle after disconnecting consumers and before asyncResetCursor, with the whole reset flow chained into a single error path. Without a dispatcher, clean the residual bucket snapshots like the unsubscribe path does, so a later consumer reconnect cannot recover pre-reset buckets from the cursor properties. Review-feedback follow-ups folded in: restore the disconnect-failure branch (SubscriptionBusyException / HTTP 412, resetCloseFuture on both outcomes), unwrap CompletionException in the terminal error path so typed exceptions pass through unwrapped, and await the clear via thenCompose so the reset only starts after the clear settles (a handle() stage does not flatten a returned future, which let asyncResetCursor race the bucket cursor-property writes and fail with BadVersion). Rebased onto master: preserves the nonDurableCursorCleanupStarted guard, lastCursorResetFuture publication, and the compaction fallback to forceReset=false from intervening commits; inherits apache#26401, which made BucketDelayedDeliveryTracker.clear() also wait for in-flight segment loads and deletes. Assisted-by: Claude Code
Motivation
BucketDelayedDeliveryTracker.clear()can finish while bucket work is still in flight. Callers such as clear-backlog, and the cursor reset in #26420, rely onclear()as the point where the old delayed state is gone.clear()only waits fortrimFuture. A segment load that is still running re-adds pre-clear indexes tosharedBucketPriorityQueueafter the reset.clear()returns while they are still running.immutableBuckets, so no laterclear()retries it.immutableBucketsbut keep their snapshots.clear()can't find them, and a restart recovers them again.instanceof BucketNotExistExceptioncheck never matches, because the exception arrives wrapped in aCompletionException. A stale cursor property therefore fails recovery instead of being removed.Modifications
clear()waits for. It waits for the trim/merge chain, the currentpendingLoadand every tracked snapshot delete before resetting in-memory state.clear()itself go throughtrackDelete(bucket, future).uncleanedBuckets, and so do the sources of a failed merge.clear()deletes those buckets too. While that cleanup fails,clear()fails, and the nextclear()retries.pendingLoad, sogetScheduledMessages()keeps delivering while it runs.CompletionExceptionbefore matchingBucketNotExistException.Fencing work that starts after
clear()has begun, and revalidating trim/merge ownership, are left to a follow-up (see #26280).Verifying this change
BucketDelayedDeliveryTrackerTestadds deterministic tests that use gated storage. They cover:clear(), and is retried by the nextclear()clear()after a failed mergeAll
org.apache.pulsar.broker.delayed.*tests pass (110). The four new tests fail without the change.