Skip to content

Commit 7e8d419

Browse files
authored
Merge pull request #84 from codecrafters-io/TropicolX-patch-7
Revise "List for an unknown topic #vt6"
2 parents dd04b70 + 1995bdb commit 7e8d419

File tree

1 file changed

+98
-11
lines changed

1 file changed

+98
-11
lines changed

stage_descriptions/listing-partitions-02-vt6.md

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,98 @@
11
In this stage, you'll implement the `DescribeTopicPartitions` response for an unknown topic.
22

3+
### The `DescribeTopicPartitions` API (Recap)
4+
5+
As a recap, the [`DescribeTopicPartitions`](https://kafka.apache.org/protocol.html#The_Messages_DescribeTopicPartitions) API returns metadata about [topics](https://kafka.apache.org/documentation/#intro_concepts_and_terms) and their [partitions](https://kafka.apache.org/documentation/#:~:text=partitioned). For this stage, you'll handle cases where a client requests a topic that doesn't exist.
6+
37
We've created an interactive protocol inspector for the `DescribeTopicPartitions` request & response:
48

59
- 🔎 [DescribeTopicPartitions Request (v0)](https://binspec.org/kafka-describe-topic-partitions-request-v0)
610
- 🔎 [DescribeTopicPartitions Response (v0) - Unknown Topic](https://binspec.org/kafka-describe-topic-partitions-response-v0-unknown-topic)
711

8-
🚧 **We're still working on instructions for this stage**. You can find notes on how the tester works below.
12+
### Parsing the Request
13+
14+
To respond correctly, you'll need to parse the `DescribeTopicPartitions` request to extract the topic name. The [`DescribeTopicPartitions` request (v0)](https://kafka.apache.org/protocol.html#The_Messages_DescribeTopicPartitions) contains:
15+
16+
| Field | Data type | Description |
17+
| ------------ | --------------- | ------------------------------------- |
18+
| `topics` | `COMPACT_ARRAY` | Array of topics to get metadata for |
19+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Tagged fields |
20+
21+
Each topic in the `topics` array contains:
22+
23+
| Field | Data type | Description |
24+
| ------------ | ------------ | -------------------- |
25+
| `topic_name` | `STRING` | The topic name |
26+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Tagged fields |
27+
28+
For this stage, the tester will send a request with a single topic name. You'll need to extract this name and echo it back in your response.
29+
30+
### Response Header v1
31+
32+
In previous stages, you used response header v0, which only contained the `correlation_id` field. For this stage, you'll use [response header v1](https://kafka.apache.org/protocol.html#protocol_messages), which has the following structure:
33+
34+
| Field | Data type | Description |
35+
| ---------------- | ------------ | ------------------------------------- |
36+
| `correlation_id` | `INT32` | Matches the request's correlation ID |
37+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Tagged fields |
38+
39+
The main difference is the addition of the `TAG_BUFFER` field. You can leave the `TAG_BUFFER` empty.
40+
41+
### The `DescribeTopicPartitions` Response Body
942

10-
In the meantime, please use
11-
[this link](https://forum.codecrafters.io/new-topic?category=Challenges&tags=challenge%3Akafka&title=Question+about+vt6%3A+List+for+an+unknown+topic&body=%3Cyour+question+here%3E)
12-
to ask questions on the forum.
43+
For this stage, you should treat all topics as unknown. The response should indicate an error using error code `3` (`UNKNOWN_TOPIC_OR_PARTITION`).
44+
45+
The `DescribeTopicPartitions` response body has the following structure:
46+
47+
| Field | Data type | Description |
48+
| ------------------ | --------------- | ---------------------------------------------- |
49+
| `throttle_time_ms` | `INT32` | Throttle time in milliseconds (use 0) |
50+
| `topics` | `COMPACT_ARRAY` | Array of topic metadata |
51+
| `next_cursor` | `NULLABLE_INT8` | Pagination cursor (use -1 for null) |
52+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Tagged fields |
53+
54+
Each topic in the `topics` array contains:
55+
56+
| Field | Data type | Description |
57+
| ----------------- | --------------- | ------------------------------------------------- |
58+
| `error_code` | `INT16` | Error code (3 for UNKNOWN_TOPIC_OR_PARTITION) |
59+
| `topic_name` | `STRING` | The topic name (from the request) |
60+
| `topic_id` | `UUID` | Topic UUID (use all zeros for unknown topics) |
61+
| `is_internal` | `BOOLEAN` | Whether topic is internal (use false) |
62+
| `partitions` | `COMPACT_ARRAY` | Array of partition metadata (empty for unknown) |
63+
| `topic_authorized_operations` | `INT32` | Authorized operations bitfield (use 0) |
64+
| `TAG_BUFFER` | `TAGGED_FIELDS` | Tagged fields |
65+
66+
For an unknown topic, your response should:
67+
- Set `error_code` to `3` (`UNKNOWN_TOPIC_OR_PARTITION`).
68+
- Echo back the `topic_name` from the request.
69+
- Set `topic_id` to `00000000-0000-0000-0000-000000000000` (all zeros).
70+
- Set `is_internal` to `false`.
71+
- Leave the `partitions` array empty.
72+
- Set `next_cursor` to `-1` (null).
73+
74+
Here's an example of what your response might look like for a topic named `"foo"`:
75+
76+
```java
77+
00 00 00 2f // message_size: 47 bytes
78+
ab cd ef 12 // correlation_id: (matches request)
79+
00 // TAG_BUFFER: empty (response header v1)
80+
00 00 00 00 // throttle_time_ms: 0
81+
02 // topics array: 1 element
82+
00 03 // error_code: 3 (UNKNOWN_TOPIC_OR_PARTITION)
83+
04 // name length: 3 (compact string)
84+
66 6f 6f // topic_name: "foo"
85+
00 00 00 00 // topic_id: 00000000-0000-0000-
86+
00 00 00 00 // 0000-000000000000
87+
00 00 00 00 // (16 bytes total)
88+
00 00 00 00 //
89+
00 // is_internal: false
90+
01 // partitions array: 0 elements (empty)
91+
00 00 00 00 // topic_authorized_operations: 0
92+
00 // TAG_BUFFER: empty
93+
ff // next_cursor: -1 (null)
94+
00 // TAG_BUFFER: empty
95+
```
1396

1497
### Tests
1598

@@ -19,21 +102,25 @@ The tester will execute your program like this:
19102
$ ./your_program.sh /tmp/server.properties
20103
```
21104

22-
It'll then connect to your server on port 9092 and send a `DescribeTopicPartitions` (v0) request. The request will contain a single topic with 1 partition.
105+
It will then connect to your server on port `9092` and send a `DescribeTopicPartitions` (v0) request containing a single topic name.
106+
107+
```
108+
echo -n "00000020004b00000000000700096b61666b612d636c69000204666f6f0000000064ff00" | xxd -r -p | nc localhost 9092 | hexdump -C
109+
```
23110

24111
The tester will validate that:
25112

26-
- The first 4 bytes of your response (the "message length") are valid.
113+
- The `message_size` field correctly represents the size of the header and body.
27114
- The correlation ID in the response header matches the correlation ID in the request header.
28-
- The error code in the response body is 3 (`UNKNOWN_TOPIC_OR_PARTITION`).
29-
- The response body should be valid DescribeTopicPartitions (v0) Response.
115+
- The response uses response header v1 (with `TAG_BUFFER`).
116+
- The `error_code` in the response body is `3` (`UNKNOWN_TOPIC_OR_PARTITION`).
117+
- The response body should be a valid DescribeTopicPartitions (v0) Response.
30118
- The `topic_name` field in the response should be equal to the topic name sent in the request.
31119
- The `topic_id` field in the response should be equal to `00000000-0000-0000-0000-000000000000`.
32-
- The `partitions` field in the response should be empty. (As there are no partitions assigned to this non-existent topic.)
33-
- The value of `cursor` is -1, indicating that the cursor is null.
120+
- The `partitions` field in the response should be empty.
121+
- The value of `next_cursor` is `-1`, indicating that the cursor is null.
34122

35123
### Notes
36124

37125
- You'll need to parse the `DescribeTopicPartitions` request in this stage to get the topic name to send in the response.
38126
- For now, you can assume that all topics are "unknown". We'll work on identifying actual vs. unknown topics in later stages.
39-
- The official docs for the `DescribeTopicPartitions` request can be found [here](https://kafka.apache.org/protocol.html#The_Messages_DescribeTopicPartitions).

0 commit comments

Comments
 (0)