-
Notifications
You must be signed in to change notification settings - Fork 339
Support custom assignment strategy #846
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
Merged
dasch
merged 7 commits into
zendesk:master
from
abicky:support-custom-assignment-strategy
Aug 17, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bf2547
Support custom assignment strategy
abicky 8e92d34
Introduce Kafka::ConsumerGroup::Assignor
abicky fbd121d
Update CHAGELOG.md
abicky 0f7a9e9
Implement Kafka::ConsumerGroup::Assignor.register_strategy
abicky 76f6f23
Merge branch 'master' into support-custom-assignment-strategy
abicky f1a8162
Support only block call fasion strategy
abicky 1b10aed
Remove Kafka::ConsumerGroup::Assignor.register_strategy
abicky 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
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
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
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
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require "kafka/protocol/member_assignment" | ||
|
||
module Kafka | ||
class ConsumerGroup | ||
|
||
# A consumer group partition assignor | ||
class Assignor | ||
Partition = Struct.new(:topic, :partition_id) | ||
|
||
# @param cluster [Kafka::Cluster] | ||
# @param strategy [Object] an object that implements #protocol_type, | ||
# #user_data, and #assign. | ||
def initialize(cluster:, strategy:) | ||
@cluster = cluster | ||
@strategy = strategy | ||
end | ||
|
||
def protocol_name | ||
@strategy.respond_to?(:protocol_name) ? @strategy.protocol_name : @strategy.class.to_s | ||
end | ||
|
||
def user_data | ||
@strategy.user_data if @strategy.respond_to?(:user_data) | ||
end | ||
|
||
# Assign the topic partitions to the group members. | ||
# | ||
# @param members [Hash<String, Kafka::Protocol::JoinGroupResponse::Metadata>] a hash | ||
# mapping member ids to metadata. | ||
# @param topics [Array<String>] topics | ||
# @return [Hash<String, Kafka::Protocol::MemberAssignment>] a hash mapping member | ||
# ids to assignments. | ||
def assign(members:, topics:) | ||
topic_partitions = topics.flat_map do |topic| | ||
begin | ||
partition_ids = @cluster.partitions_for(topic).map(&:partition_id) | ||
rescue UnknownTopicOrPartition | ||
raise UnknownTopicOrPartition, "unknown topic #{topic}" | ||
end | ||
partition_ids.map {|partition_id| Partition.new(topic, partition_id) } | ||
end | ||
|
||
group_assignment = {} | ||
|
||
members.each_key do |member_id| | ||
group_assignment[member_id] = Protocol::MemberAssignment.new | ||
end | ||
@strategy.call(cluster: @cluster, members: members, partitions: topic_partitions).each do |member_id, partitions| | ||
Array(partitions).each do |partition| | ||
group_assignment[member_id].assign(partition.topic, [partition.partition_id]) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call to split the boilerplate from the real assignment strategy, given it's sufficiently flexible to handle real world strategies. |
||
|
||
group_assignment | ||
rescue Kafka::LeaderNotAvailable | ||
sleep 1 | ||
retry | ||
end | ||
end | ||
end | ||
end |
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
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
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,54 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "kafka/protocol/member_assignment" | ||
|
||
module Kafka | ||
|
||
# A consumer group partition assignment strategy that assigns partitions to | ||
# consumers in a round-robin fashion. | ||
class RoundRobinAssignmentStrategy | ||
def initialize(cluster:) | ||
@cluster = cluster | ||
def protocol_name | ||
"roundrobin" | ||
end | ||
|
||
# Assign the topic partitions to the group members. | ||
# | ||
# @param members [Array<String>] member ids | ||
# @param topics [Array<String>] topics | ||
# @return [Hash<String, Protocol::MemberAssignment>] a hash mapping member | ||
# ids to assignments. | ||
def assign(members:, topics:) | ||
group_assignment = {} | ||
|
||
members.each do |member_id| | ||
group_assignment[member_id] = Protocol::MemberAssignment.new | ||
end | ||
|
||
topic_partitions = topics.flat_map do |topic| | ||
begin | ||
partitions = @cluster.partitions_for(topic).map(&:partition_id) | ||
rescue UnknownTopicOrPartition | ||
raise UnknownTopicOrPartition, "unknown topic #{topic}" | ||
end | ||
Array.new(partitions.count) { topic }.zip(partitions) | ||
end | ||
|
||
partitions_per_member = topic_partitions.group_by.with_index do |_, index| | ||
index % members.count | ||
end.values | ||
|
||
members.zip(partitions_per_member).each do |member_id, member_partitions| | ||
unless member_partitions.nil? | ||
member_partitions.each do |topic, partition| | ||
group_assignment[member_id].assign(topic, [partition]) | ||
end | ||
end | ||
# @param cluster [Kafka::Cluster] | ||
# @param members [Hash<String, Kafka::Protocol::JoinGroupResponse::Metadata>] a hash | ||
# mapping member ids to metadata | ||
# @param partitions [Array<Kafka::ConsumerGroup::Assignor::Partition>] a list of | ||
# partitions the consumer group processes | ||
# @return [Hash<String, Array<Kafka::ConsumerGroup::Assignor::Partition>] a hash | ||
# mapping member ids to partitions. | ||
def call(cluster:, members:, partitions:) | ||
member_ids = members.keys | ||
partitions_per_member = Hash.new {|h, k| h[k] = [] } | ||
partitions.each_with_index do |partition, index| | ||
partitions_per_member[member_ids[index % member_ids.count]] << partition | ||
end | ||
|
||
group_assignment | ||
rescue Kafka::LeaderNotAvailable | ||
sleep 1 | ||
retry | ||
partitions_per_member | ||
end | ||
end | ||
end |
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.
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 forgot to delete
@cluster
💦I deleted the variable and force-pushed (https://github.com/zendesk/ruby-kafka/compare/34d69288a22d5593826c29ec8b787221efe237de..8e92d34b545b819b89595fbeafcb756b384bfea6).