rreddy-22 commented on code in PR #14182: URL: https://github.com/apache/kafka/pull/14182#discussion_r1300627015
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignmentBuilder.java: ########## @@ -0,0 +1,399 @@ +/* + * 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +import static java.lang.Math.min; + +/** + * Assigns Kafka partitions to members of a consumer group ensuring a balanced distribution with + * considerations for sticky assignments and rack-awareness. + * The order of priority of properties during the assignment will be: balance > rack matching (when applicable) > stickiness. + * + * <p> Here's the step-by-step breakdown of the assignment process: + * + * <ul> + * <li> Compute the quotas of partitions for each member based on the total partitions and member count.</li> + * <li> For existing assignments, retain partitions based on the determined quota and member's rack compatibility. + * <li> If a partition's rack mismatches with its member, track it with its prior owner.</li> + * <li> Identify members that haven't fulfilled their partition quota or are eligible to receive extra partitions.</li> + * <li> Derive the unassigned partitions by taking the difference between total partitions and the sticky assignments.</li> + * <li> Depending on members needing extra partitions, select members from the potentially unfilled list and add them to the unfilled list.</li> + * <li> Proceed with a round-robin assignment adhering to rack awareness. + * For each unassigned partition, locate the first compatible member from the unfilled list.</li> + * <li> If no rack-compatible member is found, revert to the tracked current owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignmentBuilder extends UniformAssignor.AbstractAssignmentBuilder { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignmentBuilder.class); + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final RackInfo rackInfo; + // Count of members to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int remainingMembersToGetExtraPartition; + // Map of members to the remaining number of partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the remaining number of partitions needed to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + private List<TopicIdPartition> unassignedPartitions; + private final Map<String, MemberAssignment> newAssignment; + // Tracks the current owner of each partition when using rack-aware strategy. + // Current refers to the existing assignment. + private final Map<TopicIdPartition, String> currentPartitionOwners; + // Indicates if a rack aware assignment can be done. + // True if racks are defined for both members and partitions. + boolean useRackAwareStrategy; + + OptimizedUniformAssignmentBuilder(AssignmentSpec assignmentSpec, SubscribedTopicDescriber subscribedTopicDescriber) { + this.assignmentSpec = assignmentSpec; + this.subscribedTopicDescriber = subscribedTopicDescriber; + subscriptionList = new ArrayList<>(assignmentSpec.members().values().iterator().next().subscribedTopicIds()); Review Comment: That depends if the members map can be empty I guess which I think is checked before, it definitely can't be null -- 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