You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/19 04:36:50 UTC

[GitHub] [pulsar] codelipenghui commented on a diff in pull request #18420: [feat][broker][PIP-195] Implement delayed message bucket snapshot recover - part5

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


##########
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 {

Review Comment:
   Is it better to add a method `CompletableFuture<Void> initialize()` to the `DelayedDeliveryTracker` to avoid blocking calls when creating the `BucketDelayedDeliveryTracker` instance



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/ImmutableBucket.java:
##########
@@ -54,7 +65,28 @@ CompletableFuture<List<DelayedIndex>> asyncLoadNextBucketSnapshotEntry(boolean i
             final long bucketId = getAndUpdateBucketId();
             CompletableFuture<Integer> loadMetaDataFuture = new CompletableFuture<>();
             if (isRecover) {
-                // TODO Recover bucket snapshot
+                final long cutoffTime = cutoffTimeSupplier.get();
+                // Load Metadata of bucket snapshot
+                bucketSnapshotStorage.getBucketSnapshotMetadata(bucketId).thenAccept(snapshotMetadata -> {
+                    List<DelayedMessageIndexBucketSnapshotFormat.SnapshotSegmentMetadata> metadataList =
+                            snapshotMetadata.getMetadataListList();
+
+                    // Skip all already reach schedule time snapshot segments
+                    int nextSnapshotEntryIndex = 0;
+                    while (nextSnapshotEntryIndex < metadataList.size()
+                            && metadataList.get(nextSnapshotEntryIndex).getMaxScheduleTimestamp() <= cutoffTime) {
+                        nextSnapshotEntryIndex++;
+                    }
+
+                    this.setLastSegmentEntryId(metadataList.size());
+                    this.recoverDelayedIndexBitMapAndNumber(nextSnapshotEntryIndex, metadataList);
+
+                    int nextSegmentEntryId = nextSnapshotEntryIndex + 1;
+                    loadMetaDataFuture.complete(nextSegmentEntryId);
+                }).exceptionally(ex -> {
+                    loadMetaDataFuture.completeExceptionally(ex);

Review Comment:
   It looks like we can simplify the logic to avoid creating the `loadMetaDataFuture`.
   It will make the code easy to read.



-- 
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