Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the default partitioner topic-aware when in round-robin mode #1112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/producer/partitioners/default/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ describe('Producer > Partitioner > Default', () => {
expect(partitionCount[2]).toEqual(10)
})

test('messages are partitioned in a round-robin fashion for each topic', () => {
const partitionMetadata = [
{ partitionId: 1, leader: 1 },
{ partitionId: 2, leader: 2 },
]
const topics = [
{ topic: 'topic-a', partitionMetadata, partitionCount: {} },
{ topic: 'topic-b', partitionMetadata, partitionCount: {} },
]

for (let i = 0; i < 30; ++i) {
for (const { topic, partitionMetadata, partitionCount } of topics) {
const partition = partitioner({ topic, partitionMetadata, message: {} })
const count = partitionCount[partition] || 0
partitionCount[partition] = count + 1
}
}

expect(topics[0].partitionCount[1]).toEqual(15)
expect(topics[0].partitionCount[2]).toEqual(15)
expect(topics[1].partitionCount[1]).toEqual(15)
expect(topics[1].partitionCount[2]).toEqual(15)
})

test('returns the configured partition if it exists', () => {
const partition = partitioner({
topic,
Expand Down
9 changes: 6 additions & 3 deletions src/producer/partitioners/default/partitioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const toPositive = x => x & 0x7fffffff
* - If no partition or key is present choose a partition in a round-robin fashion
*/
module.exports = murmur2 => () => {
let counter = randomBytes(32).readUInt32BE(0)
const counters = {}

return ({ topic, partitionMetadata, message }) => {
if (!(topic in counters)) {
counters[topic] = randomBytes(32).readUInt32BE(0)
}
const numPartitions = partitionMetadata.length
const availablePartitions = partitionMetadata.filter(p => p.leader >= 0)
const numAvailablePartitions = availablePartitions.length
Expand All @@ -34,11 +37,11 @@ module.exports = murmur2 => () => {
}

if (numAvailablePartitions > 0) {
const i = toPositive(++counter) % numAvailablePartitions
const i = toPositive(++counters[topic]) % numAvailablePartitions
return availablePartitions[i].partitionId
}

// no partitions are available, give a non-available partition
return toPositive(++counter) % numPartitions
return toPositive(++counters[topic]) % numPartitions
}
}