use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class AbstractStickyAssignorTest method testReassignmentAfterOneConsumerLeaves.
@Test
public void testReassignmentAfterOneConsumerLeaves() {
Map<String, Integer> partitionsPerTopic = new HashMap<>();
for (int i = 1; i < 20; i++) partitionsPerTopic.put(getTopicName(i, 20), i);
for (int i = 1; i < 20; i++) {
List<String> topics = new ArrayList<>();
for (int j = 1; j <= i; j++) topics.add(getTopicName(j, 20));
subscriptions.put(getConsumerName(i, 20), new Subscription(topics));
}
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
for (int i = 1; i < 20; i++) {
String consumer = getConsumerName(i, 20);
subscriptions.put(consumer, buildSubscription(subscriptions.get(consumer).topics(), assignment.get(consumer)));
}
subscriptions.remove("consumer10");
assignment = assignor.assign(partitionsPerTopic, subscriptions);
verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
assertTrue(assignor.isSticky());
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class AbstractStickyAssignorTest method testNoExceptionThrownWhenOnlySubscribedTopicDeleted.
@Test
public void testNoExceptionThrownWhenOnlySubscribedTopicDeleted() {
Map<String, Integer> partitionsPerTopic = new HashMap<>();
partitionsPerTopic.put(topic, 3);
subscriptions.put(consumerId, new Subscription(topics(topic)));
Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
subscriptions.put(consumerId, buildSubscription(topics(topic), assignment.get(consumerId)));
assignment = assignor.assign(Collections.emptyMap(), subscriptions);
assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
assertEquals(assignment.size(), 1);
assertTrue(assignment.get(consumerId).isEmpty());
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class ConsumerProtocolTest method serializeDeserializeSubscriptionAllVersions.
@Test
public void serializeDeserializeSubscriptionAllVersions() {
List<TopicPartition> ownedPartitions = Arrays.asList(new TopicPartition("foo", 0), new TopicPartition("bar", 0));
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), ByteBuffer.wrap("hello".getBytes()), ownedPartitions);
for (short version = ConsumerProtocolSubscription.LOWEST_SUPPORTED_VERSION; version <= ConsumerProtocolSubscription.HIGHEST_SUPPORTED_VERSION; version++) {
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription, version);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
assertEquals(toSet(subscription.topics()), toSet(parsedSubscription.topics()));
assertEquals(subscription.userData(), parsedSubscription.userData());
assertFalse(parsedSubscription.groupInstanceId().isPresent());
if (version >= 1) {
assertEquals(toSet(subscription.ownedPartitions()), toSet(parsedSubscription.ownedPartitions()));
} else {
assertEquals(Collections.emptyList(), parsedSubscription.ownedPartitions());
}
}
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class ConsumerProtocolTest method deserializeOldSubscriptionVersion.
@Test
public void deserializeOldSubscriptionVersion() {
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), null);
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription, (short) 0);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
assertEquals(toSet(parsedSubscription.topics()), toSet(parsedSubscription.topics()));
assertNull(parsedSubscription.userData());
assertTrue(parsedSubscription.ownedPartitions().isEmpty());
}
use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.
the class ConsumerProtocolTest method serializeDeserializeMetadataAndGroupInstanceId.
@Test
public void serializeDeserializeMetadataAndGroupInstanceId() {
Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), ByteBuffer.wrap(new byte[0]));
ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription);
Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
parsedSubscription.setGroupInstanceId(groupInstanceId);
assertEquals(toSet(subscription.topics()), toSet(parsedSubscription.topics()));
assertEquals(0, parsedSubscription.userData().limit());
assertEquals(groupInstanceId, parsedSubscription.groupInstanceId());
}
Aggregations