-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-15022: [6/N] add rack aware assignor configs and update standby optimizer #14150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
55e6dbe
KAFKA-15022: [6/N] add rack aware assignor configs and update standby…
lihaosky 9976e8b
checkstyle
lihaosky f3e0ce4
Apply suggestions from code review
lihaosky d26935e
comments
lihaosky 2751504
more doc
lihaosky e496baf
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax 5d63f92
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax 345aaac
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax a370d50
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax 769759f
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax 88c50ca
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax ab2aa86
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax 70b912d
Update streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
mjsax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.common.TopicPartitionInfo; | ||
| import org.apache.kafka.streams.KeyValue; | ||
| import org.apache.kafka.streams.StreamsConfig; | ||
| import org.apache.kafka.streams.processor.TaskId; | ||
| import org.apache.kafka.streams.processor.internals.InternalTopicManager; | ||
| import org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology; | ||
|
|
@@ -62,6 +63,9 @@ boolean canMove(final ClientState source, | |
| private static final Logger log = LoggerFactory.getLogger(RackAwareTaskAssignor.class); | ||
|
|
||
| private static final int SOURCE_ID = -1; | ||
| // This is number is picked based on testing. Usually the optimization for standby assignment | ||
| // stops after 3 rounds | ||
| private static final int STANDBY_OPTIMIZER_MAX_ITERATION = 4; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment that we picked 4 based on some testing, but it's still more or less random. |
||
|
|
||
| private final Cluster fullMetadata; | ||
| private final Map<TaskId, Set<TopicPartition>> partitionsForTask; | ||
|
|
@@ -95,13 +99,9 @@ public boolean validClientRack() { | |
| } | ||
|
|
||
| public synchronized boolean canEnableRackAwareAssignor() { | ||
| /* | ||
| TODO: enable this after we add the config | ||
| if (StreamsConfig.RACK_AWARE_ASSSIGNMENT_STRATEGY_NONE.equals(assignmentConfigs.rackAwareAssignmentStrategy)) { | ||
| canEnable = false; | ||
| if (StreamsConfig.RACK_AWARE_ASSIGNMENT_STRATEGY_NONE.equals(assignmentConfigs.rackAwareAssignmentStrategy)) { | ||
| return false; | ||
| } | ||
| */ | ||
| if (canEnable != null) { | ||
| return canEnable; | ||
| } | ||
|
|
@@ -340,6 +340,9 @@ public long optimizeActiveTasks(final SortedSet<TaskId> activeTasks, | |
| return 0; | ||
| } | ||
|
|
||
| log.info("Assignment before active task optimization is {}\n with cost {}", clientStates, | ||
| activeTasksCost(activeTasks, clientStates, trafficCost, nonOverlapCost)); | ||
|
|
||
| final List<UUID> clientList = new ArrayList<>(clientStates.keySet()); | ||
| final List<TaskId> taskIdList = new ArrayList<>(activeTasks); | ||
| final Map<TaskId, UUID> taskClientMap = new HashMap<>(); | ||
|
|
@@ -353,6 +356,7 @@ public long optimizeActiveTasks(final SortedSet<TaskId> activeTasks, | |
| assignTaskFromMinCostFlow(graph, clientList, taskIdList, clientStates, originalAssignedTaskNumber, | ||
| taskClientMap, ClientState::assignActive, ClientState::unassignActive, ClientState::hasActiveTask); | ||
|
|
||
| log.info("Assignment after active task optimization is {}\n with cost {}", clientStates, cost); | ||
| return cost; | ||
| } | ||
|
|
||
|
|
@@ -368,46 +372,63 @@ public long optimizeStandbyTasks(final SortedMap<UUID, ClientState> clientStates | |
|
|
||
| final List<UUID> clientList = new ArrayList<>(clientStates.keySet()); | ||
| final SortedSet<TaskId> standbyTasks = new TreeSet<>(); | ||
| for (int i = 0; i < clientList.size(); i++) { | ||
| final ClientState clientState1 = clientStates.get(clientList.get(i)); | ||
| standbyTasks.addAll(clientState1.standbyTasks()); | ||
| for (int j = i + 1; j < clientList.size(); j++) { | ||
| final ClientState clientState2 = clientStates.get(clientList.get(j)); | ||
|
|
||
| final String rack1 = racksForProcess.get(clientState1.processId()); | ||
| final String rack2 = racksForProcess.get(clientState2.processId()); | ||
| // Cross rack traffic can not be reduced if racks are the same | ||
| if (rack1.equals(rack2)) { | ||
| continue; | ||
| } | ||
|
|
||
| final List<TaskId> movable1 = getMovableTasks.apply(clientState1, clientState2); | ||
| final List<TaskId> movable2 = getMovableTasks.apply(clientState2, clientState1); | ||
|
|
||
| // There's no needed to optimize if one is empty because the optimization | ||
| // can only swap tasks to keep the client's load balanced | ||
| if (movable1.isEmpty() || movable2.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| final List<TaskId> taskIdList = Stream.concat(movable1.stream(), movable2.stream()) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| clientStates.values().forEach(clientState -> standbyTasks.addAll(clientState.standbyTasks())); | ||
|
|
||
| log.info("Assignment before standby task optimization is {}\n with cost {}", clientStates, | ||
| standByTasksCost(standbyTasks, clientStates, trafficCost, nonOverlapCost)); | ||
|
|
||
| boolean taskMoved = true; | ||
| int round = 0; | ||
| while (taskMoved && round < STANDBY_OPTIMIZER_MAX_ITERATION) { | ||
| taskMoved = false; | ||
| round++; | ||
| for (int i = 0; i < clientList.size(); i++) { | ||
| final ClientState clientState1 = clientStates.get(clientList.get(i)); | ||
| for (int j = i + 1; j < clientList.size(); j++) { | ||
| final ClientState clientState2 = clientStates.get(clientList.get(j)); | ||
|
|
||
| final String rack1 = racksForProcess.get(clientState1.processId()); | ||
| final String rack2 = racksForProcess.get(clientState2.processId()); | ||
| // Cross rack traffic can not be reduced if racks are the same | ||
| if (rack1.equals(rack2)) { | ||
| continue; | ||
| } | ||
|
|
||
| final Map<TaskId, UUID> taskClientMap = new HashMap<>(); | ||
| final List<UUID> clients = Stream.of(clientList.get(i), clientList.get(j)).sorted().collect( | ||
| Collectors.toList()); | ||
| final Map<UUID, Integer> originalAssignedTaskNumber = new HashMap<>(); | ||
| final List<TaskId> movable1 = getMovableTasks.apply(clientState1, clientState2); | ||
| final List<TaskId> movable2 = getMovableTasks.apply(clientState2, clientState1); | ||
|
|
||
| final Graph<Integer> graph = constructTaskGraph(clients, taskIdList, clientStates, taskClientMap, originalAssignedTaskNumber, | ||
| ClientState::hasStandbyTask, trafficCost, nonOverlapCost, true, true); | ||
| graph.solveMinCostFlow(); | ||
| // There's no needed to optimize if one is empty because the optimization | ||
| // can only swap tasks to keep the client's load balanced | ||
| if (movable1.isEmpty() || movable2.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| assignTaskFromMinCostFlow(graph, clients, taskIdList, clientStates, originalAssignedTaskNumber, | ||
| taskClientMap, ClientState::assignStandby, ClientState::unassignStandby, ClientState::hasStandbyTask); | ||
| final List<TaskId> taskIdList = Stream.concat(movable1.stream(), | ||
| movable2.stream()) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| final Map<TaskId, UUID> taskClientMap = new HashMap<>(); | ||
| final List<UUID> clients = Stream.of(clientList.get(i), clientList.get(j)) | ||
| .sorted().collect( | ||
| Collectors.toList()); | ||
| final Map<UUID, Integer> originalAssignedTaskNumber = new HashMap<>(); | ||
|
|
||
| final Graph<Integer> graph = constructTaskGraph(clients, taskIdList, | ||
| clientStates, taskClientMap, originalAssignedTaskNumber, | ||
| ClientState::hasStandbyTask, trafficCost, nonOverlapCost, true, true); | ||
| graph.solveMinCostFlow(); | ||
|
|
||
| taskMoved |= assignTaskFromMinCostFlow(graph, clients, taskIdList, clientStates, | ||
| originalAssignedTaskNumber, | ||
| taskClientMap, ClientState::assignStandby, ClientState::unassignStandby, | ||
| ClientState::hasStandbyTask); | ||
| } | ||
| } | ||
| } | ||
| return standByTasksCost(standbyTasks, clientStates, trafficCost, nonOverlapCost); | ||
| final long cost = standByTasksCost(standbyTasks, clientStates, trafficCost, nonOverlapCost); | ||
| log.info("Assignment after {} rounds of standby task optimization is {}\n with cost {}", round, clientStates, cost); | ||
| return cost; | ||
| } | ||
|
|
||
| private Graph<Integer> constructTaskGraph(final List<UUID> clientList, | ||
|
|
@@ -473,16 +494,17 @@ private Graph<Integer> constructTaskGraph(final List<UUID> clientList, | |
| return graph; | ||
| } | ||
|
|
||
| private void 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) { | ||
| private 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); | ||
|
|
@@ -498,8 +520,9 @@ private void assignTaskFromMinCostFlow(final Graph<Integer> graph, | |
| break; | ||
| } | ||
|
|
||
| unassignTask.accept(clientStates.get(originalProcessId), taskId); | ||
| unAssignTask.accept(clientStates.get(originalProcessId), taskId); | ||
| assignTask.accept(clientStates.get(processId), taskId); | ||
| taskMoved = true; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -534,5 +557,7 @@ private void assignTaskFromMinCostFlow(final Graph<Integer> graph, | |
| + " are assigned to it after assignment"); | ||
| } | ||
| } | ||
|
|
||
| return taskMoved; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using
Integerbut notint?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be null and we will use default in assignor. It's related to your questions in #14139 (comment)