-
Notifications
You must be signed in to change notification settings - Fork 14.5k
KAFKA-17747: [7/N] Add consumer group integration test for rack aware assignment #19856
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
Changes from all commits
392b850
af5a620
af08e87
cf58ce6
a3104f8
1b92ef7
55daa01
fe83294
a979564
157d171
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.clients.consumer; | ||
|
||
import org.apache.kafka.common.Uuid; | ||
import org.apache.kafka.coordinator.group.api.assignor.ConsumerGroupPartitionAssignor; | ||
import org.apache.kafka.coordinator.group.api.assignor.GroupAssignment; | ||
import org.apache.kafka.coordinator.group.api.assignor.GroupSpec; | ||
import org.apache.kafka.coordinator.group.api.assignor.MemberAssignment; | ||
import org.apache.kafka.coordinator.group.api.assignor.PartitionAssignorException; | ||
import org.apache.kafka.coordinator.group.api.assignor.SubscribedTopicDescriber; | ||
import org.apache.kafka.coordinator.group.modern.MemberAssignmentImpl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* The RackAwareAssignor is a consumer group partition assignor that takes into account the rack | ||
* information of the members when assigning partitions to them. | ||
* It needs all brokers and members to have rack information available. | ||
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. It also requires all member to subscribe same topics, right? 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. Yes, this only checks subscription of first member and assume all members use same subscription. This is a sample assignor under test module. Do we want to make it support heterogeneous group? 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.
no, it is used by testing only so it should be fine. |
||
*/ | ||
public class RackAwareAssignor implements ConsumerGroupPartitionAssignor { | ||
@Override | ||
public String name() { | ||
return "rack-aware-assignor"; | ||
} | ||
|
||
@Override | ||
public GroupAssignment assign(GroupSpec groupSpec, SubscribedTopicDescriber subscribedTopicDescriber) throws PartitionAssignorException { | ||
Map<String, String> rackIdToMemberId = new HashMap<>(); | ||
List<String> memberIds = new ArrayList<>(groupSpec.memberIds()); | ||
for (String memberId : memberIds) { | ||
if (groupSpec.memberSubscription(memberId).rackId().isEmpty()) { | ||
throw new PartitionAssignorException("Member " + memberId + " does not have rack information available."); | ||
} | ||
rackIdToMemberId.put( | ||
groupSpec.memberSubscription(memberId).rackId().get(), | ||
memberId | ||
); | ||
} | ||
|
||
Map<String, Map<Uuid, Set<Integer>>> assignments = new HashMap<>(); | ||
for (Uuid topicId : groupSpec.memberSubscription(memberIds.get(0)).subscribedTopicIds()) { | ||
int numPartitions = subscribedTopicDescriber.numPartitions(topicId); | ||
if (numPartitions == -1) { | ||
throw new PartitionAssignorException("Member is subscribed to a non-existent topic"); | ||
} | ||
|
||
for (int partitionId = 0; partitionId < numPartitions; partitionId++) { | ||
Set<String> racks = subscribedTopicDescriber.racksForPartition(topicId, partitionId); | ||
if (racks.isEmpty()) { | ||
throw new PartitionAssignorException("No racks available for partition " + partitionId + " of topic " + topicId); | ||
} | ||
|
||
String assignedRack = null; | ||
for (String rack : racks) { | ||
String memberId = rackIdToMemberId.get(rack); | ||
if (memberId == null) { | ||
continue; | ||
} | ||
assignedRack = rack; | ||
break; | ||
} | ||
|
||
if (assignedRack == null) { | ||
throw new PartitionAssignorException("No member found for racks " + racks + " for partition " + partitionId + " of topic " + topicId); | ||
} | ||
|
||
Map<Uuid, Set<Integer>> assignment = assignments.computeIfAbsent( | ||
rackIdToMemberId.get(assignedRack), | ||
k -> new HashMap<>() | ||
); | ||
Set<Integer> partitions = assignment.computeIfAbsent( | ||
topicId, | ||
k -> new java.util.HashSet<>() | ||
); | ||
partitions.add(partitionId); | ||
} | ||
} | ||
|
||
Map<String, MemberAssignment> memberAssignments = new HashMap<>(); | ||
for (Map.Entry<String, Map<Uuid, Set<Integer>>> entry : assignments.entrySet()) { | ||
memberAssignments.put(entry.getKey(), new MemberAssignmentImpl(entry.getValue())); | ||
} | ||
return new GroupAssignment(memberAssignments); | ||
} | ||
} |
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 wonder whether we could add another test which verifies that a rebalance is triggered when the racks of a partition has changed. Have you considered it?
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, I added it to the same case. It uses
alterPartitionReassignments
to change partitions to different brokers and make sure consumers get new assignments.