rreddy-22 commented on code in PR #14182:
URL: https://github.com/apache/kafka/pull/14182#discussion_r1312619017


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/UniformAssignor.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.coordinator.group.assignor;
+
+import org.apache.kafka.coordinator.group.common.TopicIdPartition;
+import org.apache.kafka.common.Uuid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * The Uniform Assignor distributes Kafka topic partitions among group members 
for balanced assignment.
+ * The assignor employs two different strategies based on the nature of topic
+ * subscriptions across the group members:
+ * <ul>
+ *     <li>
+ *         <b> Optimized Uniform Assignment Builder: </b> This strategy is 
used when all members have subscribed
+ *         to the same set of topics.
+ *     </li>
+ *     <li>
+ *         <b> General Uniform Assignment Builder: </b> This strategy is used 
when members have varied topic
+ *         subscriptions.
+ *     </li>
+ * </ul>
+ *
+ * The appropriate strategy is automatically chosen based on the current 
members' topic subscriptions.
+ *
+ * @see OptimizedUniformAssignmentBuilder
+ * @see GeneralUniformAssignmentBuilder
+ */
+public class UniformAssignor implements PartitionAssignor {
+    private static final Logger log = 
LoggerFactory.getLogger(UniformAssignor.class);
+    public static final String UNIFORM_ASSIGNOR_NAME = "uniform";
+
+    @Override
+    public String name() {
+        return UNIFORM_ASSIGNOR_NAME;
+    }
+
+    /**
+     * Perform the group assignment given the current members and
+     * topic metadata.
+     *
+     * @param assignmentSpec                The member assignment spec.
+     * @param subscribedTopicDescriber      The topic and cluster metadata 
describer {@link SubscribedTopicDescriber}.
+     * @return The new assignment for the group.
+     */
+    @Override
+    public GroupAssignment assign(
+        AssignmentSpec assignmentSpec,
+        SubscribedTopicDescriber subscribedTopicDescriber
+    ) throws PartitionAssignorException {
+
+        AbstractAssignmentBuilder assignmentBuilder;
+        if (allSubscriptionsEqual(assignmentSpec.members())) {
+            log.debug("Detected that all members are subscribed to the same 
set of topics, invoking the "
+                + "optimized assignment algorithm");
+            assignmentBuilder = new 
OptimizedUniformAssignmentBuilder(assignmentSpec, subscribedTopicDescriber);
+        } else {
+            assignmentBuilder = new GeneralUniformAssignmentBuilder();
+            log.debug("Detected that all members are subscribed to a different 
set of topics, invoking the "
+                + "general assignment algorithm");
+        }
+        return assignmentBuilder.buildAssignment();
+    }
+
+    /**
+     * Determines if all members are subscribed to the same list of topic IDs.
+     *
+     * @param members A map of member identifiers to their respective {@code 
AssignmentMemberSpec}.
+     *                Assumes the map is non-empty.
+     * @return true if all members have the same subscription list of topic 
IDs,
+     *         false otherwise.
+     */
+    private boolean allSubscriptionsEqual(Map<String, AssignmentMemberSpec> 
members) {
+        boolean allSubscriptionsEqual = true;
+        Collection<Uuid> firstSubscriptionList = 
members.values().iterator().next().subscribedTopicIds();
+        for (AssignmentMemberSpec memberSpec : members.values()) {
+            if 
(!firstSubscriptionList.equals(memberSpec.subscribedTopicIds())) {
+                allSubscriptionsEqual = false;
+                break;
+            }
+        }
+        return allSubscriptionsEqual;
+    }
+
+    protected static abstract class AbstractAssignmentBuilder {
+        protected abstract GroupAssignment buildAssignment();
+
+        protected boolean useRackAwareAssignment(
+            Set<String> consumerRacks,
+            Set<String> partitionRacks,
+            Map<TopicIdPartition, Set<String>> racksPerPartition
+        ) {
+            if (consumerRacks.isEmpty() || Collections.disjoint(consumerRacks, 
partitionRacks))
+                return false;
+            else {
+                return 
!racksPerPartition.values().stream().allMatch(partitionRacks::equals);
+            }
+        }
+
+        protected List<TopicIdPartition> getAllTopicIdPartitions(
+            List<Uuid> listAllTopics,
+            SubscribedTopicDescriber subscribedTopicDescriber
+        ) {
+            List<TopicIdPartition> allTopicIdPartitions = new ArrayList<>();
+            listAllTopics.forEach(topic ->
+                IntStream.range(0, 
subscribedTopicDescriber.numPartitions((topic)))
+                    .forEach(i -> allTopicIdPartitions.add(new 
TopicIdPartition(topic, i))
+                )
+            );
+            
+            return allTopicIdPartitions;
+        }
+
+        protected class RackInfo {
+            protected final Map<String, String> memberRacks;
+            protected final Map<TopicIdPartition, Set<String>> partitionRacks;
+            private final Map<TopicIdPartition, Integer> numMembersByPartition;
+
+            public RackInfo(
+                AssignmentSpec assignmentSpec,
+                SubscribedTopicDescriber subscribedTopicDescriber,
+                List<Uuid> topicIds
+            ) {
+                Map<String, List<String>> membersByRack = new HashMap<>();
+                assignmentSpec.members().forEach((memberId, 
assignmentMemberSpec) ->
+                    assignmentMemberSpec.rackId().filter(r -> 
!r.isEmpty()).ifPresent(
+                        rackId -> membersByRack.computeIfAbsent(rackId, k -> 
new ArrayList<>()).add(memberId)
+                    )
+                );
+
+                Set<String> allPartitionRacks;
+                Map<TopicIdPartition, Set<String>> partitionRacks;
+                List<TopicIdPartition> topicIdPartitions = 
getAllTopicIdPartitions(topicIds, subscribedTopicDescriber);
+
+                if (membersByRack.isEmpty()) {
+                    allPartitionRacks = Collections.emptySet();
+                    partitionRacks = Collections.emptyMap();
+                } else {
+                    partitionRacks = new HashMap<>();
+                    allPartitionRacks = new HashSet<>();
+                    topicIdPartitions.forEach(tp -> {
+                        Set<String> racks = 
subscribedTopicDescriber.racksForPartition(tp.topicId(), tp.partition());
+                        partitionRacks.put(tp, racks);
+                        if (!racks.isEmpty()) allPartitionRacks.addAll(racks);
+                    });
+                }
+
+                if (useRackAwareAssignment(membersByRack.keySet(), 
allPartitionRacks, partitionRacks)) {
+                    this.memberRacks = new 
HashMap<>(assignmentSpec.members().size());
+                    membersByRack.forEach((rack, rackConsumers) -> 
rackConsumers.forEach(c -> memberRacks.put(c, rack)));
+                    this.partitionRacks = partitionRacks;
+                } else {
+                    this.memberRacks = Collections.emptyMap();
+                    this.partitionRacks = Collections.emptyMap();
+                }
+
+                numMembersByPartition = partitionRacks.entrySet().stream()
+                    .collect(Collectors.toMap(Map.Entry::getKey, e -> 
e.getValue().stream()
+                        .map(r -> membersByRack.getOrDefault(r, 
Collections.emptyList()).size())
+                        .reduce(0, Integer::sum)));
+            }
+
+            /**
+             * Determines if there's a mismatch between the memberId's rack 
and the partition's replica racks.
+             *
+             * <p> Mismatch conditions (returns {@code true}):
+             * <ul>
+             *     <li> Consumer lacks an associated rack.</li>
+             *     <li> Partition lacks associated replica racks.</li>
+             *     <li> Consumer's rack isn't among the partition's replica 
racks.</li>
+             * </ul>
+             *
+             * @param memberId      The memberId identifier.
+             * @param tp            The topic partition in question.
+             * @return {@code true} for a mismatch; {@code false} if member 
and partition racks exist and align.
+             */
+            protected boolean racksMismatch(String memberId, TopicIdPartition 
tp) {
+                String consumerRack = memberRacks.get(memberId);
+                Set<String> replicaRacks = partitionRacks.get(tp);
+                return consumerRack == null || (replicaRacks == null || 
!replicaRacks.contains(consumerRack));
+            }
+
+            /**
+             * Sorts the given list of partitions based on the number of 
consumers available for each partition
+             * in a rack-aware manner.
+             *
+             * @param partitions    The list of partitions to be sorted.
+             * @return A sorted linked list of partitions with potential 
members in the same rack.
+             */
+            protected List<TopicIdPartition> 
sortPartitionsByRackConsumers(List<TopicIdPartition> partitions) {

Review Comment:
   numMembersByPartition is a map that's part of the rack info which tracks how 
many members are in the same rack as the partition. The sum of those members is 
used to sort them right, that's the "rack aware manner". Sorting happens as 
expected since I put in print statements to verify them but also without that 
the assignments wouldn't be as expected and we do a 1:1 check on every 
assignment in the test cases



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to