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] Fix negative ack not redelivery. #15312

Merged
merged 2 commits into from
Apr 26, 2022

Conversation

Technoboy-
Copy link
Contributor

@Technoboy- Technoboy- commented Apr 25, 2022

Motivation

When consumer subscribes to more than one topic, if uses negative API to redelivery msg, it will fail with the below error:

java.lang.IllegalArgumentException: null
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:131) ~[guava-31.0.1-jre.jar:?]
	at org.apache.pulsar.client.impl.ConsumerImpl.redeliverUnacknowledgedMessages(ConsumerImpl.java:1901) ~[classes/:?]
	at org.apache.pulsar.client.impl.NegativeAcksTracker.triggerRedelivery(NegativeAcksTracker.java:82) ~[classes/:?]

Because if subscribes with multi-topics, the internal message will be TopicMessageIdImpl and add to NegativeAcksTracker without conversion to MessageIdImpl(user not config with negativeAckRedeliveryBackoff):

public synchronized void add(MessageId messageId) {
if (messageId instanceof BatchMessageIdImpl) {
BatchMessageIdImpl batchMessageId = (BatchMessageIdImpl) messageId;
messageId = new MessageIdImpl(batchMessageId.getLedgerId(), batchMessageId.getEntryId(),
batchMessageId.getPartitionIndex());
}
if (nackedMessages == null) {
nackedMessages = new HashMap<>();
}
nackedMessages.put(messageId, System.nanoTime() + nackDelayNanos);
if (this.timeout == null) {
// Schedule a task and group all the redeliveries for same period. Leave a small buffer to allow for
// nack immediately following the current one will be batched into the same redeliver request.
this.timeout = timer.newTimeout(this::triggerRedelivery, timerIntervalNanos, TimeUnit.NANOSECONDS);
}
}
public synchronized void add(Message<?> message) {
if (negativeAckRedeliveryBackoff == null) {
add(message.getMessageId());
return;
}
add(message.getMessageId(), message.getRedeliveryCount());
}

Then NegativeAcksTracker will trigger redeliver and throw IllegalArgumentException at here :

public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
if (messageIds.isEmpty()) {
return;
}
checkArgument(messageIds.stream().findFirst().get() instanceof MessageIdImpl);

The issue seems introduced by #12566.

Documentation

  • no-need-doc
    (Please explain why)

Reproduce Test

@Test
    @SneakyThrows
    public void testNegativeAcks() {
        String topic = BrokerTestUtil.newUniqueName("testNegativeAcks");
        @Cleanup
        Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
                .topic(topic)
                .subscriptionName("sub1")
                .subscriptionType(SubscriptionType.Shared)
                .enableRetry(true)
                .negativeAckRedeliveryDelay(1, TimeUnit.SECONDS)
                .subscribe();

        @Cleanup
        Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
                .topic(topic)
                .create();

        producer.sendAsync("test-ack");
        producer.flush();
        while(true) {
            Message<String> msg1 = consumer.receive();
            System.out.println("received : " + msg1);
            consumer.negativeAcknowledge(msg1);
        }
    }

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Apr 25, 2022
@Technoboy- Technoboy- added type/bug The PR fixed a bug or issue reported a bug area/client release/2.10.1 labels Apr 25, 2022
@Technoboy- Technoboy- added this to the 2.11.0 milestone Apr 25, 2022
@lhotari
Copy link
Member

lhotari commented Apr 25, 2022

@Technoboy- can you check #6869 , does this PR fix that issue?

@Technoboy-
Copy link
Contributor Author

@Technoboy- can you check #6869 , does this PR fix that issue?

It's not related to that issue. This bug was introduced by #12566.

@lhotari
Copy link
Member

lhotari commented Apr 25, 2022

It's not related to that issue. This bug was introduced by #12566.

thanks. btw. would you be interested in fixing #6869 ? That's a long time issue with negative acknowledgements and batch messages.

Copy link
Member

@lhotari lhotari left a comment

Choose a reason for hiding this comment

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

LGTM

@Technoboy-
Copy link
Contributor Author

It's not related to that issue. This bug was introduced by #12566.

thanks. btw. would you be interested in fixing #6869 ? That's a long time issue with negative acknowledgements and batch messages.

I will work on it.

@Technoboy- Technoboy- merged commit 9f6532a into apache:master Apr 26, 2022
@Technoboy-
Copy link
Contributor Author

It's not related to that issue. This bug was introduced by #12566.

thanks. btw. would you be interested in fixing #6869 ? That's a long time issue with negative acknowledgements and batch messages.

Hi @lhotari , I think #6869 has been fixed by #9440.

codelipenghui pushed a commit that referenced this pull request Apr 28, 2022
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request May 9, 2022
(cherry picked from commit 9f6532a)
(cherry picked from commit 536c891)
@Technoboy- Technoboy- deleted the fix-neg-ack-bug branch August 10, 2022 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client cherry-picked/branch-2.10 doc-not-needed Your PR changes do not impact docs release/2.10.1 type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants