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

rest-proxy: allow reading messages with avro-value and string-key #1031

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jclarysse
Copy link
Contributor

@jclarysse jclarysse commented Jan 24, 2025

It is not uncommon that Kafka users produce messages with String-key and Avro-value. In this case Karapace REST typically fails to read the message since it only supports one format for deserializing both the key and the value. For more convenience, I suggest to fallback to binary deserializer for the key (only) when reading it using avro fails.

For the manual (and integration) test we actually don't care about the value. We just assume that it exists in Avro-format, important is that we produce a message with Base64 encoded String-key:

$ echo "testkey" | base64
dGVzdGtleQo=
$ curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" \
  -d '{"records": [{"key": "dGVzdGtleQ=="}]}' \
  http://localhost:8082/topics/my_topic
{"key_schema_id":null,"offsets":[{"offset":0,"partition":0}],"value_schema_id":null}

Now we want to consumer the whole message, knowing that the value is in Avro format, so we create the consumer accordingly, and subscribe to the topic:

$ curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" -H "Accept: application/vnd.kafka.v2+json" \
  --data '{"name": "my_consumer", "format": "avro", "auto.offset.reset": "earliest"}' \
  http://localhost:8082/consumers/avro_consumers
$ curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" --data '{"topics":["my_topic"]}' \
  http://localhost:8082/consumers/avro_consumers/instances/my_consumer/subscription

Reading records BEFORE the change:

$ curl -X GET -H "Accept: application/vnd.kafka.avro.v2+json" \
  http://localhost:8082/consumers/avro_consumers/instances/my_consumer/records\?timeout\=1000
{"error_code":422,"message":"key deserialization error for format avro: unpack requires a buffer of 5 bytes"}

Reading records AFTER the change:

$ curl -X GET -H "Accept: application/vnd.kafka.avro.v2+json" \
  http://localhost:8081/consumers/avro_consumers/instances/my_consumer/records\?timeout\=1000
[{"key":"dGVzdGtleQ==","offset":0,"partition":0,"timestamp":1738667083672,"topic":"test-topic","value":null}]

Note that I also tested with a record with a String-key and a non-empty Avro-value that was produced in Java.

@jclarysse jclarysse requested a review from a team as a code owner January 24, 2025 17:08
@jclarysse jclarysse force-pushed the jclarysse/rest-consume-records-with-avro-value-and-string-key branch 2 times, most recently from 7b1f452 to 5865bc7 Compare February 4, 2025 08:28
@jclarysse jclarysse closed this Feb 4, 2025
@jclarysse jclarysse force-pushed the jclarysse/rest-consume-records-with-avro-value-and-string-key branch from 20a0bf7 to 6e61dfe Compare February 4, 2025 09:30
@jclarysse jclarysse reopened this Feb 4, 2025
@jclarysse jclarysse marked this pull request as draft February 4, 2025 09:47
@jclarysse jclarysse closed this Feb 4, 2025
@jclarysse jclarysse force-pushed the jclarysse/rest-consume-records-with-avro-value-and-string-key branch from 217f53f to 6e61dfe Compare February 4, 2025 10:34
@jclarysse jclarysse deleted the jclarysse/rest-consume-records-with-avro-value-and-string-key branch February 4, 2025 10:55
@jclarysse jclarysse restored the jclarysse/rest-consume-records-with-avro-value-and-string-key branch February 4, 2025 10:55
@jclarysse jclarysse deleted the jclarysse/rest-consume-records-with-avro-value-and-string-key branch February 4, 2025 10:56
@jclarysse jclarysse restored the jclarysse/rest-consume-records-with-avro-value-and-string-key branch February 4, 2025 10:56
@jclarysse jclarysse deleted the jclarysse/rest-consume-records-with-avro-value-and-string-key branch February 4, 2025 10:56
@jclarysse jclarysse reopened this Feb 4, 2025
@jclarysse jclarysse marked this pull request as ready for review February 4, 2025 11:15
It is not uncommon that Kafka users produce messages with String-key
and Avro-value. In this case Karapace REST typically fails to read
the message since it only supports one format for deserializing both
the key and the value.
For more convenience, I suggest to fallback to `binary` deserializer
for the key (only) when reading it using `avro` fails.
@jclarysse jclarysse force-pushed the jclarysse/rest-consume-records-with-avro-value-and-string-key branch from b417e87 to 37a4e1b Compare February 4, 2025 11:16
@jclarysse jclarysse marked this pull request as draft February 4, 2025 11:24
@jclarysse jclarysse marked this pull request as ready for review February 4, 2025 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant