Skip to content

GH-2208: Fix Manual Nack with Mutating Interceptor #2210

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

Merged
merged 1 commit into from
Apr 5, 2022
Merged
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 @@ -2447,14 +2447,20 @@ private void handleNack(final ConsumerRecords<K, V> records, final ConsumerRecor
Iterator<ConsumerRecord<K, V>> iterator2 = records.iterator();
while (iterator2.hasNext()) {
ConsumerRecord<K, V> next = iterator2.next();
if (next.equals(record) || list.size() > 0) {
if (list.size() > 0 || recordsEqual(record, next)) {
list.add(next);
}
}
SeekUtils.doSeeks(list, this.consumer, null, true, (rec, ex) -> false, this.logger); // NOSONAR
pauseForNackSleep();
}

private boolean recordsEqual(ConsumerRecord<K, V> rec1, ConsumerRecord<K, V> rec2) {
return rec1.topic().equals(rec2.topic())
&& rec1.partition() == rec2.partition()
&& rec1.offset() == rec2.offset();
}

private void pauseForNackSleep() {
if (this.nackSleep > 0) {
this.nackWake = System.currentTimeMillis() + this.nackSleep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public interface RecordInterceptor<K, V> extends ThreadStateProcessor {

/**
* Perform some action on the record or return a different one. If null is returned
* the record will be skipped. Invoked before the listener.
* the record will be skipped. Invoked before the listener. IMPORTANT; if this method
* returns a different record, the topic, partition and offset must not be changed
* to avoid undesirable side-effects.
* @param record the record.
* @param consumer the consumer.
* @return the record or null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

Expand Down Expand Up @@ -243,6 +244,17 @@ public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() {
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(AckMode.MANUAL);
factory.getContainerProperties().setMissingTopicsFatal(false);
factory.setRecordInterceptor(new RecordInterceptor() {

@Override
@Nullable
public ConsumerRecord intercept(ConsumerRecord record, Consumer consumer) {
return new ConsumerRecord(record.topic(), record.partition(), record.offset(), 0L,
TimestampType.NO_TIMESTAMP_TYPE, 0, 0, record.key(), record.value(), record.headers(),
Optional.empty());
}

});
return factory;
}

Expand Down