-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Kafka::ConsumerGroup::Assignor
The assignor is responsible for getting partition information and building an assignment from the result of its strategy. That simplifies assignment strategies.
- Loading branch information
Showing
8 changed files
with
182 additions
and
119 deletions.
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
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.protocol_name | ||
end | ||
|
||
def user_data | ||
@strategy.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.assign(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 | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Kafka::ConsumerGroup::Assignor do | ||
let(:cluster) { double(:cluster) } | ||
let(:assignor) { described_class.new(cluster: cluster, strategy: strategy) } | ||
let(:strategy) do | ||
klass = Class.new do | ||
def protocol_name | ||
"test" | ||
end | ||
|
||
def user_data | ||
nil | ||
end | ||
|
||
def assign(members:, partitions:) | ||
assignment = {} | ||
partition_count_per_member = (partitions.count.to_f / members.count).ceil | ||
partitions.each_slice(partition_count_per_member).with_index do |chunk, index| | ||
assignment[members.keys[index]] = chunk | ||
end | ||
|
||
assignment | ||
end | ||
end | ||
klass.new | ||
end | ||
|
||
it "assigns all partitions" do | ||
members = Hash[(0...10).map {|i| ["member#{i}", nil] }] | ||
topics = ["greetings"] | ||
partitions = (0...30).map {|i| double(:"partition#{i}", partition_id: i) } | ||
|
||
allow(cluster).to receive(:partitions_for) { partitions } | ||
|
||
assignments = assignor.assign(members: members, topics: topics) | ||
|
||
partitions.each do |partition| | ||
member = assignments.values.find {|assignment| | ||
assignment.topics.find {|topic, partitions| | ||
partitions.include?(partition.partition_id) | ||
} | ||
} | ||
|
||
expect(member).to_not be_nil | ||
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
Oops, something went wrong.