# Question about fd8: Fetch multiple messages from disk - Expected partitions.length to be 1, got 2

**URL:** <https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [May 4, 2025, 9:03pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988 "2025-05-04T21:03:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![estenv](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/estenv/32/9644_2.png) [@estenv](https://forum.codecrafters.io/u/estenv)\
**Post date:** [May 4, 2025, 9:03pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/1 "2025-05-04T21:03:51Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/9/9e858927d31e4077e2088b902f0549fc28bad43c.png)

The test has 2 partitions for the topic. One partition has 2 record (batches) and the other has nothing.

It’s easily fixed by not including partitions with no records in the response but that will cause an earlier test to fail:  
_Stage #CM4 (Consuming Messages - Fetch with an empty topic)_

It’s quite unclear what the desired behavior is and the 2 stages seem contradicting.

What am I missing here?

Relevant code:

```rust
fn topic_handler(topic_record: &TopicRecord, metadata: &MetadataFile) -> FetchTopicResponse {
    let mut partitions = vec![];
    let partition_iter = metadata.get_topic_partitions(&topic_record.uuid);
    for partition in partition_iter {
        let mut records = vec![];
        if let Ok(partition_metadata) =
            read_partition_metadata(topic_record.topic_name.data.clone(), partition.partition_id)
        {
            for record in partition_metadata.record_batches {
                records.push(record);
            }
        }
        partitions.push(FetchTopicPartition {
            partition_index: partition.partition_id,
            error_code: ErrorCode::NoError,
            high_watermark: 0,
            last_stable_offset: 0,
            log_start_offset: 0,
            aborted_transactions: CompactArray::new(vec![]),
            preferred_read_replica: 0,
            record_batches: records,
        });
    }
    FetchTopicResponse {
        topic_id: topic_record.uuid,
        partitions: CompactArray::new(partitions),
    }
}

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 4, 2025, 9:19pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/3 "2025-05-04T21:19:45Z")

</div>

Hey @estenv, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![estenv](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/estenv/32/9644_2.png) [@estenv](https://forum.codecrafters.io/u/estenv)\
**Post date:** [May 4, 2025, 9:24pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/4 "2025-05-04T21:24:26Z")

</div>

Sure thing

> **[GitHub - asdfman/codecrafters-kafka-rust](https://github.com/asdfman/codecrafters-kafka-rust)**
>
> Contribute to asdfman/codecrafters-kafka-rust development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 5, 2025, 3:31pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/6 "2025-05-05T15:31:37Z")

</div>

@estenv ~~The empty partition should be excluded (unless certain conditions apply)~~. What specific errors are you seeing for stage #CM4 “Fetch with an empty topic”?

Here’s the relevant Kafka code for reference:

> <https://github.com/apache/kafka/blob/810beef50e189cc9d1a0839961dffd14930ffe2e/core/src/main/scala/kafka/server/FetchSession.scala#L138-L144>

---

<div class="post-metadata">

**Author:** ![estenv](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/estenv/32/9644_2.png) [@estenv](https://forum.codecrafters.io/u/estenv)\
**Post date:** [May 5, 2025, 11:04pm UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/8 "2025-05-05T23:04:30Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/3/327947efd065b2a20b885ebf2c9557e2d855eb07.png)  
#CM4 expects one partition in the response

Changed code (passes last stage now)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/7/7a6ab9dc1b6aa25e18bf13c2f8bd448d6efe502e.png)

CM4 instructions seem pretty clear about expecting a partition in the response even though it has no data:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/2/21d44aebf85834be33a1f6f6b771742987480182.png)

Unless I’m missing something it seems like CM4 contradicts the Kafka code you posted above..?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 6, 2025, 12:05am UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/10 "2025-05-06T00:05:50Z")

</div>

@estenv You’re absolutely right! My previous reply was incorrect. The filtering only applies to incremental fetches.

The real reason why #fd8 doesn’t expect the empty partition isn’t because it’s empty, but because the request doesn’t ask for it. The request only asks for Partition 0:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/8/8153cf1576d5dc9f747e7485a0a42a92eddb4825.jpeg)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/b/be2e8ff2a6506f76788c420750d366509640415a.png)

Note that the empty partition is Partition 1, which the request doesn’t ask for:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/a/a9d5dd31d2a975dc2538a7e573eba6f53ba8dd15.png)

---

<div class="post-metadata">

**Author:** ![estenv](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/estenv/32/9644_2.png) [@estenv](https://forum.codecrafters.io/u/estenv)\
**Post date:** [May 6, 2025, 8:40am UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/12 "2025-05-06T08:40:21Z")

</div>

Ah, that explains it. I knew I had to be missing something. I was not reading the fetch topic request beyond topic name so I never even realized it specifies partition as well.

Thanks for your help.

As a future improvement I would suggest changing the instructions in these stages:

- The `partitions` array has 1 element, and in that element:
- The `partition_index` field is `0`.

Should rather mention that partition index should match what was requested, that would make it a lot clearer. An instruction change would go a long way even if the tester always looks for index 0, if test case parameters cannot be randomized.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [May 9, 2025, 1:28am UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/15 "2025-05-09T01:28:00Z")

</div>

Quick update: we’ve improved the instructions a bit in [this PR](https://github.com/codecrafters-io/build-your-own-kafka/pull/33/files).

Thanks again for highlighting the issue! @estenv

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [May 18, 2025, 8:34am UTC](https://forum.codecrafters.io/t/question-about-fd8-fetch-multiple-messages-from-disk-expected-partitions-length-to-be-1-got-2/9988/16 "2025-05-18T08:34:15Z")

</div>

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