Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -473,6 +473,11 @@ private CompletableFuture<Long> individualAckNormal(CommandAck ack, Map<String,
long ackedCount = 0;
long batchSize = getBatchSize(msgId);
Consumer ackOwnerConsumer = getAckOwnerConsumer(msgId.getLedgerId(), msgId.getEntryId());
if (Subscription.isIndividualAckMode(ackOwnerConsumer.subType())
&& !ackOwnerConsumer.getPendingAcks().containsKey(msgId.getLedgerId(), msgId.getEntryId())) {
// this message has been acked, skip to keep stats correct
continue;
}
Comment on lines +476 to +480

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ackOwnerConsumer can be a different consumer.

private Consumer getAckOwnerConsumer(long ledgerId, long entryId) {
    Consumer ackOwnerConsumer = this;
    if (Subscription.isIndividualAckMode(subType)) {
        if (!getPendingAcks().containsKey(ledgerId, entryId)) {
            for (Consumer consumer : subscription.getConsumers()) {
                if (consumer != this && consumer.getPendingAcks().containsKey(ledgerId, entryId)) {
                    ackOwnerConsumer = consumer;
                    break;
                }
            }
        }
    }
    return ackOwnerConsumer;
}

We will still have the issue that the unacked messages count is decreased for this consumer, but actually the owner consumer is another consumer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, if the actual consumer is another consumer, addAndGetUnAckedMsgs can update the unack count of that consumer:

private int addAndGetUnAckedMsgs(Consumer consumer, int ackedMessages) {
int unackedMsgs = 0;
if (Subscription.isIndividualAckMode(subType)) {
subscription.addUnAckedMessages(ackedMessages);
unackedMsgs = UNACKED_MESSAGES_UPDATER.addAndGet(consumer, ackedMessages);
}
if (unackedMsgs < 0 && System.currentTimeMillis() - negtiveUnackedMsgsTimestamp >= 10_000) {
negtiveUnackedMsgsTimestamp = System.currentTimeMillis();
log.warn("unackedMsgs is : {}, ackedMessages : {}, consumer : {}", unackedMsgs, ackedMessages, consumer);
}
return unackedMsgs;
}

if (msgId.getAckSetsCount() > 0) {
long[] ackSets = new long[msgId.getAckSetsCount()];
for (int j = 0; j < msgId.getAckSetsCount(); j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,30 @@ public void testAvgMessagesPerEntry() throws Exception {
int avgMessagesPerEntry = consumerStats.getAvgMessagesPerEntry();
assertEquals(3, avgMessagesPerEntry);
}


@Test
public void testDuplicateAcknowledgement()
throws PulsarClientException, PulsarAdminException {
final String topicName = "persistent://public/default/duplicated-acknowledgement-test";
@Cleanup
Producer<byte[]> producer1 = pulsarClient.newProducer()
.topic(topicName)
.create();
@Cleanup
Consumer<byte[]> consumer1 = pulsarClient.newConsumer()
.topic(topicName)
.subscriptionName("sub-1")
.acknowledgmentGroupTime(0, TimeUnit.SECONDS)
.subscriptionType(SubscriptionType.Shared)
.subscribe();
producer1.send("1".getBytes());
Message<byte[]> message = consumer1.receive();
assertEquals(admin.topics().getStats(topicName).getSubscriptions()
.get("sub-1").getUnackedMessages(), 1);
consumer1.acknowledge(message);
consumer1.acknowledge(message);
assertEquals(admin.topics().getStats(topicName).getSubscriptions()
.get("sub-1").getUnackedMessages(), 0);
}
}