Skip to content

Commit dde23bd

Browse files
authored
NIFI-16626 Retry ConsumeKafka consumer creation after failure (#11962)
Release the consumer slot and return no consumer when the Kafka Connection Service fails to create a consumer, so that the processor yields and tries again on the next trigger instead of stopping until it is restarted.
1 parent 81ca807 commit dde23bd

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎nifi-extension-bundles/nifi-kafka-bundle/nifi-kafka-processors/src/main/java/org/apache/nifi/kafka/processors/ConsumeKafka.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,15 @@ private KafkaConsumerService getConsumerService(final ProcessContext context) {
878878

879879
getLogger().info("No Kafka Consumer Service available; creating a new one. Active count: {}", activeCount);
880880
final KafkaConnectionService connectionService = context.getProperty(CONNECTION_SERVICE).asControllerService(KafkaConnectionService.class);
881-
final KafkaConsumerService newService = connectionService.getConsumerService(pollingContext);
881+
final KafkaConsumerService newService;
882+
try {
883+
newService = connectionService.getConsumerService(pollingContext);
884+
} catch (final RuntimeException e) {
885+
getLogger().error("Failed to create Kafka Consumer Service", e);
886+
activeConsumerCount.decrementAndGet();
887+
return null;
888+
}
889+
882890
newService.setRebalanceCallback(createRebalanceCallback());
883891
return newService;
884892
}

‎nifi-extension-bundles/nifi-kafka-bundle/nifi-kafka-processors/src/test/java/org/apache/nifi/kafka/processors/ConsumeKafkaTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static org.mockito.ArgumentMatchers.anyLong;
6666
import static org.mockito.Mockito.mock;
6767
import static org.mockito.Mockito.never;
68+
import static org.mockito.Mockito.times;
6869
import static org.mockito.Mockito.verify;
6970
import static org.mockito.Mockito.when;
7071

@@ -322,6 +323,28 @@ public void testOnStoppedPreservesLastCaughtUp() throws Exception {
322323
assertEquals(OptionalLong.of(5L), afterStop.get().getRecordCount());
323324
}
324325

326+
@Test
327+
public void testOnTriggerConsumesRecordsAfterConsumerCreationFailure() throws InitializationException, IOException {
328+
setConnectionService();
329+
configureProcessor();
330+
when(kafkaConnectionService.getConsumerService(any()))
331+
.thenThrow(new RuntimeException("Failed to construct kafka consumer"))
332+
.thenReturn(kafkaConsumerService);
333+
final ByteRecord record = new ByteRecord(TEST_TOPIC_NAME, FIRST_PARTITION, 0, 0L, List.of(), null, FIRST_RECORD_VALUE, 1, false);
334+
when(kafkaConsumerService.poll(any())).thenReturn(List.of(record)).thenReturn(List.of());
335+
336+
processor.onScheduled(runner.getProcessContext());
337+
338+
processor.onTrigger(runner.getProcessContext(), runner.getProcessSessionFactory().createSession());
339+
runner.assertTransferCount(ConsumeKafka.SUCCESS, 0);
340+
341+
processor.onTrigger(runner.getProcessContext(), runner.getProcessSessionFactory().createSession());
342+
343+
verify(kafkaConnectionService, times(2)).getConsumerService(any());
344+
runner.assertTransferCount(ConsumeKafka.SUCCESS, 1);
345+
runner.getFlowFilesForRelationship(ConsumeKafka.SUCCESS).getFirst().assertContentEquals(FIRST_RECORD_VALUE);
346+
}
347+
325348
@Test
326349
public void testOnTriggerRecordsBytesAndLagMetrics() throws InitializationException {
327350
setConnectionService();

0 commit comments

Comments
 (0)