Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema-reader: Log the erroring kafka message key #963

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion karapace/schema_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,14 @@ def consume_messages(self, msgs: list[Message], watch_offsets: bool) -> None:

assert message_key is not None
key = json_decode(message_key)
except AssertionError as exc:
LOG.warning("Empty msg.key() at offset %s", msg.offset())
self.offset = msg.offset() # Invalid entry shall also move the offset so Karapace makes progress.
self.kafka_error_handler.handle_error(location=KafkaErrorLocation.SCHEMA_READER, error=exc)
continue # [non-strict mode]
Comment on lines +366 to +370
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this looks like a bit of a sketchy idea, an AssertionError could come from deeper in the code, and this essentially abuses the assert to function as a goto. AssertionError should really never be caught, bar some special cases in test harnessing code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an AssertionError is raised from msg.key() or msg.error(), the correct behavior is for the code to fall over, and be restarted by the scheduler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this had existed prior to this change, we can take a look later.

except JSONDecodeError as exc:
LOG.warning("Invalid JSON in msg.key() at offset %s", msg.offset())
non_bytes_key = msg.key().decode() # type: ignore[union-attr]
LOG.warning("Invalid JSON in msg.key(): %s at offset %s", non_bytes_key, msg.offset())
self.offset = msg.offset() # Invalid entry shall also move the offset so Karapace makes progress.
self.kafka_error_handler.handle_error(location=KafkaErrorLocation.SCHEMA_READER, error=exc)
continue # [non-strict mode]
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/test_schema_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_schema_reader_can_end_to_ready_state_if_last_message_is_invalid_in_sche
ok1_message.value.return_value = schema_str
ok1_message.offset.return_value = 1
invalid_key_message = Mock(spec=Message)
invalid_key_message.key.return_value = "invalid-key"
invalid_key_message.key.return_value = b"invalid-key"
invalid_key_message.error.return_value = None
invalid_key_message.value.return_value = schema_str
invalid_key_message.offset.return_value = 2
Expand Down Expand Up @@ -388,7 +388,16 @@ def factory(key: bytes, value: bytes, offset: int = 1) -> Message:
schema_type=None,
message_type=MessageType.schema,
expected_error=CorruptKafkaRecordException,
expected_log_message="Invalid JSON in msg.key() at offset 1",
expected_log_message='Invalid JSON in msg.key(): {subject1::::"test""version":1"magic":1} at offset 1',
),
KafkaMessageHandlingErrorTestCase(
test_name="Message key is empty, i.e. `null/None`",
key=None,
value=b'{"value": "value does not matter at this stage, just correct JSON"}',
schema_type=None,
message_type=MessageType.schema,
expected_error=CorruptKafkaRecordException,
expected_log_message="Empty msg.key() at offset 1",
),
KafkaMessageHandlingErrorTestCase(
test_name="Keytype is missing from message key",
Expand Down
Loading