Search in sources :

Example 16 with Subtopology

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);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) HashMap(java.util.HashMap) Function(java.util.function.Function) Utils.entriesToMap(org.apache.kafka.common.utils.Utils.entriesToMap) TreeSet(java.util.TreeSet) AdminClient(org.apache.kafka.clients.admin.AdminClient) HashSet(java.util.HashSet) ListOffsetsResult(org.apache.kafka.clients.admin.ListOffsetsResult) BaseMatcher(org.hamcrest.BaseMatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EasyMock.anyObject(org.easymock.EasyMock.anyObject) TopicPartition(org.apache.kafka.common.TopicPartition) Description(org.hamcrest.Description) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) LATEST_SUPPORTED_VERSION(org.apache.kafka.streams.processor.internals.assignment.StreamsAssignmentProtocolVersions.LATEST_SUPPORTED_VERSION) Collection(java.util.Collection) Set(java.util.Set) EasyMock(org.easymock.EasyMock) UUID(java.util.UUID) EasyMock.expect(org.easymock.EasyMock.expect) Collectors(java.util.stream.Collectors) Task(org.apache.kafka.streams.processor.internals.Task) Objects(java.util.Objects) ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) TreeMap(java.util.TreeMap) Utils.intersection(org.apache.kafka.common.utils.Utils.intersection) Matcher(org.hamcrest.Matcher) Entry(java.util.Map.Entry) Matchers.is(org.hamcrest.Matchers.is) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) TaskId(org.apache.kafka.streams.processor.TaskId) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TreeSet(java.util.TreeSet) UUID(java.util.UUID) HashMap(java.util.HashMap) Utils.entriesToMap(org.apache.kafka.common.utils.Utils.entriesToMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) TreeMap(java.util.TreeMap)

Aggregations

Subtopology (org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology)16 TaskId (org.apache.kafka.streams.processor.TaskId)13 HashMap (java.util.HashMap)12 Set (java.util.Set)12 TopicPartition (org.apache.kafka.common.TopicPartition)11 HashSet (java.util.HashSet)10 Map (java.util.Map)8 TopicsInfo (org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo)7 Test (org.junit.Test)7 TreeSet (java.util.TreeSet)5 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)5 TreeMap (java.util.TreeMap)4 UUID (java.util.UUID)4 SortedSet (java.util.SortedSet)3 UUID.randomUUID (java.util.UUID.randomUUID)3 Collectors (java.util.stream.Collectors)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Objects (java.util.Objects)2