use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.
the class StickyAssignorTest method testAddRemoveConsumerOneTopic.
@Test
public void testAddRemoveConsumerOneTopic() {
String topic = "topic";
String consumer1 = "consumer";
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put(topic, 3);
Map<String, Subscription> subscriptions = new HashMap<>();
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);
assertTrue(isFullyBalanced(assignment));
String consumer2 = "consumer2";
subscriptions.put(consumer1, new Subscription(topics(topic), StickyAssignor.serializeTopicPartitionAssignment(assignment.get(consumer1))));
subscriptions.put(consumer2, new Subscription(topics(topic)));
assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertEquals(partitions(tp(topic, 1), tp(topic, 2)), assignment.get(consumer1));
assertEquals(partitions(tp(topic, 0)), assignment.get(consumer2));
verifyValidityAndBalance(subscriptions, assignment);
assertTrue(isFullyBalanced(assignment));
assertTrue(assignor.isSticky());
subscriptions.remove(consumer1);
subscriptions.put(consumer2, new Subscription(topics(topic), StickyAssignor.serializeTopicPartitionAssignment(assignment.get(consumer2))));
assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertTrue(assignment.get(consumer2).contains(tp(topic, 0)));
assertTrue(assignment.get(consumer2).contains(tp(topic, 1)));
assertTrue(assignment.get(consumer2).contains(tp(topic, 2)));
verifyValidityAndBalance(subscriptions, assignment);
assertTrue(isFullyBalanced(assignment));
assertTrue(assignor.isSticky());
}
use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.
the class StickyAssignorTest method testOneConsumerMultipleTopics.
@Test
public void testOneConsumerMultipleTopics() {
String topic1 = "topic1";
String topic2 = "topic2";
String consumerId = "consumer";
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put(topic1, 1);
partitionsPerTopic.put(topic2, 2);
Map<String, Subscription> subscriptions = Collections.singletonMap(consumerId, new Subscription(topics(topic1, topic2)));
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertEquals(partitions(tp(topic1, 0), tp(topic2, 0), tp(topic2, 1)), assignment.get(consumerId));
verifyValidityAndBalance(subscriptions, assignment);
assertTrue(isFullyBalanced(assignment));
}
use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.
the class StickyAssignorTest method testTwoConsumersOneTopicOnePartition.
@Test
public void testTwoConsumersOneTopicOnePartition() {
String topic = "topic";
String consumer1 = "consumer1";
String consumer2 = "consumer2";
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put(topic, 1);
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(Collections.<TopicPartition>emptyList(), assignment.get(consumer2));
verifyValidityAndBalance(subscriptions, assignment);
assertTrue(isFullyBalanced(assignment));
}
use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.
the class ConsumerProtocolTest method serializeDeserializeMetadata.
@Test
public void serializeDeserializeMetadata() {
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"));
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
assertEquals(subscription.topics(), parsedSubscription.topics());
}
use of org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription in project apache-kafka-on-k8s by banzaicloud.
the class ConsumerProtocolTest method serializeDeserializeNullSubscriptionUserData.
@Test
public void serializeDeserializeNullSubscriptionUserData() {
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), null);
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
assertEquals(subscription.topics(), parsedSubscription.topics());
assertNull(subscription.userData());
}
Aggregations