Search in sources :

Example 6 with Field

use of org.apache.kafka.common.protocol.types.Field 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 7 with Field

use of org.apache.kafka.common.protocol.types.Field in project kafka by apache.

the class ConsumerProtocolTest method deserializeFutureAssignmentVersion.

@Test
public void deserializeFutureAssignmentVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema assignmentSchemaV100 = new Schema(new Field("assigned_partitions", new ArrayOf(ConsumerProtocolAssignment.TopicPartition.SCHEMA_0)), new Field("user_data", Type.BYTES), new Field("foo", Type.STRING));
    Struct assignmentV100 = new Struct(assignmentSchemaV100);
    assignmentV100.set("assigned_partitions", new Object[] { new Struct(ConsumerProtocolAssignment.TopicPartition.SCHEMA_0).set("topic", tp1.topic()).set("partitions", new Object[] { tp1.partition() }) });
    assignmentV100.set("user_data", ByteBuffer.wrap(new byte[0]));
    assignmentV100.set("foo", "bar");
    Struct headerV100 = new Struct(new Schema(new Field("version", Type.INT16)));
    headerV100.set("version", version);
    ByteBuffer buffer = ByteBuffer.allocate(assignmentV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    assignmentV100.writeTo(buffer);
    buffer.flip();
    Assignment assignment = ConsumerProtocol.deserializeAssignment(buffer);
    assertEquals(toSet(Collections.singletonList(tp1)), toSet(assignment.partitions()));
}
Also used : Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) ConsumerProtocolAssignment(org.apache.kafka.common.message.ConsumerProtocolAssignment) Field(org.apache.kafka.common.protocol.types.Field) ArrayOf(org.apache.kafka.common.protocol.types.ArrayOf) Schema(org.apache.kafka.common.protocol.types.Schema) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)7 Field (org.apache.kafka.common.protocol.types.Field)7 Schema (org.apache.kafka.common.protocol.types.Schema)7 Struct (org.apache.kafka.common.protocol.types.Struct)7 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)6 Test (org.junit.Test)4 Subscription (org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.jupiter.api.Test)2 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)1 Subscription (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription)1 ConsumerProtocolAssignment (org.apache.kafka.common.message.ConsumerProtocolAssignment)1 ConsumerProtocolSubscription (org.apache.kafka.common.message.ConsumerProtocolSubscription)1