Search in sources :

Example 51 with Subscription

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());
}
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)

Example 52 with Subscription

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());
}
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 53 with Subscription

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());
        }
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) ConsumerProtocolSubscription(org.apache.kafka.common.message.ConsumerProtocolSubscription) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 54 with Subscription

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());
}
Also used : Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) ConsumerProtocolSubscription(org.apache.kafka.common.message.ConsumerProtocolSubscription) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 55 with Subscription

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());
}
Also used : Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) ConsumerProtocolSubscription(org.apache.kafka.common.message.ConsumerProtocolSubscription) ByteBuffer(java.nio.ByteBuffer) 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