mattisonchao commented on code in PR #18420:
URL: https://github.com/apache/pulsar/pull/18420#discussion_r1052789262


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/Bucket.java:
##########
@@ -131,7 +131,8 @@ CompletableFuture<Long> asyncSaveBucketSnapshot(
                     bucketState.setBucketId(newBucketId);
                     String bucketKey = bucketState.bucketKey();
                     return putBucketKeyId(bucketKey, 
newBucketId).exceptionally(ex -> {
-                        log.warn("Failed to record bucketId to cursor 
property, bucketKey: {}", bucketKey);
+                        log.warn("Failed to record bucketId to cursor 
property, bucketKey: {}, bucketId: {}",

Review Comment:
   Is swallowing exception what we expect?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketDelayedDeliveryTracker.java:
##########
@@ -91,9 +98,66 @@ public 
BucketDelayedDeliveryTracker(PersistentDispatcherMultipleConsumers dispat
         this.sharedBucketPriorityQueue = new TripleLongPriorityQueue();
         this.immutableBuckets = TreeRangeMap.create();
         this.snapshotSegmentLastIndexTable = HashBasedTable.create();
-        this.numberDelayedMessages = 0L;
         ManagedCursor cursor = dispatcher.getCursor();
         this.lastMutableBucket = new MutableBucket(cursor, 
bucketSnapshotStorage);
+        this.numberDelayedMessages = recoverBucketSnapshot();
+    }
+
+    private long recoverBucketSnapshot() throws RuntimeException {
+        ManagedCursor cursor = this.lastMutableBucket.cursor;
+        cursor.getCursorProperties().keySet().forEach(key -> {
+            if (key.startsWith(DELAYED_BUCKET_KEY_PREFIX)) {
+                String[] keys = key.split(DELIMITER);
+                checkArgument(keys.length == 3);
+                ImmutableBucket immutableBucket =
+                        new ImmutableBucket(cursor, 
this.lastMutableBucket.bucketSnapshotStorage,
+                                Long.parseLong(keys[1]), 
Long.parseLong(keys[2]));
+                
immutableBuckets.put(Range.closed(immutableBucket.startLedgerId, 
immutableBucket.endLedgerId),
+                        immutableBucket);
+            }
+        });
+
+        if (immutableBuckets.asMapOfRanges().isEmpty()) {
+            return 0;
+        }
+
+        List<CompletableFuture<Void>> futures = new 
ArrayList<>(immutableBuckets.asMapOfRanges().size());
+        for (ImmutableBucket immutableBucket : 
immutableBuckets.asMapOfRanges().values()) {
+            CompletableFuture<Void> future =
+                    
immutableBucket.asyncRecoverBucketSnapshotEntry(this::getCutoffTime).thenAccept(indexList
 -> {
+                        if (CollectionUtils.isEmpty(indexList)) {
+                            return;
+                        }
+                        DelayedIndex lastDelayedIndex = 
indexList.get(indexList.size() - 1);
+                        synchronized (this.snapshotSegmentLastIndexTable) {

Review Comment:
   Is there a race condition for this method? if yes, we'd better make this 
method thread safe to avoid reading the same entries.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to