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

[fix][client] Copy eventTime to retry letter topic and DLQ messages #24059

Merged
merged 3 commits into from
Mar 5, 2025
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 @@ -25,6 +25,7 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -261,6 +262,67 @@ public void testDeadLetterTopicMessagesWithOrderingKey() throws Exception {
consumer.close();
}

@Test
public void testDeadLetterTopicMessagesWithEventTime() throws Exception {
final String topic = "persistent://my-property/my-ns/dead-letter-topic";

final int maxRedeliveryCount = 1;

final int sendMessages = 100;

Consumer<byte[]> consumer = pulsarClient.newConsumer(Schema.BYTES)
.topic(topic)
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Shared)
.ackTimeout(1, TimeUnit.SECONDS)
.deadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(maxRedeliveryCount).build())
.receiverQueueSize(100)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();

@Cleanup
PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection
Consumer<byte[]> deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES)
.topic("persistent://my-property/my-ns/dead-letter-topic-my-subscription-DLQ")
.subscriptionName("my-subscription")
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();

Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES)
.topic(topic)
.create();

long testEventTime = Instant.now().toEpochMilli();
for (int i = 0; i < sendMessages; i++) {
producer.newMessage()
.eventTime(testEventTime)
.value(String.format("Hello Pulsar, eventTime: [%d]", testEventTime).getBytes())
.send();
}

producer.close();

int totalReceived = 0;
do {
Message<byte[]> message = consumer.receive();
log.info("consumer received message : {} {}", message.getMessageId(),
new String(message.getData()));
totalReceived++;
} while (totalReceived < sendMessages * (maxRedeliveryCount + 1));

int totalInDeadLetter = 0;
do {
Message<byte[]> message = deadLetterConsumer.receive();
assertEquals(message.getEventTime(), testEventTime);
log.info("dead letter consumer received message : {} {}", message.getMessageId(), new String(message.getData()));
deadLetterConsumer.acknowledge(message);
totalInDeadLetter++;
} while (totalInDeadLetter < sendMessages);

deadLetterConsumer.close();
consumer.close();
}

public void testDeadLetterTopicWithProducerName() throws Exception {
final String topic = "persistent://my-property/my-ns/dead-letter-topic";
final String subscription = "my-subscription";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -300,6 +301,7 @@ public void testRetryTopicProperties() throws Exception {

byte[] key = "key".getBytes();
byte[] orderingKey = "orderingKey".getBytes();
long eventTime = Instant.now().toEpochMilli();

final int maxRedeliveryCount = 3;

Expand Down Expand Up @@ -333,6 +335,7 @@ public void testRetryTopicProperties() throws Exception {
.value(String.format("Hello Pulsar [%d]", i).getBytes())
.keyBytes(key)
.orderingKey(orderingKey)
.eventTime(eventTime)
.send();
originMessageIds.add(msgId.toString());
}
Expand All @@ -350,6 +353,7 @@ public void testRetryTopicProperties() throws Exception {
assertEquals(message.getKeyBytes(), key);
assertTrue(message.hasOrderingKey());
assertEquals(message.getOrderingKey(), orderingKey);
assertEquals(message.getEventTime(), eventTime);
retryMessageIds.add(message.getProperty(RetryMessageUtil.SYSTEM_PROPERTY_ORIGIN_MESSAGE_ID));
}
consumer.reconsumeLater(message, 1, TimeUnit.SECONDS);
Expand All @@ -373,6 +377,7 @@ public void testRetryTopicProperties() throws Exception {
assertEquals(message.getKeyBytes(), key);
assertTrue(message.hasOrderingKey());
assertEquals(message.getOrderingKey(), orderingKey);
assertEquals(message.getEventTime(), eventTime);
deadLetterMessageIds.add(message.getProperty(RetryMessageUtil.SYSTEM_PROPERTY_ORIGIN_MESSAGE_ID));
}
deadLetterConsumer.acknowledge(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ protected CompletableFuture<Void> doReconsumeLater(Message<?> message, AckType a
.value(retryMessage.getData())
.properties(propertiesMap);
copyMessageKeysIfNeeded(message, typedMessageBuilderNew);
copyMessageEventTimeIfNeeded(message, typedMessageBuilderNew);
typedMessageBuilderNew.sendAsync().thenAccept(msgId -> {
consumerDlqMessagesCounter.increment();

Expand Down Expand Up @@ -749,6 +750,7 @@ protected CompletableFuture<Void> doReconsumeLater(Message<?> message, AckType a
typedMessageBuilderNew.deliverAfter(delayTime, unit);
}
copyMessageKeysIfNeeded(message, typedMessageBuilderNew);
copyMessageEventTimeIfNeeded(message, typedMessageBuilderNew);
typedMessageBuilderNew.sendAsync()
.thenCompose(
__ -> doAcknowledge(finalMessageId, ackType, Collections.emptyMap(), null))
Expand Down Expand Up @@ -824,6 +826,13 @@ private MessageImpl<?> getMessageImpl(Message<?> message) {
return null;
}

private static void copyMessageEventTimeIfNeeded(Message<?> message,
TypedMessageBuilder<byte[]> typedMessageBuilderNew) {
if (message.getEventTime() > 0) {
typedMessageBuilderNew.eventTime(message.getEventTime());
}
}

@Override
public void negativeAcknowledge(MessageId messageId) {
consumerNacksCounter.increment();
Expand Down Expand Up @@ -2242,6 +2251,7 @@ private CompletableFuture<Boolean> processPossibleToDLQ(MessageIdAdv messageId)
.value(message.getData())
.properties(getPropertiesMap(message, originMessageIdStr, originTopicNameStr));
copyMessageKeysIfNeeded(message, typedMessageBuilderNew);
copyMessageEventTimeIfNeeded(message, typedMessageBuilderNew);
typedMessageBuilderNew.sendAsync()
.thenAccept(messageIdInDLQ -> {
possibleSendToDeadLetterTopicMessages.remove(messageId);
Expand Down
Loading