Search in sources :

Example 11 with Subtopology

use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.

the class ChangelogTopicsTest method shouldOnlyContainPreExistingNonSourceBasedChangelogs.

@Test
public void shouldOnlyContainPreExistingNonSourceBasedChangelogs() {
    expect(internalTopicManager.makeReady(mkMap(mkEntry(CHANGELOG_TOPIC_NAME1, CHANGELOG_TOPIC_CONFIG)))).andStubReturn(Collections.emptySet());
    final Map<Subtopology, TopicsInfo> topicGroups = mkMap(mkEntry(SUBTOPOLOGY_0, TOPICS_INFO1));
    final Set<TaskId> tasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final Map<Subtopology, Set<TaskId>> tasksForTopicGroup = mkMap(mkEntry(SUBTOPOLOGY_0, tasks));
    replay(internalTopicManager);
    final ChangelogTopics changelogTopics = new ChangelogTopics(internalTopicManager, topicGroups, tasksForTopicGroup, "[test] ");
    changelogTopics.setup();
    verify(internalTopicManager);
    assertThat(CHANGELOG_TOPIC_CONFIG.numberOfPartitions().orElse(Integer.MIN_VALUE), is(3));
    final TopicPartition changelogPartition0 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 0);
    final TopicPartition changelogPartition1 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 1);
    final TopicPartition changelogPartition2 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 2);
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_0), is(mkSet(changelogPartition0)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_1), is(mkSet(changelogPartition1)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_2), is(mkSet(changelogPartition2)));
    assertThat(changelogTopics.preExistingSourceTopicBasedPartitions(), is(Collections.emptySet()));
    assertThat(changelogTopics.preExistingNonSourceTopicBasedPartitions(), is(mkSet(changelogPartition0, changelogPartition1, changelogPartition2)));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) TopicPartition(org.apache.kafka.common.TopicPartition) TopicsInfo(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) Test(org.junit.Test)

Example 12 with Subtopology

use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.

the class ChangelogTopicsTest method shouldOnlyContainPreExistingSourceBasedChangelogs.

@Test
public void shouldOnlyContainPreExistingSourceBasedChangelogs() {
    expect(internalTopicManager.makeReady(Collections.emptyMap())).andStubReturn(Collections.emptySet());
    final Map<Subtopology, TopicsInfo> topicGroups = mkMap(mkEntry(SUBTOPOLOGY_0, TOPICS_INFO3));
    final Set<TaskId> tasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final Map<Subtopology, Set<TaskId>> tasksForTopicGroup = mkMap(mkEntry(SUBTOPOLOGY_0, tasks));
    replay(internalTopicManager);
    final ChangelogTopics changelogTopics = new ChangelogTopics(internalTopicManager, topicGroups, tasksForTopicGroup, "[test] ");
    changelogTopics.setup();
    verify(internalTopicManager);
    final TopicPartition changelogPartition0 = new TopicPartition(SOURCE_TOPIC_NAME, 0);
    final TopicPartition changelogPartition1 = new TopicPartition(SOURCE_TOPIC_NAME, 1);
    final TopicPartition changelogPartition2 = new TopicPartition(SOURCE_TOPIC_NAME, 2);
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_0), is(mkSet(changelogPartition0)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_1), is(mkSet(changelogPartition1)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_2), is(mkSet(changelogPartition2)));
    assertThat(changelogTopics.preExistingSourceTopicBasedPartitions(), is(mkSet(changelogPartition0, changelogPartition1, changelogPartition2)));
    assertThat(changelogTopics.preExistingNonSourceTopicBasedPartitions(), is(Collections.emptySet()));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) TopicPartition(org.apache.kafka.common.TopicPartition) TopicsInfo(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) Test(org.junit.Test)

Example 13 with Subtopology

use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.

the class ChangelogTopicsTest method shouldContainBothTypesOfPreExistingChangelogs.

