-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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][PIP-286] Make the TopicCompactionService to support find entry based on publishTime or index #21208
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,18 +22,23 @@ | |
import static org.apache.pulsar.compaction.CompactedTopicImpl.COMPACT_LEDGER_EMPTY; | ||
import static org.apache.pulsar.compaction.CompactedTopicImpl.NEWER_THAN_COMPACTED; | ||
import static org.apache.pulsar.compaction.CompactedTopicImpl.findStartPoint; | ||
import static org.apache.pulsar.compaction.CompactedTopicImpl.readEntries; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
import javax.annotation.Nonnull; | ||
import org.apache.bookkeeper.client.BookKeeper; | ||
import org.apache.bookkeeper.client.LedgerHandle; | ||
import org.apache.bookkeeper.mledger.Entry; | ||
import org.apache.bookkeeper.mledger.Position; | ||
import org.apache.bookkeeper.mledger.impl.PositionImpl; | ||
import org.apache.pulsar.common.api.proto.BrokerEntryMetadata; | ||
import org.apache.pulsar.common.protocol.Commands; | ||
import org.apache.pulsar.common.util.FutureUtil; | ||
|
||
|
||
|
@@ -106,6 +111,76 @@ public CompletableFuture<Position> getLastCompactedPosition() { | |
return CompletableFuture.completedFuture(compactedTopic.getCompactionHorizon().orElse(null)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Entry> findEntryByPublishTime(long publishTime) { | ||
final Predicate<Entry> predicate = entry -> { | ||
return Commands.parseMessageMetadata(entry.getDataBuffer()).getPublishTime() >= publishTime; | ||
}; | ||
return findFirstMatchEntry(predicate); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Entry> findEntryByEntryIndex(long entryIndex) { | ||
final Predicate<Entry> predicate = entry -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you handle if the entry index is minus value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will return the first entry of compacted data, this is also consistent with the method definition, what do you think? |
||
BrokerEntryMetadata brokerEntryMetadata = Commands.parseBrokerEntryMetadataIfExist(entry.getDataBuffer()); | ||
if (brokerEntryMetadata == null || !brokerEntryMetadata.hasIndex()) { | ||
return false; | ||
} | ||
return brokerEntryMetadata.getIndex() >= entryIndex; | ||
}; | ||
return findFirstMatchEntry(predicate); | ||
} | ||
|
||
private CompletableFuture<Entry> findFirstMatchEntry(final Predicate<Entry> predicate) { | ||
var compactedTopicContextFuture = compactedTopic.getCompactedTopicContextFuture(); | ||
|
||
if (compactedTopicContextFuture == null) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
return compactedTopicContextFuture.thenCompose(compactedTopicContext -> { | ||
LedgerHandle lh = compactedTopicContext.getLedger(); | ||
CompletableFuture<Long> promise = new CompletableFuture<>(); | ||
findFirstMatchIndexLoop(predicate, 0L, lh.getLastAddConfirmed(), promise, null, lh); | ||
return promise.thenCompose(index -> { | ||
if (index == null) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
return readEntries(lh, index, index).thenApply(entries -> entries.get(0)); | ||
}); | ||
}); | ||
} | ||
|
||
private static void findFirstMatchIndexLoop(final Predicate<Entry> predicate, | ||
final long start, final long end, | ||
final CompletableFuture<Long> promise, | ||
final Long lastMatchIndex, | ||
final LedgerHandle lh) { | ||
if (start > end) { | ||
promise.complete(lastMatchIndex); | ||
return; | ||
} | ||
|
||
long mid = (start + end) / 2; | ||
readEntries(lh, mid, mid).thenAccept(entries -> { | ||
Entry entry = entries.get(0); | ||
final boolean isMatch; | ||
try { | ||
isMatch = predicate.test(entry); | ||
} finally { | ||
entry.release(); | ||
} | ||
|
||
if (isMatch) { | ||
findFirstMatchIndexLoop(predicate, start, mid - 1, promise, mid, lh); | ||
mattisonchao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
findFirstMatchIndexLoop(predicate, mid + 1, end, promise, lastMatchIndex, lh); | ||
} | ||
}).exceptionally(ex -> { | ||
promise.completeExceptionally(ex); | ||
return null; | ||
}); | ||
} | ||
|
||
public CompactedTopicImpl getCompactedTopic() { | ||
return compactedTopic; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,20 @@ public interface TopicCompactionService extends AutoCloseable { | |
* @return a future that will be completed with the last compacted position, this position can be null. | ||
*/ | ||
CompletableFuture<Position> getLastCompactedPosition(); | ||
|
||
/** | ||
* Find the first entry that greater or equal to target publishTime. | ||
* | ||
* @param publishTime the publish time of entry. | ||
* @return the first entry metadata that greater or equal to target publishTime, this entry can be null. | ||
*/ | ||
CompletableFuture<Entry> findEntryByPublishTime(long publishTime); | ||
|
||
/** | ||
* Find the first entry that greater or equal to target entryIndex. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It requires the broker entry metadata. So we'd better improve the Javadoc to explain the behavior of broker entry metadata missed. And please also add a test for this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I changed the test to cover this case. |
||
* | ||
* @param entryIndex the index of entry. | ||
* @return the first entry that greater or equal to target entryIndex, this entry can be null. | ||
*/ | ||
CompletableFuture<Entry> findEntryByEntryIndex(long entryIndex); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you handle if the publishTime is minus value?