Regarding Number of api keys

def create_msg(id,api_key: int, error_code: int = 0):
    response_header = id.to_bytes(4, byteorder="big")
    err = error_code.to_bytes(2, byteorder="big")
    api_key_bytes = api_key.to_bytes(2, byteorder="big")
    min_version, max_version = 0,4
    tag_buffer = b"\x00"
    num_api_keys = int(2).to_bytes(1, byteorder="big") 
    throttle_time_ms = 0

    response_body = (
        err+
        num_api_keys+
        api_key_bytes+
        min_version.to_bytes(2)+
        max_version.to_bytes(2)+
        tag_buffer+
        throttle_time_ms.to_bytes(4, byteorder="big")+
        tag_buffer
    )
    total_len = len(response_header) + len(response_body)
    return int(total_len).to_bytes(4, byteorder="big") + response_header + response_body

here in this code if we take num_api_keys = 1 decoder takes it value as 0 while if we take num_api_keys = 2 decoder takes it value as 2 and i don’t seem to understand this why this happening?

@MihirDesai564 The field api_keys is of the COMPACT_ARRAY type, meaning if the length of the compact array is N, the first byte should be N + 1.

Kafka uses N + 1 to distinguish between a null array (represented as 0) and an empty array (which has an actual length of 0, but is represented as 1).

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.