Search in sources :

Example 61 with Subscription

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

the class AbstractStickyAssignorTest method testLargeAssignmentAndGroupWithNonEqualSubscription.

@Timeout(60)
@Test
public void testLargeAssignmentAndGroupWithNonEqualSubscription() {
    // 1 million partitions!
    int topicCount = 500;
    int partitionCount = 2_000;
    int consumerCount = 2_000;
    List<String> topics = new ArrayList<>();
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    for (int i = 0; i < topicCount; i++) {
        String topicName = getTopicName(i, topicCount);
        topics.add(topicName);
        partitionsPerTopic.put(topicName, partitionCount);
    }
    for (int i = 0; i < consumerCount; i++) {
        if (i == consumerCount - 1) {
            subscriptions.put(getConsumerName(i, consumerCount), new Subscription(topics.subList(0, 1)));
        } else {
            subscriptions.put(getConsumerName(i, consumerCount), new Subscription(topics));
        }
    }
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    for (int i = 1; i < consumerCount; i++) {
        String consumer = getConsumerName(i, consumerCount);
        if (i == consumerCount - 1) {
            subscriptions.put(consumer, buildSubscription(topics.subList(0, 1), assignment.get(consumer)));
        } else {
            subscriptions.put(consumer, buildSubscription(topics, assignment.get(consumer)));
        }
    }
    assignor.assign(partitionsPerTopic, subscriptions);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 62 with Subscription

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

the class AbstractStickyAssignorTest method testLargeAssignmentAndGroupWithUniformSubscription.

@Timeout(30)
@Test
public void testLargeAssignmentAndGroupWithUniformSubscription() {
    // 1 million partitions!
    int topicCount = 500;
    int partitionCount = 2_000;
    int consumerCount = 2_000;
    List<String> topics = new ArrayList<>();
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    for (int i = 0; i < topicCount; i++) {
        String topicName = getTopicName(i, topicCount);
        topics.add(topicName);
        partitionsPerTopic.put(topicName, partitionCount);
    }
    for (int i = 0; i < consumerCount; i++) {
        subscriptions.put(getConsumerName(i, consumerCount), new Subscription(topics));
    }
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    for (int i = 1; i < consumerCount; i++) {
        String consumer = getConsumerName(i, consumerCount);
        subscriptions.put(consumer, buildSubscription(topics, assignment.get(consumer)));
    }
    assignor.assign(partitionsPerTopic, subscriptions);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 63 with Subscription

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

the class AbstractStickyAssignorTest method testStickiness.

@Test
public void testStickiness() {
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic1, 3);
    subscriptions.put(consumer1, new Subscription(topics(topic1)));
    subscriptions.put(consumer2, new Subscription(topics(topic1)));
    subscriptions.put(consumer3, new Subscription(topics(topic1)));
    subscriptions.put(consumer4, new Subscription(topics(topic1)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    Map<String, TopicPartition> partitionsAssigned = new HashMap<>();
    Set<Map.Entry<String, List<TopicPartition>>> assignments = assignment.entrySet();
    for (Map.Entry<String, List<TopicPartition>> entry : assignments) {
        String consumer = entry.getKey();
        List<TopicPartition> topicPartitions = entry.getValue();
        int size = topicPartitions.size();
        assertTrue(size <= 1, "Consumer " + consumer + " is assigned more topic partitions than expected.");
        if (size == 1)
            partitionsAssigned.put(consumer, topicPartitions.get(0));
    }
    // removing the potential group leader
    subscriptions.remove(consumer1);
    subscriptions.put(consumer2, buildSubscription(topics(topic1), assignment.get(consumer2)));
    subscriptions.put(consumer3, buildSubscription(topics(topic1), assignment.get(consumer3)));
    subscriptions.put(consumer4, buildSubscription(topics(topic1), assignment.get(consumer4)));
    assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assignments = assignment.entrySet();
    for (Map.Entry<String, List<TopicPartition>> entry : assignments) {
        String consumer = entry.getKey();
        List<TopicPartition> topicPartitions = entry.getValue();
        assertEquals(1, topicPartitions.size(), "Consumer " + consumer + " is assigned more topic partitions than expected.");
        assertTrue((!partitionsAssigned.containsKey(consumer)) || (assignment.get(consumer).contains(partitionsAssigned.get(consumer))), "Stickiness was not honored for consumer " + consumer);
    }
}
Also used : HashMap(java.util.HashMap) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) HashMap(java.util.HashMap) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 64 with Subscription

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

the class AbstractStickyAssignorTest method testAddRemoveConsumerOneTopic.

@Test
public void testAddRemoveConsumerOneTopic() {
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic, 3);
    subscriptions.put(consumer1, new Subscription(topics(topic)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(partitions(tp(topic, 0), tp(topic, 1), tp(topic, 2)), assignment.get(consumer1));
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertTrue(isFullyBalanced(assignment));
    subscriptions.put(consumer1, buildSubscription(topics(topic), assignment.get(consumer1)));
    subscriptions.put(consumer2, buildSubscription(topics(topic), Collections.emptyList()));
    assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(Collections.singletonMap(tp(topic, 2), consumer2), assignor.partitionsTransferringOwnership);
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertEquals(partitions(tp(topic, 0), tp(topic, 1)), assignment.get(consumer1));
    assertEquals(partitions(tp(topic, 2)), assignment.get(consumer2));
    assertTrue(isFullyBalanced(assignment));
    subscriptions.remove(consumer1);
    subscriptions.put(consumer2, buildSubscription(topics(topic), assignment.get(consumer2)));
    assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(new HashSet<>(partitions(tp(topic, 2), tp(topic, 1), tp(topic, 0))), new HashSet<>(assignment.get(consumer2)));
    assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertTrue(isFullyBalanced(assignment));
}
Also used : HashMap(java.util.HashMap) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Example 65 with Subscription

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

the class RoundRobinAssignorTest method testTwoConsumersOneTopicTwoPartitions.

@Test
public void testTwoConsumersOneTopicTwoPartitions() {
    String consumer1 = "consumer1";
    String consumer2 = "consumer2";
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic, 2);
    Map<String, Subscription> consumers = new HashMap<>();
    consumers.put(consumer1, new Subscription(topics(topic)));
    consumers.put(consumer2, new Subscription(topics(topic)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(partitions(tp(topic, 0)), assignment.get(consumer1));
    assertEquals(partitions(tp(topic, 1)), assignment.get(consumer2));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)89 HashMap (java.util.HashMap)50 ArrayList (java.util.ArrayList)49 List (java.util.List)45 Test (org.junit.jupiter.api.Test)44 GroupSubscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription)39 Test (org.junit.Test)33 Collections.emptyList (java.util.Collections.emptyList)28 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)24 TopicPartition (org.apache.kafka.common.TopicPartition)20 TaskId (org.apache.kafka.streams.processor.TaskId)19 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)18 ByteBuffer (java.nio.ByteBuffer)15 MockKeyValueStoreBuilder (org.apache.kafka.test.MockKeyValueStoreBuilder)15 HashSet (java.util.HashSet)14 Map (java.util.Map)13 Cluster (org.apache.kafka.common.Cluster)9 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)9 MockInternalTopicManager (org.apache.kafka.test.MockInternalTopicManager)9 Collections.emptyMap (java.util.Collections.emptyMap)8