Skip to content

Commit 3e5ee31

Browse files
authored
Merge pull request #75 from codecrafters-io/TropicolX-patch-2
Revise "Send Correlation ID #nv3" (stage 2)
2 parents 635b939 + 15535b1 commit 3e5ee31

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

stage_descriptions/base-02-nv3.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
In this stage, you'll send a response with a correlation ID.
22

3-
### Response message
3+
### The Kafka Wire Protocol
44

5-
Kafka brokers communicate with clients through the [Kafka wire protocol](https://kafka.apache.org/protocol.html). The protocol uses a request-response model, where the client sends a request message and the broker replies with a response message.
5+
Kafka brokers communicate with clients through the [Kafka wire protocol](https://kafka.apache.org/protocol.html). The protocol uses a request-response model: the client sends a request message, and the broker replies with a response message.
66

77
A Kafka response message has three parts:
88
1. `message_size`
@@ -11,26 +11,32 @@ A Kafka response message has three parts:
1111

1212
For this stage, you can ignore the body and just focus on `message_size` and the header. You'll learn about response bodies in a later stage.
1313

14-
#### The `message_size` field
14+
### The `message_size` Field
1515

16-
The [`message_size`](https://kafka.apache.org/protocol.html#protocol_common) field is a 32-bit signed integer. It specifies the size of the header and body.
16+
The [`message_size`](https://kafka.apache.org/protocol.html#protocol_common) field is a 32-bit signed integer that specifies the size of the header and body in bytes.
1717

18-
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.
18+
All integers in the Kafka protocol are in [big-endian](https://developer.mozilla.org/en-US/docs/Glossary/Endianness) byte order. For example, a `message_size` of `2` looks like this:
1919

20-
#### Header
20+
```
21+
00 00 00 02 // message_size: 2
22+
```
23+
24+
For this stage, you only need to ensure that your `message_size` field is 4 bytes long. You'll implement the correct `message_size` values in a later stage.
2125

22-
Kafka has a few different header versions. The way Kafka determines which header version to use is a bit complicated and is outside the scope of this challenge. For more information, take a look at [KIP-482](https://cwiki.apache.org/confluence/display/KAFKA/KIP-482%3A+The+Kafka+Protocol+should+Support+Optional+Tagged+Fields) and this [Stack Overflow answer](https://stackoverflow.com/a/71853003).
26+
### Response Header v0
2327

24-
In this stage, you will use [response header v0](https://kafka.apache.org/protocol.html#protocol_messages) (scroll down).
28+
Kafka has multiple header versions. In this stage, you will use [response header v0](https://kafka.apache.org/protocol.html#protocol_messages), which is the simplest version.
2529

26-
Response header v0 contains a single field: [`correlation_id`](https://developer.confluent.io/patterns/event/correlation-identifier/). This field lets clients match responses to their original requests. Here's how it works:
30+
Response header v0 contains a single field: [`correlation_id`](https://developer.confluent.io/patterns/event/correlation-identifier/). This field lets clients match responses to their original requests.
2731

28-
1. The client generates a correlation ID.
29-
2. The client sends a request that includes the correlation ID.
30-
3. The broker sends a response that includes the same correlation ID.
31-
4. The client receives the response and matches the correlation ID to the original request.
32+
The `correlation_id` field is also a 32-bit signed integer. For this stage, your program must respond with a hard-coded `correlation_id` of `7`.
3233

33-
The `correlation_id` field is a 32-bit signed integer. For this stage, your program must respond with a hard-coded `correlation_id` of 7.
34+
Putting it all together, your complete response should be 8 bytes long: 4 bytes for `message_size` and 4 bytes for the header (`correlation_id`).
35+
36+
```
37+
00 00 00 00 // message_size: 0 (any value works for this stage)
38+
00 00 00 07 // correlation_id: 7
39+
```
3440

3541
### Tests
3642

@@ -39,18 +45,22 @@ The tester will execute your program like this:
3945
$ ./your_program.sh
4046
```
4147

42-
It'll then connect to your broker on port 9092 and send a request:
48+
It will then connect to your broker on port `9092` and send a request:
4349
```
4450
$ echo -n "Placeholder request" | nc -v localhost 9092 | hexdump -C
4551
```
4652

47-
Your broker must send a response with a correlation ID of 7:
48-
```java
49-
00 00 00 00 // message_size: 0 (any value works)
53+
Your broker must send a response with this structure:
54+
```
55+
00 00 00 00 // message_size: 0 (any value works for this stage)
5056
00 00 00 07 // correlation_id: 7
5157
```
5258

59+
The tester will verify that:
60+
- Your response is 8 bytes total (4 for `message_size`, 4 for `correlation_id`).
61+
- The `correlation_id` field contains the value `7`.
62+
5363
### Notes
5464

5565
- For this stage, you don't need to parse the request. You'll learn about request parsing in a later stage.
56-
- All integers are in [big-endian](https://developer.mozilla.org/en-US/docs/Glossary/Endianness) order.
66+
- All integers are in [big-endian](https://developer.mozilla.org/en-US/docs/Glossary/Endianness) order.

0 commit comments

Comments
 (0)