Skip to content

Commit 248470e

Browse files
wn1331spring-builds
authored andcommitted
GH-4907: Recreate producer after invalid PID (#4919)
Fixes: #4907 Kafka clients treat InvalidPidMappingException as fatal to the current producer. DefaultKafkaProducerFactory kept that producer cached, so every later send reused the defunct instance. Remove and close the producer when the send callback receives this exception, as already done for OutOfOrderSequenceException, and verify that the next request creates a fresh producer. Signed-off-by: Jonghun Ju <85150438+wn1331@users.noreply.github.com> (cherry picked from commit 934fdf1)
1 parent bab4b32 commit 248470e

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎spring-kafka/src/main/java/org/springframework/kafka/core/DefaultKafkaProducerFactory.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.kafka.common.PartitionInfo;
3737
import org.apache.kafka.common.TopicPartition;
3838
import org.apache.kafka.common.Uuid;
39+
import org.apache.kafka.common.errors.InvalidPidMappingException;
3940
import org.apache.kafka.common.errors.OutOfOrderSequenceException;
4041
import org.apache.kafka.common.errors.ProducerFencedException;
4142
import org.apache.kafka.common.errors.TimeoutException;
@@ -1118,7 +1119,7 @@ public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callbac
11181119

11191120
@Override
11201121
public void onCompletion(RecordMetadata metadata, Exception exception) {
1121-
if (exception instanceof OutOfOrderSequenceException) {
1122+
if (exception instanceof OutOfOrderSequenceException || exception instanceof InvalidPidMappingException) {
11221123
CloseSafeProducer.this.producerFailed = exception;
11231124
close(CloseSafeProducer.this.closeTimeout);
11241125
}

‎spring-kafka/src/test/java/org/springframework/kafka/core/DefaultKafkaProducerFactoryTests.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.kafka.common.KafkaException;
2323
import org.apache.kafka.common.Metric;
2424
import org.apache.kafka.common.MetricName;
25+
import org.apache.kafka.common.errors.InvalidPidMappingException;
2526
import org.apache.kafka.common.errors.UnknownProducerIdException;
2627
import org.apache.kafka.common.serialization.Serializer;
2728
import org.junit.jupiter.api.Test;
@@ -496,11 +497,20 @@ protected Producer createKafkaProducer() {
496497
}
497498

498499
@Test
499-
@SuppressWarnings({"rawtypes", "unchecked"})
500500
void testUnknownProducerIdException() {
501+
assertProducerRecreatedAfterSendFailure(new UnknownProducerIdException("test"));
502+
}
503+
504+
@Test
505+
void testInvalidPidMappingException() {
506+
assertProducerRecreatedAfterSendFailure(new InvalidPidMappingException("test"));
507+
}
508+
509+
@SuppressWarnings({"rawtypes", "unchecked"})
510+
private void assertProducerRecreatedAfterSendFailure(Exception exception) {
501511
final Producer producer1 = mock(Producer.class);
502512
willAnswer(inv -> {
503-
((Callback) inv.getArgument(1)).onCompletion(null, new UnknownProducerIdException("test"));
513+
((Callback) inv.getArgument(1)).onCompletion(null, exception);
504514
return null;
505515
}).given(producer1).send(isNull(), any());
506516
final Producer producer2 = mock(Producer.class);

0 commit comments

Comments
 (0)