Skip to content

Commit

Permalink
[improve][broker] Support not retaining null-key message during topic…
Browse files Browse the repository at this point in the history
… compaction (apache#21578)
  • Loading branch information
coderzc authored and nodece committed Feb 23, 2024
1 parent 6c2bba9 commit e2ffadb
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 34 deletions.
3 changes: 3 additions & 0 deletions conf/broker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ brokerServiceCompactionThresholdInBytes=0
# If the execution time of the compaction phase one loop exceeds this time, the compaction will not proceed.
brokerServiceCompactionPhaseOneLoopTimeInSeconds=30

# Whether retain null-key message during topic compaction
topicCompactionRemainNullKey=true

# Whether to enable the delayed delivery for messages.
# If disabled, messages will be immediately delivered and there will
# be no tracking overhead.
Expand Down
3 changes: 3 additions & 0 deletions conf/standalone.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1102,3 +1102,6 @@ zookeeperServers=
# Configuration Store connection string
# Deprecated: use configurationMetadataStoreUrl
configurationStoreServers=

# Whether retain null-key message during topic compaction
topicCompactionRemainNullKey=true
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,12 @@ public class ServiceConfiguration implements PulsarConfiguration {
)
private long brokerServiceCompactionPhaseOneLoopTimeInSeconds = 30;

@FieldContext(
category = CATEGORY_SERVER,
doc = "Whether retain null-key message during topic compaction."
)
private boolean topicCompactionRemainNullKey = true;

@FieldContext(
category = CATEGORY_SCHEMA,
doc = "Enforce schema validation on following cases:\n\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,20 @@ public static List<ImmutableTriple<MessageId, String, Integer>> extractIdsAndKey
return idsAndKeysAndSize;
}

public static Optional<RawMessage> rebatchMessage(RawMessage msg,
BiPredicate<String, MessageId> filter) throws IOException {
return rebatchMessage(msg, filter, true);
}

/**
* Take a batched message and a filter, and returns a message with the only the sub-messages
* which match the filter. Returns an empty optional if no messages match.
*
* NOTE: this message does not alter the reference count of the RawMessage argument.
*/
public static Optional<RawMessage> rebatchMessage(RawMessage msg,
BiPredicate<String, MessageId> filter)
BiPredicate<String, MessageId> filter,
boolean retainNullKey)
throws IOException {
checkArgument(msg.getMessageIdData().getBatchIndex() == -1);

Expand Down Expand Up @@ -125,9 +131,14 @@ public static Optional<RawMessage> rebatchMessage(RawMessage msg,
msg.getMessageIdData().getPartition(),
i);
if (!singleMessageMetadata.hasPartitionKey()) {
messagesRetained++;
Commands.serializeSingleMessageInBatchWithPayload(singleMessageMetadata,
singleMessagePayload, batchBuffer);
if (retainNullKey) {
messagesRetained++;
Commands.serializeSingleMessageInBatchWithPayload(singleMessageMetadata,
singleMessagePayload, batchBuffer);
} else {
Commands.serializeSingleMessageInBatchWithPayload(emptyMetadata,
Unpooled.EMPTY_BUFFER, batchBuffer);
}
} else if (filter.test(singleMessageMetadata.getPartitionKey(), id)
&& singleMessagePayload.readableBytes() > 0) {
messagesRetained++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ public class TwoPhaseCompactor extends Compactor {
private static final int MAX_OUTSTANDING = 500;
private static final String COMPACTED_TOPIC_LEDGER_PROPERTY = "CompactedTopicLedger";
private final Duration phaseOneLoopReadTimeout;
private final boolean topicCompactionRemainNullKey;

public TwoPhaseCompactor(ServiceConfiguration conf,
PulsarClient pulsar,
BookKeeper bk,
ScheduledExecutorService scheduler) {
super(conf, pulsar, bk, scheduler);
phaseOneLoopReadTimeout = Duration.ofSeconds(conf.getBrokerServiceCompactionPhaseOneLoopTimeInSeconds());
topicCompactionRemainNullKey = conf.isTopicCompactionRemainNullKey();
}

@Override
Expand Down Expand Up @@ -133,8 +135,16 @@ private void phaseOneLoop(RawReader reader,
int numMessagesInBatch = metadata.getNumMessagesInBatch();
int deleteCnt = 0;
for (ImmutableTriple<MessageId, String, Integer> e : RawBatchConverter
.extractIdsAndKeysAndSize(m, false)) {
.extractIdsAndKeysAndSize(m, true)) {
if (e != null) {
if (e.getMiddle() == null) {
if (!topicCompactionRemainNullKey) {
// record delete null-key message event
deleteCnt++;
mxBean.addCompactionRemovedEvent(reader.getTopic());
}
continue;
}
if (e.getRight() > 0) {
MessageId old = latestForKey.put(e.getMiddle(), e.getLeft());
if (old != null) {
Expand Down Expand Up @@ -164,6 +174,10 @@ private void phaseOneLoop(RawReader reader,
deletedMessage = true;
latestForKey.remove(keyAndSize.getLeft());
}
} else {
if (!topicCompactionRemainNullKey) {
deletedMessage = true;
}
}
if (replaceMessage || deletedMessage) {
mxBean.addCompactionRemovedEvent(reader.getTopic());
Expand Down Expand Up @@ -253,7 +267,7 @@ private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId>
if (RawBatchConverter.isReadableBatch(m)) {
try {
messageToAdd = RawBatchConverter.rebatchMessage(
m, (key, subid) -> subid.equals(latestForKey.get(key)));
m, (key, subid) -> subid.equals(latestForKey.get(key)), topicCompactionRemainNullKey);
} catch (IOException ioe) {
log.info("Error decoding batch for message {}. Whole batch will be included in output",
id, ioe);
Expand All @@ -262,8 +276,8 @@ private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId>
} else {
Pair<String, Integer> keyAndSize = extractKeyAndSize(m);
MessageId msg;
if (keyAndSize == null) { // pass through messages without a key
messageToAdd = Optional.of(m);
if (keyAndSize == null) {
messageToAdd = topicCompactionRemainNullKey ? Optional.of(m) : Optional.empty();
} else if ((msg = latestForKey.get(keyAndSize.getLeft())) != null
&& msg.equals(id)) { // consider message only if present into latestForKey map
if (keyAndSize.getRight() <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class PendingAckPersistentTest extends TransactionTestBase {

private static final int NUM_PARTITIONS = 16;

@BeforeMethod
@BeforeMethod(alwaysRun = true)
public void setup() throws Exception {
setUpBase(1, NUM_PARTITIONS, PENDING_ACK_REPLAY_TOPIC, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -570,7 +572,7 @@ public void testBatchMessageWithNullValue() throws Exception {

// compact the topic
Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
compactor.compact(topic).join();
compactor.compact(topic).get();

// Read messages before compaction to get ids
List<Message<byte[]>> messages = new ArrayList<>();
Expand Down Expand Up @@ -628,8 +630,16 @@ public void testWholeBatchCompactedOut() throws Exception {
}
}

@Test
public void testKeyLessMessagesPassThrough() throws Exception {
@DataProvider(name = "retainNullKey")
public static Object[][] retainNullKey() {
return new Object[][] {{true}, {false}};
}

@Test(dataProvider = "retainNullKey")
public void testKeyLessMessagesPassThrough(boolean retainNullKey) throws Exception {
conf.setTopicCompactionRemainNullKey(retainNullKey);
restartBroker();

String topic = "persistent://my-property/use/my-ns/my-topic1";

// subscribe before sending anything, so that we get all messages
Expand Down Expand Up @@ -659,29 +669,25 @@ public void testKeyLessMessagesPassThrough() throws Exception {

try (Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic)
.subscriptionName("sub1").readCompacted(true).subscribe()){
Message<byte[]> message1 = consumer.receive();
Assert.assertFalse(message1.hasKey());
Assert.assertEquals(new String(message1.getData()), "my-message-1");

Message<byte[]> message2 = consumer.receive();
Assert.assertFalse(message2.hasKey());
Assert.assertEquals(new String(message2.getData()), "my-message-2");

Message<byte[]> message3 = consumer.receive();
Assert.assertEquals(message3.getKey(), "key1");
Assert.assertEquals(new String(message3.getData()), "my-message-4");

Message<byte[]> message4 = consumer.receive();
Assert.assertEquals(message4.getKey(), "key2");
Assert.assertEquals(new String(message4.getData()), "my-message-6");

Message<byte[]> message5 = consumer.receive();
Assert.assertFalse(message5.hasKey());
Assert.assertEquals(new String(message5.getData()), "my-message-7");
List<Pair<String, String>> result = new ArrayList<>();
while (true) {
Message<byte[]> message = consumer.receive(10, TimeUnit.SECONDS);
if (message == null) {
break;
}
result.add(Pair.of(message.getKey(), message.getData() == null ? null : new String(message.getData())));
}

Message<byte[]> message6 = consumer.receive();
Assert.assertFalse(message6.hasKey());
Assert.assertEquals(new String(message6.getData()), "my-message-8");
List<Pair<String, String>> expectList;
if (retainNullKey) {
expectList = Lists.newArrayList(
Pair.of(null, "my-message-1"), Pair.of(null, "my-message-2"),
Pair.of("key1", "my-message-4"), Pair.of("key2", "my-message-6"),
Pair.of(null, "my-message-7"), Pair.of(null, "my-message-8"));
} else {
expectList = Lists.newArrayList(Pair.of("key1", "my-message-4"), Pair.of("key2", "my-message-6"));
}
Assert.assertEquals(result, expectList);
}
}

Expand Down

0 comments on commit e2ffadb

Please sign in to comment.