mjsax commented on code in PR #23924:
URL: https://github.com/apache/kafka/pull/23924#discussion_r4560560676


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/BalancedTaskAssignor.java:
##########
@@ -373,29 +447,143 @@ private static Map<String, Set<Integer>> 
toCompactedTaskIds(final Set<TaskId> ta
         return ret;
     }
 
+    /**
+     * The per-member quota on active plus standby tasks: the tasks left over 
the members left, rounded up, where a
+     * member that reaches it leaves with its tasks. The members of a process 
are levelled once the placement is final,
+     * so the member that takes the n-th task of a process with c members 
reaches ceil(n / c).
+     */
+    private static final class TaskQuota {
+        private int totalMembersWithTaskCapacity;
+        private long totalTasks;
+        private int totalTasksPerMember;
+
+        /**
+         * @param standbysToPlace The standbys still to place; the tasks the 
processes hold count as placed one by one.
+         */
+        private TaskQuota(final Collection<ProcessTasks> processes, final long 
standbysToPlace) {
+            totalTasks = standbysToPlace;
+            for (final ProcessTasks process : processes) {
+                totalMembersWithTaskCapacity += process.capacity();
+                totalTasks += process.assignedTaskCount();
+            }
+            totalTasksPerMember = computeTasksPerMember(totalTasks, 
totalMembersWithTaskCapacity);
+            for (final ProcessTasks process : processes) {
+                for (int taskCount = 1; taskCount <= 
process.assignedTaskCount(); taskCount++) {
+                    maybeUpdateTotalTasksPerMember(memberTaskCount(taskCount, 
process.capacity()));
+                }
+            }
+        }
+
+        /** Whether one of the members of the process is below the quota. */
+        private boolean hasRoom(final ProcessTasks process) {
+            return process.assignedTaskCount() < (long) process.capacity() * 
totalTasksPerMember;
+        }
+
+        private void maybeUpdateTotalTasksPerMember(final int taskNo) {
+            if (taskNo == totalTasksPerMember) {
+                totalMembersWithTaskCapacity--;
+                totalTasks -= taskNo;
+                totalTasksPerMember = computeTasksPerMember(totalTasks, 
totalMembersWithTaskCapacity);
+            }
+        }
+
+        private static int computeTasksPerMember(final long numberOfTasks, 
final int numberOfMembers) {
+            return numberOfMembers == 0 ? 0 : (int) ((numberOfTasks + 
numberOfMembers - 1) / numberOfMembers);
+        }
+
+        /** The task count of the member that takes the {@code 
processTaskCount}-th task of a levelled process. */
+        private static int memberTaskCount(final int processTaskCount, final 
int capacity) {
+            return (processTaskCount + capacity - 1) / capacity;
+        }
+    }
+
+    /**
+     * The processes holding each stateful task, to tell whether moving a 
standby lowers the rack diversity of its task:
+     * the first tag key, in {@code rack.aware.assignment.tags} order, whose 
distinct values among the holders change
+     * may not lose one. A missing value counts as no value.
+     */
+    static final class RackDiversity {
+        private final List<String> tagKeys;
+        private final Map<TaskId, List<ProcessTasks>> holders;
+
+        RackDiversity(final Collection<ProcessTasks> processes, final int 
numStatefulTasks, final List<String> tagKeys) {
+            this.tagKeys = tagKeys;
+            this.holders = Utils.newHashMap(numStatefulTasks);
+            for (final ProcessTasks process : processes) {
+                for (final TaskId task : process.statefulActiveTasks) {
+                    holders.computeIfAbsent(task, taskId -> new 
ArrayList<>()).add(process);
+                }
+                for (final TaskId task : process.standbyTasks) {
+                    holders.computeIfAbsent(task, taskId -> new 
ArrayList<>()).add(process);
+                }
+            }
+        }
+
+        /** Whether moving the task's standby from source to destination would 
not decrease its rack diversity. */
+        private boolean wouldNotDecrease(final TaskId task, final ProcessTasks 
source, final ProcessTasks destination) {
+            for (final String key : tagKeys) {
+                final String sourceValue = source.clientTags.get(key);
+                final String destinationValue = 
destination.clientTags.get(key);
+                if (Objects.equals(sourceValue, destinationValue)) {
+                    continue;
+                }
+                // The source's value stays if another holder has it; the 
destination's value is new if none has it.
+                boolean sourceValueStays = sourceValue == null;
+                boolean destinationValueNew = destinationValue != null;
+                for (final ProcessTasks holder : holders.get(task)) {
+                    if (holder != source) {
+                        final String value = holder.clientTags.get(key);
+                        sourceValueStays |= Objects.equals(value, sourceValue);
+                        destinationValueNew &= !Objects.equals(value, 
destinationValue);
+                    }
+                }
+                // The first key whose distinct values change decides: a gain 
allows the move, a loss blocks it.
+                if (sourceValueStays && destinationValueNew) {
+                    return true;
+                }
+                if (!sourceValueStays && !destinationValueNew) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        private void moved(final TaskId task, final ProcessTasks source, final 
ProcessTasks destination) {
+            final List<ProcessTasks> taskHolders = holders.get(task);
+            taskHolders.set(taskHolders.indexOf(source), destination);
+        }
+    }
+
     /**
      * The tasks placed on one process, and the members the process 
contributes. The assignment across processes
      * works on this level; the tasks are spread over the members only once 
the placement is final.
      */
-    private static final class ProcessTasks {
+    static final class ProcessTasks {
         private final String processId;
         private final TreeSet<String> memberIds = new TreeSet<>();
-        private final SortedSet<TaskId> statefulActiveTasks = new TreeSet<>();
+        final SortedSet<TaskId> statefulActiveTasks = new TreeSet<>();
         private final SortedSet<TaskId> statelessActiveTasks = new TreeSet<>();
-        private final SortedSet<TaskId> standbyTasks = new TreeSet<>();
+        final SortedSet<TaskId> standbyTasks = new TreeSet<>();
         // The member of this process that currently holds a task in any role, 
so that a stateful task can stay on
         // the member that has its state, as the classic client does through 
the offset sums each thread reports.
         private final Map<TaskId, String> currentOwner = new HashMap<>();
+        private Map<String, String> clientTags = Map.of();
 
-        private ProcessTasks(final String processId) {
+        ProcessTasks(final String processId) {
             this.processId = processId;
         }
 
         private String processId() {
             return processId;
         }
 
-        private void addMember(final String memberId, final 
MemberAssignmentState currentAssignment) {
+        void addMember(final String memberId,
+                       final Map<String, String> memberClientTags,
+                       final MemberAssignmentState currentAssignment) {

Review Comment:
   ```suggestion
           void addMember(
               final String memberId,
               final Map<String, String> memberClientTags,
               final MemberAssignmentState currentAssignment
           ) {
   ```



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