jeffkbkim commented on code in PR #14182: URL: https://github.com/apache/kafka/pull/14182#discussion_r1290585581
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * Review Comment: I would include the priorities when considering uniform vs. sticky vs. rack-aware ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the number of partitions they still need to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + // Partitions that still need to be assigned. Review Comment: i don't think we need this comment ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the number of partitions they still need to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + // Partitions that still need to be assigned. + private List<TopicIdPartition> unassignedPartitions; + private final Map<String, MemberAssignment> newAssignment; + // Tracks the previous owner of each partition when using rack-aware strategy. + private final Map<TopicIdPartition, String> partitionToPrevOwner; + // Indicates if a rack aware assignment can be done. + // True if racks are defined for both members and partitions. + boolean useRackAwareStrategy; + + OptimizedUniformAssignor(AssignmentSpec assignmentSpec, SubscribedTopicDescriber subscribedTopicDescriber) { + this.subscribedTopicDescriber = subscribedTopicDescriber; + this.assignmentSpec = assignmentSpec; Review Comment: nit: can we match the argument ordering? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the number of partitions they still need to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + // Partitions that still need to be assigned. + private List<TopicIdPartition> unassignedPartitions; + private final Map<String, MemberAssignment> newAssignment; + // Tracks the previous owner of each partition when using rack-aware strategy. + private final Map<TopicIdPartition, String> partitionToPrevOwner; Review Comment: how's `previousOwners`? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; Review Comment: i'm noticing a pattern where we are using class variables when they can just be method variables. we should aim to use the smallest scope for all variables. ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, Review Comment: is this "remaining number of partitions"? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the number of partitions they still need to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + // Partitions that still need to be assigned. + private List<TopicIdPartition> unassignedPartitions; + private final Map<String, MemberAssignment> newAssignment; + // Tracks the previous owner of each partition when using rack-aware strategy. + private final Map<TopicIdPartition, String> partitionToPrevOwner; + // Indicates if a rack aware assignment can be done. + // True if racks are defined for both members and partitions. + boolean useRackAwareStrategy; + + OptimizedUniformAssignor(AssignmentSpec assignmentSpec, SubscribedTopicDescriber subscribedTopicDescriber) { + this.subscribedTopicDescriber = subscribedTopicDescriber; + this.assignmentSpec = assignmentSpec; + + subscriptionList = new ArrayList<>(assignmentSpec.members().values().iterator().next().subscribedTopicIds()); + + int totalPartitionsCount = 0; + // Removes the current topic from subscriptionList if the topic doesn't exist in the topic metadata. + Iterator<Uuid> iterator = subscriptionList.iterator(); + while (iterator.hasNext()) { + Uuid topicId = iterator.next(); + int partitionCount = subscribedTopicDescriber.numPartitions(topicId); + if (partitionCount == -1) { + log.warn("Members are subscribed to topic " + topicId + " which doesn't exist in the topic metadata."); + iterator.remove(); + } else { + totalPartitionsCount += partitionCount; + } + } + + RackInfo rackInfo = new RackInfo(assignmentSpec, subscribedTopicDescriber, subscriptionList); + this.rackInfo = rackInfo; + + // Without rack-aware strategy, tracking previous owners of unassigned partitions is unnecessary + // as all sticky partitions are retained until a member meets its quota. + if (rackInfo.memberRacks.isEmpty() || rackInfo.partitionRacks.isEmpty()) { + this.useRackAwareStrategy = false; + partitionToPrevOwner = Collections.emptyMap(); + } else { + this.useRackAwareStrategy = true; + partitionToPrevOwner = new HashMap<>(); + } + + int numberOfMembers = assignmentSpec.members().size(); + minQuota = (int) Math.floor(((double) totalPartitionsCount) / numberOfMembers); + expectedNumMembersWithExtraPartition = totalPartitionsCount % numberOfMembers; + + potentiallyUnfilledMembers = new HashMap<>(); + unfilledMembers = new HashMap<>(); + newAssignment = new HashMap<>(); Review Comment: why is all of this under the constructor? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; Review Comment: is "expected" in the name bring necessary? ########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/OptimizedUniformAssignor.java: ########## @@ -0,0 +1,396 @@ +/* + * 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. + * + * <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 previous owner. + * If that member can't accommodate the partition due to quota limits, resort to a generic round-robin assignment.</li> + * </ul> + */ +public class OptimizedUniformAssignor extends UniformAssignor { + private static final Logger log = LoggerFactory.getLogger(OptimizedUniformAssignor.class); + // List of topics subscribed to by all members. + private final List<Uuid> subscriptionList; + private final AssignmentSpec assignmentSpec; + private final SubscribedTopicDescriber subscribedTopicDescriber; + private final RackInfo rackInfo; + // The minimum required quota that each member needs to meet for a balanced assignment. + // This is the same for all members. + private final int minQuota; + // Count of members expected to receive an extra partition beyond the minimum quota, + // to account for the distribution of the remaining partitions. + private int expectedNumMembersWithExtraPartition; + // Map of members to their remaining partitions needed to meet the minimum quota, + // including members eligible for an extra partition. + private final Map<String, Integer> potentiallyUnfilledMembers; + // Members mapped to the number of partitions they still need to meet the full quota. + // Full quota = minQuota + one extra partition (if applicable). + private Map<String, Integer> unfilledMembers; + // Partitions that still need to be assigned. + private List<TopicIdPartition> unassignedPartitions; + private final Map<String, MemberAssignment> newAssignment; + // Tracks the previous owner of each partition when using rack-aware strategy. + private final Map<TopicIdPartition, String> partitionToPrevOwner; + // Indicates if a rack aware assignment can be done. + // True if racks are defined for both members and partitions. + boolean useRackAwareStrategy; + + OptimizedUniformAssignor(AssignmentSpec assignmentSpec, SubscribedTopicDescriber subscribedTopicDescriber) { + this.subscribedTopicDescriber = subscribedTopicDescriber; + this.assignmentSpec = assignmentSpec; + + subscriptionList = new ArrayList<>(assignmentSpec.members().values().iterator().next().subscribedTopicIds()); + + int totalPartitionsCount = 0; + // Removes the current topic from subscriptionList if the topic doesn't exist in the topic metadata. + Iterator<Uuid> iterator = subscriptionList.iterator(); + while (iterator.hasNext()) { + Uuid topicId = iterator.next(); + int partitionCount = subscribedTopicDescriber.numPartitions(topicId); + if (partitionCount == -1) { + log.warn("Members are subscribed to topic " + topicId + " which doesn't exist in the topic metadata."); + iterator.remove(); + } else { + totalPartitionsCount += partitionCount; + } + } + + RackInfo rackInfo = new RackInfo(assignmentSpec, subscribedTopicDescriber, subscriptionList); + this.rackInfo = rackInfo; + + // Without rack-aware strategy, tracking previous owners of unassigned partitions is unnecessary + // as all sticky partitions are retained until a member meets its quota. + if (rackInfo.memberRacks.isEmpty() || rackInfo.partitionRacks.isEmpty()) { + this.useRackAwareStrategy = false; + partitionToPrevOwner = Collections.emptyMap(); + } else { + this.useRackAwareStrategy = true; + partitionToPrevOwner = new HashMap<>(); + } + + int numberOfMembers = assignmentSpec.members().size(); + minQuota = (int) Math.floor(((double) totalPartitionsCount) / numberOfMembers); + expectedNumMembersWithExtraPartition = totalPartitionsCount % numberOfMembers; + + potentiallyUnfilledMembers = new HashMap<>(); + unfilledMembers = new HashMap<>(); + newAssignment = new HashMap<>(); + } + + protected GroupAssignment build() { Review Comment: `buildAssignment()` makes more sense. also, this should be an abstract method under `UniformAssignor` -- 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