This KIP is aimed at improving the error-handling semantics in Kafka Streams when Kafka Steams fails to serialize a message to the downstream sink by providing an interface that can provide custom massaging of the error (e.g. report to a custom metrics system) and indicate to Streams whether or not it should re-throw the Exception, thus causing the application to fall over.
Status
Current state: Under Discussion
Discussion thread: here [Change the link from the KIP proposal email archive to your own email thread]
JIRA: KAFKA-7499
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
In KIP-210, an exception handler for the write path was introduced. This exception handler covers exception that are raised in the producer callback.
However, serialization happens before the data is handed to the producer with Kafka Streams itself and the producer uses `byte[]/byte[]` key-value-pair types.
Thus, we might want to extend the ProductionExceptionHandler to cover serialization exception, too, to skip over corrupted output messages. An example could be a "String" message that contains invalid JSON and should be serialized as JSON.
Public Interfaces
We are proposing addition of a new overloaded method in ProductionExceptionHandler interface, handle
, that has the following signature:
ProductionExceptionHandlerResponse handle(ProducerRecord record, boolean isKey, Exception exception)
Proposed Changes
This implementation will add the overloaded method, handle
, in the following two classes:
DefaultProductionExceptionHandler
and returns response asFAIL
AlwaysProductionExceptionHandler
and returns response asCONTINUE
We'll implement the following error handling logic to the send in RecordCollectorImpl. The overloaded method, handle
, in ProductionExceptionHandler will be invoked when
ClassCastException
is thrown while serializing record key / value. Today, we are throwingStreamsException
on hitting this exception. Whether to throw the
exception to the user will be decided based on the response received from ProductionExceptionHandlerhandle
method.- If the result is
CONTINUE
, log a note atDEBUG
that we received that result and are not failing Streams as a result. - If the result is
FAIL
, log a message atERROR
that we received that result and throwStreamsException
so Streams will fail.
- If the result is
- Any other unchecked exceptions, that thrown during record key / value serialization.
- If the result is
CONTINUE
, log a note atDEBUG
that we received that result and are not failing Streams as a result. - If the result is
FAIL
, log a message atERROR
that we received that result and setsendException
so Streams will fail.
- If the result is
Earlier, we are invoking the error handler only when there are any exceptions in producer callback. Now, we also invoke the handler when hitting the serialization exception. As explained in KIP-210, this will facilitate a number of error handling scenarios.
Compatibility, Deprecation, and Migration Plan
The default behavior will be consistent with the existing behavior. The new overloaded method, handle
, will have a implementation that is set to FAIL
by default.
Rejected Alternatives
We have considered to reuse the existing handle(ProducerRecord<byte[], byte[]> record, Exception exception)
method in ProductionExceptionHandler
, but it has following limitation:
- The parameter
ProducerRecord
key and value type is set tobyte[],
on hitting the serialization exception the record key and value type may not bebyte[].
- To explicitly tell the user that the serialization exception occurred when serializing the record key or value.