Skip to content

fix: reduce out-of-range partitions modulo the partition count in getSendBrokers - #432

Merged
ShogunPanda merged 1 commit into
platformatic:mainfrom
fadiroot:fix/get-send-brokers-partition-modulo
Oct 1, 2026
Merged

ShogunPanda merged 1 commit into
platformatic:mainfrom
fadiroot:fix/get-send-brokers-partition-modulo

Conversation

@fadiroot

Copy link
Copy Markdown
Contributor

Summary

Producer.getSendBrokers() (and getSendConnections(), which builds on it) computed the target partition with a bitwise AND against the topic's partition count:

const partition = rawPartition & metadata!.topics.get(topic)!.partitionsCount

while send() (producer.ts) and the transaction path (transaction.ts) reduce it with a modulo:

message.partition! %= metadata!.topics.get(message.topic)!.partitionsCount

The two only agree when the partition count is a power of two and the raw partition is below it. For a 3-partition topic:

message partition send() routes to getSendBrokers() before after
3 0 3 & 3 = 3 → partitions[3] is undefined → TypeError: Cannot read properties of undefined (reading 'leader') inside the metadata callback 0
4 1 4 & 3 = 0 → broker reported under topic:0 1

This PR uses the same modulo reduction as send().

Test

getSendBrokers should wrap out-of-range partitions like send does in test/clients/producer/producer.test.ts: a 3-partition topic with messages on partitions 3, 4 and 2 must yield the keys topic:0, topic:1, topic:2. On main it fails with the TypeError above; with this change the five getSendBrokers tests pass against a Kafka 3.9.1 broker. pnpm lint is clean.

…SendBrokers

getSendBrokers (and getSendConnections through it) computed the target
partition with a bitwise AND against the partition count, while send()
and the transaction path reduce it with a modulo. For a topic with 3
partitions, `partition: 3` gave `3 & 3 = 3`, an index that does not
exist, so reading `.leader` threw a TypeError inside the metadata
callback; `partition: 4` gave `4 & 3 = 0` although send() routes it to
`4 % 3 = 1`, so the broker was reported under the wrong key. The AND
form only agrees with the modulo when the count is a power of two.

Use the same modulo reduction as send().

Signed-off-by: Fadi Romdhan <fadiromdhan3@gmail.com>
@ShogunPanda
ShogunPanda merged commit 68d6315 into platformatic:main Oct 1, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants