question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manual commit - acknowledge() returns void and CommitFailedException happens after acknowledge() has already been called

See original GitHub issue

Hello,

I am currently trying to write some code to prevent duplicate processing on the consumer side as mentioned in #858.

On the consumer side, there are no exactly once guarantees; you are on your own to implement idempotency; but this is relatively easy, just store the topic/partition/offset along with the data and check that you haven’t already processed this record. The topic/partition/offset are available in the Message<?> headers.

So I am implementing using manual commit. As per spring cloud stream kafka binder documentation, I am doing something very similar:

 @StreamListener(Sink.INPUT)
 public void process(Message<?> message) {
     Acknowledgment acknowledgment = message.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class);
     if (acknowledgment != null) {
         System.out.println("Acknowledgment provided");
         acknowledgment.acknowledge();
     }
 }

When I am debugging (on IDE) through my @StreamListener and manual acking, I sometimes see CommitFailedException due to rebalancing some time after acknowledgement.acknowledge() has already been stepped through. I know that CommitFailedException is related to configuration such as max.poll.interval.ms and others. I am going to set proper configs. But at the same time, I want to be able to handle this in code as well. And I am not using batch-mode, so I am processing 1 record at a time.

Knowing that acknowledge() method returns void, Is it possible to catch Exceptions that happen during/after acknowledge() ?

I think it would be nice and easy to do something like

try {
   ack.acknowledge();
}
catch (Exception e) {
    ... handle exception ...
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jskim1991commented, Mar 13, 2020

@garyrussell Using AbstractMessageListenerContainer allows me to catch exceptions after calling acknowledge(). Once again, thank you so much.

0reactions
jskim1991commented, Mar 13, 2020

@garyrussell here is my minimum set of a sample I have been testing all day just for reference. https://github.com/jskim1991/kafka-test-manual-commit/tree/master

Read more comments on GitHub >

github_iconTop Results From Across the Web

kafka Offset commit failing org.apache.kafka.clients.consumer ...
If there is no processing then after processing a single record , I call ack.acknowledge() which should commit the offset after 2 sec....
Read more >
Solution for Kafka CommitFailedException - Medium
In this article, I'll explain how we resolved the CommitFailedException that was frequently occurring in our Kafka Consumer applications.
Read more >
KafkaProducer (kafka 2.6.0 API)
A Kafka client that publishes records to the Kafka cluster. The producer is thread safe and sharing a single producer instance across threads...
Read more >
Chapter 4. Kafka Consumers: Reading Data from Kafka
Suppose you have an application that needs to read messages from a Kafka topic, ... The default is 1 MB, which means that...
Read more >
Acknowledgment (Spring for Apache Kafka 3.0.0 API)
acknowledge. void acknowledge(). Invoked when the record or batch for which the acknowledgment has been created has been processed. Calling this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found