You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "lihaosky (via GitHub)" <gi...@apache.org> on 2023/11/08 01:32:02 UTC

[PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

lihaosky opened a new pull request, #14714:
URL: https://github.com/apache/kafka/pull/14714

   ### Description
   Refactor graph construction and assignment in `RackAwareAssignor` to new interface. Will add implementation for subtopology case and unit test later
   


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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #14714:
URL: https://github.com/apache/kafka/pull/14714#discussion_r1408740752


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareGraphConstructor.java:
##########
@@ -0,0 +1,47 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+/**
+ * Construct graph for rack aware task assignor
+ */
+public interface RackAwareGraphConstructor {
+    int SOURCE_ID = -1;
+
+    int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup);
+
+    int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex);
+
+    int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex);
+
+    Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList,

Review Comment:
   nit: align indention (same below)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size();
+    }
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientIndex + taskIdList.size();
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size();
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // Make task and client Node id in graph deterministic
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            final TaskId taskId = taskIdList.get(taskNodeId);
+            for (int j = 0; j < clientList.size(); j++) {
+                final int clientNodeId = getClientNodeId(j, taskIdList, clientList, 0);

Review Comment:
   Given that this is min-cost case, so we need to pass `clientList` or could we pass `null` (similar to calling `getSinkNodeID(...)` with `null` below) ?
   
   Should we pass `0` or `-1` to highly "unused" better?
   
   Might be worth to add a comment?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {

Review Comment:
   nit: formatting
   ```
   // either
   public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
   
   // or
   public int getSinkNodeID(
       final List<TaskId> taskIdList,
       final List<UUID> clientList,
       final Map<Subtopology, Set<TaskId>> tasksForTopicGroup
   ) {
   ```



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareGraphConstructor.java:
##########
@@ -0,0 +1,47 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+/**
+ * Construct graph for rack aware task assignor
+ */
+public interface RackAwareGraphConstructor {
+    int SOURCE_ID = -1;
+
+    int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup);
+
+    int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex);
+
+    int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex);
+
+    Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList,
+        final SortedMap<UUID, ClientState> clientStates,

Review Comment:
   Thia was a `Map` in the old code -- why the change to `SortedMap` ?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareTaskAssignor.java:
##########
@@ -61,19 +59,30 @@ boolean canMove(final ClientState source,
                         final Map<UUID, ClientState> clientStateMap);
     }
 
