You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by te...@apache.org on 2023/05/09 06:22:20 UTC
[pulsar] branch branch-2.11 updated: [fix][client] Fix NPE when acknowledging multiple messages (#19874)
This is an automated email from the ASF dual-hosted git repository.
technoboy pushed a commit to branch branch-2.11
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/branch-2.11 by this push:
new 0413eb869d2 [fix][client] Fix NPE when acknowledging multiple messages (#19874)
0413eb869d2 is described below
commit 0413eb869d20c4c55efe406ff09bca21d156d087
Author: Yunze Xu <xy...@163.com>
AuthorDate: Mon Mar 27 14:24:18 2023 +0800
[fix][client] Fix NPE when acknowledging multiple messages (#19874)
---
.../pulsar/client/api/ConsumerAckListTest.java | 39 ++++++++++++++++++++++
.../client/impl/MultiTopicsConsumerImpl.java | 13 ++++++--
2 files changed, 50 insertions(+), 2 deletions(-)
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerAckListTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerAckListTest.java
index e101a513c37..85d3ae04ae8 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerAckListTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerAckListTest.java
@@ -111,4 +111,43 @@ public class ConsumerAckListTest extends ProducerConsumerBase {
latch.await();
}
+ @Test(timeOut = 30000)
+ public void testAckMessageInAnotherTopic() throws Exception {
+ final String[] topics = {
+ "persistent://my-property/my-ns/test-ack-message-in-other-topic1" + UUID.randomUUID(),
+ "persistent://my-property/my-ns/test-ack-message-in-other-topic2" + UUID.randomUUID(),
+ "persistent://my-property/my-ns/test-ack-message-in-other-topic3" + UUID.randomUUID()
+ };
+ @Cleanup final Consumer<String> allTopicsConsumer = pulsarClient.newConsumer(Schema.STRING)
+ .topic(topics)
+ .subscriptionName("sub1")
+ .subscribe();
+ Consumer<String> partialTopicsConsumer = pulsarClient.newConsumer(Schema.STRING)
+ .topic(topics[0], topics[1])
+ .subscriptionName("sub2")
+ .subscribe();
+ for (int i = 0; i < topics.length; i++) {
+ final Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
+ .topic(topics[i])
+ .create();
+ producer.send("msg-" + i);
+ producer.close();
+ }
+ final List<MessageId> messageIdList = new ArrayList<>();
+ for (int i = 0; i < topics.length; i++) {
+ messageIdList.add(allTopicsConsumer.receive().getMessageId());
+ }
+ try {
+ partialTopicsConsumer.acknowledge(messageIdList);
+ Assert.fail();
+ } catch (PulsarClientException.NotConnectedException ignored) {
+ }
+ partialTopicsConsumer.close();
+ partialTopicsConsumer = pulsarClient.newConsumer(Schema.STRING).topic(topics[0])
+ .subscriptionName("sub2").subscribe();
+ pulsarClient.newProducer(Schema.STRING).topic(topics[0]).create().send("done");
+ final Message<String> msg = partialTopicsConsumer.receive();
+ Assert.assertEquals(msg.getValue(), "msg-0");
+ partialTopicsConsumer.close();
+ }
}
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
index 89190376092..92551f2055b 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
@@ -32,6 +32,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -539,8 +540,16 @@ public class MultiTopicsConsumerImpl<T> extends ConsumerBase<T> {
topicToMessageIdMap.putIfAbsent(topicMessageId.getTopicPartitionName(), new ArrayList<>());
topicToMessageIdMap.get(topicMessageId.getTopicPartitionName()).add(topicMessageId.getInnerMessageId());
}
- topicToMessageIdMap.forEach((topicPartitionName, messageIds) -> {
- ConsumerImpl<T> consumer = consumers.get(topicPartitionName);
+ final Map<ConsumerImpl<T>, List<MessageId>> consumerToMessageIds = new IdentityHashMap<>();
+ for (Map.Entry<String, List<MessageId>> entry : topicToMessageIdMap.entrySet()) {
+ ConsumerImpl<T> consumer = consumers.get(entry.getKey());
+ if (consumer == null) {
+ return FutureUtil.failedFuture(new PulsarClientException.NotConnectedException());
+ }
+ // Trigger the acknowledgment later to avoid sending partial acknowledgments
+ consumerToMessageIds.put(consumer, entry.getValue());
+ }
+ consumerToMessageIds.forEach((consumer, messageIds) -> {
resultFutures.add(consumer.doAcknowledgeWithTxn(messageIds, ackType, properties, txn)
.thenAccept((res) -> messageIdList.forEach(unAckedMessageTracker::remove)));
});