This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27305-kafka-share-poll-error in repository https://gitbox.apache.org/repos/asf/camel.git
commit 551de31aae43ee7b12cd13dcabe5f36df037f461 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 17:47:51 2026 +0100 CAMEL-27305: camel-kafka-share - the share consumer logs and terminates visibly when its poll fails with an Error The Kafka share client wraps only a RuntimeException of a deserializer in a RecordDeserializationException: an Error, such as the ExceptionInInitializerError of a class that cannot be initialized, is thrown out of poll as is. The poll loop handled only exceptions, so the Error ended the consumer thread, and the executor kept it in a Future that nobody reads: nothing was logged, and the health check said "recovery in progress" without an error, although the consumer never recovered. The consumer thread now logs what it does not handle at ERROR, and terminates with the error as the last error of its health check, which reports it as terminated. The share consumer is closed as before, so its records go to the other consumers. A VirtualMachineError is thrown on as is. This is the same fix as CAMEL-27298 in camel-kafka. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/docs/kafka-share-component.adoc | 6 ++++++ .../src/main/docs/kafka-share-component.adoc | 6 ++++++ .../kafka/share/KafkaShareFetchRecords.java | 18 ++++++++++++++++ .../kafka/share/KafkaShareConsumerErrorTest.java | 24 ++++++++++++++++++++++ .../kafka/share/RecordingShareConsumer.java | 14 ++++++++++--- 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/kafka-share-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/kafka-share-component.adoc index e6fe1a5e3b1b..04c6edbbf5ef 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/kafka-share-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/kafka-share-component.adoc @@ -218,3 +218,9 @@ naming them, when they are set as `additionalProperties`. The consumer is ready when all its share consumers are created and subscribed. When a share consumer cannot be created, the health check reports it as down, with the error and the `bootstrap.servers`, `group.id`, `client.id`, `route.id` and `topic` details. + +The `pollOnError` option applies to exceptions only. The Kafka client wraps only a `RuntimeException` of a +deserializer in a `RecordDeserializationException`. A `java.lang.Error`, such as an `ExceptionInInitializerError` or +`NoClassDefFoundError` of a deserializer whose classes cannot be loaded, fails every poll the same way. The consumer +thread then logs the error, closes its share consumer and terminates, and the health check reports it as down. Fix the +cause (for example, a dependency version), and then restart the route or the application. diff --git a/components/camel-kafka-share/src/main/docs/kafka-share-component.adoc b/components/camel-kafka-share/src/main/docs/kafka-share-component.adoc index e6fe1a5e3b1b..04c6edbbf5ef 100644 --- a/components/camel-kafka-share/src/main/docs/kafka-share-component.adoc +++ b/components/camel-kafka-share/src/main/docs/kafka-share-component.adoc @@ -218,3 +218,9 @@ naming them, when they are set as `additionalProperties`. The consumer is ready when all its share consumers are created and subscribed. When a share consumer cannot be created, the health check reports it as down, with the error and the `bootstrap.servers`, `group.id`, `client.id`, `route.id` and `topic` details. + +The `pollOnError` option applies to exceptions only. The Kafka client wraps only a `RuntimeException` of a +deserializer in a `RecordDeserializationException`. A `java.lang.Error`, such as an `ExceptionInInitializerError` or +`NoClassDefFoundError` of a deserializer whose classes cannot be loaded, fails every poll the same way. The consumer +thread then logs the error, closes its share consumer and terminates, and the health check reports it as down. Fix the +cause (for example, a dependency version), and then restart the route or the application. diff --git a/components/camel-kafka-share/src/main/java/org/apache/camel/component/kafka/share/KafkaShareFetchRecords.java b/components/camel-kafka-share/src/main/java/org/apache/camel/component/kafka/share/KafkaShareFetchRecords.java index f865d9e4dab8..b8358cba97b3 100644 --- a/components/camel-kafka-share/src/main/java/org/apache/camel/component/kafka/share/KafkaShareFetchRecords.java +++ b/components/camel-kafka-share/src/main/java/org/apache/camel/component/kafka/share/KafkaShareFetchRecords.java @@ -113,6 +113,15 @@ class KafkaShareFetchRecords implements Runnable { pollAndProcess(); } LOG.info("Terminating Kafka share consumer thread {} receiving from {}", threadId, topics); + } catch (VirtualMachineError e) { + // an OutOfMemoryError or StackOverflowError is thrown on as is, as the JVM may not be able to log it + throw e; + } catch (RuntimeException | Error e) { + // what the poll loop does not handle, such as an Error that a deserializer throws in poll (the Kafka client + // only wraps a RuntimeException of a deserializer in a RecordDeserializationException): this task runs in an + // executor that keeps what it throws in a Future that nobody reads, so terminate visibly + terminateOnUnhandledFailure(e); + throw e; } finally { closeConsumer(); if (reconnectPool != null) { @@ -122,6 +131,15 @@ class KafkaShareFetchRecords implements Runnable { } } + private void terminateOnUnhandledFailure(Throwable cause) { + String msg = "Terminating Kafka share consumer thread " + threadId + " receiving from " + topics + " due to " + + cause + ". Restart the route or the application to recover."; + LOG.error(msg, cause); + lastError = new KafkaConsumerFatalException(msg, cause); + // the share consumer is closed when run() ends, and its records are delivered to the other consumers + terminated = true; + } + /** * Creates and subscribes the share consumer, retrying with the create consumer backoff of the component. * diff --git a/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/KafkaShareConsumerErrorTest.java b/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/KafkaShareConsumerErrorTest.java index 82a00e1e4f73..7d9086533c5b 100644 --- a/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/KafkaShareConsumerErrorTest.java +++ b/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/KafkaShareConsumerErrorTest.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.kafka.KafkaConsumerFatalException; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.health.HealthCheck; import org.apache.camel.health.HealthCheckHelper; @@ -118,6 +119,9 @@ class KafkaShareConsumerErrorTest extends CamelTestSupport { .errorHandler(deadLetterChannel("mock:dead")) .to("mock:result"); + from("kafka-share:" + TOPIC + options + "poll-error").routeId("pollError") + .to("mock:poll-error"); + from("kafka-share:" + TOPIC + options + "unreachable").routeId("unreachable") .to("mock:unreachable"); @@ -160,6 +164,26 @@ class KafkaShareConsumerErrorTest extends CamelTestSupport { assertThat(consumers.get("stop")).hasSize(1); } + @Test + void pollFailingWithAnErrorTerminatesVisibly() { + // CAMEL-27305: the Kafka client throws an Error of a deserializer out of poll as is, such as the + // ExceptionInInitializerError of a class that cannot be initialized + RecordingShareConsumer consumer = subscribedConsumer("poll-error", 0); + consumer.failNextPoll(new ExceptionInInitializerError("Forced class initialization failure")); + + await().atMost(10, TimeUnit.SECONDS).until(consumer::isClosed); + HealthCheck.Result result = readiness("pollError"); + assertThat(result.getState()).isEqualTo(HealthCheck.State.DOWN); + assertThat(result.getMessage()).hasValueSatisfying(message -> assertThat(message) + .contains("terminated") + .contains("Forced class initialization failure")); + assertThat(result.getError()).hasValueSatisfying(error -> assertThat(error) + .isInstanceOf(KafkaConsumerFatalException.class) + .hasCauseInstanceOf(ExceptionInInitializerError.class)); + // the consumer does not reconnect, as the Error fails every poll the same way + assertThat(consumers.get("poll-error")).hasSize(1); + } + @Test void pollErrorWithErrorHandlerIsRoutedToTheErrorHandlerAndPollingContinues() throws Exception { RecordingShareConsumer consumer = subscribedConsumer("error-handler", 0); diff --git a/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/RecordingShareConsumer.java b/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/RecordingShareConsumer.java index 02066c78557b..51fc14b692ba 100644 --- a/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/RecordingShareConsumer.java +++ b/components/camel-kafka-share/src/test/java/org/apache/camel/component/kafka/share/RecordingShareConsumer.java @@ -46,7 +46,8 @@ class RecordingShareConsumer extends MockShareConsumer<Object, Object> { private volatile int commits; private volatile int polls; private volatile boolean closed; - private RuntimeException pollFailure; + // an unchecked exception or an Error + private Throwable pollFailure; private RuntimeException subscribeFailure; private boolean hasRecords; @@ -67,9 +68,12 @@ class RecordingShareConsumer extends MockShareConsumer<Object, Object> { public synchronized ConsumerRecords<Object, Object> poll(Duration timeout) { polls++; if (pollFailure != null) { - RuntimeException failure = pollFailure; + Throwable failure = pollFailure; pollFailure = null; - throw failure; + if (failure instanceof Error error) { + throw error; + } + throw (RuntimeException) failure; } if (!hasRecords) { try { @@ -121,6 +125,10 @@ class RecordingShareConsumer extends MockShareConsumer<Object, Object> { this.pollFailure = failure; } + synchronized void failNextPoll(Error failure) { + this.pollFailure = failure; + } + int getPolls() { return polls; }
