Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][broker] Move bitmap from lastMutableBucket to ImmutableBucket #20156

Merged
merged 7 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private synchronized long recoverBucketSnapshot() throws RuntimeException {
}

try {
FutureUtil.waitForAll(futures.values()).get(AsyncOperationTimeoutSeconds * 2, TimeUnit.SECONDS);
FutureUtil.waitForAll(futures.values()).get(AsyncOperationTimeoutSeconds * 5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.error("[{}] Failed to recover delayed message index bucket snapshot.", dispatcher.getName(), e);
if (e instanceof InterruptedException) {
Expand Down Expand Up @@ -343,6 +343,7 @@ public synchronized boolean addMessage(long ledgerId, long entryId, long deliver
// If (ledgerId < startLedgerId || existBucket) means that message index belong to previous bucket range,
// enter sharedBucketPriorityQueue directly
sharedBucketPriorityQueue.add(deliverAt, ledgerId, entryId);
lastMutableBucket.putIndexBit(ledgerId, entryId);
} else {
checkArgument(ledgerId >= lastMutableBucket.endLedgerId);
lastMutableBucket.addMessage(ledgerId, entryId, deliverAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.pulsar.broker.delayed.bucket;

import static com.google.common.base.Preconditions.checkArgument;
import com.google.protobuf.ByteString;
import com.google.protobuf.UnsafeByteOperations;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -74,6 +74,8 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(

List<SnapshotSegment> bucketSnapshotSegments = new ArrayList<>();
List<SnapshotSegmentMetadata> segmentMetadataList = new ArrayList<>();
Map<Long, RoaringBitmap> immutableBucketBitMap = new HashMap<>();

Map<Long, RoaringBitmap> bitMap = new HashMap<>();
SnapshotSegment snapshotSegment = new SnapshotSegment();
SnapshotSegmentMetadata.Builder segmentMetadataBuilder = SnapshotSegmentMetadata.newBuilder();
Expand All @@ -82,18 +84,20 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(
long currentTimestampUpperLimit = 0;
long currentFirstTimestamp = 0L;
while (!delayedIndexQueue.isEmpty()) {
DelayedIndex delayedIndex = snapshotSegment.addIndexe();
delayedIndexQueue.popToObject(delayedIndex);

long timestamp = delayedIndex.getTimestamp();
final long timestamp = delayedIndexQueue.peekTimestamp();
if (currentTimestampUpperLimit == 0) {
currentFirstTimestamp = timestamp;
firstScheduleTimestamps.add(currentFirstTimestamp);
currentTimestampUpperLimit = timestamp + timeStepPerBucketSnapshotSegment - 1;
}

long ledgerId = delayedIndex.getLedgerId();
long entryId = delayedIndex.getEntryId();
DelayedIndex delayedIndex = snapshotSegment.addIndexe();
delayedIndexQueue.popToObject(delayedIndex);

final long ledgerId = delayedIndex.getLedgerId();
final long entryId = delayedIndex.getEntryId();

removeIndexBit(ledgerId, entryId);

checkArgument(ledgerId >= startLedgerId && ledgerId <= endLedgerId);

Expand All @@ -102,10 +106,10 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(
sharedQueue.add(timestamp, ledgerId, entryId);
}

numMessages++;

bitMap.computeIfAbsent(ledgerId, k -> new RoaringBitmap()).add(entryId, entryId + 1);

numMessages++;

if (delayedIndexQueue.isEmpty() || delayedIndexQueue.peekTimestamp() > currentTimestampUpperLimit
|| (maxIndexesPerBucketSnapshotSegment != -1
&& snapshotSegment.getIndexesCount() >= maxIndexesPerBucketSnapshotSegment)) {
Expand All @@ -119,9 +123,17 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(
final var lId = entry.getKey();
final var bm = entry.getValue();
bm.runOptimize();
final var array = new byte[bm.serializedSizeInBytes()];
bm.serialize(ByteBuffer.wrap(array));
segmentMetadataBuilder.putDelayedIndexBitMap(lId, ByteString.copyFrom(array));
ByteBuffer byteBuffer = ByteBuffer.allocate(bm.serializedSizeInBytes());
bm.serialize(byteBuffer);
byteBuffer.flip();
segmentMetadataBuilder.putDelayedIndexBitMap(lId, UnsafeByteOperations.unsafeWrap(byteBuffer));
immutableBucketBitMap.compute(lId, (__, bm0) -> {
if (bm0 == null) {
return bm;
}
bm0.or(bm);
return bm0;
});
iterator.remove();
}

Expand All @@ -133,6 +145,10 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(
}
}

// optimize bm
immutableBucketBitMap.values().forEach(RoaringBitmap::runOptimize);
this.delayedIndexBitMap.values().forEach(RoaringBitmap::runOptimize);

SnapshotMetadata bucketSnapshotMetadata = SnapshotMetadata.newBuilder()
.addAllMetadataList(segmentMetadataList)
.build();
Expand All @@ -145,6 +161,7 @@ Pair<ImmutableBucket, DelayedIndex> createImmutableBucketAndAsyncPersistent(
bucket.setNumberBucketDelayedMessages(numMessages);
bucket.setLastSegmentEntryId(lastSegmentEntryId);
bucket.setFirstScheduleTimestamps(firstScheduleTimestamps);
bucket.setDelayedIndexBitMap(immutableBucketBitMap);

// Skip first segment, because it has already been loaded
List<SnapshotSegment> snapshotSegments = bucketSnapshotSegments.subList(1, bucketSnapshotSegments.size());
Expand Down