[FLINK-31951][format] Reset decoder on Avro deserialization failure#28715
Open
spratt wants to merge 1 commit into
Open
[FLINK-31951][format] Reset decoder on Avro deserialization failure#28715spratt wants to merge 1 commit into
spratt wants to merge 1 commit into
Conversation
- Add resetDecoder() to AvroDeserializationSchema; reinitialises the BinaryDecoder against the existing input stream via DecoderFactory - Wrap datumReader.read() in RegistryAvroDeserializationSchema with try-catch(IOException | RuntimeException) that calls resetDecoder() before rethrowing, clearing stale bytes from the internal read-ahead buffer so the next message is not corrupted - Add RegistryAvroDeserializationSchemaDecoderResetTest verifying that a valid message decodes correctly after a prior malformed-payload failure
Collaborator
Author
|
@flinkbot run azure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of the change
This PR addresses FLINK-31951. We are encountering this bug in production and opened this PR after finding the two prior fix attempts closed due to inactivity: #22507 (approved, closed inactive) and #26397 (closed inactive).
This fix was developed independently. The prior PRs applied the reset in
AvroDeserializationSchema.deserialize(), butRegistryAvroDeserializationSchemaoverrides that method entirely, so the base-class fix is never reached when using the registry schema. This PR targetsRegistryAvroDeserializationSchema.deserialize()directly and also catchesRuntimeException(coveringAvroRuntimeException, the exception type thrown when the decoder reads malformed data). We are happy to defer if the maintainers prefer to revive one of the earlier approaches instead.RegistryAvroDeserializationSchemareuses a singleBinaryDecoderinstance across all messages on a Kafka partition. WhendatumReader.read()fails mid-message (e.g. a malformed payload, a schema mismatch, or anAvroRuntimeException), stale bytes remain in the decoder's internal read-ahead buffer. The next call todatumReader.read()then starts from those leftover bytes rather than the beginning of the next message, producing corrupt output or cascading failures.This fix wraps
datumReader.read()in a try-catch and callsresetDecoder()before re-throwing, discarding any pre-fetched bytes and leaving the decoder ready for the next message.Brief change log
AvroDeserializationSchema: add package-privateresetDecoder()method that reinitialises theBinaryDecoderagainst the existinginputStreamviaDecoderFactory.get().binaryDecoder(inputStream, decoder); no-op for JSON encodingRegistryAvroDeserializationSchema: wrapdatumReader.read()in a try-catch onIOException | RuntimeException; callresetDecoder()before re-throwing so that a failed decode does not corrupt the bytes available to the next messageRegistryAvroDeserializationSchemaDecoderResetTest: serialises a malformed message followed by a valid one and asserts the valid message deserialises correctly after the failed decodeVerifying this change
Please make sure both new and modified tests in this PR follow the conventions for tests defined in our code quality guide.
This change added tests and can be verified as follows:
RegistryAvroDeserializationSchemaDecoderResetTestwhich writes a Confluent Schema Registry framed message with a garbage Avro payload (triggering anAvroRuntimeExceptionmid-decode), followed by a correctly encoded message, then deserialises both in sequence and asserts that the second message produces the expected field values (id=42,name="hello"). Without the fix, the second decode reads from the stale bytes left by the first failure and also throws.Does this pull request potentially affect one of the following parts:
@Public(Evolving): no (resetDecoder()is package-private; no public method signatures are changed)datumReader.read()is now wrapped in a try-catch on every deserialization call. In the happy path (no exception thrown) this has negligible overhead in modern JVMsDocumentation