Skip to content

Commit 5bc5a44

Browse files
authored
Merge pull request #76 from codecrafters-io/TropicolX-patch-3
Revise "Parse Correlation ID #wa6" (stage 3)
2 parents 9f3f527 + 44aeba1 commit 5bc5a44

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

stage_descriptions/base-03-wa6.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
In this stage, you'll replace the hard-coded correlation ID with the actual correlation ID from the request.
1+
In this stage, you'll replace the hardcoded correlation ID with the actual correlation ID from the request.
22

3-
### Request message
3+
### Kafka Message Structure
44

5-
A request message has three parts:
6-
1. `message_size`
7-
2. Header
8-
3. Body
5+
As a recap, Kafka request and response messages have the following format:
96

10-
To get the `correlation_id` field, you need to find its offset. You already know that `message_size` is 4 bytes long. And here's what the request header looks like (in this stage, we're using [request header v2](https://kafka.apache.org/protocol.html#protocol_messages)):
7+
1. `message_size`: The size of the header and body
8+
2. Header: Contains metadata about the request
9+
3. Body: Contains the actual request data
1110

12-
| Field | Data type | Description |
13-
| --------------------- | ----------------- | -------------------------------------- |
14-
| `request_api_key` | `INT16` | The API key for the request |
15-
| `request_api_version` | `INT16` | The version of the API for the request |
16-
| `correlation_id` | `INT32` | A unique identifier for the request |
17-
| `client_id` | `NULLABLE_STRING` | The client ID for the request |
18-
| `TAG_BUFFER` | `COMPACT_ARRAY` | Optional tagged fields |
11+
In a previous stage, you sent a response with a hardcoded `correlation_id`. Now you'll extract the actual `correlation_id` from the incoming request header and echo it back.
1912

20-
To learn more about the different data types, see [Protocol Primitive Types](https://kafka.apache.org/protocol.html#protocol_types).
13+
### Parsing the Request Header
2114

22-
#### Example
15+
To extract the `correlation_id`, you need to parse the request header. For this stage, you'll work with [request header v2](https://kafka.apache.org/protocol.html#protocol_messages).
16+
17+
Here’s what the structure looks like:
18+
19+
| Field | Data type | Size (bytes) | Description |
20+
| --------------------- | ----------------- | ------------ | -------------------------------------- |
21+
| `request_api_key` | `INT16` | 2 | The API key for the request |
22+
| `request_api_version` | `INT16` | 2 | The version of the API for the request |
23+
| `correlation_id` | `INT32` | 4 | A unique identifier for the request |
24+
| `client_id` | `COMPACT_NULLABLE_STRING` | Variable | The client ID for the request |
25+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Variable | Optional tagged fields |
26+
27+
*To learn more about the different data types, see [Protocol Primitive Types](https://kafka.apache.org/protocol.html#protocol_types).*
28+
29+
To get the `correlation_id` field, you need to find its offset in the request message. Here's an example request showing where each field is located:
2330

24-
Here's an example of a request message:
2531
```java
2632
00 00 00 23 // message_size: 35
2733
00 12 // request_api_key: 18
@@ -30,26 +36,34 @@ Here's an example of a request message:
3036
...
3137
```
3238

39+
Your response will use the same structure from the previous stage:
40+
41+
```
42+
00 00 00 00 // message_size: 0 (any value works for this stage)
43+
6f 7f c6 61 // correlation_id: 1870644833 (extracted from request)
44+
```
45+
3346
### Tests
3447

3548
The tester will execute your program like this:
3649
```
3750
$ ./your_program.sh
3851
```
3952

40-
It'll then connect to your broker on port 9092 and send a request with a request header v2:
53+
It will then connect to your broker on port `9092` and send a request with a request header v2:
4154
```
4255
$ echo -n "00000023001200046f7fc66100096b61666b612d636c69000a6b61666b612d636c6904302e3100" | xxd -r -p | nc localhost 9092 | hexdump -C
4356
```
4457

45-
Your broker must send a response with the correct correlation ID:
46-
```java
47-
00 00 00 00 // message_size: 0 (any value works)
58+
Your broker must extract the correlation ID from the request and echo it back:
59+
```
60+
00 00 00 00 // message_size: 0 (any value works for this stage)
4861
6f 7f c6 61 // correlation_id: 1870644833
4962
```
5063

5164
### Notes
5265

53-
- For this stage, you don't need to worry about what the request is asking for. You'll handle that in the next stage.
66+
- For this stage, you don't need to worry about what the request is asking for. You'll handle that at a later stage.
5467
- For this stage, the tester will only assert that your `message_size` field is 4 bytes long—it won't check the value. You'll implement correct `message_size` values in a later stage.
55-
- The request header version and response header version are unrelated to each other and do not have to match.
68+
- The request header version and response header version are unrelated to each other and do not have to match.
69+

0 commit comments

Comments
 (0)