Search in sources :

Example 31 with GroupSubscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription in project kafka by apache.

the class StreamsPartitionAssignorTest method testAssignWithInternalTopics.

@Test
public void testAssignWithInternalTopics() {
    builder.addInternalTopic("topicX", InternalTopicProperties.empty());
    builder.addSource(null, "source1", null, null, null, "topic1");
    builder.addProcessor("processor1", new MockApiProcessorSupplier<>(), "source1");
    builder.addSink("sink1", "topicX", null, null, null, "processor1");
    builder.addSource(null, "source2", null, null, null, "topicX");
    builder.addProcessor("processor2", new MockApiProcessorSupplier<>(), "source2");
    final List<String> topics = asList("topic1", APPLICATION_ID + "-topicX");
    final Set<TaskId> allTasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final MockInternalTopicManager internalTopicManager = configureDefault();
    subscriptions.put("consumer10", new Subscription(topics, defaultSubscriptionInfo.encode()));
    partitionAssignor.assign(metadata, new GroupSubscription(subscriptions));
    // check prepared internal topics
    assertEquals(1, internalTopicManager.readyTopics.size());
    assertEquals(allTasks.size(), (long) internalTopicManager.readyTopics.get(APPLICATION_ID + "-topicX"));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) MockInternalTopicManager(org.apache.kafka.test.MockInternalTopicManager) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.Test)

Example 32 with GroupSubscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription in project kafka by apache.

the class StreamsPartitionAssignorTest method testAssignEmptyMetadata.

@Test
public void testAssignEmptyMetadata() {
    builder.addSource(null, "source1", null, null, null, "topic1");
    builder.addSource(null, "source2", null, null, null, "topic2");
    builder.addProcessor("processor", new MockApiProcessorSupplier<>(), "source1", "source2");
    final List<String> topics = asList("topic1", "topic2");
    final Set<TaskId> allTasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final Set<TaskId> prevTasks10 = mkSet(TASK_0_0);
    final Set<TaskId> standbyTasks10 = mkSet(TASK_0_1);
    final Cluster emptyMetadata = new Cluster("cluster", Collections.singletonList(Node.noNode()), emptySet(), emptySet(), emptySet());
    createMockTaskManager(prevTasks10, standbyTasks10);
    configureDefaultPartitionAssignor();
    subscriptions.put("consumer10", new Subscription(topics, getInfo(UUID_1, prevTasks10, standbyTasks10).encode()));
    // initially metadata is empty
    Map<String, Assignment> assignments = partitionAssignor.assign(emptyMetadata, new GroupSubscription(subscriptions)).groupAssignment();
    // check assigned partitions
    assertEquals(emptySet(), new HashSet<>(assignments.get("consumer10").partitions()));
    // check assignment info
    AssignmentInfo info10 = checkAssignment(emptySet(), assignments.get("consumer10"));
    final Set<TaskId> allActiveTasks = new HashSet<>(info10.activeTasks());
    assertEquals(0, allActiveTasks.size());
    // then metadata gets populated
    assignments = partitionAssignor.assign(metadata, new GroupSubscription(subscriptions)).groupAssignment();
    // check assigned partitions
    assertEquals(mkSet(mkSet(t1p0, t2p0, t1p0, t2p0, t1p1, t2p1, t1p2, t2p2)), mkSet(new HashSet<>(assignments.get("consumer10").partitions())));
    // the first consumer
    info10 = checkAssignment(allTopics, assignments.get("consumer10"));
    allActiveTasks.addAll(info10.activeTasks());
    assertEquals(3, allActiveTasks.size());
    assertEquals(allTasks, new HashSet<>(allActiveTasks));
    assertEquals(3, allActiveTasks.size());
    assertEquals(allTasks, allActiveTasks);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Cluster(org.apache.kafka.common.Cluster) Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with GroupSubscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription in project kafka by apache.

the class StreamsPartitionAssignorTest method shouldEncodeAssignmentErrorIfPreVersionProbingSubscriptionAndFutureSubscriptionIsMixed.

private void shouldEncodeAssignmentErrorIfPreVersionProbingSubscriptionAndFutureSubscriptionIsMixed(final int oldVersion) {
    subscriptions.put("consumer1", new Subscription(Collections.singletonList("topic1"), getInfoForOlderVersion(oldVersion, UUID_1, EMPTY_TASKS, EMPTY_TASKS).encode()));
    subscriptions.put("future-consumer", new Subscription(Collections.singletonList("topic1"), encodeFutureSubscription()));
    configureDefault();
    final Map<String, Assignment> assignment = partitionAssignor.assign(metadata, new GroupSubscription(subscriptions)).groupAssignment();
    assertThat(AssignmentInfo.decode(assignment.get("consumer1").userData()).errCode(), equalTo(AssignorError.ASSIGNMENT_ERROR.code()));
    assertThat(AssignmentInfo.decode(assignment.get("future-consumer").userData()).errCode(), equalTo(AssignorError.ASSIGNMENT_ERROR.code()));
}
Also used : Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)

Aggregations

GroupSubscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription)33 Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)33 Test (org.junit.Test)29 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)24 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)18 TaskId (org.apache.kafka.streams.processor.TaskId)17 MockKeyValueStoreBuilder (org.apache.kafka.test.MockKeyValueStoreBuilder)15 HashSet (java.util.HashSet)13 HashMap (java.util.HashMap)10 TopicPartition (org.apache.kafka.common.TopicPartition)10 Map (java.util.Map)9 Cluster (org.apache.kafka.common.Cluster)9 MockInternalTopicManager (org.apache.kafka.test.MockInternalTopicManager)9 ArrayList (java.util.ArrayList)8 Collections.emptyMap (java.util.Collections.emptyMap)8 Collections.singletonMap (java.util.Collections.singletonMap)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)8 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)8 StreamsConfig (org.apache.kafka.streams.StreamsConfig)8 Matchers.anEmptyMap (org.hamcrest.Matchers.anEmptyMap)8