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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/AbstractUniformAssignmentBuilder.java:
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.common.Uuid;
+import org.apache.kafka.server.common.TopicIdPartition;
+
+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 assignment builder is used to construct the target assignment based on 
the members' subscriptions.
+ *
+ * This class contains common utility methods and a class for obtaining and 
storing rack information.
+ */
+public abstract class AbstractUniformAssignmentBuilder {
+    protected abstract GroupAssignment buildAssignment();
+
+    /**
+     * Determines if rack-aware assignment is appropriate based on the 
provided rack information.
+     *
+     * @param memberRacks               Racks where members are located.
+     * @param allPartitionRacks         Racks where partitions are located.
+     * @param racksPerPartition         Map of partitions to their associated 
racks.
+     *
+     * @return {@code true} if rack-aware assignment should be applied; {@code 
false} otherwise.
+     */
+    protected static boolean useRackAwareAssignment(
+        Set<String> memberRacks,
+        Set<String> allPartitionRacks,
+        Map<TopicIdPartition, Set<String>> racksPerPartition
+    ) {
+        if (memberRacks.isEmpty() || Collections.disjoint(memberRacks, 
allPartitionRacks))
+            return false;
+        else {
+            return 
!racksPerPartition.values().stream().allMatch(allPartitionRacks::equals);
+        }
+    }
+
+    /**
+     * Adds the topic's partition to the member's target assignment.
+     */
+    protected static void addPartitionToAssignment(
+        Map<String, MemberAssignment> memberAssignments,
+        String memberId,
+        Uuid topicId,
+        int partition
+    ) {
+        memberAssignments.get(memberId)
+            .targetPartitions()
+            .computeIfAbsent(topicId, __ -> new HashSet<>())
+            .add(partition);
+    }
+
+    /**
+     * Constructs a list of {@code TopicIdPartition} for each topic Id based 
on its partition count.
+     *
+     * @param allTopicIds                   The subscribed topic Ids.
+     * @param subscribedTopicDescriber      Utility to fetch the partition 
count for a given topic.
+     *
+     * @return List of sorted {@code TopicIdPartition} for all provided topic 
Ids.
+     */
+    protected static List<TopicIdPartition> allTopicIdPartitions(
+        Collection<Uuid> allTopicIds,
+        SubscribedTopicDescriber subscribedTopicDescriber
+    ) {
+        List<TopicIdPartition> allTopicIdPartitions = new ArrayList<>();
+        // Sorted so that partitions from each topic can be distributed 
amongst its subscribers equally.
+        allTopicIds.stream().sorted().forEach(topic ->
+            IntStream.range(0, subscribedTopicDescriber.numPartitions(topic))
+                .forEach(i -> allTopicIdPartitions.add(new 
TopicIdPartition(topic, i)))
+        );
+
+        return allTopicIdPartitions;
+    }
+
+    /**
+     * Represents the rack information of members and partitions along with 
utility methods
+     * to facilitate rack-aware assignment strategies for a given consumer 
group.
+     */
+    protected static class RackInfo {
+        /**
+         * Map of every member to its rack.
+         */
+        protected final Map<String, String> memberRacks;
+
+        /**
+         * Map of every partition to a list of its racks.
+         */
+        protected final Map<TopicIdPartition, Set<String>> partitionRacks;
+
+        /**
+         * List of members with the same rack as the partition.
+         */
+        protected final Map<TopicIdPartition, List<String>> 
membersWithSameRackAsPartition;
+
+        /**
+         * Indicates if a rack aware assignment can be done.
+         * True if racks are defined for both members and partitions and there 
is an intersection between the sets.
+         */
+        protected final boolean useRackStrategy;
+
+        /**
+         * Constructs rack information based on the assignment specification 
and subscribed topics.
+         *
+         * @param assignmentSpec                The current assignment 
specification.
+         * @param subscribedTopicDescriber      Topic and partition metadata 
of the subscribed topics.
+         * @param topicIds                      List of topic Ids.
+         */
+        public RackInfo(
+            AssignmentSpec assignmentSpec,
+            SubscribedTopicDescriber subscribedTopicDescriber,
+            Set<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, __ -> new 
ArrayList<>()).add(memberId)
+                )
+            );
+
+            Set<String> allPartitionRacks;
+            Map<TopicIdPartition, Set<String>> racksPerPartition;
+            List<TopicIdPartition> topicIdPartitions = 
allTopicIdPartitions(topicIds, subscribedTopicDescriber);
+
+            if (membersByRack.isEmpty()) {
+                allPartitionRacks = Collections.emptySet();
+                racksPerPartition = Collections.emptyMap();
+            } else {
+                racksPerPartition = new HashMap<>();
+                allPartitionRacks = new HashSet<>();
+                topicIdPartitions.forEach(tp -> {

Review Comment:
   yes agreed, I changed it now, thanks!



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to