You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "squah-confluent (via GitHub)" <gi...@apache.org> on 2026/10/14 03:07:12 UTC
Re: [PR] KAFKA-21214: [3/N] Add the quotas and the first two steps of the uniform2 assignor [kafka]
squah-confluent commented on code in PR #23733:
URL: https://github.com/apache/kafka/pull/23733#discussion_r4264726474
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/Quotas.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+/**
+ * The quotas of the members: for every topic, how many of its partitions each subscriber gets.
+ *
+ * <p>A topic with {@code P} partitions and {@code N} subscribers has a minimum quota of
+ * {@code P / N} partitions and {@code P % N} extra partitions, which go to as many distinct
+ * subscribers, one each. The quota of a subscriber is thus the minimum quota, or one more when it
+ * gets an extra partition, and the assignment size of a member is the sum of its quotas.
+ *
+ * <p>The quotas start at the minimum quota. The steps deciding the quotas give the extra
+ * partitions with {@link #giveExtraPartition} and move them with {@link #moveExtraPartition},
+ * every extra partition going to a distinct subscriber of its topic.
+ */
+final class Quotas {
+ /**
+ * The empty list of members with an extra partition shared by all the topics without extra
+ * partitions. It never changes: {@link #giveExtraPartition} rejects such a topic, and
+ * {@link #moveExtraPartition} a giver without an extra partition of the topic.
+ */
+ private static final IntList NONE = new IntList(0);
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * Per topic, its minimum quota: the number of partitions that every subscriber gets.
+ */
+ private final int[] minQuotas;
+
+ /**
+ * Per topic, the number of subscribers getting one partition more than the minimum quota.
+ */
+ private final int[] extraPartitions;
+
+ /**
+ * Per cohort, the sum of the minimum quotas of its topics: the smallest assignment size of
+ * its members.
+ */
+ private final int[] minAssignmentSizes;
+
+ /**
+ * Per topic, the members getting one of its extra partitions.
Review Comment:
Can members appear more than once in this list? I think they can if `giveExtraPartition` is misused. We can't check for and forbid it efficiently so I'm happy to leave it as undefined behavior and leave the javadoc as-is.
(just checking, not a request to update the javadoc)
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/Quotas.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+/**
+ * The quotas of the members: for every topic, how many of its partitions each subscriber gets.
+ *
+ * <p>A topic with {@code P} partitions and {@code N} subscribers has a minimum quota of
+ * {@code P / N} partitions and {@code P % N} extra partitions, which go to as many distinct
+ * subscribers, one each. The quota of a subscriber is thus the minimum quota, or one more when it
+ * gets an extra partition, and the assignment size of a member is the sum of its quotas.
+ *
+ * <p>The quotas start at the minimum quota. The steps deciding the quotas give the extra
+ * partitions with {@link #giveExtraPartition} and move them with {@link #moveExtraPartition},
+ * every extra partition going to a distinct subscriber of its topic.
+ */
+final class Quotas {
+ /**
+ * The empty list of members with an extra partition shared by all the topics without extra
+ * partitions. It never changes: {@link #giveExtraPartition} rejects such a topic, and
+ * {@link #moveExtraPartition} a giver without an extra partition of the topic.
+ */
+ private static final IntList NONE = new IntList(0);
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * Per topic, its minimum quota: the number of partitions that every subscriber gets.
Review Comment:
javadoc, nit:
How about
```suggestion
* Per topic, the minimum number of partitions that every subscriber gets.
```
and then mirror the updated description to the getter.
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/Quotas.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+/**
+ * The quotas of the members: for every topic, how many of its partitions each subscriber gets.
+ *
+ * <p>A topic with {@code P} partitions and {@code N} subscribers has a minimum quota of
+ * {@code P / N} partitions and {@code P % N} extra partitions, which go to as many distinct
+ * subscribers, one each. The quota of a subscriber is thus the minimum quota, or one more when it
+ * gets an extra partition, and the assignment size of a member is the sum of its quotas.
+ *
+ * <p>The quotas start at the minimum quota. The steps deciding the quotas give the extra
+ * partitions with {@link #giveExtraPartition} and move them with {@link #moveExtraPartition},
+ * every extra partition going to a distinct subscriber of its topic.
+ */
+final class Quotas {
+ /**
+ * The empty list of members with an extra partition shared by all the topics without extra
+ * partitions. It never changes: {@link #giveExtraPartition} rejects such a topic, and
+ * {@link #moveExtraPartition} a giver without an extra partition of the topic.
+ */
+ private static final IntList NONE = new IntList(0);
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * Per topic, its minimum quota: the number of partitions that every subscriber gets.
+ */
+ private final int[] minQuotas;
+
+ /**
+ * Per topic, the number of subscribers getting one partition more than the minimum quota.
+ */
+ private final int[] extraPartitions;
+
+ /**
+ * Per cohort, the sum of the minimum quotas of its topics: the smallest assignment size of
+ * its members.
+ */
+ private final int[] minAssignmentSizes;
+
+ /**
+ * Per topic, the members getting one of its extra partitions.
+ */
+ private final IntList[] membersWithExtraPartition;
+
+ /**
+ * Per member, the number of topics of which it gets an extra partition.
+ */
+ private final int[] extraPartitionCounts;
+
+ /**
+ * Creates the quotas at the minimum quota: no member has an extra partition yet.
+ *
+ * @param group The members and topics of the group.
+ */
+ Quotas(IndexedGroup group) {
+ this.group = group;
+ int topicCount = group.topicCount();
+ minQuotas = new int[topicCount];
+ extraPartitions = new int[topicCount];
+ membersWithExtraPartition = new IntList[topicCount];
+ for (int topic = 0; topic < topicCount; topic++) {
+ minQuotas[topic] = group.partitionCount(topic) / group.subscriberCount(topic);
+ extraPartitions[topic] = group.partitionCount(topic) % group.subscriberCount(topic);
+ membersWithExtraPartition[topic] = extraPartitions[topic] == 0 ? NONE : new IntList(extraPartitions[topic]);
+ }
+ minAssignmentSizes = new int[group.cohortCount()];
+ for (int cohort = 0; cohort < group.cohortCount(); cohort++) {
+ for (int topic : group.topicsOf(cohort)) {
+ minAssignmentSizes[cohort] += minQuotas[topic];
+ }
+ }
+ extraPartitionCounts = new int[group.memberCount()];
+ }
+
+ /**
+ * @return The minimum quota of the topic: the number of its partitions that every subscriber
+ * gets.
+ */
+ int minQuota(int topic) {
+ return minQuotas[topic];
+ }
+
+ /**
+ * @return The number of subscribers of the topic getting one partition more than the minimum
+ * quota.
+ */
+ int extraPartitions(int topic) {
+ return extraPartitions[topic];
+ }
+
+ /**
+ * @return The members getting an extra partition of the topic. The list must not be changed.
+ */
+ IntList membersWithExtraPartition(int topic) {
+ return membersWithExtraPartition[topic];
+ }
+
+ /**
+ * @return The number of topics of which the member gets an extra partition.
+ */
+ int extraPartitionCount(int member) {
+ return extraPartitionCounts[member];
+ }
+
+ /**
+ * @return The assignment size of the member: the number of partitions it gets over all its
+ * topics.
+ */
+ int assignmentSize(int member) {
+ return minAssignmentSizes[group.cohortOf(member)] + extraPartitionCounts[member];
+ }
+
+ /**
+ * @return The sum of the minimum quotas of the topics of the cohort: the smallest assignment
+ * size of its members.
+ */
+ int minAssignmentSize(int cohort) {
+ return minAssignmentSizes[cohort];
+ }
+
+ /**
+ * Gives an extra partition of the topic to the member, which does not have one yet.
Review Comment:
javadoc, nit, claudish: "which {property}" tells a human that the property is always true, not that the caller must not use the method when the property is false.
```suggestion
* Gives an extra partition of the topic to the member. The member must not already
* have an extra partition of the topic.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/Quotas.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+/**
+ * The quotas of the members: for every topic, how many of its partitions each subscriber gets.
+ *
+ * <p>A topic with {@code P} partitions and {@code N} subscribers has a minimum quota of
+ * {@code P / N} partitions and {@code P % N} extra partitions, which go to as many distinct
+ * subscribers, one each. The quota of a subscriber is thus the minimum quota, or one more when it
+ * gets an extra partition, and the assignment size of a member is the sum of its quotas.
+ *
+ * <p>The quotas start at the minimum quota. The steps deciding the quotas give the extra
+ * partitions with {@link #giveExtraPartition} and move them with {@link #moveExtraPartition},
+ * every extra partition going to a distinct subscriber of its topic.
+ */
+final class Quotas {
+ /**
+ * The empty list of members with an extra partition shared by all the topics without extra
+ * partitions. It never changes: {@link #giveExtraPartition} rejects such a topic, and
+ * {@link #moveExtraPartition} a giver without an extra partition of the topic.
+ */
+ private static final IntList NONE = new IntList(0);
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * Per topic, its minimum quota: the number of partitions that every subscriber gets.
+ */
+ private final int[] minQuotas;
+
+ /**
+ * Per topic, the number of subscribers getting one partition more than the minimum quota.
+ */
+ private final int[] extraPartitions;
+
+ /**
+ * Per cohort, the sum of the minimum quotas of its topics: the smallest assignment size of
+ * its members.
+ */
+ private final int[] minAssignmentSizes;
+
+ /**
+ * Per topic, the members getting one of its extra partitions.
+ */
+ private final IntList[] membersWithExtraPartition;
+
+ /**
+ * Per member, the number of topics of which it gets an extra partition.
+ */
+ private final int[] extraPartitionCounts;
+
+ /**
+ * Creates the quotas at the minimum quota: no member has an extra partition yet.
+ *
+ * @param group The members and topics of the group.
+ */
+ Quotas(IndexedGroup group) {
+ this.group = group;
+ int topicCount = group.topicCount();
+ minQuotas = new int[topicCount];
+ extraPartitions = new int[topicCount];
+ membersWithExtraPartition = new IntList[topicCount];
+ for (int topic = 0; topic < topicCount; topic++) {
+ minQuotas[topic] = group.partitionCount(topic) / group.subscriberCount(topic);
+ extraPartitions[topic] = group.partitionCount(topic) % group.subscriberCount(topic);
+ membersWithExtraPartition[topic] = extraPartitions[topic] == 0 ? NONE : new IntList(extraPartitions[topic]);
+ }
+ minAssignmentSizes = new int[group.cohortCount()];
+ for (int cohort = 0; cohort < group.cohortCount(); cohort++) {
+ for (int topic : group.topicsOf(cohort)) {
+ minAssignmentSizes[cohort] += minQuotas[topic];
+ }
+ }
+ extraPartitionCounts = new int[group.memberCount()];
+ }
+
+ /**
+ * @return The minimum quota of the topic: the number of its partitions that every subscriber
+ * gets.
+ */
+ int minQuota(int topic) {
+ return minQuotas[topic];
+ }
+
+ /**
+ * @return The number of subscribers of the topic getting one partition more than the minimum
+ * quota.
+ */
+ int extraPartitions(int topic) {
+ return extraPartitions[topic];
+ }
+
+ /**
+ * @return The members getting an extra partition of the topic. The list must not be changed.
+ */
+ IntList membersWithExtraPartition(int topic) {
+ return membersWithExtraPartition[topic];
+ }
+
+ /**
+ * @return The number of topics of which the member gets an extra partition.
+ */
+ int extraPartitionCount(int member) {
+ return extraPartitionCounts[member];
+ }
+
+ /**
+ * @return The assignment size of the member: the number of partitions it gets over all its
+ * topics.
+ */
+ int assignmentSize(int member) {
+ return minAssignmentSizes[group.cohortOf(member)] + extraPartitionCounts[member];
+ }
+
+ /**
+ * @return The sum of the minimum quotas of the topics of the cohort: the smallest assignment
+ * size of its members.
+ */
+ int minAssignmentSize(int cohort) {
+ return minAssignmentSizes[cohort];
+ }
+
+ /**
+ * Gives an extra partition of the topic to the member, which does not have one yet.
+ *
+ * @throws IllegalStateException If every extra partition of the topic is given already.
+ */
+ void giveExtraPartition(int member, int topic) {
+ if (membersWithExtraPartition[topic].size() == extraPartitions[topic]) {
+ throw new IllegalStateException("No extra partition of topic " + group.topicId(topic) + " left for member "
+ + group.memberId(member));
+ }
+ membersWithExtraPartition[topic].add(member);
+ extraPartitionCounts[member]++;
+ }
+
+ /**
+ * Moves an extra partition of the topic from the giver, which has one, to the receiver,
+ * which does not.
Review Comment:
javadoc, nit, claudish: Same here.
```suggestion
* Moves an extra partition of the topic from the giver to the receiver. The giver must have an
* extra partition of the topic and the receiver must not already have one.
```
or similar explicit wording.
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
Review Comment:
javadoc, nit: This part is a little confusing. In particular, it's not clear that the last clause describes how ties are broken.
```suggestion
* When more owners qualify than the topic has extra partitions, the ones with the smallest
* assignment sizes keep them. The assignment sizes include the extra partitions kept for the
* topics so far. Ties are broken by member order.
```
or similar
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
+ */
+final class KeepStickyExtraPartitions {
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The current assignment, which tells the owners of every topic.
+ */
+ private final CurrentAssignment current;
Review Comment:
naming, nit: `currentAssignment`
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
+ */
+final class KeepStickyExtraPartitions {
Review Comment:
I wonder if we could distinguish the step classes (which I see as bags of methods) from the data classes (`IndexedGroup`, `CurrentAssignment`, ...). If we can communicate the structure of the assignor through names, it helps others understand the assignor better without having to read through javadocs.
One option could be to add a `Step` (or similar) suffix to the step classes, but their names are already fairly long. Another option could be to move the steps into their own `.steps` subpackage (or similar).
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
+ */
+final class KeepStickyExtraPartitions {
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The current assignment, which tells the owners of every topic.
+ */
+ private final CurrentAssignment current;
+
+ /**
+ * The quotas, which the step completes.
Review Comment:
javadoc, nit: We don't actually consider the quotas done until the rest of the steps have run.
```suggestion
* The quotas, which the step updates.
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/QuotasFixture.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.api.assignor.GroupSpec;
+import org.apache.kafka.coordinator.group.api.assignor.SubscribedTopicDescriber;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.IntPredicate;
+
+/**
+ * The indexed group, the current assignment and the quotas of a group, for the tests of the steps
+ * deciding the quotas. The quotas start at the minimum quota, to which a test adds the extra
+ * partitions it starts from. The members and topics are referred to by their ids.
+ */
+final class QuotasFixture {
+ /**
+ * The members and topics of the group.
+ */
+ final IndexedGroup group;
+
+ /**
+ * The current assignment of the group.
+ */
+ final CurrentAssignment currentAssignment;
+
+ /**
+ * The quotas of the members.
+ */
+ final Quotas quotas;
+
+ /**
+ * Creates the fixture, with the quotas at the minimum quota.
+ *
+ * @param groupSpec The group spec.
+ * @param describer The describer of the topics.
+ */
+ QuotasFixture(GroupSpec groupSpec, SubscribedTopicDescriber describer) {
+ group = new IndexedGroup(groupSpec, describer);
+ currentAssignment = new CurrentAssignment(groupSpec, group);
+ quotas = new Quotas(group);
+ }
+
+ /**
+ * @return The index of the member.
+ */
+ int member(String memberId) {
+ for (int member = 0; member < group.memberCount(); member++) {
+ if (group.memberId(member).equals(memberId)) {
+ return member;
+ }
+ }
+ throw new IllegalArgumentException("Unknown member " + memberId);
+ }
+
+ /**
+ * @return The index of the topic.
+ */
+ int topic(Uuid topicId) {
+ return group.topicIndex(topicId);
+ }
+
+ /**
+ * Gives the member an extra partition of every topic, which it must not have yet.
Review Comment:
javadoc, nit: `every` reads like "all" and not "the topics in the list".
```suggestion
* Gives the member an extra partition of each of the topics. It must not have any extra
* partitions from any of the topics yet.
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/QuotasTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.function.IntUnaryOperator;
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests of {@link Quotas}.
+ *
+ * <p>The member ids sort as A < B < C, so that A is member 0, B is member 1 and C is member
+ * 2, and the topic ids as T1 < T2 < T3, so that T1 is topic 0, T2 is topic 1 and T3 is
+ * topic 2 when all of them are subscribed.
+ */
+public class QuotasTest {
+ private static final Uuid T1 = new Uuid(1L, 1L);
+ private static final Uuid T2 = new Uuid(2L, 1L);
+ private static final Uuid T3 = new Uuid(3L, 1L);
+
+ private static final int A = 0;
+ private static final int B = 1;
+
+ /**
+ * 3 partitions over 2 subscribers give a minimum quota of 1 and 1 extra partition; 7 over 3
+ * a minimum quota of 2 and 1 extra partition; 2 over 3 a minimum quota of 0 and 2 extra
+ * partitions. The members of cohort 0, A and B, subscribe to all three topics and get at least
+ * 1 + 2 + 0 = 3 partitions each; the member of cohort 1, C, subscribes to T2 and T3 and gets
+ * at least 2 + 0 = 2.
+ */
+ @Test
+ public void testMinQuotaAndExtraPartitions() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2, T3))
+ .withMember("B", Set.of(T1, T2, T3))
+ .withMember("C", Set.of(T2, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 3)
+ .withTopic(T2, 7)
+ .withTopic(T3, 2)
+ .build();
+ var group = new IndexedGroup(spec, describer);
+
+ var quotas = new Quotas(group);
+
+ assertEquals(List.of(1, 2, 0), values(group.topicCount(), quotas::minQuota));
+ assertEquals(List.of(1, 1, 2), values(group.topicCount(), quotas::extraPartitions));
+ assertEquals(List.of(3, 2), values(group.cohortCount(), quotas::minAssignmentSize));
+ assertEquals(List.of(3, 3, 2), values(group.memberCount(), quotas::assignmentSize));
+ assertEquals(List.of(0, 0, 0), values(group.memberCount(), quotas::extraPartitionCount));
+ assertEquals(List.of(List.of(), List.of(), List.of()), membersWithExtraPartition(group, quotas));
+ }
+
+ /**
+ * A and B subscribe to T1, of 3 partitions, and T2, of 2: T1 has 1 extra partition, which A
+ * gets, and T2 none. Neither a second extra partition of T1 nor one of T2 can be given, and an
+ * extra partition of T1 cannot move from B, which has none: A keeps its only extra partition,
+ * and the assignment sizes are 3 and 2.
+ */
+ @Test
+ public void testExtraPartitionsBeyondThoseOfTheTopicAreRejected() {
Review Comment:
This test actually tests three things which we all care about:
* The happy path for `giveExtraPartition`
* The exceptional path for `giveExtraPartition` where we try to over-give extra partitions
* `moveExtraPartition` from a member which does not have one.
The name doesn't really capture all three. Perhaps we could rename this to `testGiveExtraPartition` and move the `moveExtraPartition` assert into `testMoveExtraPartition`.
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/QuotasFixture.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.api.assignor.GroupSpec;
+import org.apache.kafka.coordinator.group.api.assignor.SubscribedTopicDescriber;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.IntPredicate;
+
+/**
+ * The indexed group, the current assignment and the quotas of a group, for the tests of the steps
+ * deciding the quotas. The quotas start at the minimum quota, to which a test adds the extra
+ * partitions it starts from. The members and topics are referred to by their ids.
+ */
+final class QuotasFixture {
+ /**
+ * The members and topics of the group.
+ */
+ final IndexedGroup group;
+
+ /**
+ * The current assignment of the group.
+ */
+ final CurrentAssignment currentAssignment;
+
+ /**
+ * The quotas of the members.
+ */
+ final Quotas quotas;
+
+ /**
+ * Creates the fixture, with the quotas at the minimum quota.
+ *
+ * @param groupSpec The group spec.
+ * @param describer The describer of the topics.
+ */
+ QuotasFixture(GroupSpec groupSpec, SubscribedTopicDescriber describer) {
+ group = new IndexedGroup(groupSpec, describer);
+ currentAssignment = new CurrentAssignment(groupSpec, group);
+ quotas = new Quotas(group);
+ }
+
+ /**
+ * @return The index of the member.
+ */
+ int member(String memberId) {
+ for (int member = 0; member < group.memberCount(); member++) {
+ if (group.memberId(member).equals(memberId)) {
+ return member;
+ }
+ }
+ throw new IllegalArgumentException("Unknown member " + memberId);
+ }
+
+ /**
+ * @return The index of the topic.
+ */
+ int topic(Uuid topicId) {
+ return group.topicIndex(topicId);
+ }
+
+ /**
+ * Gives the member an extra partition of every topic, which it must not have yet.
+ */
+ void addExtras(String memberId, Uuid... topicIds) {
+ for (Uuid topicId : topicIds) {
+ if (quotas.membersWithExtraPartition(topic(topicId)).indexOf(member(memberId)) >= 0) {
+ throw new IllegalArgumentException(memberId + " already has an extra partition of " + topicId);
+ }
+ quotas.giveExtraPartition(member(memberId), topic(topicId));
+ }
+ }
+
+ /**
+ * @return Per topic of which some members have an extra partition, these members, in member
+ * id order.
Review Comment:
javadoc, nit: How about
```suggestion
* @return Per topic, the members which have an extra partition, in member order. Topics where
* no members have an extra partition are excluded.
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/QuotasTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.function.IntUnaryOperator;
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests of {@link Quotas}.
+ *
+ * <p>The member ids sort as A < B < C, so that A is member 0, B is member 1 and C is member
+ * 2, and the topic ids as T1 < T2 < T3, so that T1 is topic 0, T2 is topic 1 and T3 is
+ * topic 2 when all of them are subscribed.
+ */
+public class QuotasTest {
+ private static final Uuid T1 = new Uuid(1L, 1L);
+ private static final Uuid T2 = new Uuid(2L, 1L);
+ private static final Uuid T3 = new Uuid(3L, 1L);
+
+ private static final int A = 0;
+ private static final int B = 1;
+
+ /**
+ * 3 partitions over 2 subscribers give a minimum quota of 1 and 1 extra partition; 7 over 3
+ * a minimum quota of 2 and 1 extra partition; 2 over 3 a minimum quota of 0 and 2 extra
+ * partitions. The members of cohort 0, A and B, subscribe to all three topics and get at least
+ * 1 + 2 + 0 = 3 partitions each; the member of cohort 1, C, subscribes to T2 and T3 and gets
+ * at least 2 + 0 = 2.
+ */
+ @Test
+ public void testMinQuotaAndExtraPartitions() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2, T3))
+ .withMember("B", Set.of(T1, T2, T3))
+ .withMember("C", Set.of(T2, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 3)
+ .withTopic(T2, 7)
+ .withTopic(T3, 2)
+ .build();
+ var group = new IndexedGroup(spec, describer);
+
+ var quotas = new Quotas(group);
+
+ assertEquals(List.of(1, 2, 0), values(group.topicCount(), quotas::minQuota));
+ assertEquals(List.of(1, 1, 2), values(group.topicCount(), quotas::extraPartitions));
+ assertEquals(List.of(3, 2), values(group.cohortCount(), quotas::minAssignmentSize));
+ assertEquals(List.of(3, 3, 2), values(group.memberCount(), quotas::assignmentSize));
+ assertEquals(List.of(0, 0, 0), values(group.memberCount(), quotas::extraPartitionCount));
+ assertEquals(List.of(List.of(), List.of(), List.of()), membersWithExtraPartition(group, quotas));
+ }
+
+ /**
+ * A and B subscribe to T1, of 3 partitions, and T2, of 2: T1 has 1 extra partition, which A
+ * gets, and T2 none. Neither a second extra partition of T1 nor one of T2 can be given, and an
+ * extra partition of T1 cannot move from B, which has none: A keeps its only extra partition,
+ * and the assignment sizes are 3 and 2.
+ */
+ @Test
+ public void testExtraPartitionsBeyondThoseOfTheTopicAreRejected() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2))
+ .withMember("B", Set.of(T1, T2))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 3)
+ .withTopic(T2, 2)
+ .build();
+ var group = new IndexedGroup(spec, describer);
+ var quotas = new Quotas(group);
+ int t1 = group.topicIndex(T1);
+ int t2 = group.topicIndex(T2);
+ quotas.giveExtraPartition(A, t1);
+
+ assertThrows(IllegalStateException.class, () -> quotas.giveExtraPartition(B, t1));
+ assertThrows(IllegalStateException.class, () -> quotas.giveExtraPartition(A, t2));
+ assertThrows(IndexOutOfBoundsException.class, () -> quotas.moveExtraPartition(B, t1, A));
+
+ assertEquals(List.of(List.of(A), List.of()), membersWithExtraPartition(group, quotas));
+ assertEquals(List.of(1, 0), values(group.memberCount(), quotas::extraPartitionCount));
+ assertEquals(List.of(3, 2), values(group.memberCount(), quotas::assignmentSize));
Review Comment:
nit: In the previous method, we assert on `assignmentSize`, then `extraPartitionCount`, then `membersWithExtraPartition`. Can align on the same order of asserts for consistency? Same for `testMoveExtraPartition`
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitionsTest.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkAssignment;
+import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests of {@link KeepStickyExtraPartitions}. The topic ids sort as T1 < T2 < T3.
Review Comment:
javadoc, int: I think we can drop the comments about the ordering.
```suggestion
* Tests of {@link KeepStickyExtraPartitions}.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
Review Comment:
javadoc, nit:
Is perfect balance is guaranteed? I thought it just tends to lead to more balanced assignments.
```suggestion
* candidates: when cohorts have subscriptions that are subsets of each other, this leads to more balanced assignment sizes. The
```
\+ line wrap
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
Review Comment:
javadoc, nit: Worded weirdly. Topics always have the same number of partitions.
```suggestion
* <p>On exit, every extra partition of every topic has been assigned to distinct subscribers.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
Review Comment:
javadoc, nit:
```suggestion
* The quotas, which the step updates.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
+
+ /**
+ * Per cohort, its state.
+ */
+ private CohortState[] cohortStates;
+
+ /**
+ * Per member, whether it has an extra partition of the topic being handed out.
+ */
+ private boolean[] hasExtra;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param quotas The quotas, as the first step left them, which the step completes.
+ */
+ AssignRemainingExtraPartitions(IndexedGroup group, Quotas quotas) {
+ this.group = group;
+ this.quotas = quotas;
+ }
+
+ /**
+ * @return The key of the cohort in the heap of candidates: the pair of the smallest assignment
+ * size among its members and the cohort, so that the heap orders the cohorts by that
+ * assignment size, then cohort order. That assignment size does not change while the
+ * cohort is a candidate, since only the members of a polled cohort get an extra
+ * partition.
+ */
+ private long sizeAndCohort(int cohort) {
+ return IntPair.pack(quotas.minAssignmentSize(cohort) + cohortStates[cohort].lowest, cohort);
+ }
+
+ /**
+ * Hands out the extra partitions that no owner kept. When the owners kept them all, as in a
+ * stable group, returns at once without allocating anything.
+ */
+ void run() {
+ if (!hasExtraPartitionsToHandOut()) {
+ return;
+ }
+ cohortStates = new CohortState[group.cohortCount()];
+ for (int cohort = 0; cohort < cohortStates.length; cohort++) {
+ cohortStates[cohort] = new CohortState(group.membersOf(cohort));
+ findLowest(cohortStates[cohort]);
+ }
+ hasExtra = new boolean[group.memberCount()];
+ candidates = new LongHeap(16);
+ for (int topic : topicsToHandOut()) {
+ handOutTopic(topic);
+ }
+ }
+
+ /**
+ * @return {@code true} if a topic has extra partitions that no owner kept.
+ */
+ private boolean hasExtraPartitionsToHandOut() {
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.membersWithExtraPartition(topic).size() < quotas.extraPartitions(topic)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Hands out the extra partitions of the topic that no owner kept, one at a time, each to the
+ * next member of the cohort holding the smallest assignment among those still having a member
+ * without one.
+ */
+ private void handOutTopic(int topic) {
+ // The members keeping an extra partition of the topic, which grows as the topic is handed
+ // out.
+ var members = quotas.membersWithExtraPartition(topic);
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = true;
+ cohortStates[group.cohortOf(members.get(i))].holders++;
+ }
+ // The cohorts of the topic with a member still without an extra partition of it, the only
+ // ones that can take one.
+ candidates.clear();
+ for (int cohort : group.cohortsOf(topic)) {
+ var state = cohortStates[cohort];
+ if (state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // The extra partitions that no owner kept, counted once since the members grow while they
+ // are handed out. There is at least one: topicsToHandOut only lists the topics with some.
+ for (int toHandOut = quotas.extraPartitions(topic) - members.size(); toHandOut > 0; toHandOut--) {
+ int cohort = IntPair.low(candidates.poll());
+ var state = cohortStates[cohort];
+ int member = nextMember(state);
+ int count = quotas.extraPartitionCount(member);
+ quotas.giveExtraPartition(member, topic);
+ hasExtra[member] = true;
+ // The last member of the cohort at the lowest number of extra partitions moves up.
+ if (count == state.lowest && --state.atLowest == 0) {
+ findLowest(state);
+ }
+ if (++state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // Resets the scratch state for the next topic, including the members just served.
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = false;
+ cohortStates[group.cohortOf(members.get(i))].holders = 0;
+ }
+ }
+
+ /**
+ * @return The topics with extra partitions nobody kept, by increasing number of subscribers,
+ * then topic order.
+ */
+ private int[] topicsToHandOut() {
+ // Pairs of the number of subscribers and the topic, which sort by subscribers, then topic.
+ var keys = new long[group.topicCount()];
+ int count = 0;
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.extraPartitions(topic) > quotas.membersWithExtraPartition(topic).size()) {
+ keys[count++] = IntPair.pack(group.subscriberCount(topic), topic);
+ }
+ }
+ Arrays.sort(keys, 0, count);
+ var topics = new int[count];
+ for (int i = 0; i < count; i++) {
+ topics[i] = IntPair.low(keys[i]);
+ }
+ return topics;
+ }
+
+ /**
+ * Finds the lowest number of extra partitions of the members of the cohort, and how many of
+ * them have it.
+ */
+ private void findLowest(CohortState state) {
Review Comment:
naming, nit: `find` methods usually return something and `Lowest` on its own is too vague.
`computeLowestExtraPartitions`?
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
+
+ /**
+ * Per cohort, its state.
+ */
+ private CohortState[] cohortStates;
+
+ /**
+ * Per member, whether it has an extra partition of the topic being handed out.
+ */
+ private boolean[] hasExtra;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param quotas The quotas, as the first step left them, which the step completes.
+ */
+ AssignRemainingExtraPartitions(IndexedGroup group, Quotas quotas) {
+ this.group = group;
+ this.quotas = quotas;
+ }
+
+ /**
+ * @return The key of the cohort in the heap of candidates: the pair of the smallest assignment
+ * size among its members and the cohort, so that the heap orders the cohorts by that
+ * assignment size, then cohort order. That assignment size does not change while the
+ * cohort is a candidate, since only the members of a polled cohort get an extra
+ * partition.
+ */
+ private long sizeAndCohort(int cohort) {
+ return IntPair.pack(quotas.minAssignmentSize(cohort) + cohortStates[cohort].lowest, cohort);
+ }
+
+ /**
+ * Hands out the extra partitions that no owner kept. When the owners kept them all, as in a
+ * stable group, returns at once without allocating anything.
+ */
+ void run() {
+ if (!hasExtraPartitionsToHandOut()) {
+ return;
+ }
+ cohortStates = new CohortState[group.cohortCount()];
+ for (int cohort = 0; cohort < cohortStates.length; cohort++) {
+ cohortStates[cohort] = new CohortState(group.membersOf(cohort));
+ findLowest(cohortStates[cohort]);
+ }
+ hasExtra = new boolean[group.memberCount()];
+ candidates = new LongHeap(16);
+ for (int topic : topicsToHandOut()) {
+ handOutTopic(topic);
+ }
+ }
+
+ /**
+ * @return {@code true} if a topic has extra partitions that no owner kept.
+ */
+ private boolean hasExtraPartitionsToHandOut() {
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.membersWithExtraPartition(topic).size() < quotas.extraPartitions(topic)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Hands out the extra partitions of the topic that no owner kept, one at a time, each to the
+ * next member of the cohort holding the smallest assignment among those still having a member
+ * without one.
+ */
+ private void handOutTopic(int topic) {
+ // The members keeping an extra partition of the topic, which grows as the topic is handed
+ // out.
+ var members = quotas.membersWithExtraPartition(topic);
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = true;
+ cohortStates[group.cohortOf(members.get(i))].holders++;
+ }
+ // The cohorts of the topic with a member still without an extra partition of it, the only
+ // ones that can take one.
+ candidates.clear();
+ for (int cohort : group.cohortsOf(topic)) {
+ var state = cohortStates[cohort];
+ if (state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // The extra partitions that no owner kept, counted once since the members grow while they
+ // are handed out. There is at least one: topicsToHandOut only lists the topics with some.
Review Comment:
nit:
I can't follow what's being emphasized here / why this needs emphasis.
```
// counted once since the members grow while they are handed out
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
+ */
+final class KeepStickyExtraPartitions {
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The current assignment, which tells the owners of every topic.
+ */
+ private final CurrentAssignment current;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param current The current assignment.
+ * @param quotas The quotas, at the minimum quota.
+ */
+ KeepStickyExtraPartitions(IndexedGroup group, CurrentAssignment current, Quotas quotas) {
+ this.group = group;
+ this.current = current;
+ this.quotas = quotas;
+ }
+
+ /**
+ * Lets the owners of more than the minimum quota of every topic keep an extra partition.
+ */
+ void run() {
+ var counts = new int[group.memberCount()];
+ var owners = new IntList(16);
+ var candidates = new IntList(16);
Review Comment:
I can't understand what these are without reading the implementation below. We could try adding javadoc-style descriptions and tweaking the names.
```suggestion
// Per member, the number of partitions owned in the current assignment for the current topic.
var partitionCounts = new int[group.memberCount()];
// The members owning any partitions in the current assignment for the current topic.
var partitionOwners = new IntList(16);
// The members owning more partitions than the minimum quota in the current assignment for the current topic.
var currentMembersWithExtraPartitions = new IntList(16);
```
or similar
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
+
+ /**
+ * Per cohort, its state.
+ */
+ private CohortState[] cohortStates;
+
+ /**
+ * Per member, whether it has an extra partition of the topic being handed out.
+ */
+ private boolean[] hasExtra;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param quotas The quotas, as the first step left them, which the step completes.
+ */
+ AssignRemainingExtraPartitions(IndexedGroup group, Quotas quotas) {
+ this.group = group;
+ this.quotas = quotas;
+ }
+
+ /**
+ * @return The key of the cohort in the heap of candidates: the pair of the smallest assignment
+ * size among its members and the cohort, so that the heap orders the cohorts by that
+ * assignment size, then cohort order. That assignment size does not change while the
+ * cohort is a candidate, since only the members of a polled cohort get an extra
+ * partition.
+ */
+ private long sizeAndCohort(int cohort) {
+ return IntPair.pack(quotas.minAssignmentSize(cohort) + cohortStates[cohort].lowest, cohort);
+ }
+
+ /**
+ * Hands out the extra partitions that no owner kept. When the owners kept them all, as in a
+ * stable group, returns at once without allocating anything.
+ */
+ void run() {
+ if (!hasExtraPartitionsToHandOut()) {
+ return;
+ }
+ cohortStates = new CohortState[group.cohortCount()];
+ for (int cohort = 0; cohort < cohortStates.length; cohort++) {
+ cohortStates[cohort] = new CohortState(group.membersOf(cohort));
+ findLowest(cohortStates[cohort]);
+ }
+ hasExtra = new boolean[group.memberCount()];
+ candidates = new LongHeap(16);
+ for (int topic : topicsToHandOut()) {
+ handOutTopic(topic);
+ }
+ }
+
+ /**
+ * @return {@code true} if a topic has extra partitions that no owner kept.
+ */
+ private boolean hasExtraPartitionsToHandOut() {
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.membersWithExtraPartition(topic).size() < quotas.extraPartitions(topic)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Hands out the extra partitions of the topic that no owner kept, one at a time, each to the
+ * next member of the cohort holding the smallest assignment among those still having a member
+ * without one.
+ */
+ private void handOutTopic(int topic) {
+ // The members keeping an extra partition of the topic, which grows as the topic is handed
+ // out.
+ var members = quotas.membersWithExtraPartition(topic);
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = true;
+ cohortStates[group.cohortOf(members.get(i))].holders++;
+ }
+ // The cohorts of the topic with a member still without an extra partition of it, the only
+ // ones that can take one.
+ candidates.clear();
+ for (int cohort : group.cohortsOf(topic)) {
+ var state = cohortStates[cohort];
+ if (state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // The extra partitions that no owner kept, counted once since the members grow while they
+ // are handed out. There is at least one: topicsToHandOut only lists the topics with some.
+ for (int toHandOut = quotas.extraPartitions(topic) - members.size(); toHandOut > 0; toHandOut--) {
+ int cohort = IntPair.low(candidates.poll());
+ var state = cohortStates[cohort];
+ int member = nextMember(state);
+ int count = quotas.extraPartitionCount(member);
+ quotas.giveExtraPartition(member, topic);
+ hasExtra[member] = true;
+ // The last member of the cohort at the lowest number of extra partitions moves up.
+ if (count == state.lowest && --state.atLowest == 0) {
+ findLowest(state);
+ }
+ if (++state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // Resets the scratch state for the next topic, including the members just served.
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = false;
+ cohortStates[group.cohortOf(members.get(i))].holders = 0;
+ }
+ }
+
+ /**
+ * @return The topics with extra partitions nobody kept, by increasing number of subscribers,
+ * then topic order.
+ */
+ private int[] topicsToHandOut() {
+ // Pairs of the number of subscribers and the topic, which sort by subscribers, then topic.
+ var keys = new long[group.topicCount()];
+ int count = 0;
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.extraPartitions(topic) > quotas.membersWithExtraPartition(topic).size()) {
+ keys[count++] = IntPair.pack(group.subscriberCount(topic), topic);
+ }
+ }
+ Arrays.sort(keys, 0, count);
+ var topics = new int[count];
+ for (int i = 0; i < count; i++) {
+ topics[i] = IntPair.low(keys[i]);
+ }
+ return topics;
+ }
+
+ /**
+ * Finds the lowest number of extra partitions of the members of the cohort, and how many of
+ * them have it.
+ */
+ private void findLowest(CohortState state) {
+ int min = Integer.MAX_VALUE;
+ int count = 0;
+ for (int member : state.members) {
+ int extras = quotas.extraPartitionCount(member);
+ if (extras < min) {
+ min = extras;
+ count = 1;
+ } else if (extras == min) {
+ count++;
+ }
+ }
+ state.lowest = min;
+ state.atLowest = count;
+ }
+
+ /**
+ * @return The next member of the cohort, in round robin order from the cursor, having the
+ * lowest number of extra partitions among its members and no extra partition of the
+ * topic. If every member having the lowest number has one, the first member without
+ * one having the fewest extra partitions.
Review Comment:
nit: This part is quite difficult to read.
```suggestion
* topic. If every member having the lowest number of extra partitions already has an
* extra partition of the topic, the first member without one having the fewest extra
* partitions.
```
but open to other suggestions
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
+
+ /**
+ * Per cohort, its state.
+ */
+ private CohortState[] cohortStates;
+
+ /**
+ * Per member, whether it has an extra partition of the topic being handed out.
+ */
+ private boolean[] hasExtra;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param quotas The quotas, as the first step left them, which the step completes.
+ */
+ AssignRemainingExtraPartitions(IndexedGroup group, Quotas quotas) {
+ this.group = group;
+ this.quotas = quotas;
+ }
+
+ /**
+ * @return The key of the cohort in the heap of candidates: the pair of the smallest assignment
+ * size among its members and the cohort, so that the heap orders the cohorts by that
+ * assignment size, then cohort order. That assignment size does not change while the
+ * cohort is a candidate, since only the members of a polled cohort get an extra
+ * partition.
+ */
+ private long sizeAndCohort(int cohort) {
+ return IntPair.pack(quotas.minAssignmentSize(cohort) + cohortStates[cohort].lowest, cohort);
+ }
+
+ /**
+ * Hands out the extra partitions that no owner kept. When the owners kept them all, as in a
+ * stable group, returns at once without allocating anything.
+ */
+ void run() {
+ if (!hasExtraPartitionsToHandOut()) {
+ return;
+ }
+ cohortStates = new CohortState[group.cohortCount()];
+ for (int cohort = 0; cohort < cohortStates.length; cohort++) {
+ cohortStates[cohort] = new CohortState(group.membersOf(cohort));
+ findLowest(cohortStates[cohort]);
+ }
+ hasExtra = new boolean[group.memberCount()];
+ candidates = new LongHeap(16);
+ for (int topic : topicsToHandOut()) {
+ handOutTopic(topic);
+ }
+ }
+
+ /**
+ * @return {@code true} if a topic has extra partitions that no owner kept.
+ */
+ private boolean hasExtraPartitionsToHandOut() {
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.membersWithExtraPartition(topic).size() < quotas.extraPartitions(topic)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Hands out the extra partitions of the topic that no owner kept, one at a time, each to the
+ * next member of the cohort holding the smallest assignment among those still having a member
+ * without one.
+ */
+ private void handOutTopic(int topic) {
+ // The members keeping an extra partition of the topic, which grows as the topic is handed
+ // out.
+ var members = quotas.membersWithExtraPartition(topic);
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = true;
+ cohortStates[group.cohortOf(members.get(i))].holders++;
+ }
+ // The cohorts of the topic with a member still without an extra partition of it, the only
+ // ones that can take one.
+ candidates.clear();
+ for (int cohort : group.cohortsOf(topic)) {
+ var state = cohortStates[cohort];
+ if (state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // The extra partitions that no owner kept, counted once since the members grow while they
+ // are handed out. There is at least one: topicsToHandOut only lists the topics with some.
+ for (int toHandOut = quotas.extraPartitions(topic) - members.size(); toHandOut > 0; toHandOut--) {
+ int cohort = IntPair.low(candidates.poll());
+ var state = cohortStates[cohort];
+ int member = nextMember(state);
+ int count = quotas.extraPartitionCount(member);
+ quotas.giveExtraPartition(member, topic);
+ hasExtra[member] = true;
+ // The last member of the cohort at the lowest number of extra partitions moves up.
+ if (count == state.lowest && --state.atLowest == 0) {
+ findLowest(state);
+ }
+ if (++state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // Resets the scratch state for the next topic, including the members just served.
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = false;
+ cohortStates[group.cohortOf(members.get(i))].holders = 0;
+ }
+ }
+
+ /**
+ * @return The topics with extra partitions nobody kept, by increasing number of subscribers,
+ * then topic order.
+ */
+ private int[] topicsToHandOut() {
+ // Pairs of the number of subscribers and the topic, which sort by subscribers, then topic.
+ var keys = new long[group.topicCount()];
+ int count = 0;
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.extraPartitions(topic) > quotas.membersWithExtraPartition(topic).size()) {
+ keys[count++] = IntPair.pack(group.subscriberCount(topic), topic);
+ }
+ }
+ Arrays.sort(keys, 0, count);
+ var topics = new int[count];
+ for (int i = 0; i < count; i++) {
+ topics[i] = IntPair.low(keys[i]);
+ }
+ return topics;
+ }
+
+ /**
+ * Finds the lowest number of extra partitions of the members of the cohort, and how many of
+ * them have it.
+ */
+ private void findLowest(CohortState state) {
+ int min = Integer.MAX_VALUE;
+ int count = 0;
+ for (int member : state.members) {
+ int extras = quotas.extraPartitionCount(member);
+ if (extras < min) {
+ min = extras;
+ count = 1;
+ } else if (extras == min) {
+ count++;
+ }
+ }
+ state.lowest = min;
+ state.atLowest = count;
+ }
+
+ /**
+ * @return The next member of the cohort, in round robin order from the cursor, having the
+ * lowest number of extra partitions among its members and no extra partition of the
+ * topic. If every member having the lowest number has one, the first member without
+ * one having the fewest extra partitions.
+ */
+ private int nextMember(CohortState state) {
+ var members = state.members;
+ for (int step = 0; step < members.length; step++) {
+ int index = (state.cursor + step) % members.length;
+ int member = members[index];
+ if (quotas.extraPartitionCount(member) == state.lowest && !hasExtra[member]) {
+ state.cursor = (index + 1) % members.length;
+ return member;
+ }
+ }
+ // Every member at the lowest number has an extra partition of the topic.
+ int best = -1;
+ for (int member : members) {
+ if (!hasExtra[member] && (best < 0 || quotas.extraPartitionCount(member) < quotas.extraPartitionCount(best))) {
+ best = member;
+ }
+ }
+ return best;
Review Comment:
Something for later?:
I'm a little suspicious of this part in terms of performance. If we ever fall into this case, we will iterate through the entire cohort's members for every extra partition of the topic we need to hand out.
We will also never reorder the cohort in the min heap, so it's going to receive all the extra partitions of the topic until every member has one.
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitions.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+
+import java.util.Arrays;
+
+/**
+ * The first step deciding the {@link Quotas}: every member owning more than the minimum quota of a
+ * topic keeps one of its extra partitions, so that it only gives up what it owns beyond its quota.
+ * When more owners qualify than the topic has extra partitions, the ones with the smallest
+ * assignments keep them, the assignment sizes counting the extra partitions kept for the previous
+ * topics, then the members in member order.
+ */
+final class KeepStickyExtraPartitions {
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The current assignment, which tells the owners of every topic.
+ */
+ private final CurrentAssignment current;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param current The current assignment.
+ * @param quotas The quotas, at the minimum quota.
+ */
+ KeepStickyExtraPartitions(IndexedGroup group, CurrentAssignment current, Quotas quotas) {
+ this.group = group;
+ this.current = current;
+ this.quotas = quotas;
+ }
+
+ /**
+ * Lets the owners of more than the minimum quota of every topic keep an extra partition.
+ */
+ void run() {
+ var counts = new int[group.memberCount()];
+ var owners = new IntList(16);
+ var candidates = new IntList(16);
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ int extraPartitions = quotas.extraPartitions(topic);
+ if (extraPartitions == 0) {
+ continue;
+ }
+ current.countOwned(topic, counts, owners);
+ candidates.clear();
+ for (int i = 0; i < owners.size(); i++) {
+ int owner = owners.get(i);
+ if (counts[owner] > quotas.minQuota(topic)) {
+ candidates.add(owner);
+ }
+ // Reset for the next topic, as countOwned requires.
+ counts[owner] = 0;
+ }
+ // More owners qualify than the topic has extra partitions, as after a join. They are
+ // at most the subscribers of the topic, so the sort stays small.
+ if (candidates.size() > extraPartitions) {
+ sortBySize(candidates);
+ }
+ for (int i = 0; i < Math.min(candidates.size(), extraPartitions); i++) {
+ quotas.giveExtraPartition(candidates.get(i), topic);
+ }
+ }
+ }
+
+ /**
+ * Sorts the members by increasing assignment size, then member order.
+ */
+ private void sortBySize(IntList members) {
+ // Pairs of the assignment size and the member, which sort by assignment size, then member.
+ var keys = new long[members.size()];
Review Comment:
naming, nit: `keys` is a little vague. `assignmentSizesAndMembers`?
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/QuotasTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntList;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.function.IntUnaryOperator;
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests of {@link Quotas}.
+ *
+ * <p>The member ids sort as A < B < C, so that A is member 0, B is member 1 and C is member
+ * 2, and the topic ids as T1 < T2 < T3, so that T1 is topic 0, T2 is topic 1 and T3 is
+ * topic 2 when all of them are subscribed.
Review Comment:
javadoc, nit: We can drop this I think.
```suggestion
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/KeepStickyExtraPartitionsTest.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkAssignment;
+import static org.apache.kafka.coordinator.group.AssignmentTestUtil.mkTopicAssignment;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests of {@link KeepStickyExtraPartitions}. The topic ids sort as T1 < T2 < T3.
+ */
+public class KeepStickyExtraPartitionsTest {
+ private static final Uuid T1 = new Uuid(1L, 1L);
+ private static final Uuid T2 = new Uuid(2L, 1L);
+ private static final Uuid T3 = new Uuid(3L, 1L);
+
+ /**
+ * T1, 3 partitions over 3 members, has a minimum quota of 1 and no extra partition. T2, 5
+ * partitions, has a minimum quota of 1 and 2 extra partitions; A and C own 2 partitions of it,
+ * more than the minimum quota, so they keep them, and the assignment sizes are 3, 2 and 3.
+ */
+ @Test
+ public void testOwnersAboveTheMinQuotaKeepAnExtraPartition() {
Review Comment:
Shall we update the setup so that we also test a member which currently owns 2 partitions over the minimum quota? We would expect that only one of those extra partitions is kept.
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitionsTest.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests of {@link AssignRemainingExtraPartitions}. The topic ids sort as T1 < T2 < T3 <
+ * T4, and the cohorts are numbered in the order of their first member.
+ */
+public class AssignRemainingExtraPartitionsTest {
+ private static final Uuid T1 = new Uuid(1L, 1L);
+ private static final Uuid T2 = new Uuid(2L, 1L);
+ private static final Uuid T3 = new Uuid(3L, 1L);
+ private static final Uuid T4 = new Uuid(4L, 1L);
+
+ /**
+ * A subscribes to T1 and T2, B and D to T1 and T3, and C to T2 and T4. T1, 4 partitions over
+ * A, B and D, has 1 extra partition, and T2, 3 partitions over A and C, has 1 too; T3 and T4
+ * have none. The minimum quotas give assignment sizes of 2 to A, B and D, and 3 to C. T2 has
+ * the fewest subscribers and goes first: its extra partition goes to A, the smaller of A and
+ * C. Then the one of T1 goes to B, since A now has 3. The assignment sizes are within one of
+ * each other where the subscriptions allow it. Handing out T1 first would have given both
+ * extra partitions to A, the first cohort on ties: 4 partitions for A and 2 for B, which
+ * subscribes to T1.
+ */
+ @Test
+ public void testTopicsWithTheFewestSubscribersGoFirst() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2))
+ .withMember("B", Set.of(T1, T3))
+ .withMember("C", Set.of(T2, T4))
+ .withMember("D", Set.of(T1, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 4)
+ .withTopic(T2, 3)
+ .withTopic(T3, 2)
+ .withTopic(T4, 2)
+ .build();
+ var fixture = new QuotasFixture(spec, describer);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("B"), T2, List.of("A")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(3, 3, 3, 2), fixture.assignmentSizes());
+ }
+
+ /**
+ * A subscribes to T1 and T2, B to T1 and T3. T1, 3 partitions, has 1 extra partition. T2
+ * gives A 2 partitions and T3 gives B 1, so B has the smaller assignment and gets it.
+ */
+ @Test
+ public void testExtraPartitionGoesToTheCohortWithTheSmallestAssignment() {
+ var fixture = twoCohortGroup(2, 1);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("B")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(3, 3), fixture.assignmentSizes());
+ }
+
+ /**
+ * The same group, with 1 partition for T2 and T3: A and B have the same assignment size, and
+ * the extra partition of T1 goes to the first cohort, A's.
+ */
+ @Test
+ public void testTiesGoToTheFirstCohort() {
+ var fixture = twoCohortGroup(1, 1);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("A")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(3, 2), fixture.assignmentSizes());
+ }
+
+ /**
+ * A, B and C subscribe to T1, T2 and T3, of 4 partitions each: 1 extra partition each. Each
+ * goes to the next member having no extra partition yet, and every member ends with one.
+ */
+ @Test
+ public void testMembersOfACohortAreServedInTurn() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2, T3))
+ .withMember("B", Set.of(T1, T2, T3))
+ .withMember("C", Set.of(T1, T2, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 4)
+ .withTopic(T2, 4)
+ .withTopic(T3, 4)
+ .build();
+ var fixture = new QuotasFixture(spec, describer);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("A"), T2, List.of("B"), T3, List.of("C")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(4, 4, 4), fixture.assignmentSizes());
+ }
+
+ /**
+ * A, B and C subscribe to T1, of 4 partitions, and T2, of 5: 1 extra partition of T1 and 2
+ * of T2. A kept one of T2. The extra partition of T1 skips A, which has more extra partitions
+ * than B and C, and goes to B; the remaining one of T2 goes to C, the only member without
+ * any.
+ */
+ @Test
+ public void testMembersWithTheFewestExtraPartitionsAreServedFirst() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2))
+ .withMember("B", Set.of(T1, T2))
+ .withMember("C", Set.of(T1, T2))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 4)
+ .withTopic(T2, 5)
+ .build();
+ var fixture = new QuotasFixture(spec, describer);
+ fixture.addExtras("A", T2);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("B"), T2, List.of("A", "C")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(3, 3, 3), fixture.assignmentSizes());
+ }
+
+ /**
+ * A, B and C subscribe to T1, T2 and T3, of 5 partitions each: 2 extra partitions each. A
+ * kept one of T1, and B and C both of T2 and T3. The last extra partition of T1 cannot go to
+ * A, the member with the fewest extra partitions, which has one of T1 already: it goes to the
+ * member without one having the fewest extra partitions, B before C in member order. The
+ * balance step evens the assignment sizes out later.
+ */
+ @Test
+ public void testAMemberWithMoreExtraPartitionsGetsOneWhenTheOthersHaveOneOfTheTopic() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2, T3))
+ .withMember("B", Set.of(T1, T2, T3))
+ .withMember("C", Set.of(T1, T2, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 5)
+ .withTopic(T2, 5)
+ .withTopic(T3, 5)
+ .build();
+ var fixture = new QuotasFixture(spec, describer);
+ fixture.addExtras("A", T1);
+ fixture.addExtras("B", T2, T3);
+ fixture.addExtras("C", T2, T3);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("A", "B"), T2, List.of("B", "C"), T3, List.of("B", "C")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(4, 6, 5), fixture.assignmentSizes());
+ }
+
+ /**
+ * A subscribes to T1 only, B and C to T1 and T2. T1, 5 partitions, has 2 extra partitions,
+ * and A kept one. A has the smallest assignment, but its cohort has no member left without an
+ * extra partition of T1, and the other one goes to B.
+ */
+ @Test
+ public void testACohortWhoseMembersAllHaveAnExtraPartitionGetsNoMore() {
Review Comment:
In this test, the cohort is never added to the `candidates` heap. We're missing a test where we do not re-add a newly "saturated" cohort back to the `candidates` heap.
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitionsTest.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests of {@link AssignRemainingExtraPartitions}. The topic ids sort as T1 < T2 < T3 <
+ * T4, and the cohorts are numbered in the order of their first member.
Review Comment:
javadoc, nit: We can probably drop this.
```suggestion
* Tests of {@link AssignRemainingExtraPartitions}.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
Review Comment:
javadoc, nit: Are we talking about the fresh assignment case here?
```suggestion
* cohort then gets it: the balance step moves it if it has to. With an empty current assignment, a homogeneous group
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
+
+ /**
+ * Per cohort, its state.
+ */
+ private CohortState[] cohortStates;
+
+ /**
+ * Per member, whether it has an extra partition of the topic being handed out.
+ */
+ private boolean[] hasExtra;
+
+ /**
+ * @param group The members and topics of the group.
+ * @param quotas The quotas, as the first step left them, which the step completes.
+ */
+ AssignRemainingExtraPartitions(IndexedGroup group, Quotas quotas) {
+ this.group = group;
+ this.quotas = quotas;
+ }
+
+ /**
+ * @return The key of the cohort in the heap of candidates: the pair of the smallest assignment
+ * size among its members and the cohort, so that the heap orders the cohorts by that
+ * assignment size, then cohort order. That assignment size does not change while the
+ * cohort is a candidate, since only the members of a polled cohort get an extra
+ * partition.
+ */
+ private long sizeAndCohort(int cohort) {
+ return IntPair.pack(quotas.minAssignmentSize(cohort) + cohortStates[cohort].lowest, cohort);
+ }
+
+ /**
+ * Hands out the extra partitions that no owner kept. When the owners kept them all, as in a
+ * stable group, returns at once without allocating anything.
+ */
+ void run() {
+ if (!hasExtraPartitionsToHandOut()) {
+ return;
+ }
+ cohortStates = new CohortState[group.cohortCount()];
+ for (int cohort = 0; cohort < cohortStates.length; cohort++) {
+ cohortStates[cohort] = new CohortState(group.membersOf(cohort));
+ findLowest(cohortStates[cohort]);
+ }
+ hasExtra = new boolean[group.memberCount()];
+ candidates = new LongHeap(16);
+ for (int topic : topicsToHandOut()) {
+ handOutTopic(topic);
+ }
+ }
+
+ /**
+ * @return {@code true} if a topic has extra partitions that no owner kept.
+ */
+ private boolean hasExtraPartitionsToHandOut() {
+ for (int topic = 0; topic < group.topicCount(); topic++) {
+ if (quotas.membersWithExtraPartition(topic).size() < quotas.extraPartitions(topic)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Hands out the extra partitions of the topic that no owner kept, one at a time, each to the
+ * next member of the cohort holding the smallest assignment among those still having a member
+ * without one.
+ */
+ private void handOutTopic(int topic) {
+ // The members keeping an extra partition of the topic, which grows as the topic is handed
+ // out.
+ var members = quotas.membersWithExtraPartition(topic);
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = true;
+ cohortStates[group.cohortOf(members.get(i))].holders++;
+ }
+ // The cohorts of the topic with a member still without an extra partition of it, the only
+ // ones that can take one.
+ candidates.clear();
+ for (int cohort : group.cohortsOf(topic)) {
+ var state = cohortStates[cohort];
+ if (state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // The extra partitions that no owner kept, counted once since the members grow while they
+ // are handed out. There is at least one: topicsToHandOut only lists the topics with some.
+ for (int toHandOut = quotas.extraPartitions(topic) - members.size(); toHandOut > 0; toHandOut--) {
+ int cohort = IntPair.low(candidates.poll());
+ var state = cohortStates[cohort];
+ int member = nextMember(state);
+ int count = quotas.extraPartitionCount(member);
+ quotas.giveExtraPartition(member, topic);
+ hasExtra[member] = true;
+ // The last member of the cohort at the lowest number of extra partitions moves up.
+ if (count == state.lowest && --state.atLowest == 0) {
+ findLowest(state);
+ }
+ if (++state.holders < state.members.length) {
+ candidates.add(sizeAndCohort(cohort));
+ }
+ }
+ // Resets the scratch state for the next topic, including the members just served.
+ for (int i = 0; i < members.size(); i++) {
+ hasExtra[members.get(i)] = false;
+ cohortStates[group.cohortOf(members.get(i))].holders = 0;
+ }
+ }
+
+ /**
+ * @return The topics with extra partitions nobody kept, by increasing number of subscribers,
+ * then topic order.
+ */
+ private int[] topicsToHandOut() {
+ // Pairs of the number of subscribers and the topic, which sort by subscribers, then topic.
+ var keys = new long[group.topicCount()];
Review Comment:
naming, nit: `subscribersAndTopics`?
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
Review Comment:
naming, nit: Can we come up with less ambiguous names?
Perhaps `lowestExtraPartitions`, `membersWithLowestExtraPartitions`, `membersWithExtraPartitions`.
And then for consistency below, we would have to rename `hasExtra` to `hasExtraPartitions`.
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitionsTest.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.common.Uuid;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests of {@link AssignRemainingExtraPartitions}. The topic ids sort as T1 < T2 < T3 <
+ * T4, and the cohorts are numbered in the order of their first member.
+ */
+public class AssignRemainingExtraPartitionsTest {
+ private static final Uuid T1 = new Uuid(1L, 1L);
+ private static final Uuid T2 = new Uuid(2L, 1L);
+ private static final Uuid T3 = new Uuid(3L, 1L);
+ private static final Uuid T4 = new Uuid(4L, 1L);
+
+ /**
+ * A subscribes to T1 and T2, B and D to T1 and T3, and C to T2 and T4. T1, 4 partitions over
+ * A, B and D, has 1 extra partition, and T2, 3 partitions over A and C, has 1 too; T3 and T4
+ * have none. The minimum quotas give assignment sizes of 2 to A, B and D, and 3 to C. T2 has
+ * the fewest subscribers and goes first: its extra partition goes to A, the smaller of A and
+ * C. Then the one of T1 goes to B, since A now has 3. The assignment sizes are within one of
+ * each other where the subscriptions allow it. Handing out T1 first would have given both
+ * extra partitions to A, the first cohort on ties: 4 partitions for A and 2 for B, which
+ * subscribes to T1.
+ */
+ @Test
+ public void testTopicsWithTheFewestSubscribersGoFirst() {
+ var spec = new GroupSpecFixture()
+ .withMember("A", Set.of(T1, T2))
+ .withMember("B", Set.of(T1, T3))
+ .withMember("C", Set.of(T2, T4))
+ .withMember("D", Set.of(T1, T3))
+ .build();
+ var describer = new TopicsFixture()
+ .withTopic(T1, 4)
+ .withTopic(T2, 3)
+ .withTopic(T3, 2)
+ .withTopic(T4, 2)
+ .build();
+ var fixture = new QuotasFixture(spec, describer);
+
+ new AssignRemainingExtraPartitions(fixture.group, fixture.quotas).run();
+
+ assertEquals(Map.of(T1, List.of("B"), T2, List.of("A")), fixture.membersWithExtraPartition());
+ assertEquals(List.of(3, 3, 3, 2), fixture.assignmentSizes());
+ }
+
+ /**
+ * A subscribes to T1 and T2, B to T1 and T3. T1, 3 partitions, has 1 extra partition. T2
+ * gives A 2 partitions and T3 gives B 1, so B has the smaller assignment and gets it.
+ */
+ @Test
+ public void testExtraPartitionGoesToTheCohortWithTheSmallestAssignment() {
+ var fixture = twoCohortGroup(2, 1);
Review Comment:
`twoCohortGroup` is only used twice. Shall we inline it? It contains the test setup which you have to read anyway to understand these tests.
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/AssignRemainingExtraPartitions.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.uniform2;
+
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.IntPair;
+import org.apache.kafka.coordinator.group.assignor.uniform2.util.LongHeap;
+
+import java.util.Arrays;
+
+/**
+ * The second step deciding the {@link Quotas}: assigns the extra partitions that no owner kept,
+ * one at a time, to subscribers not having one of the topic yet, among the ones with the smallest
+ * assignments.
+ *
+ * <p>The topics with the fewest subscribers go first, so that they get the smallest of their few
+ * candidates: when the subscriptions are nested, this alone balances the assignment sizes. The
+ * extra partition goes to the cohort holding the smallest assignment, and among its members those
+ * with the smallest assignment are served round robin in member order. The member with the
+ * smallest assignment may already have an extra partition of the topic, and another member of the
+ * cohort then gets it: the balance step moves it if it has to. Without owners, a homogeneous group
+ * is served round robin.
+ *
+ * <p>On exit every topic has exactly its number of extra partitions, on distinct subscribers.
+ */
+final class AssignRemainingExtraPartitions {
+ /**
+ * The state of a cohort while the extra partitions are handed out.
+ */
+ private static final class CohortState {
+ /**
+ * The members of the cohort.
+ */
+ final int[] members;
+
+ /**
+ * The position in the members of the next member to serve, round robin.
+ */
+ int cursor;
+
+ /**
+ * The lowest number of extra partitions of the members.
+ */
+ int lowest;
+
+ /**
+ * How many members have the lowest number of extra partitions.
+ */
+ int atLowest;
+
+ /**
+ * How many members have an extra partition of the topic being handed out.
+ */
+ int holders;
+
+ CohortState(int[] members) {
+ this.members = members;
+ }
+ }
+
+ /**
+ * The members and topics of the group.
+ */
+ private final IndexedGroup group;
+
+ /**
+ * The quotas, which the step completes.
+ */
+ private final Quotas quotas;
+
+ /**
+ * The cohorts of the topic being handed out which can still take an extra partition of it, by
+ * increasing smallest assignment size among their members, whether that member has an extra
+ * partition of the topic or not, then in cohort order, see {@link #sizeAndCohort(int)}.
+ */
+ private LongHeap candidates;
Review Comment:
naming, nit: Not a fan of just "candidates" but I can't figure out a name which tells the reader that it contains cohorts and the cohorts are the ones that can take an extra partition.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org