Search in sources :

Example 46 with Subscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.

the class StickyAssignorTest method buildSubscriptionWithOldSchema.

private static Subscription buildSubscriptionWithOldSchema(List<String> topics, List<TopicPartition> partitions) {
    Struct struct = new Struct(StickyAssignor.STICKY_ASSIGNOR_USER_DATA_V0);
    List<Struct> topicAssignments = new ArrayList<>();
    for (Map.Entry<String, List<Integer>> topicEntry : CollectionUtils.groupPartitionsByTopic(partitions).entrySet()) {
        Struct topicAssignment = new Struct(StickyAssignor.TOPIC_ASSIGNMENT);
        topicAssignment.set(StickyAssignor.TOPIC_KEY_NAME, topicEntry.getKey());
        topicAssignment.set(StickyAssignor.PARTITIONS_KEY_NAME, topicEntry.getValue().toArray());
        topicAssignments.add(topicAssignment);
    }
    struct.set(StickyAssignor.TOPIC_PARTITIONS_KEY_NAME, topicAssignments.toArray());
    ByteBuffer buffer = ByteBuffer.allocate(StickyAssignor.STICKY_ASSIGNOR_USER_DATA_V0.sizeOf(struct));
    StickyAssignor.STICKY_ASSIGNOR_USER_DATA_V0.write(buffer, struct);
    buffer.flip();
    return new Subscription(topics, buffer);
}
Also used : 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) HashMap(java.util.HashMap) Map(java.util.Map) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 47 with Subscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.

the class CooperativeStickyAssignorTest method testEncodeAndDecodeGeneration.

@Test
public void testEncodeAndDecodeGeneration() {
    Subscription subscription = new Subscription(topics(topic), assignor.subscriptionUserData(new HashSet<>(topics(topic))));
    Optional<Integer> encodedGeneration = ((CooperativeStickyAssignor) assignor).memberData(subscription).generation;
    assertTrue(encodedGeneration.isPresent());
    assertEquals(encodedGeneration.get(), DEFAULT_GENERATION);
    int generation = 10;
    assignor.onAssignment(null, new ConsumerGroupMetadata("dummy-group-id", generation, "dummy-member-id", Optional.empty()));
    subscription = new Subscription(topics(topic), assignor.subscriptionUserData(new HashSet<>(topics(topic))));
    encodedGeneration = ((CooperativeStickyAssignor) assignor).memberData(subscription).generation;
    assertTrue(encodedGeneration.isPresent());
    assertEquals(encodedGeneration.get(), generation);
}
Also used : Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) HashSet(java.util.HashSet) AbstractStickyAssignorTest(org.apache.kafka.clients.consumer.internals.AbstractStickyAssignorTest) Test(org.junit.jupiter.api.Test)

Example 48 with Subscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.

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(toSet(subscription.topics()), toSet(parsedSubscription.topics()));
    assertNull(parsedSubscription.userData());
}
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 49 with Subscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.

the class ConsumerProtocolTest method deserializeFutureSubscriptionVersion.

@Test
public void deserializeFutureSubscriptionVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema subscriptionSchemaV100 = new Schema(new Field("topics", new ArrayOf(Type.STRING)), new Field("user_data", Type.NULLABLE_BYTES), new Field("owned_partitions", new ArrayOf(ConsumerProtocolSubscription.TopicPartition.SCHEMA_1)), new Field("foo", Type.STRING));
    Struct subscriptionV100 = new Struct(subscriptionSchemaV100);
    subscriptionV100.set("topics", new Object[] { "topic" });
    subscriptionV100.set("user_data", ByteBuffer.wrap(new byte[0]));
    subscriptionV100.set("owned_partitions", new Object[] { new Struct(ConsumerProtocolSubscription.TopicPartition.SCHEMA_1).set("topic", tp2.topic()).set("partitions", new Object[] { tp2.partition() }) });
    subscriptionV100.set("foo", "bar");
    Struct headerV100 = new Struct(new Schema(new Field("version", Type.INT16)));
    headerV100.set("version", version);
    ByteBuffer buffer = ByteBuffer.allocate(subscriptionV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    subscriptionV100.writeTo(buffer);
    buffer.flip();
    Subscription subscription = ConsumerProtocol.deserializeSubscription(buffer);
    subscription.setGroupInstanceId(groupInstanceId);
    assertEquals(Collections.singleton("topic"), toSet(subscription.topics()));
    assertEquals(Collections.singleton(tp2), toSet(subscription.ownedPartitions()));
    assertEquals(groupInstanceId, subscription.groupInstanceId());
}
Also used : Field(org.apache.kafka.common.protocol.types.Field) ArrayOf(org.apache.kafka.common.protocol.types.ArrayOf) Schema(org.apache.kafka.common.protocol.types.Schema) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) ConsumerProtocolSubscription(org.apache.kafka.common.message.ConsumerProtocolSubscription) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.jupiter.api.Test)

Example 50 with Subscription

use of org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription in project kafka by apache.

the class ConsumerProtocolTest method serializeDeserializeMetadata.

@Test
public void serializeDeserializeMetadata() {
    Subscription subscription = new Subscription(Arrays.asList("foo", "bar"), ByteBuffer.wrap(new byte[0]));
    ByteBuffer buffer = ConsumerProtocol.serializeSubscription(subscription);
    Subscription parsedSubscription = ConsumerProtocol.deserializeSubscription(buffer);
    assertEquals(toSet(subscription.topics()), toSet(parsedSubscription.topics()));
    assertEquals(0, parsedSubscription.userData().limit());
    assertFalse(parsedSubscription.groupInstanceId().isPresent());
}
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