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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/AbstractUniformAssignmentBuilder.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.coordinator.group.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 partitionRacks        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> partitionRacks,
+        Map<TopicIdPartition, Set<String>> racksPerPartition
+    ) {
+        if (memberRacks.isEmpty() || Collections.disjoint(memberRacks, 
partitionRacks))
+            return false;
+        else {
+            return 
!racksPerPartition.values().stream().allMatch(partitionRacks::equals);
+        }
+    }
+
+    /**
+     * Adds the topic's partition to the member's target assignment.
+     */
+    protected static void addPartitionToAssignment(
+        int partition,
+        Uuid topicId,
+        String memberId,
+        Map<String, MemberAssignment> targetAssignment
+    ) {
+        targetAssignment.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))
+                )

Review Comment:
   I wasn't sure where it went tbh xD



-- 
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