You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "Denovo1998 (via GitHub)" <gi...@apache.org> on 2026/08/04 13:05:11 UTC

Re: [PR] [fix][broker] Fix snapshot creation race causing delayed delivery bucket trim failures [pulsar]

Denovo1998 commented on code in PR #26260:
URL: https://github.com/apache/pulsar/pull/26260#discussion_r3712748650


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketDelayedDeliveryTracker.java:
##########
@@ -918,20 +923,32 @@ private synchronized CompletableFuture<Void> asyncTrimImmutableBuckets() {
 
     private CompletableFuture<Void> deleteBucketSnapshot(String ledgerName,
                                                           Range<Long> range, ImmutableBucket bucket) {
-        return bucket.asyncDeleteBucketSnapshot(stats)
-                .handle((__, t) -> {
-                    if (t != null) {
-                        log.warn().attr("LedgerName", ledgerName)
-                                .attr("BucketKey", bucket.bucketKey())
-                                .log("Failed to delete bucket snapshot");
-                        throw new CompletionException(t);
-                    }
+        return bucket.getSnapshotCreateFuture().orElse(NULL_LONG_PROMISE)
+                .thenCompose(bucketId -> {
                     synchronized (this) {
-                        snapshotSegmentLastIndexMap.entrySet().removeIf(entry -> entry.getValue() == bucket);
-                        removeBucket(range);
-                        numberDelayedMessages.addAndGet(-bucket.getNumberBucketDelayedMessages());
+                        Long firstLedgerId = firstActiveLedgerId();
+                        if (INVALID_BUCKET_ID.equals(bucketId)
+                                || firstLedgerId == null
+                                || range.upperEndpoint() >= firstLedgerId
+                                || immutableBuckets.asMapOfRanges().get(range) != bucket) {
+                            return CompletableFuture.completedFuture(null);
+                        }
                     }
-                    return null;
+                    return bucket.asyncDeleteBucketSnapshot(stats).thenRun(() -> {

Review Comment:
   The orphan eligibility is validated only before the asynchronous snapshot deletion begins. Once this synchronized block is released, the cursor may be reset or its mark‑deleted position could move backward while `asyncDeleteBucketSnapshot()` is still running.
   
   In such cases, the completion block only rechecks the range‑to‑bucket identity, so it may still delete bucket state even if `range.upperEndpoint() < firstActiveLedgerId()` no longer holds. The existing test updates `firstLedgerId` before the snapshot‑create future is released, but it does not cover modifications after this validation and after storage deletion has started.
   
   Could we add a deletion ownership or version guard that spans the duration of the asynchronous operation, or base orphan cleanup on a monotonic physical‑ledger boundary? Please also include a test that blocks `deleteBucketSnapshot()`, changes `firstLedgerId` after deletion begins, and then completes the delete.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketDelayedDeliveryTracker.java:
##########
@@ -918,20 +923,32 @@ private synchronized CompletableFuture<Void> asyncTrimImmutableBuckets() {
 
     private CompletableFuture<Void> deleteBucketSnapshot(String ledgerName,
                                                           Range<Long> range, ImmutableBucket bucket) {
-        return bucket.asyncDeleteBucketSnapshot(stats)
-                .handle((__, t) -> {
-                    if (t != null) {
-                        log.warn().attr("LedgerName", ledgerName)
-                                .attr("BucketKey", bucket.bucketKey())
-                                .log("Failed to delete bucket snapshot");
-                        throw new CompletionException(t);
-                    }
+        return bucket.getSnapshotCreateFuture().orElse(NULL_LONG_PROMISE)

Review Comment:
   This ensures the global `trimFuture` waits for every selected bucket's snapshot-creation future. If any create future remains pending for an extended period, `trimFuture.isDone()` stays false, which prevents later `addMessage()` calls from triggering another trim/merge and also chains `clear()` behind the same future.
   
   Would it be safer to skip buckets still in the CREATING state and retrigger trim when their creation finishes, rather than holding the global trim/merge gate? At minimum, it would be helpful to document the bounded-completion guarantee or add a test for a create future that never completes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org