dajac commented on code in PR #24220:
URL: https://github.com/apache/kafka/pull/24220#discussion_r4616273739


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/steps/AssignRemainingExtraPartitions.java:
##########
@@ -248,22 +264,63 @@ private void computeLowestExtraPartitions(CohortState 
state) {
      */
     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.lowestExtraPartitions && !hasExtraPartitionOfTopic[member]) {
-                state.cursor = (index + 1) % members.length;
-                return member;
+        if (state.lowestExtraPartitionsWithoutTopic < 0) {
+            for (int step = 0; step < members.length; step++) {
+                int index = (state.cursor + step) % members.length;
+                int member = members[index];
+                if (quotas.extraPartitionCount(member) == 
state.lowestExtraPartitions && !hasExtraPartitionOfTopic[member]) {
+                    state.cursor = (index + 1) % members.length;
+                    return member;
+                }
+            }
+            // Every member with the lowest number of extra partitions already 
has an extra
+            // partition of the topic. None of them can get another one, so 
this holds until the
+            // next topic, and the round robin is not tried again.
+            computeLowestExtraPartitionsWithoutTopic(state);
+        }
+        // A member gets an extra partition of the topic, and one more extra 
partition, only when
+        // it is returned. So no member that the cursor passed can be returned 
before the lowest
+        // number of extra partitions among the members without one is 
computed again, which
+        // moves the cursor back to the first member.
+        int index = nextMemberWithoutTopic(state, state.cursorWithoutTopic);
+        if (index == members.length) {
+            computeLowestExtraPartitionsWithoutTopic(state);
+            index = nextMemberWithoutTopic(state, 0);
+        }
+        state.cursorWithoutTopic = index + 1;
+        return members[index];
+    }
+
+    /**
+     * Computes the lowest number of extra partitions among the members of the 
cohort without an
+     * extra partition of the topic, and moves {@link 
CohortState#cursorWithoutTopic} back to the
+     * first member.
+     */
+    private void computeLowestExtraPartitionsWithoutTopic(CohortState state) {
+        int lowest = Integer.MAX_VALUE;
+        for (int member : state.members) {
+            if (!hasExtraPartitionOfTopic[member]) {
+                lowest = Math.min(lowest, quotas.extraPartitionCount(member));
             }
         }
-        // Every member with the lowest number of extra partitions already has 
one of the topic.
-        int best = -1;
-        for (int member : members) {
-            if (!hasExtraPartitionOfTopic[member] &&
-                (best < 0 || quotas.extraPartitionCount(member) < 
quotas.extraPartitionCount(best))) {
-                best = member;
+        state.lowestExtraPartitionsWithoutTopic = lowest;
+        state.cursorWithoutTopic = 0;
+    }
+
+    /**
+     * @return The position of the first member of the cohort, at or after the 
position
+     *         {@code from} in member order, that has
+     *         {@link CohortState#lowestExtraPartitionsWithoutTopic} extra 
partitions and no extra
+     *         partition of the topic, or the number of members if there is 
none.
+     */
+    private int nextMemberWithoutTopic(CohortState state, int from) {
+        var members = state.members;
+        for (int index = from; index < members.length; index++) {
+            int member = members[index];
+            if (quotas.extraPartitionCount(member) == 
state.lowestExtraPartitionsWithoutTopic && !hasExtraPartitionOfTopic[member]) {
+                return index;
             }
         }
-        return best;
+        return members.length;

Review Comment:
   Done, `nextMemberWithoutTopic` is now `indexOfMemberWithoutTopic` and 
returns -1 when there is none.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/uniform2/steps/AssignRemainingExtraPartitions.java:
##########
@@ -248,22 +264,63 @@ private void computeLowestExtraPartitions(CohortState 
state) {
      */
     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.lowestExtraPartitions && !hasExtraPartitionOfTopic[member]) {
-                state.cursor = (index + 1) % members.length;
-                return member;
+        if (state.lowestExtraPartitionsWithoutTopic < 0) {
+            for (int step = 0; step < members.length; step++) {
+                int index = (state.cursor + step) % members.length;
+                int member = members[index];
+                if (quotas.extraPartitionCount(member) == 
state.lowestExtraPartitions && !hasExtraPartitionOfTopic[member]) {
+                    state.cursor = (index + 1) % members.length;
+                    return member;
+                }
+            }

Review Comment:
   They differ in two ways: this loop wraps around from the cursor and looks 
for `lowestExtraPartitions`, while `nextMemberWithoutTopic` does not wrap and 
looks for `lowestExtraPartitionsWithoutTopic`. The condition was written twice 
though, so both now use `indexOfMemberWithoutTopic(state, from, to, 
extraPartitionCount)`. This loop calls it for `[cursor, length)` and then `[0, 
cursor)`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to