+    @FunctionalInterface
+    public interface GetCostFunction {

Review Comment:
   `GetCostFunction` -> `CostFunction`
   
   (This interface does not _return_ a cost function, but _is_ a cost function, right?)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size();
+    }
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientIndex + taskIdList.size();
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size();
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {

Review Comment:
   nit: formatting (one parameter per line -- cf comment above)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size();
+    }
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientIndex + taskIdList.size();
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size();
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // Make task and client Node id in graph deterministic
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            final TaskId taskId = taskIdList.get(taskNodeId);
+            for (int j = 0; j < clientList.size(); j++) {
+                final int clientNodeId = getClientNodeId(j, taskIdList, clientList, 0);
+                final UUID processId = clientList.get(j);
+
+                final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                final int cost = getCostFunction.getCost(taskId, processId, flow == 1, trafficCost,
+                    nonOverlapCost, isStandby);
+                if (flow == 1) {
+                    if (!hasReplica && taskClientMap.containsKey(taskId)) {
+                        throw new IllegalArgumentException("Task " + taskId + " assigned to multiple clients "
+                            + processId + ", " + taskClientMap.get(taskId));
+                    }
+                    taskClientMap.put(taskId, processId);
+                }
+
+                graph.addEdge(taskNodeId, clientNodeId, 1, cost, flow);
+            }
+            if (!taskClientMap.containsKey(taskId)) {
+                throw new IllegalArgumentException("Task " + taskId + " not assigned to any client");
+            }
+
+            // Add edge from source to task
+            graph.addEdge(SOURCE_ID, taskNodeId, 1, 0, 1);
+        }
+
+        final int sinkId = getSinkNodeID(taskIdList, clientList, null);
+        // It's possible that some clients have 0 task assign. These clients will have 0 tasks assigned
+        // even though it may have higher traffic cost. This is to maintain the original assigned task count
+        for (int i = 0; i < clientList.size(); i++) {
+            final int clientNodeId = getClientNodeId(i, taskIdList, clientList, 0);
+            final int capacity = originalAssignedTaskNumber.getOrDefault(clientList.get(i), 0);
+            // Flow equals to capacity for edges to sink
+            graph.addEdge(clientNodeId, sinkId, capacity, 0, capacity);
+        }
+
+        graph.setSourceNode(SOURCE_ID);
+        graph.setSinkNode(sinkId);
+
+        return graph;
+    }
+
+    @Override
+    public boolean assignTaskFromMinCostFlow(final Graph<Integer> graph,
+        final List<UUID> clientList, final List<TaskId> taskIdList,
+        final Map<UUID, ClientState> clientStates,
+        final Map<UUID, Integer> originalAssignedTaskNumber, final Map<TaskId, UUID> taskClientMap,
+        final BiConsumer<ClientState, TaskId> assignTask,
+        final BiConsumer<ClientState, TaskId> unAssignTask,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask) {
+
+        int tasksAssigned = 0;
+        boolean taskMoved = false;
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            final TaskId taskId = taskIdList.get(taskNodeId);
+            final Map<Integer, Graph<Integer>.Edge> edges = graph.edges(taskNodeId);
+            for (final Graph<Integer>.Edge edge : edges.values()) {
+                if (edge.flow > 0) {
+                    tasksAssigned++;
+                    final int clientIndex = getClientIndex(edge.destination, taskIdList, clientList, 0);

Review Comment:
   as above?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {

Review Comment:
   formatting (cf other comments)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareTaskAssignor.java:
##########
@@ -327,8 +325,8 @@ private long tasksCost(final SortedSet<TaskId> tasks,
         }
         final List<UUID> clientList = new ArrayList<>(clientStates.keySet());
         final List<TaskId> taskIdList = new ArrayList<>(tasks);
-        final Graph<Integer> graph = constructTaskGraph(clientList, taskIdList,
-            clientStates, new HashMap<>(), new HashMap<>(), hasAssignedTask, trafficCost, nonOverlapCost, hasReplica, isStandby);
+        final Graph<Integer> graph = RackAwareGraphConstructorFactory.create(assignmentConfigs, tasksForTopicGroup).constructTaskGraph(clientList, taskIdList,
+            clientStates, new HashMap<>(), new HashMap<>(), hasAssignedTask, this::getCost, trafficCost, nonOverlapCost, hasReplica, isStandby);

Review Comment:
   nit: formatting (pass one parameter per line):
   ```
   final Graph<Integer> graph = RackAwareGraphConstructorFactory
       .create(assignmentConfigs, tasksForTopicGroup)
       .constructTaskGraph(
           clientList,
           taskIdList,
           ...,
           hasReplica,
           isStandby
       );
   ````



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);
+                    startingTaskNodeId++;
+                }
+
+                final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+                final int capacity = (int) Math.ceil(originalAssignedTaskNumber.get(processId) * 1.0 / taskIdList.size() * taskIds.size());
+                graph.addEdge(clientNodeId, secondStageClientNodeId, capacity, 0, 0);
+            }
+
+            taskNodeId += taskIds.size();
+            topicGroupIndex++;
+        }
+
+        for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+            final UUID processId = clientList.get(clientIndex);
+            final int capacity = originalAssignedTaskNumber.get(processId);
+            final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+            graph.addEdge(secondStageClientNodeId, sinkId, capacity, 0, 0);
+        }
+
+        graph.setSourceNode(SOURCE_ID);
+        graph.setSinkNode(sinkId);
+
+        // Run max flow algorithm to get a solution first
+        final long maxFlow = graph.calculateMaxFlow();
+        if (maxFlow != taskIdList.size()) {
+            throw new IllegalStateException("max flow calculated: " + maxFlow + " doesn't match taskSize: " + taskIdList.size());
+        }
+
+        return graph;
+    }
+
+    @Override
+    public boolean assignTaskFromMinCostFlow(final Graph<Integer> graph,
+        final List<UUID> clientList, final List<TaskId> taskIdList,
+        final Map<UUID, ClientState> clientStates,
+        final Map<UUID, Integer> originalAssignedTaskNumber, final Map<TaskId, UUID> taskClientMap,
+        final BiConsumer<ClientState, TaskId> assignTask,
+        final BiConsumer<ClientState, TaskId> unAssignTask,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask) {
+
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        int tasksAssigned = 0;
+        boolean taskMoved = false;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (final TaskId taskId : taskIds) {
+                final Map<Integer, Graph<Integer>.Edge> edges = graph.edges(taskNodeId);
+                for (final Graph<Integer>.Edge edge : edges.values()) {
+                    if (edge.flow > 0) {
+                        tasksAssigned++;
+                        final int clientIndex = getClientIndex(edge.destination, taskIdList, clientList, topicGroupIndex);
+                        final UUID processId = clientList.get(clientIndex);
+                        final UUID originalProcessId = taskClientMap.get(taskId);
+
+                        // Don't need to assign this task to other client
+                        if (processId.equals(originalProcessId)) {
+                            break;
+                        }
+
+                        unAssignTask.accept(clientStates.get(originalProcessId), taskId);
+                        assignTask.accept(clientStates.get(processId), taskId);
+                        taskMoved = true;
+                    }
+                }
+                taskNodeId++;
+            }
+            topicGroupIndex++;
+        }
+
+        // Validate task assigned

Review Comment:
   Seems this validation code (starting here to the end of the method) is the same as for min-traffic? Should we extract a helper method and share code?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java:
##########
@@ -220,6 +222,78 @@ public void solveMinCostFlow() {
         }
     }
 
+    public long calculateMaxFlow() {
+        final Graph<V> residualGraph = residualGraph();
+        residualGraph.fordFulkson();
+
+        long maxFlow = 0;
+        for (final Entry<V, SortedMap<V, Edge>> nodeEdges : adjList.entrySet()) {
+            final V node = nodeEdges.getKey();
+            for (final Entry<V, Edge> nodeEdge : nodeEdges.getValue().entrySet()) {
+                final V destination = nodeEdge.getKey();
+                final Edge edge = nodeEdge.getValue();
+                final Edge residualEdge = residualGraph.adjList.get(node).get(destination);
+                edge.flow = residualEdge.flow;
+                edge.residualFlow = residualEdge.residualFlow;
+
+                if (node == sourceNode) {
+                    maxFlow += edge.flow;
+                }
+            }
+        }
+
+        return maxFlow;
+    }
+
+    private void fordFulkson() {
+        if (!isResidualGraph) {
+            throw new IllegalStateException("Should be residual graph to cancel negative cycles");
+        }
+
+        Map<V, V> parents = new HashMap<>();
+        while (bfs(sourceNode, sinkNode, parents)) {
+            int flow = Integer.MAX_VALUE;
+            for (V node = sinkNode; node != sourceNode; node = parents.get(node)) {
+                final V parent = parents.get(node);
+                flow = Math.min(flow, adjList.get(parent).get(node).residualFlow);
+            }
+
+            for (V node = sinkNode; node != sourceNode; node = parents.get(node)) {
+                final V parent = parents.get(node);
+                adjList.get(parent).get(node).residualFlow -= flow;
+                adjList.get(node).get(parent).residualFlow += flow;
+            }
+
+            parents = new HashMap<>();
+        }
+    }
+
+    private boolean bfs(final V source, final V target, final Map<V, V> parents) {

Review Comment:
   `bfs` -> avoid abbreviations



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);
+                    startingTaskNodeId++;
+                }
+
+                final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+                final int capacity = (int) Math.ceil(originalAssignedTaskNumber.get(processId) * 1.0 / taskIdList.size() * taskIds.size());
+                graph.addEdge(clientNodeId, secondStageClientNodeId, capacity, 0, 0);
+            }
+
+            taskNodeId += taskIds.size();
+            topicGroupIndex++;
+        }
+
+        for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+            final UUID processId = clientList.get(clientIndex);
+            final int capacity = originalAssignedTaskNumber.get(processId);
+            final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+            graph.addEdge(secondStageClientNodeId, sinkId, capacity, 0, 0);
+        }
+
+        graph.setSourceNode(SOURCE_ID);
+        graph.setSinkNode(sinkId);
+
+        // Run max flow algorithm to get a solution first
+        final long maxFlow = graph.calculateMaxFlow();
+        if (maxFlow != taskIdList.size()) {
+            throw new IllegalStateException("max flow calculated: " + maxFlow + " doesn't match taskSize: " + taskIdList.size());
+        }
+
+        return graph;
+    }
+
+    @Override
+    public boolean assignTaskFromMinCostFlow(final Graph<Integer> graph,
+        final List<UUID> clientList, final List<TaskId> taskIdList,
+        final Map<UUID, ClientState> clientStates,
+        final Map<UUID, Integer> originalAssignedTaskNumber, final Map<TaskId, UUID> taskClientMap,
+        final BiConsumer<ClientState, TaskId> assignTask,
+        final BiConsumer<ClientState, TaskId> unAssignTask,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask) {

Review Comment:
   nit: formatting



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size();
+    }
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientIndex + taskIdList.size();
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size();
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // Make task and client Node id in graph deterministic
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            final TaskId taskId = taskIdList.get(taskNodeId);
+            for (int j = 0; j < clientList.size(); j++) {
+                final int clientNodeId = getClientNodeId(j, taskIdList, clientList, 0);
+                final UUID processId = clientList.get(j);
+
+                final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                final int cost = getCostFunction.getCost(taskId, processId, flow == 1, trafficCost,
+                    nonOverlapCost, isStandby);
+                if (flow == 1) {
+                    if (!hasReplica && taskClientMap.containsKey(taskId)) {
+                        throw new IllegalArgumentException("Task " + taskId + " assigned to multiple clients "
+                            + processId + ", " + taskClientMap.get(taskId));
+                    }
+                    taskClientMap.put(taskId, processId);
+                }
+
+                graph.addEdge(taskNodeId, clientNodeId, 1, cost, flow);
+            }
+            if (!taskClientMap.containsKey(taskId)) {
+                throw new IllegalArgumentException("Task " + taskId + " not assigned to any client");
+            }
+
+            // Add edge from source to task
+            graph.addEdge(SOURCE_ID, taskNodeId, 1, 0, 1);
+        }
+
+        final int sinkId = getSinkNodeID(taskIdList, clientList, null);
+        // It's possible that some clients have 0 task assign. These clients will have 0 tasks assigned
+        // even though it may have higher traffic cost. This is to maintain the original assigned task count
+        for (int i = 0; i < clientList.size(); i++) {
+            final int clientNodeId = getClientNodeId(i, taskIdList, clientList, 0);
+            final int capacity = originalAssignedTaskNumber.getOrDefault(clientList.get(i), 0);
+            // Flow equals to capacity for edges to sink
+            graph.addEdge(clientNodeId, sinkId, capacity, 0, capacity);
+        }
+
+        graph.setSourceNode(SOURCE_ID);
+        graph.setSinkNode(sinkId);
+
+        return graph;
+    }
+
+    @Override
+    public boolean assignTaskFromMinCostFlow(final Graph<Integer> graph,
+        final List<UUID> clientList, final List<TaskId> taskIdList,
+        final Map<UUID, ClientState> clientStates,
+        final Map<UUID, Integer> originalAssignedTaskNumber, final Map<TaskId, UUID> taskClientMap,
+        final BiConsumer<ClientState, TaskId> assignTask,
+        final BiConsumer<ClientState, TaskId> unAssignTask,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask) {

Review Comment:
   nit: fix indention (cf other comments).



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/MinTrafficGraphConstructor.java:
##########
@@ -0,0 +1,162 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class MinTrafficGraphConstructor implements RackAwareGraphConstructor {
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size();
+    }
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientIndex + taskIdList.size();
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size();
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // Make task and client Node id in graph deterministic
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            final TaskId taskId = taskIdList.get(taskNodeId);
+            for (int j = 0; j < clientList.size(); j++) {
+                final int clientNodeId = getClientNodeId(j, taskIdList, clientList, 0);
+                final UUID processId = clientList.get(j);
+
+                final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                final int cost = getCostFunction.getCost(taskId, processId, flow == 1, trafficCost,
+                    nonOverlapCost, isStandby);
+                if (flow == 1) {
+                    if (!hasReplica && taskClientMap.containsKey(taskId)) {
+                        throw new IllegalArgumentException("Task " + taskId + " assigned to multiple clients "
+                            + processId + ", " + taskClientMap.get(taskId));
+                    }
+                    taskClientMap.put(taskId, processId);
+                }
+
+                graph.addEdge(taskNodeId, clientNodeId, 1, cost, flow);
+            }
+            if (!taskClientMap.containsKey(taskId)) {
+                throw new IllegalArgumentException("Task " + taskId + " not assigned to any client");
+            }
+
+            // Add edge from source to task
+            graph.addEdge(SOURCE_ID, taskNodeId, 1, 0, 1);
+        }
+
+        final int sinkId = getSinkNodeID(taskIdList, clientList, null);
+        // It's possible that some clients have 0 task assign. These clients will have 0 tasks assigned
+        // even though it may have higher traffic cost. This is to maintain the original assigned task count
+        for (int i = 0; i < clientList.size(); i++) {
+            final int clientNodeId = getClientNodeId(i, taskIdList, clientList, 0);

Review Comment:
   as above?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareTaskAssignor.java:
##########
@@ -363,13 +361,14 @@ public long optimizeActiveTasks(final SortedSet<TaskId> activeTasks,
         final List<TaskId> taskIdList = new ArrayList<>(activeTasks);
         final Map<TaskId, UUID> taskClientMap = new HashMap<>();
         final Map<UUID, Integer> originalAssignedTaskNumber = new HashMap<>();
-        final Graph<Integer> graph = constructTaskGraph(clientList, taskIdList,
-            clientStates, taskClientMap, originalAssignedTaskNumber, ClientState::hasActiveTask, trafficCost, nonOverlapCost, false, false);
+        final RackAwareGraphConstructor graphConstructor = RackAwareGraphConstructorFactory.create(assignmentConfigs, tasksForTopicGroup);
+        final Graph<Integer> graph = graphConstructor.constructTaskGraph(clientList, taskIdList,
+            clientStates, taskClientMap, originalAssignedTaskNumber, ClientState::hasActiveTask, this::getCost, trafficCost, nonOverlapCost, false, false);

Review Comment:
   nit: formatting (pass one parameter per line):
   ```
   final Graph<Integer> graph = graphConstructor.constructTaskGraph(
       clientList,
       taskIdList,
       ...,
       false,
       false
   );
   ```



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);

Review Comment:
   It seems we add this edge multiple times?
   
   Would it be better to flip the nesting of `for (final TaskId taskId : taskIds)` and `for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {` to allow us to assign this edge only once?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);
+                    startingTaskNodeId++;
+                }
+
+                final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);

Review Comment:
   Seems that if we flip the nesting, we move the issue to here? -- Mabye you try to do to many things at one in the same loops? Would it be better to break it a little bit better, and loop over all sub-topology two times (instead of ones?)



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {

Review Comment:
   nit: formatting



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);
+                    startingTaskNodeId++;
+                }
+
+                final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+                final int capacity = (int) Math.ceil(originalAssignedTaskNumber.get(processId) * 1.0 / taskIdList.size() * taskIds.size());
+                graph.addEdge(clientNodeId, secondStageClientNodeId, capacity, 0, 0);
+            }
+
+            taskNodeId += taskIds.size();
+            topicGroupIndex++;
+        }
+
+        for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+            final UUID processId = clientList.get(clientIndex);
+            final int capacity = originalAssignedTaskNumber.get(processId);
+            final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);
+            graph.addEdge(secondStageClientNodeId, sinkId, capacity, 0, 0);
+        }
+
+        graph.setSourceNode(SOURCE_ID);
+        graph.setSinkNode(sinkId);
+
+        // Run max flow algorithm to get a solution first
+        final long maxFlow = graph.calculateMaxFlow();
+        if (maxFlow != taskIdList.size()) {
+            throw new IllegalStateException("max flow calculated: " + maxFlow + " doesn't match taskSize: " + taskIdList.size());
+        }
+
+        return graph;
+    }
+
+    @Override
+    public boolean assignTaskFromMinCostFlow(final Graph<Integer> graph,
+        final List<UUID> clientList, final List<TaskId> taskIdList,
+        final Map<UUID, ClientState> clientStates,
+        final Map<UUID, Integer> originalAssignedTaskNumber, final Map<TaskId, UUID> taskClientMap,
+        final BiConsumer<ClientState, TaskId> assignTask,
+        final BiConsumer<ClientState, TaskId> unAssignTask,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask) {
+
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        int tasksAssigned = 0;
+        boolean taskMoved = false;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (final TaskId taskId : taskIds) {
+                final Map<Integer, Graph<Integer>.Edge> edges = graph.edges(taskNodeId);
+                for (final Graph<Integer>.Edge edge : edges.values()) {

Review Comment:
   Seems this `for` loop is the same as for min-cost? Should we extract the code into a helper method?



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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "lihaosky (via GitHub)" <gi...@apache.org>.
lihaosky commented on code in PR #14714:
URL: https://github.com/apache/kafka/pull/14714#discussion_r1409723863


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -0,0 +1,186 @@
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import java.util.function.BiPredicate;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.apache.kafka.streams.processor.internals.assignment.RackAwareTaskAssignor.GetCostFunction;
+
+public class BalanceSubtopologyGraphConstructor implements RackAwareGraphConstructor {
+
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup;
+
+    public BalanceSubtopologyGraphConstructor(final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        this.tasksForTopicGroup = tasksForTopicGroup;
+    }
+
+    @Override
+    public int getSinkNodeID(final List<TaskId> taskIdList, final List<UUID> clientList,
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup) {
+        return clientList.size() + taskIdList.size() + clientList.size() * tasksForTopicGroup.size();
+    }
+
+
+    @Override
+    public int getClientNodeId(final int clientIndex, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return taskIdList.size() + clientList.size() * topicGroupIndex + clientIndex;
+    }
+
+    @Override
+    public int getClientIndex(final int clientNodeId, final List<TaskId> taskIdList, final List<UUID> clientList, final int topicGroupIndex) {
+        return clientNodeId - taskIdList.size() - clientList.size() * topicGroupIndex;
+    }
+
+    private static int getSecondStageClientNodeId(final List<TaskId> taskIdList, final List<UUID> clientList, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final int clientIndex) {
+        return taskIdList.size() + clientList.size() * tasksForTopicGroup.size() + clientIndex;
+    }
+
+    @Override
+    public Graph<Integer> constructTaskGraph(final List<UUID> clientList,
+        final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStates,
+        final Map<TaskId, UUID> taskClientMap, final Map<UUID, Integer> originalAssignedTaskNumber,
+        final BiPredicate<ClientState, TaskId> hasAssignedTask, final GetCostFunction getCostFunction, final int trafficCost,
+        final int nonOverlapCost, final boolean hasReplica, final boolean isStandby) {
+        final Graph<Integer> graph = new Graph<>();
+
+        for (final TaskId taskId : taskIdList) {
+            for (final Entry<UUID, ClientState> clientState : clientStates.entrySet()) {
+                if (hasAssignedTask.test(clientState.getValue(), taskId)) {
+                    originalAssignedTaskNumber.merge(clientState.getKey(), 1, Integer::sum);
+                }
+            }
+        }
+
+        // TODO: validate tasks in tasksForTopicGroup and taskIdList
+        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);
+        final int sinkId = getSinkNodeID(taskIdList, clientList, tasksForTopicGroup);
+
+        int taskNodeId = 0;
+        int topicGroupIndex = 0;
+        for (final Entry<Subtopology, Set<TaskId>> kv : sortedTasksForTopicGroup.entrySet()) {
+            final SortedSet<TaskId> taskIds = new TreeSet<>(kv.getValue());
+            for (int clientIndex = 0; clientIndex < clientList.size(); clientIndex++) {
+                final UUID processId = clientList.get(clientIndex);
+                final int clientNodeId = getClientNodeId(clientIndex, taskIdList, clientList, topicGroupIndex);
+                int startingTaskNodeId = taskNodeId;
+                for (final TaskId taskId : taskIds) {
+                    final int flow = hasAssignedTask.test(clientStates.get(processId), taskId) ? 1 : 0;
+                    graph.addEdge(startingTaskNodeId, clientNodeId, 1, getCostFunction.getCost(taskId, processId, false, trafficCost, nonOverlapCost, isStandby), flow);
+                    graph.addEdge(SOURCE_ID, startingTaskNodeId, 1, 0, 0);
+                    startingTaskNodeId++;
+                }
+
+                final int secondStageClientNodeId = getSecondStageClientNodeId(taskIdList, clientList, tasksForTopicGroup, clientIndex);

Review Comment:
   Right, I initially put task in outer loop and found this issue. Will make it more readable.



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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #14714:
URL: https://github.com/apache/kafka/pull/14714#discussion_r1414683411


##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareGraphConstructorTest.java:
##########
@@ -97,6 +98,17 @@ private int getCost(final TaskId taskId, final UUID processId, final boolean inC
         return 1;
     }
 
+    @Test
+    public void testSubtopicShouldContainAllTasks() {

Review Comment:
   `Subtopology` instead of `Subtopic` ?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -88,55 +93,20 @@ public Graph<Integer> constructTaskGraph(
             }
         }
 
-        // TODO: validate tasks in tasksForTopicGroup and taskIdList
-        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);

Review Comment:
   Was this code only moved, or altered?



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/AssignmentTestUtils.java:
##########
@@ -676,18 +676,28 @@ static SortedMap<TaskId, Set<TopicPartition>> getTaskTopicPartitionMap(final int
         return taskTopicPartitionMap;
     }
 
-    static Map<String, Object> configProps(final boolean enableRackAwareAssignor) {
-        return configProps(enableRackAwareAssignor, 0);
+    static Map<Subtopology, Set<TaskId>> getTasksForTopicGroup(final int tpSize, final int partitionSize) {
+        final Map<Subtopology, Set<TaskId>> tasksForTopicGroup = new HashMap<>();
+        for (int i = 0; i < tpSize; i++) {
+            for (int j = 0; j < partitionSize; j++) {
+                final Subtopology subtopology = new Subtopology(i, null);
+                tasksForTopicGroup.computeIfAbsent(subtopology, k -> new HashSet<>()).add(new TaskId(i, j));
+            }
+        }
+        return tasksForTopicGroup;
+    }
+
+    static Map<String, Object> configProps(final String rackAwareConfig) {
+        return configProps(rackAwareConfig, 0);
     }
 
-    static Map<String, Object> configProps(final boolean enableRackAwareAssignor, final int replicaNum) {
+    static Map<String, Object> configProps(final String rackAwareConfig, final int replicaNum) {
         final Map<String, Object> configurationMap = new HashMap<>();
         configurationMap.put(StreamsConfig.APPLICATION_ID_CONFIG, APPLICATION_ID);
         configurationMap.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, USER_END_POINT);
         configurationMap.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, replicaNum);
-        if (enableRackAwareAssignor) {
-            configurationMap.put(StreamsConfig.RACK_AWARE_ASSIGNMENT_STRATEGY_CONFIG, StreamsConfig.RACK_AWARE_ASSIGNMENT_STRATEGY_MIN_TRAFFIC);
-        }
+        // configurationMap.put(StreamsConfig.RACK_AWARE_ASSIGNMENT_STRATEGY_CONFIG, StreamsConfig.RACK_AWARE_ASSIGNMENT_STRATEGY_MIN_TRAFFIC);

Review Comment:
   Needs some cleanup



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/GraphTest.java:
##########
@@ -394,6 +394,106 @@ public void testDeterministic() {
         }
     }
 
+    @Test
+    public void testMaxFlowOnlySourceAndSink() {
+        final Graph<Integer> graph1 = new Graph<>();

Review Comment:
   Nit: Why `graph1` but not `graph`? (similar below)



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/RackAwareGraphConstructorTest.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.streams.processor.internals.assignment;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptySet;
+import static org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertBalancedTasks;
+import static org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertValidAssignment;
+import static org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.getRandomClientState;
+import static org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.getTaskTopicPartitionMap;
+import static org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.getTasksForTopicGroup;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.UUID;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+
+@RunWith(Parameterized.class)
+public class RackAwareGraphConstructorTest {
+    private static final String MIN_COST = "min_cost";
+    private static final String BALANCE_SUBTOPOLOGY = "balance_sub_topology";
+
+    private static final int TP_SIZE = 40;
+    private static final int PARTITION_SIZE = 3;
+    private static final int TOPIC_GROUP_SIZE = TP_SIZE;
+    private static final int CLIENT_SIZE = 20;
+
+    private Graph<Integer> graph;
+    private final SortedMap<TaskId, Set<TopicPartition>> taskTopicPartitionMap = getTaskTopicPartitionMap(
+        TP_SIZE, PARTITION_SIZE, false);
+    private final SortedSet<TaskId> taskIds = (SortedSet<TaskId>) taskTopicPartitionMap.keySet();
+    private final List<TaskId> taskIdList = new ArrayList<>(taskIds);
+    private final SortedMap<UUID, ClientState> clientStateMap = getRandomClientState(CLIENT_SIZE,
+        TP_SIZE, PARTITION_SIZE, 1, false, taskIds);
+    private final List<UUID> clientList = new ArrayList<>(clientStateMap.keySet());
+    private final Map<TaskId, UUID> taskClientMap = new HashMap<>();
+    private final Map<UUID, Integer> originalAssignedTaskNumber = new HashMap<>();
+    private final Map<Subtopology, Set<TaskId>> tasksForTopicGroup = getTasksForTopicGroup(TP_SIZE,
+        PARTITION_SIZE);
+    private RackAwareGraphConstructor constructor;
+
+    @Parameter
+    public String constructorType;
+
+    @Parameterized.Parameters(name = "constructorType={0}")
+    public static Collection<Object[]> getParamStoreType() {
+        return asList(new Object[][] {
+            {MIN_COST},
+            {BALANCE_SUBTOPOLOGY}
+        });
+    }
+
+    @Before
+    public void setUp() {
+        randomAssignTasksToClient(taskIdList, clientStateMap);
+
+        if (constructorType.equals(MIN_COST)) {
+            constructor = new MinTrafficGraphConstructor();
+        } else if (constructorType.equals(BALANCE_SUBTOPOLOGY)) {
+            constructor = new BalanceSubtopologyGraphConstructor(tasksForTopicGroup);
+        }
+        graph = constructor.constructTaskGraph(
+            clientList, taskIdList, clientStateMap, taskClientMap, originalAssignedTaskNumber, ClientState::hasAssignedTask, this::getCost, 10, 1, false, false);
+    }
+
+    private int getCost(final TaskId taskId, final UUID processId, final boolean inCurrentAssignment, final int trafficCost, final int nonOverlapCost, final boolean isStandby) {
+        return 1;
+    }
+
+    @Test
+    public void testMinCostGraphConstructor() {
+        if (constructorType.equals(BALANCE_SUBTOPOLOGY)) {
+            return;
+        }
+        // Flow should equal to task size which means each task is assigned now
+        assertEquals(taskIdList.size(), graph.flow());
+
+        // Total node size should match. Extra 2 is for source and sink nodes
+        assertEquals(taskIdList.size() + clientList.size() + 2, graph.nodes().size());
+
+        // Check edges from source to task nodes
+        SortedMap<Integer, Graph<Integer>.Edge> edges = graph.edges(RackAwareGraphConstructor.SOURCE_ID);
+        for (final Graph<Integer>.Edge edge : edges.values()) {
+            assertEquals(1, edge.flow);
+            assertEquals(1, edge.capacity);
+            assertEquals(0, edge.residualFlow);
+            assertEquals(0, edge.cost);
+            Assertions.assertTrue(edge.forwardEdge);
+        }
+
+        // Check edges from task nodes to clients nodes,
+        for (int taskNodeId = 0; taskNodeId < taskIdList.size(); taskNodeId++) {
+            edges = graph.edges(taskNodeId);
+            assertEquals(clientList.size(), edges.size());
+            int assignedClient = 0;
+            for (final Graph<Integer>.Edge edge : edges.values()) {
+                final int flow = edge.flow;
+                if (flow == 1) {
+                    assignedClient++;
+                }
+                assertEquals(1, edge.capacity);
+                assertEquals(flow == 1 ? 0 : 1, edge.residualFlow);
+                assertEquals(1, edge.cost);
+                Assertions.assertTrue(edge.forwardEdge);
+            }
+            // Task should be assigned to exact 1 client
+            assertEquals(1, assignedClient);
+            taskNodeId++;
+        }
+
+        // Check edges from second stage client node to sink
+        final int sinkId = clientList.size() + taskIdList.size();
+        int totalFlow = 0;
+        for (int i = 0; i < clientList.size(); i++) {
+            final UUID clientId = clientList.get(i);
+            final int originalAssignedCount = originalAssignedTaskNumber.get(clientId);
+
+            final int clientNodeId = i + taskIdList.size();
+            edges = graph.edges(clientNodeId);
+            assertEquals(1, edges.size());
+            for (final Entry<Integer, Graph<Integer>.Edge> nodeEdge : edges.entrySet()) {
+                final Integer nodeId = nodeEdge.getKey();
+                assertEquals(sinkId, nodeId);
+                totalFlow += nodeEdge.getValue().flow;
+                assertEquals(originalAssignedCount, nodeEdge.getValue().capacity);
+                Assertions.assertTrue(nodeEdge.getValue().forwardEdge);
+            }
+        }
+        assertEquals(taskIdList.size(), totalFlow);
+    }
+
+    @Test
+    public void testBalanceSubtopologyGraphConstructor() {
+        if (constructorType.equals(MIN_COST)) {
+            return;
+        }
+        // Flow should equal to task size which means each task is assigned now
+        assertEquals(taskIdList.size(), graph.flow());
+
+        // Total node size should match. Extra 2 is for source and sink nodes
+        assertEquals(taskIdList.size() + TOPIC_GROUP_SIZE * clientList.size() + clientList.size() + 2, graph.nodes().size());
+
+        // Check edges from source to task nodes
+        SortedMap<Integer, Graph<Integer>.Edge> edges = graph.edges(RackAwareGraphConstructor.SOURCE_ID);
+        for (final Graph<Integer>.Edge edge : edges.values()) {
+            assertEquals(1, edge.flow);
+            assertEquals(1, edge.capacity);
+            assertEquals(0, edge.residualFlow);
+            assertEquals(0, edge.cost);
+            Assertions.assertTrue(edge.forwardEdge);
+        }
+
+        // Check edges from task nodes to first stage clients node,
+        int taskNodeId = 0;
+        for (final Set<TaskId> unsortedTaskIds : tasksForTopicGroup.values()) {
+            for (int i = 0; i < unsortedTaskIds.size(); i++) {
+                edges = graph.edges(taskNodeId);
+                assertEquals(clientList.size(), edges.size());
+                int assignedClient = 0;
+                for (final Graph<Integer>.Edge edge : edges.values()) {
+                    final int flow = edge.flow;
+                    if (flow == 1) {
+                        assignedClient++;
+                    }
+                    assertEquals(1, edge.capacity);
+                    assertEquals(flow == 1 ? 0 : 1, edge.residualFlow);
+                    assertEquals(1, edge.cost);
+                    Assertions.assertTrue(edge.forwardEdge);
+                }
+                // Task should be assigned to exact 1 client
+                assertEquals(1, assignedClient);
+                taskNodeId++;
+            }
+        }
+
+        // Check edges from first stage client node to second stage client node
+        int topicGroupIndex = 0;
+        for (final Set<TaskId> tasks : tasksForTopicGroup.values()) {
+            final int taskCount = tasks.size();
+            for (int j = 0; j < clientList.size(); j++) {
+                final UUID clientId = clientList.get(j);
+                final int originalAssignedCount = originalAssignedTaskNumber.get(clientId);
+                final int expectedCapacity = (int) Math.ceil(originalAssignedCount * 1.0 / taskIdList.size() * taskCount);
+                final int clientNodeId = topicGroupIndex * clientList.size() + taskIdList.size() + j;
+                edges = graph.edges(clientNodeId);
+                assertEquals(1, edges.size());
+                for (final Entry<Integer, Graph<Integer>.Edge> nodeEdge : edges.entrySet()) {
+                    final Integer nodeId = nodeEdge.getKey();
+                    assertEquals(clientList.size() * tasksForTopicGroup.size() + taskIdList.size() + j, nodeId);
+                    final Graph<Integer>.Edge edge = nodeEdge.getValue();
+                    assertEquals(expectedCapacity, edge.capacity);
+                    assertEquals(0, edge.cost);
+                    Assertions.assertTrue(edge.forwardEdge);
+                }
+            }
+            topicGroupIndex++;
+        }
+
+        // Check edges from second stage client node to sink
+        final int sinkId = clientList.size() + tasksForTopicGroup.size() * clientList.size() + taskIdList.size();
+        int totalFlow = 0;
+        for (int i = 0; i < clientList.size(); i++) {
+            final UUID clientId = clientList.get(i);
+            final int originalAssignedCount = originalAssignedTaskNumber.get(clientId);
+
+            final int clientNodeId =
+                i + tasksForTopicGroup.size() * clientList.size() + taskIdList.size();
+            edges = graph.edges(clientNodeId);
+            assertEquals(1, edges.size());
+            for (final Entry<Integer, Graph<Integer>.Edge> nodeEdge : edges.entrySet()) {
+                final Integer nodeId = nodeEdge.getKey();
+                assertEquals(sinkId, nodeId);
+                totalFlow += nodeEdge.getValue().flow;
+                assertEquals(originalAssignedCount, nodeEdge.getValue().capacity);
+                Assertions.assertTrue(nodeEdge.getValue().forwardEdge);
+            }
+        }
+        assertEquals(taskIdList.size(), totalFlow);
+    }
+
+    @Test
+    public void testAssignTaskFromMinCostFlow() {
+        graph.solveMinCostFlow();
+        constructor.assignTaskFromMinCostFlow(
+            graph,
+            clientList,
+            taskIdList,
+            clientStateMap,
+            originalAssignedTaskNumber,
+            taskClientMap,
+            ClientState::assignActive,
+            ClientState::unassignActive,
+            ClientState::hasAssignedTask
+        );
+        assertValidAssignment(0, taskIds, emptySet(), clientStateMap, new StringBuilder());
+        if (constructorType.equals(BALANCE_SUBTOPOLOGY)) {
+            assertBalancedTasks(clientStateMap);
+        }
+    }
+
+    private void randomAssignTasksToClient(final List<TaskId> taskIdList, final SortedMap<UUID, ClientState> clientStateMap) {
+        int totalAssigned = 0;
+        for (final ClientState clientState : clientStateMap.values()) {
+            clientState.assignActive(taskIdList.get(totalAssigned++));
+            clientState.assignActive(taskIdList.get(totalAssigned++));
+        }
+        while (totalAssigned < taskIdList.size()) {
+            for (final ClientState clientState : clientStateMap.values()) {
+                if (AssignmentTestUtils.getRandom().nextInt(3) == 0) {
+                    clientState.assignActive(taskIdList.get(totalAssigned));
+                    totalAssigned++;
+                    if (totalAssigned >= taskIdList.size()) {
+                        break;
+                    }
+                } else {
+                    System.out.println();

Review Comment:
   need cleanup?



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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "lihaosky (via GitHub)" <gi...@apache.org>.
lihaosky commented on code in PR #14714:
URL: https://github.com/apache/kafka/pull/14714#discussion_r1414713123


##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/GraphTest.java:
##########
@@ -394,6 +394,106 @@ public void testDeterministic() {
         }
     }
 
+    @Test
+    public void testMaxFlowOnlySourceAndSink() {
+        final Graph<Integer> graph1 = new Graph<>();

Review Comment:
   `graph` was used in setup



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/BalanceSubtopologyGraphConstructor.java:
##########
@@ -88,55 +93,20 @@ public Graph<Integer> constructTaskGraph(
             }
         }
 
-        // TODO: validate tasks in tasksForTopicGroup and taskIdList
-        final SortedMap<Subtopology, Set<TaskId>> sortedTasksForTopicGroup = new TreeMap<>(tasksForTopicGroup);

Review Comment:
   It's put in `validateTasks`



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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #14714:
URL: https://github.com/apache/kafka/pull/14714#issuecomment-1842147167

   Jenkins issues:
   - build 7: `JDK 11 and Scala 2.13` failed
   - build 6: `JDK 8 and Scala 2.12` failed
   
   Test failures seems to be unrelated to this PR. Merging.


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


Re: [PR] KAFKA-15022: introduce interface to control graph constructor [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax merged PR #14714:
URL: https://github.com/apache/kafka/pull/14714


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