use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.
the class AssignmentTestUtils method analyzeTaskAssignmentBalance.
static TaskSkewReport analyzeTaskAssignmentBalance(final Map<UUID, ClientState> clientStates) {
final Function<Integer, Map<UUID, AtomicInteger>> initialClientCounts = i -> clientStates.keySet().stream().collect(Collectors.toMap(c -> c, c -> new AtomicInteger(0)));
final Map<Integer, Map<UUID, AtomicInteger>> subtopologyToClientsWithPartition = new TreeMap<>();
for (final Map.Entry<UUID, ClientState> entry : clientStates.entrySet()) {
final UUID client = entry.getKey();
final ClientState clientState = entry.getValue();
for (final TaskId task : clientState.activeTasks()) {
final int subtopology = task.subtopology();
subtopologyToClientsWithPartition.computeIfAbsent(subtopology, initialClientCounts).get(client).incrementAndGet();
}
}
int maxTaskSkew = 0;
final Set<Integer> skewedSubtopologies = new TreeSet<>();
for (final Map.Entry<Integer, Map<UUID, AtomicInteger>> entry : subtopologyToClientsWithPartition.entrySet()) {
final Map<UUID, AtomicInteger> clientsWithPartition = entry.getValue();
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (final AtomicInteger count : clientsWithPartition.values()) {
max = Math.max(max, count.get());
min = Math.min(min, count.get());
}
final int taskSkew = max - min;
maxTaskSkew = Math.max(maxTaskSkew, taskSkew);
if (taskSkew > 1) {
skewedSubtopologies.add(entry.getKey());
}
}
return new TaskSkewReport(maxTaskSkew, skewedSubtopologies, subtopologyToClientsWithPartition);
}
Aggregations