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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/UniformAssignor.java:
##########
@@ -0,0 +1,288 @@
+/*
+ * 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.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 (assignmentSpec.members().isEmpty())
+            return new GroupAssignment(Collections.emptyMap());
+
+        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) {
+        Set<Uuid> firstSubscriptionSet = new 
HashSet<>(members.values().iterator().next().subscribedTopicIds());
+        for (AssignmentMemberSpec memberSpec : members.values()) {
+            Set<Uuid> currentSubscriptionSet = new 
HashSet<>(memberSpec.subscribedTopicIds());
+            if (!firstSubscriptionSet.equals(currentSubscriptionSet)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * The assignment builder is used to construct the final assignment in a 
series of steps that
+     * are determined by the type of subscriptions.
+     *
+     * There are common methods present that are used by any type of 
assignment strategy.
+     */
+     protected static abstract class AbstractAssignmentBuilder {
+        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 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);
+            }
+        }
+
+        /**
+         * Constructs a list of {@code TopicIdPartition} for each topic Id 
based on its partition count.
+         *
+         * @param allTopicIds                   The list of subscribed topic 
Ids.
+         * @param subscribedTopicDescriber      Utility to fetch the partition 
count for a given topic.
+         *
+         * @return List of generated {@code TopicIdPartition} for all provided 
topic Ids.
+         */
+        protected List<TopicIdPartition> allTopicIdPartitions(
+            List<Uuid> allTopicIds,
+            SubscribedTopicDescriber subscribedTopicDescriber
+        ) {
+            List<TopicIdPartition> allTopicIdPartitions = new ArrayList<>();
+            allTopicIds.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 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;
+            /**
+             * Number of members with the same rack as the partition.
+             */
+            private final Map<TopicIdPartition, Integer> 
numMembersWithSameRackByPartition;
+            protected final boolean useRackStrategy;
+
+            /**
+             * Constructs rack information based on 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>> partitionRacks;
+                List<TopicIdPartition> topicIdPartitions = 
allTopicIdPartitions(new ArrayList<>(topicIds), subscribedTopicDescriber);

Review Comment:
   I believe it's easier to understand since allTopicIdPartitions is a method 
that returns this list which is used later on no?



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