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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/BalancedTaskAssignor.java:
##########
@@ -273,7 +283,63 @@ private static void assignStandbyReplicaTasks(final 
Collection<ProcessTasks> pro
                 tasksShortOfReplicas, statefulTasks.size(), 
numStandbyReplicas, missingReplicas, processes.size());
         }
 
-        balanceTasksOverProcesses(processes, process -> process.standbyTasks);
+        balanceTasksOverProcesses(
+            processes,
+            process -> process.standbyTasks,
+            rackAwareAssignmentTags.isEmpty() ? null : new 
RackDiversity(processes, statefulTasks.size(), rackAwareAssignmentTags)
+        );
+    }
+
+    /**
+     * Places the standby tasks that make their task more rack-diverse, each 
on the least loaded process among the
+     * most diverse ones, and returns the number placed per task. A process 
takes them while one of its members is
+     * below the per-member task quota.
+     */
+    private static Map<TaskId, Integer> assignRackAwareStandbys(final 
Collection<ProcessTasks> processes,
+                                                               final 
SortedSet<TaskId> statefulTasks,
+                                                               final int 
numStandbyReplicas,
+                                                               final 
List<String> rackAwareAssignmentTags) {
+        final Map<TaskId, ProcessTasks> activeTaskToProcess = 
Utils.newHashMap(statefulTasks.size());
+        for (final ProcessTasks process : processes) {
+            for (final TaskId task : process.statefulActiveTasks) {
+                activeTaskToProcess.put(task, process);
+            }
+        }
+
+        final TaskQuota quota = new TaskQuota(processes, (long) 
statefulTasks.size() * numStandbyReplicas);
+        // The processes are in sorted order of their ID, so a load tie goes 
to the smaller ID, as in BY_ASSIGNED_LOAD.
+        final RackAwareStandbyPicker<ProcessTasks> picker = new 
RackAwareStandbyPicker<>(
+            rackAwareAssignmentTags,
+            processes,
+            process -> process.clientTags,
+            ProcessTasks::assignedTaskLoad,
+            quota::hasRoom
+        );
+        final Map<TaskId, Integer> rackAwareStandbys = 
Utils.newHashMap(statefulTasks.size());
+        for (final TaskId task : statefulTasks) {
+            picker.startTask(List.of(activeTaskToProcess.get(task)));
+            int placed = 0;
+            while (placed < numStandbyReplicas) {

Review Comment:
    I flipped the loop order and tested it. For numStandbyReplicas=2:           
    
     - Rack: with tags [key1, key2], the fraction of tasks that lose all their 
replicas when one value of key1 fails goes from 9.0% to 5.0%, and when one 
value of key2 fails from 12.1% to 6.9%, so about 40% fewer.               
     - Balance: load balance per process and task balance per member both 
improve by about 10–20%.                                                        
                                                                          
     - Standbys placed on a process without state go up by about 5%. For the 
balanced assignor this should not be an issue.                                  
                                                                       
     - Runtime (JMH, 1000 members, 50 members per process, 10,000 tasks) goes 
up by about 5%.  (each time we need to clear the mark and mark again)           
                                                                                
                                          
     So I changed it to the flipped order in this PR.   For sticky assignor, I 
will do it in a follow up pr after testing.
   



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