@Test
public void shouldContainBothTypesOfPreExistingChangelogs() {
    expect(internalTopicManager.makeReady(mkMap(mkEntry(CHANGELOG_TOPIC_NAME1, CHANGELOG_TOPIC_CONFIG)))).andStubReturn(Collections.emptySet());
    final Map<Subtopology, TopicsInfo> topicGroups = mkMap(mkEntry(SUBTOPOLOGY_0, TOPICS_INFO4));
    final Set<TaskId> tasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final Map<Subtopology, Set<TaskId>> tasksForTopicGroup = mkMap(mkEntry(SUBTOPOLOGY_0, tasks));
    replay(internalTopicManager);
    final ChangelogTopics changelogTopics = new ChangelogTopics(internalTopicManager, topicGroups, tasksForTopicGroup, "[test] ");
    changelogTopics.setup();
    verify(internalTopicManager);
    assertThat(CHANGELOG_TOPIC_CONFIG.numberOfPartitions().orElse(Integer.MIN_VALUE), is(3));
    final TopicPartition changelogPartition0 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 0);
    final TopicPartition changelogPartition1 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 1);
    final TopicPartition changelogPartition2 = new TopicPartition(CHANGELOG_TOPIC_NAME1, 2);
    final TopicPartition sourcePartition0 = new TopicPartition(SOURCE_TOPIC_NAME, 0);
    final TopicPartition sourcePartition1 = new TopicPartition(SOURCE_TOPIC_NAME, 1);
    final TopicPartition sourcePartition2 = new TopicPartition(SOURCE_TOPIC_NAME, 2);
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_0), is(mkSet(sourcePartition0, changelogPartition0)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_1), is(mkSet(sourcePartition1, changelogPartition1)));
    assertThat(changelogTopics.preExistingPartitionsFor(TASK_0_2), is(mkSet(sourcePartition2, changelogPartition2)));
    assertThat(changelogTopics.preExistingSourceTopicBasedPartitions(), is(mkSet(sourcePartition0, sourcePartition1, sourcePartition2)));
    assertThat(changelogTopics.preExistingNonSourceTopicBasedPartitions(), is(mkSet(changelogPartition0, changelogPartition1, changelogPartition2)));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) TopicPartition(org.apache.kafka.common.TopicPartition) TopicsInfo(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) Test(org.junit.Test)

Example 14 with Subtopology

use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.

the class StreamsPartitionAssignor method populateTasksForMaps.

/**
 * Populates the taskForPartition and tasksForTopicGroup maps, and checks that partitions are assigned to exactly
 * one task.
 *
 * @param taskForPartition a map from partition to the corresponding task. Populated here.
 * @param tasksForTopicGroup a map from the topicGroupId to the set of corresponding tasks. Populated here.
 * @param allSourceTopics a set of all source topics in the topology
 * @param partitionsForTask a map from task to the set of input partitions
 * @param fullMetadata the cluster metadata
 */
