-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to commit offset in case of serialization errors
- Loading branch information
Showing
2 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
.../micronaut/configuration/kafka/exceptions/DefaultKafkaListenerExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package io.micronaut.configuration.kafka.exceptions | ||
|
||
import io.micronaut.context.annotation.Property | ||
import org.apache.kafka.clients.consumer.OffsetAndMetadata | ||
import org.apache.kafka.clients.producer.ProducerConfig | ||
import io.micronaut.configuration.kafka.AbstractEmbeddedServerSpec | ||
import io.micronaut.configuration.kafka.annotation.KafkaClient | ||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Requires | ||
import org.apache.kafka.common.TopicPartition | ||
|
||
import static io.micronaut.configuration.kafka.annotation.OffsetReset.EARLIEST | ||
import static io.micronaut.configuration.kafka.annotation.OffsetStrategy.DISABLED | ||
|
||
class DefaultKafkaListenerExceptionHandlerSpec extends AbstractEmbeddedServerSpec { | ||
|
||
private static final String TEST_TOPIC = "uuids" | ||
|
||
void "test seek past record on deserialization error by default"() { | ||
given: | ||
StringProducer stringProducer = context.getBean(StringProducer) | ||
DefaultBehaviorOnDeserializationErrorConsumer consumer = context.getBean(DefaultBehaviorOnDeserializationErrorConsumer) | ||
|
||
when: "A producer sends a message with wrong serialization" | ||
stringProducer.sendMessage("not-a-uuid") | ||
|
||
then: "The message is skipped with a seek() but not committed" | ||
conditions.eventually { | ||
consumer.currentPosition == 1 | ||
consumer.committedOffset == 0 | ||
} | ||
} | ||
|
||
void "test commit record on deserialization error"() { | ||
given: | ||
StringProducer stringProducer = context.getBean(StringProducer) | ||
CommitOnDeserializationErrorConsumer consumer = context.getBean(CommitOnDeserializationErrorConsumer) | ||
|
||
when: "A producer sends a message with wrong serialization" | ||
stringProducer.sendMessage("not-a-uuid") | ||
|
||
then: "The message is skipped and committed" | ||
conditions.eventually { | ||
consumer.currentPosition == 1 | ||
consumer.committedOffset == 1 | ||
} | ||
} | ||
|
||
void "test do nothing on deserialization error"() { | ||
given: | ||
StringProducer stringProducer = context.getBean(StringProducer) | ||
DoNothingOnDeserializationErrorConsumer consumer = context.getBean(DoNothingOnDeserializationErrorConsumer) | ||
|
||
when: "A producer sends a message with wrong serialization" | ||
stringProducer.sendMessage("not-a-uuid") | ||
|
||
then: "The message is neither skipped nor committed" | ||
conditions.eventually { | ||
consumer.currentPosition == 0 | ||
consumer.committedOffset == 0 | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'DefaultKafkaListenerExceptionHandlerSpec') | ||
@KafkaListener( | ||
groupId = "DefaultBehaviorOnDeserializationErrorConsumer", | ||
offsetReset = EARLIEST, | ||
offsetStrategy = DISABLED, | ||
properties = [@Property( | ||
name = ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | ||
value = "org.apache.kafka.common.serialization.UUIDSerializer" | ||
)] | ||
) | ||
static class DefaultBehaviorOnDeserializationErrorConsumer implements KafkaListenerExceptionHandler { | ||
Long currentPosition = -1 | ||
Long committedOffset = -1 | ||
DefaultKafkaListenerExceptionHandler errorHandler = new DefaultKafkaListenerExceptionHandler() | ||
|
||
@Topic(TEST_TOPIC) | ||
void receive(UUID uuid) { | ||
} | ||
|
||
@Override | ||
void handle(KafkaListenerException exception) { | ||
errorHandler.handle(exception) | ||
TopicPartition tp = new TopicPartition(TEST_TOPIC, 0) | ||
currentPosition = exception.kafkaConsumer.position(tp) | ||
OffsetAndMetadata committedOffsetAndMetadata = exception.kafkaConsumer.committed(tp) | ||
if (committedOffsetAndMetadata != null) { | ||
committedOffset = committedOffsetAndMetadata.offset() | ||
} else { | ||
committedOffset = 0 | ||
} | ||
} | ||
} | ||
|
||
@KafkaListener( | ||
groupId = "CommitOnDeserializationErrorConsumer", | ||
offsetReset = EARLIEST, | ||
offsetStrategy = DISABLED, | ||
properties = [@Property( | ||
name = ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | ||
value = "org.apache.kafka.common.serialization.UUIDSerializer" | ||
)] | ||
) | ||
static class CommitOnDeserializationErrorConsumer implements KafkaListenerExceptionHandler { | ||
Long currentPosition = -1 | ||
Long committedOffset = -1 | ||
DefaultKafkaListenerExceptionHandler errorHandler = new DefaultKafkaListenerExceptionHandler() | ||
|
||
CommitOnDeserializationErrorConsumer() { | ||
errorHandler.setSkipRecordOnDeserializationFailure(true) | ||
errorHandler.setCommitRecordOnDeserializationFailure(true) | ||
} | ||
|
||
@Topic(TEST_TOPIC) | ||
void receive(UUID uuid) { | ||
} | ||
|
||
@Override | ||
void handle(KafkaListenerException exception) { | ||
errorHandler.handle(exception) | ||
TopicPartition tp = new TopicPartition(TEST_TOPIC, 0) | ||
currentPosition = exception.kafkaConsumer.position(tp) | ||
OffsetAndMetadata committedOffsetAndMetadata = exception.kafkaConsumer.committed(tp) | ||
if (committedOffsetAndMetadata != null) { | ||
committedOffset = committedOffsetAndMetadata.offset() | ||
} else { | ||
committedOffset = 0 | ||
} | ||
} | ||
} | ||
|
||
@KafkaListener( | ||
groupId = "DoNothingOnDeserializationErrorConsumer", | ||
offsetReset = EARLIEST, | ||
offsetStrategy = DISABLED, | ||
properties = [@Property( | ||
name = ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | ||
value = "org.apache.kafka.common.serialization.UUIDSerializer" | ||
)] | ||
) | ||
static class DoNothingOnDeserializationErrorConsumer implements KafkaListenerExceptionHandler { | ||
Long currentPosition = -1 | ||
Long committedOffset = -1 | ||
DefaultKafkaListenerExceptionHandler errorHandler = new DefaultKafkaListenerExceptionHandler() | ||
|
||
DoNothingOnDeserializationErrorConsumer() { | ||
errorHandler.setSkipRecordOnDeserializationFailure(false) | ||
} | ||
|
||
@Topic(TEST_TOPIC) | ||
void receive(UUID uuid) { | ||
} | ||
|
||
@Override | ||
void handle(KafkaListenerException exception) { | ||
errorHandler.handle(exception) | ||
TopicPartition tp = new TopicPartition(TEST_TOPIC, 0) | ||
currentPosition = exception.kafkaConsumer.position(tp) | ||
OffsetAndMetadata committedOffsetAndMetadata = exception.kafkaConsumer.committed(tp) | ||
if (committedOffsetAndMetadata != null) { | ||
committedOffset = committedOffsetAndMetadata.offset() | ||
} else { | ||
committedOffset = 0 | ||
} | ||
} | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'DefaultKafkaListenerExceptionHandlerSpec') | ||
@KafkaClient | ||
static interface StringProducer { | ||
@Topic(TEST_TOPIC) | ||
void sendMessage(String message) | ||
} | ||
} |