davsclaus opened a new pull request, #30496: URL: https://github.com/apache/camel/pull/30496
## What [CAMEL-27298](https://issues.apache.org/jira/browse/CAMEL-27298): when the value deserializer failed with an `Error`, the Kafka consumer stopped reading, logged nothing, and the route and the health check stayed UP. ## Root cause - The Kafka client (4.3.2, `CompletedFetch.parseRecord`) wraps only a `RuntimeException` of a deserializer in a `RecordDeserializationException`. An `Error` is thrown out of `poll()` as is. - `KafkaFetchRecords.startPolling` handles `Exception` only. The `Error` left `run()`, and `run()` is started with `executor.submit(task)`, so the `Error` went into a `Future` that nothing reads. The thread ended without a log line, the Kafka consumer was not closed, and `healthStates()` still said ready, not terminated, recoverable, with no last error. - With the Apicurio protobuf serde and protobuf-java 4.35.1 (CAMEL-27300), `ProtobufDeserializer.internalReadData` calls `RefOuterClass.Ref.parseDelimitedFrom`, and that class fails to initialize (`ExceptionInInitializerError`, then `NoClassDefFoundError` on later polls). The code there catches only `IOException`/`Exception`. What I tested and what I inferred: - Tested against a real broker with a scratch IT (not part of this PR). A value deserializer that throws `ExceptionInInitializerError` reproduced the problem: the records after the failing one were never routed, the route stayed Started, and the health state was `ready=true terminated=false lastError=null`. Nothing about the failure was logged. With the fix, the stack trace runs from `CompletedFetch.parseRecord` through `KafkaConsumer.poll` to `KafkaFetchRecords.startPolling`, the error is logged at ERROR, and the state is `ready=false terminated=true recoverable=false` with the error. - Inferred from the bytecode, not run end to end with an Apicurio registry: that the Apicurio deserializer is what throws the `Error`. The CAMEL-27300 PR test shows that these classes fail to initialize with 4.35.1. ## Change `KafkaFetchRecords.run()` now catches what the poll loop does not handle (`RuntimeException | Error`, but not `VirtualMachineError`). It logs the failure at ERROR, records a `KafkaConsumerFatalException` as the last error, marks the task terminated and not connected, unsubscribes and closes the Kafka consumer (so its partitions go to the other group members), and rethrows. The consumer health check then reports DOWN: "gave up recovering and terminated the kafka consumer; restart route or application to recover", with the root cause message. `isRecoverable()` is false once the task has terminated. Why the consumer stops instead of handing the `Error` to the `pollOnError` strategy: - It is not a record failure. Kafka gives no offset for it, so DISCARD and ERROR_HANDLER cannot skip the record. - A class that failed to initialize fails on every poll. RETRY, RECONNECT and ERROR_HANDLER would loop forever on the same failure. - Stopping is how `AuthenticationException` is already handled. - `VirtualMachineError` (such as `OutOfMemoryError`) is thrown on unchanged, the same as in `BackgroundTask`. The kafka component doc, "Consumer error handling" section, now explains that `pollOnError` applies to exceptions only, and what happens with an `Error`. The catalog copy of the doc is updated too. Related: [CAMEL-27300](https://issues.apache.org/jira/browse/CAMEL-27300) / #30495 (protobuf: upgrade protobuf-java to 4.36.2, the gencode version of the Apicurio Registry 3.3.4 protobuf serdes) fixes the version mismatch that triggered this. ## Tests - New `KafkaConsumerPollErrorTerminatesTest`, a unit test with a `MockConsumer` whose `poll` throws `ExceptionInInitializerError`, as the Kafka client does. It checks that the Kafka consumer is closed, the task is terminated, not ready and not recoverable, the last error is a `KafkaConsumerFatalException` that wraps the `Error`, and `KafkaConsumerHealthCheck` is DOWN. Without the fix it fails: the consumer is never closed, and the test log has no WARN/ERROR line. - camel-kafka unit tests: 1421 tests, 0 failures, 1 skipped. - camel-kafka ITs (Kafka container): `KafkaConsumer*HealthCheckIT`, `KafkaConsumerStopIT`, `KafkaConsumerFullIT`, `KafkaConsumerAuthInvalid*IT`, `KafkaBreakOnFirstErrorReleaseResourcesIT`: 27 tests, 0 failures, 3 skipped. _Claude Code on behalf of Claus Ibsen_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