private void populateTasksForMaps(final Map<TopicPartition, TaskId> taskForPartition, final Map<Subtopology, Set<TaskId>> tasksForTopicGroup, final Set<String> allSourceTopics, final Map<TaskId, Set<TopicPartition>> partitionsForTask, final Cluster fullMetadata) {
    // check if all partitions are assigned, and there are no duplicates of partitions in multiple tasks
    final Set<TopicPartition> allAssignedPartitions = new HashSet<>();
    for (final Map.Entry<TaskId, Set<TopicPartition>> entry : partitionsForTask.entrySet()) {
        final TaskId id = entry.getKey();
        final Set<TopicPartition> partitions = entry.getValue();
        for (final TopicPartition partition : partitions) {
            taskForPartition.put(partition, id);
            if (allAssignedPartitions.contains(partition)) {
                log.warn("Partition {} is assigned to more than one tasks: {}", partition, partitionsForTask);
            }
        }
        allAssignedPartitions.addAll(partitions);
        tasksForTopicGroup.computeIfAbsent(new Subtopology(id.subtopology(), id.topologyName()), k -> new HashSet<>()).add(id);
    }
    checkAllPartitions(allSourceTopics, partitionsForTask, allAssignedPartitions, fullMetadata);
}
Also used : ClientUtils.fetchEndOffsetsFuture(org.apache.kafka.streams.processor.internals.ClientUtils.fetchEndOffsetsFuture) FallbackPriorTaskAssignor(org.apache.kafka.streams.processor.internals.assignment.FallbackPriorTaskAssignor) SortedSet(java.util.SortedSet) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) PriorityQueue(java.util.PriorityQueue) KafkaException(org.apache.kafka.common.KafkaException) StreamsException(org.apache.kafka.streams.errors.StreamsException) ClientUtils.fetchCommittedOffsets(org.apache.kafka.streams.processor.internals.ClientUtils.fetchCommittedOffsets) ByteBuffer(java.nio.ByteBuffer) UNKNOWN_OFFSET_SUM(org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo.UNKNOWN_OFFSET_SUM) Cluster(org.apache.kafka.common.Cluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) MissingSourceTopicException(org.apache.kafka.streams.errors.MissingSourceTopicException) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) Configurable(org.apache.kafka.common.Configurable) Time(org.apache.kafka.common.utils.Time) ReferenceContainer(org.apache.kafka.streams.processor.internals.assignment.ReferenceContainer) LATEST_SUPPORTED_VERSION(org.apache.kafka.streams.processor.internals.assignment.StreamsAssignmentProtocolVersions.LATEST_SUPPORTED_VERSION) Collection(java.util.Collection) Set(java.util.Set) KafkaFuture(org.apache.kafka.common.KafkaFuture) PartitionInfo(org.apache.kafka.common.PartitionInfo) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) AssignorConfiguration(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration) Objects(java.util.Objects) ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) Utils.filterMap(org.apache.kafka.common.utils.Utils.filterMap) List(java.util.List) Node(org.apache.kafka.common.Node) Queue(java.util.Queue) SubscriptionInfo(org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo) TaskId(org.apache.kafka.streams.processor.TaskId) AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) HashMap(java.util.HashMap) AssignmentListener(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentListener) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CopartitionedTopicsEnforcer(org.apache.kafka.streams.processor.internals.assignment.CopartitionedTopicsEnforcer) HashSet(java.util.HashSet) UNKNOWN(org.apache.kafka.streams.processor.internals.assignment.StreamsAssignmentProtocolVersions.UNKNOWN) StickyTaskAssignor(org.apache.kafka.streams.processor.internals.assignment.StickyTaskAssignor) Admin(org.apache.kafka.clients.admin.Admin) LinkedList(java.util.LinkedList) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) Utils(org.apache.kafka.common.utils.Utils) EARLIEST_PROBEABLE_VERSION(org.apache.kafka.streams.processor.internals.assignment.StreamsAssignmentProtocolVersions.EARLIEST_PROBEABLE_VERSION) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException) TopicsInfo(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo) AssignorError(org.apache.kafka.streams.processor.internals.assignment.AssignorError) AtomicLong(java.util.concurrent.atomic.AtomicLong) UUID.randomUUID(java.util.UUID.randomUUID) TreeMap(java.util.TreeMap) ClientState(org.apache.kafka.streams.processor.internals.assignment.ClientState) TaskAssignor(org.apache.kafka.streams.processor.internals.assignment.TaskAssignor) Comparator(java.util.Comparator) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) Collections(java.util.Collections) TaskId(org.apache.kafka.streams.processor.TaskId) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) Utils.filterMap(org.apache.kafka.common.utils.Utils.filterMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) HashSet(java.util.HashSet)

Example 15 with Subtopology

use of org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology in project kafka by apache.

the class StreamsPartitionAssignorTest method tasksForState.

private static Set<TaskId> tasksForState(final String storeName, final List<TaskId> tasks, final Map<Subtopology, InternalTopologyBuilder.TopicsInfo> topicGroups) {
    final String changelogTopic = ProcessorStateManager.storeChangelogTopic(APPLICATION_ID, storeName, null);
    final Set<TaskId> ids = new HashSet<>();
    for (final Map.Entry<Subtopology, InternalTopologyBuilder.TopicsInfo> entry : topicGroups.entrySet()) {
        final Set<String> stateChangelogTopics = entry.getValue().stateChangelogTopics.keySet();
        if (stateChangelogTopics.contains(changelogTopic)) {
            for (final TaskId id : tasks) {
                if (id.subtopology() == entry.getKey().nodeGroupId) {
                    ids.add(id);
                }
            }
        }
    }
    return ids;
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Subtopology(org.apache.kafka.streams.processor.internals.TopologyMetadata.Subtopology) HashSet(java.util.HashSet)

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