You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -743,6 +744,88 @@ consumer.each_message do |message|
743
744
end
744
745
```
745
746
747
+
#### Customizing Partition Assignment Strategy
748
+
749
+
In some cases, you might want to assign more partitions to some consumers. For example, in applications inserting some records to a database, the consumers running on hosts nearby the database can process more messages than the consumers running on other hosts.
750
+
You can use a custom assignment strategy by passing an object that implements `#call` as the argument `assignment_strategy` like below:
751
+
752
+
```ruby
753
+
classCustomAssignmentStrategy
754
+
definitialize(user_data)
755
+
@user_data= user_data
756
+
end
757
+
758
+
# Assign the topic partitions to the group members.
759
+
#
760
+
#@paramcluster[Kafka::Cluster]
761
+
#@parammembers[Hash<String, Kafka::Protocol::JoinGroupResponse::Metadata>] a hash
762
+
# mapping member ids to metadata
763
+
#@parampartitions[Array<Kafka::ConsumerGroup::Assignor::Partition>] a list of
764
+
# partitions the consumer group processes
765
+
#@return[Hash<String, Array<Kafka::ConsumerGroup::Assignor::Partition>] a hash
`members` is a hash mapping member IDs to metadata, and partitions is a list of partitions the consumer group processes. The method `call` must return a hash mapping member IDs to partitions. For example, the following strategy assigns partitions randomly:
777
+
778
+
```ruby
779
+
classRandomAssignmentStrategy
780
+
defcall(cluster:, members:, partitions:)
781
+
member_ids = members.keys
782
+
partitions.each_with_object(Hash.new {|h, k| h[k] = [] }) do |partition, partitions_per_member|
If the strategy needs user data, you should define the method `user_data` that returns user data on each consumer. For example, the following strategy uses the consumers' IP addresses as user data:
As the method `call` might receive different user data from what it expects, you should avoid using the same protocol name as another strategy that uses different user data.
0 commit comments