use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class RoundRobinAssignorTest method testTwoStaticConsumersTwoTopicsSixPartitions.
@Test
public void testTwoStaticConsumersTwoTopicsSixPartitions() {
// although consumer 2 has a higher rank than 1, the comparison happens on
// instance id level.
String topic1 = "topic1";
String topic2 = "topic2";
String consumer1 = "consumer-b";
String instance1 = "instance1";
String consumer2 = "consumer-a";
String instance2 = "instance2";
Map<String, Integer> partitionsPerTopic = setupPartitionsPerTopicWithTwoTopics(3, 3);
Map<String, Subscription> consumers = new HashMap<>();
Subscription consumer1Subscription = new Subscription(topics(topic1, topic2), null);
consumer1Subscription.setGroupInstanceId(Optional.of(instance1));
consumers.put(consumer1, consumer1Subscription);
Subscription consumer2Subscription = new Subscription(topics(topic1, topic2), null);
consumer2Subscription.setGroupInstanceId(Optional.of(instance2));
consumers.put(consumer2, consumer2Subscription);
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
assertEquals(partitions(tp(topic1, 0), tp(topic1, 2), tp(topic2, 1)), assignment.get(consumer1));
assertEquals(partitions(tp(topic1, 1), tp(topic2, 0), tp(topic2, 2)), assignment.get(consumer2));
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class RoundRobinAssignorTest method testTwoDynamicConsumersTwoTopicsSixPartitions.
@Test
public void testTwoDynamicConsumersTwoTopicsSixPartitions() {
String topic1 = "topic1";
String topic2 = "topic2";
String consumer1 = "consumer1";
String consumer2 = "consumer2";
Map<String, Integer> partitionsPerTopic = setupPartitionsPerTopicWithTwoTopics(3, 3);
Map<String, Subscription> consumers = new HashMap<>();
consumers.put(consumer1, new Subscription(topics(topic1, topic2)));
consumers.put(consumer2, new Subscription(topics(topic1, topic2)));
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, consumers);
assertEquals(partitions(tp(topic1, 0), tp(topic1, 2), tp(topic2, 1)), assignment.get(consumer1));
assertEquals(partitions(tp(topic1, 1), tp(topic2, 0), tp(topic2, 2)), assignment.get(consumer2));
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class RoundRobinAssignorTest method testTwoConsumersOneTopicOnePartition.
@Test
public void testTwoConsumersOneTopicOnePartition() {
String consumer1 = "consumer1";
String consumer2 = "consumer2";
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put(topic, 1);
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(Collections.<TopicPartition>emptyList(), assignment.get(consumer2));
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class ConsumerProtocolTest method deserializeNewSubscriptionWithOldVersion.
@Test
public void deserializeNewSubscriptionWithOldVersion() {
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), null, Collections.singletonList(tp2));
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription);
// ignore the version assuming it is the old byte code, as it will blindly deserialize as V0
ConsumerProtocol.deserializeVersion(buffer);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer, (short) 0);
assertEquals(toSet(subscription.topics()), toSet(parsedSubscription.topics()));
assertNull(parsedSubscription.userData());
assertTrue(parsedSubscription.ownedPartitions().isEmpty());
assertFalse(parsedSubscription.groupInstanceId().isPresent());
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class AbstractStickyAssignorTest method testReassignmentAfterOneConsumerAdded.
@Test
public void testReassignmentAfterOneConsumerAdded() {
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put("topic", 20);
for (int i = 1; i < 10; i++) subscriptions.put(getConsumerName(i, 10), new Subscription(topics("topic")));
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
// add a new consumer
subscriptions.put(getConsumerName(10, 10), new Subscription(topics("topic")));
assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
}
Aggregations