poorbarcode commented on code in PR #19138:
URL: https://github.com/apache/pulsar/pull/19138#discussion_r1066530809


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/CombinedSegmentDelayedIndexQueue.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.delayed.bucket;
+
+import java.util.List;
+import javax.annotation.concurrent.NotThreadSafe;
+import 
org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.DelayedIndex;
+import 
org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment;
+
+@NotThreadSafe
+public class CombinedSegmentDelayedIndexQueue implements DelayedIndexQueue {

Review Comment:
   I notice that this class is used to make `peek` and `pop` easier to use, and 
it is only used by merging two queues in `BucketDelayedDeliveryTracker`, should 
it not exist as a separate public class?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/TripleLongPriorityDelayedIndexQueue.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.delayed.bucket;
+
+import javax.annotation.concurrent.NotThreadSafe;
+import 
org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat;
+import org.apache.pulsar.common.util.collections.TripleLongPriorityQueue;
+
+@NotThreadSafe
+public class TripleLongPriorityDelayedIndexQueue implements DelayedIndexQueue {

Review Comment:
   I notice that this class is used to make `peek` and `pop` easier to use, and 
it is only used by `MutableBucket`, should it not exist as a separate public 
class?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/DelayedIndexQueue.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.delayed.bucket;
+
+import java.util.Comparator;
+import java.util.Objects;
+import 
org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat;
+
+public interface DelayedIndexQueue {

Review Comment:
   This interface is only used by `MutableBucket` and is not universal. Is this 
interface not needed?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketDelayedDeliveryTracker.java:
##########
@@ -243,6 +263,53 @@ public synchronized boolean addMessage(long ledgerId, long 
entryId, long deliver
         return true;
     }
 
+    private synchronized CompletableFuture<Void> asyncMergeBucketSnapshot() {
+        List<ImmutableBucket> values = 
immutableBuckets.asMapOfRanges().values().stream().toList();
+        long minNumberMessages = Long.MAX_VALUE;
+        int minIndex = -1;
+        for (int i = 0; i + 1 < values.size(); i++) {
+            ImmutableBucket bucketL = values.get(i);
+            ImmutableBucket bucketR = values.get(i + 1);
+            long numberMessages = bucketL.numberBucketDelayedMessages + 
bucketR.numberBucketDelayedMessages;
+            if (numberMessages < minNumberMessages) {
+                minNumberMessages = (int) numberMessages;
+                minIndex = i;
+            }
+        }
+        return asyncMergeBucketSnapshot(values.get(minIndex), 
values.get(minIndex + 1));
+    }
+
+    private synchronized CompletableFuture<Void> 
asyncMergeBucketSnapshot(ImmutableBucket bucketA,
+                                                                          
ImmutableBucket bucketB) {
+        immutableBuckets.remove(Range.closed(bucketA.startLedgerId, 
bucketA.endLedgerId));
+        immutableBuckets.remove(Range.closed(bucketB.startLedgerId, 
bucketB.endLedgerId));
+
+        CompletableFuture<Long> snapshotCreateFutureA =
+                
bucketA.getSnapshotCreateFuture().orElse(CompletableFuture.completedFuture(null));
+        CompletableFuture<Long> snapshotCreateFutureB =
+                
bucketB.getSnapshotCreateFuture().orElse(CompletableFuture.completedFuture(null));
+
+        return CompletableFuture.allOf(snapshotCreateFutureA, 
snapshotCreateFutureB).thenCompose(__ -> {
+            
CompletableFuture<List<DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment>>
 futureA =
+                    bucketA.getRemainSnapshotSegment();
+            
CompletableFuture<List<DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment>>
 futureB =
+                    bucketB.getRemainSnapshotSegment();
+            return futureA.thenCombine(futureB, 
CombinedSegmentDelayedIndexQueue::wrap)
+                    .thenCompose(combinedDelayedIndexQueue -> {
+                        CompletableFuture<Void> removeAFuture = 
bucketA.asyncDeleteBucketSnapshot();
+                        CompletableFuture<Void> removeBFuture = 
bucketB.asyncDeleteBucketSnapshot();
+
+                        return CompletableFuture.allOf(removeAFuture, 
removeBFuture).thenRun(() -> {

Review Comment:
   We do save the newly merged bucket after finished delete the original 
buckets, it may be caused bucket loss. I think losing buckets is tolerable, 
right?



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