Search in sources :

Example 6 with Subscription

use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.

the class RoundRobinAssignorTest method testMultipleConsumersMixedTopics.

@Test
public void testMultipleConsumersMixedTopics() {
    String topic1 = "topic1";
    String topic2 = "topic2";
    String consumer1 = "consumer1";
    String consumer2 = "consumer2";
    String consumer3 = "consumer3";
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic1, 3);
    partitionsPerTopic.put(topic2, 2);
    Map<String, Subscription> consumers = new HashMap<>();
    consumers.put(consumer1, new Subscription(topics(topic1)));
    consumers.put(consumer2, new Subscription(topics(topic1, topic2)));
    consumers.put(consumer3, new Subscription(topics(topic1)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
    assertEquals(partitions(tp(topic1, 0)), assignment.get(consumer1));
    assertEquals(partitions(tp(topic1, 1), tp(topic2, 0), tp(topic2, 1)), assignment.get(consumer2));
    assertEquals(partitions(tp(topic1, 2)), assignment.get(consumer3));
}
Also used : HashMap(java.util.HashMap) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription) Test(org.junit.Test)

Example 7 with Subscription

use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.

the class StickyAssignorTest method testOneConsumerNonexistentTopic.

@Test
public void testOneConsumerNonexistentTopic() {
    String topic = "topic";
    String consumerId = "consumer";
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic, 0);
    Map<String, Subscription> subscriptions = Collections.singletonMap(consumerId, new Subscription(topics(topic)));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(Collections.singleton(consumerId), assignment.keySet());
    assertTrue(assignment.get(consumerId).isEmpty());
    verifyValidityAndBalance(subscriptions, assignment);
    assertTrue(isFullyBalanced(assignment));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription) Test(org.junit.Test)

Example 8 with Subscription

use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.

the class StickyAssignorTest method testOneConsumerOneTopic.

@Test
public void testOneConsumerOneTopic() {
    String topic = "topic";
    String consumerId = "consumer";
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic, 3);
    Map<String, Subscription> subscriptions = Collections.singletonMap(consumerId, 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(consumerId));
    verifyValidityAndBalance(subscriptions, assignment);
    assertTrue(isFullyBalanced(assignment));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription) Test(org.junit.Test)

Example 9 with Subscription

use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.

the class StickyAssignorTest method testOnlyAssignsPartitionsFromSubscribedTopics.

@Test
public void testOnlyAssignsPartitionsFromSubscribedTopics() {
    String topic = "topic";
    String otherTopic = "other";
    String consumerId = "consumer";
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic, 3);
    partitionsPerTopic.put(otherTopic, 3);
    Map<String, Subscription> subscriptions = Collections.singletonMap(consumerId, 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(consumerId));
    verifyValidityAndBalance(subscriptions, assignment);
    assertTrue(isFullyBalanced(assignment));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription) Test(org.junit.Test)

Example 10 with Subscription

use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.

the class StickyAssignorTest method testTwoConsumersOneTopicTwoPartitions.

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

Aggregations

Subscription (org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription)35 Test (org.junit.Test)32 HashMap (java.util.HashMap)30 List (java.util.List)28 ArrayList (java.util.ArrayList)21 ByteBuffer (java.nio.ByteBuffer)7 TopicPartition (org.apache.kafka.common.TopicPartition)4 Map (java.util.Map)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Assignment (org.apache.kafka.clients.consumer.internals.PartitionAssignor.Assignment)2 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)2 Field (org.apache.kafka.common.protocol.types.Field)2 Schema (org.apache.kafka.common.protocol.types.Schema)2 Struct (org.apache.kafka.common.protocol.types.Struct)2 Entry (java.util.Map.Entry)1 ProtocolMetadata (org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata)1