-
-
Notifications
You must be signed in to change notification settings - Fork 527
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
Range partition assigner #1631
Open
pimpelsang
wants to merge
3
commits into
tulios:master
Choose a base branch
from
pimpelsang:range-partition-assigner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Range partition assigner #1631
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const roundRobin = require('./roundRobinAssigner') | ||
const range = require('./rangeAssigner') | ||
|
||
module.exports = { | ||
roundRobin, | ||
range, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { MemberMetadata, MemberAssignment } = require('../../assignerProtocol') | ||
|
||
/** | ||
* RangeAssigner | ||
* @type {import('types').PartitionAssigner} | ||
*/ | ||
module.exports = ({ cluster }) => ({ | ||
name: 'RangeAssigner', | ||
version: 0, | ||
|
||
/** | ||
* Assign the topics to the provided members using range strategy. | ||
* | ||
* The members array contains information about each member, `memberMetadata` is the result of the | ||
* `protocol` operation. | ||
* | ||
* @param {object} group | ||
* @param {import('types').GroupMember[]} group.members array of members, e.g: | ||
[{ memberId: 'test-5f93f5a3', memberMetadata: Buffer }] | ||
* @param {string[]} group.topics | ||
* @returns {Promise<import('types').GroupMemberAssignment[]>} object partitions per topic per member | ||
*/ | ||
async assign({ members, topics }) { | ||
// logic inspired from kafka java client: | ||
// https://kafka.apache.org/24/javadoc/org/apache/kafka/clients/consumer/RangeAssignor.html | ||
// https://github.com/a0x8o/kafka/blob/master/clients/src/main/java/org/apache/kafka/clients/consumer/RangeAssignor.java | ||
|
||
const sortedMembers = members.map(({ memberId }) => memberId).sort() | ||
const membersCount = sortedMembers.length | ||
const assignment = {} | ||
|
||
for (const topic of topics) { | ||
const partitionMetadata = cluster.findTopicPartitionMetadata(topic) | ||
|
||
const numPartitionsForTopic = partitionMetadata.length | ||
const numPartitionsPerConsumer = Math.floor(numPartitionsForTopic / membersCount) | ||
const consumersWithExtraPartition = numPartitionsForTopic % membersCount | ||
|
||
for (var i = 0; i < membersCount; i++) { | ||
const start = numPartitionsPerConsumer * i + Math.min(i, consumersWithExtraPartition) | ||
const length = numPartitionsPerConsumer + (i + 1 > consumersWithExtraPartition ? 0 : 1) | ||
|
||
const assignee = sortedMembers[i] | ||
|
||
for (let partition = start; partition < start + length; partition++) { | ||
if (!assignment[assignee]) { | ||
assignment[assignee] = Object.create(null) | ||
} | ||
|
||
if (!assignment[assignee][topic]) { | ||
assignment[assignee][topic] = [] | ||
} | ||
|
||
assignment[assignee][topic].push(partition) | ||
} | ||
} | ||
} | ||
|
||
return Object.keys(assignment).map(memberId => ({ | ||
memberId, | ||
memberAssignment: MemberAssignment.encode({ | ||
version: this.version, | ||
assignment: assignment[memberId], | ||
}), | ||
})) | ||
}, | ||
|
||
protocol({ topics }) { | ||
return { | ||
name: this.name, | ||
metadata: MemberMetadata.encode({ | ||
version: this.version, | ||
topics, | ||
}), | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
const RoundRobinAssigner = require('./index') | ||
const { MemberAssignment, MemberMetadata } = require('../../assignerProtocol') | ||
|
||
describe('Consumer > assigners > RoundRobinAssigner', () => { | ||
let cluster, topics, metadata, assigner | ||
|
||
beforeEach(() => { | ||
metadata = {} | ||
cluster = { findTopicPartitionMetadata: topic => metadata[topic] } | ||
assigner = RoundRobinAssigner({ cluster }) | ||
}) | ||
|
||
describe('#assign', () => { | ||
test('java example topic-partitions assignment', async () => { | ||
topics = ['t0', 't1'] | ||
metadata['t0'] = Array(3) | ||
.fill() | ||
.map((_, i) => ({ partitionId: i })) | ||
|
||
metadata['t1'] = Array(3) | ||
.fill() | ||
.map((_, i) => ({ partitionId: i })) | ||
|
||
const members = [{ memberId: 'C0' }, { memberId: 'C1' }] | ||
|
||
const assignment = await assigner.assign({ members, topics }) | ||
|
||
expect(assignment).toEqual([ | ||
{ | ||
memberId: 'C0', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
t0: [0, 1], | ||
t1: [0, 1], | ||
}, | ||
}), | ||
}, | ||
{ | ||
memberId: 'C1', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
t0: [2], | ||
t1: [2], | ||
}, | ||
}), | ||
}, | ||
]) | ||
}) | ||
|
||
test('more complex topic-partitions assignment', async () => { | ||
topics = ['topic-A', 'topic-B', 'topic-C'] | ||
|
||
metadata['topic-A'] = Array(12) | ||
.fill() | ||
.map((_, i) => ({ partitionId: i })) | ||
|
||
metadata['topic-B'] = Array(12) | ||
.fill() | ||
.map((_, i) => ({ partitionId: i })) | ||
|
||
metadata['topic-C'] = Array(3) | ||
.fill() | ||
.map((_, i) => ({ partitionId: i })) | ||
|
||
const members = [ | ||
{ memberId: 'member-3' }, | ||
{ memberId: 'member-1' }, | ||
{ memberId: 'member-4' }, | ||
{ memberId: 'member-2' }, | ||
] | ||
|
||
const assignment = await assigner.assign({ members, topics }) | ||
|
||
expect(assignment).toEqual([ | ||
{ | ||
memberId: 'member-1', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
'topic-A': [0, 1, 2], | ||
'topic-B': [0, 1, 2], | ||
'topic-C': [0], | ||
}, | ||
}), | ||
}, | ||
{ | ||
memberId: 'member-2', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
'topic-A': [3, 4, 5], | ||
'topic-B': [3, 4, 5], | ||
'topic-C': [1], | ||
}, | ||
}), | ||
}, | ||
{ | ||
memberId: 'member-3', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
'topic-A': [6, 7, 8], | ||
'topic-B': [6, 7, 8], | ||
'topic-C': [2], | ||
}, | ||
}), | ||
}, | ||
{ | ||
memberId: 'member-4', | ||
memberAssignment: MemberAssignment.encode({ | ||
version: assigner.version, | ||
assignment: { | ||
'topic-A': [9, 10, 11], | ||
'topic-B': [9, 10, 11], | ||
}, | ||
}), | ||
}, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('#protocol', () => { | ||
test('returns the assigner name and metadata', () => { | ||
expect(assigner.protocol({ topics })).toEqual({ | ||
name: assigner.name, | ||
metadata: MemberMetadata.encode({ version: assigner.version, topics }), | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have 3 levels of for loop which doesn't seem ideal, on the other hand, this is only used once assigning members to the consumers right? So it should be fine I guess. I wanted to ask if you have reasoned about this before as well?
Edit: Well, I noticed the same behaviour from the java version as well :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, same flow in java client. There isn't that much data to loop through and like you said it happens once. I see no reason to optimise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM