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][PIP-286] Make the TopicCompactionService to support find entry based on publishTime or index #21208

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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;


Expand Down Expand Up @@ -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 -> {
Copy link
Member

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?

return Commands.parseMessageMetadata(entry.getDataBuffer()).getPublishTime() >= publishTime;
};
return findFirstMatchEntry(predicate);
}

@Override
public CompletableFuture<Entry> findEntryByEntryIndex(long entryIndex) {
final Predicate<Entry> predicate = entry -> {
Copy link
Member

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 entry index is minus value?

Copy link
Member Author

@coderzc coderzc Oct 16, 2023

Choose a reason for hiding this comment

The 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.pulsar.compaction.Compactor.COMPACTED_TOPIC_LEDGER_PROPERTY;
import static org.apache.pulsar.compaction.Compactor.COMPACTION_SUBSCRIPTION;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.IOException;
import java.util.List;
Expand All @@ -40,8 +41,11 @@
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.impl.MessageImpl;
import org.apache.pulsar.common.api.proto.BrokerEntryMetadata;
import org.apache.pulsar.common.api.proto.MessageMetadata;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.apache.pulsar.common.protocol.Commands;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -55,6 +59,11 @@ public class TopicCompactionServiceTest extends MockedPulsarServiceBaseTest {
@BeforeMethod
@Override
public void setup() throws Exception {
conf.setBrokerEntryMetadataInterceptors(org.assertj.core.util.Sets.newTreeSet(
"org.apache.pulsar.common.intercept.AppendIndexMetadataInterceptor"
));
conf.setExposingBrokerEntryMetadataToClientEnabled(true);

super.internalSetup();

admin.clusters().createCluster("test", ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress()).build());
Expand Down Expand Up @@ -93,6 +102,8 @@ public void test() throws PulsarClientException, PulsarAdminException {
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.create();

long startTime = System.currentTimeMillis();

producer.newMessage()
.key("a")
.value("A_1".getBytes())
Expand Down Expand Up @@ -151,5 +162,21 @@ public void test() throws PulsarClientException, PulsarAdminException {

List<Entry> entries2 = service.readCompactedEntries(PositionImpl.EARLIEST, 1).join();
assertEquals(entries2.size(), 1);

Entry entry = service.findEntryByEntryIndex(3).join();
BrokerEntryMetadata brokerEntryMetadata = Commands.peekBrokerEntryMetadataIfExist(entry.getDataBuffer());
assertNotNull(brokerEntryMetadata);
assertEquals(brokerEntryMetadata.getIndex(), 4);
MessageMetadata metadata = Commands.parseMessageMetadata(entry.getDataBuffer());
assertEquals(metadata.getPartitionKey(), "b");
entry.release();

entry = service.findEntryByPublishTime(startTime).join();
brokerEntryMetadata = Commands.peekBrokerEntryMetadataIfExist(entry.getDataBuffer());
assertNotNull(brokerEntryMetadata);
assertEquals(brokerEntryMetadata.getIndex(), 2);
metadata = Commands.parseMessageMetadata(entry.getDataBuffer());
assertEquals(metadata.getPartitionKey(), "a");
entry.release();
}
}
Loading