Since I spent the last two hours trying to determine what was expected from the tests and it wasn’t fun because the instructions are unclear and nowhere can we find the right sequence that’s expected.
I’ll share them here since it doesn’t seem the format of the expected response is listed on Kafka doc, it’s not exactly the v3 response as @rohitpaulk shared but rather something a bit different:
For the num_of_api_keys I had to input 2 despite having only one API key, not sure why but the decoder seems to decode my 2 as a 1, when I input 1 it errors in the decode.
The _tagged_field is just a single byte with a value of 0
For num_of_api_keys, I think this is just the way lengths are encoded (see COMPACT_ARRAY type definition) :
Represents a sequence of objects of a given type T. Type T can be either a primitive type (e.g. STRING) or a structure. First, the length N + 1 is given as an UNSIGNED_VARINT. Then N instances of type T follow. A null array is represented with a length of 0. In protocol documentation an array of T instances is referred to as [T].
I don’t understand why they do this instead of just using 0 for 0, 1 for 1, … but adding this extra + 1 for non empty arrays made it work
Regarding the TAG_BUFFER, they said we don’t use tag in the CodeCrafter challenge, so it os just one byte to encode a length of 0 for the tag array. At least, that’s the more or less logical explanation I came up with after being puzzled just like you are and deriving this 0 on one byte empirically TAG_BUFFER explanation · romainfd/codecrafters-kafka-python@8b3ecf9 · GitHub
So I agree the instructions are not super clear but I think this is also a problem of the Kafka doc not always being super clear…
I also had a LOT of trouble finding out when to use Response Header V0 vs V1…
Since we don’t parse the Request content, I am curious why not only implement ApiVersions Request v2 ?
@romainfd We implemented ApiVersionsRequest:v4 to stay on the bleeding edge of Kafka. Our users probably don’t want to implement older versions of requests.
I don’t understand why they do this instead of just using 0 for 0, 1 for 1, … but adding this extra + 1 for non empty arrays made it work
For COMPACT_ARRAY, the reason why Kafka uses N + 1 as the length is to differentiate a null array (represented as 0) and an empty array (actual length of 0, but represented as 1).
I also had a LOT of trouble finding out when to use Response Header V0 vs V1
If you don’t need to support older versions of Kafka, use Response Header v1, because of KIP-482:
In order to have flexible version support across all requests and responses, we will bump the version of all requests and responses.
…
The new ResponseHeader version will be version 1, superseding version 0.
The only exception is that “ApiVersionsResponse always includes a v0 header”.