Search in sources :

Example 1 with Field

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

the class ConsumerProtocolTest method deserializeNewSubscriptionVersion.

@Test
public void deserializeNewSubscriptionVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema subscriptionSchemaV100 = new Schema(new Field(ConsumerProtocol.TOPICS_KEY_NAME, new ArrayOf(Type.STRING)), new Field(ConsumerProtocol.USER_DATA_KEY_NAME, Type.BYTES), new Field("foo", Type.STRING));
    Struct subscriptionV100 = new Struct(subscriptionSchemaV100);
    subscriptionV100.set(ConsumerProtocol.TOPICS_KEY_NAME, new Object[] { "topic" });
    subscriptionV100.set(ConsumerProtocol.USER_DATA_KEY_NAME, ByteBuffer.wrap(new byte[0]));
    subscriptionV100.set("foo", "bar");
    Struct headerV100 = new Struct(ConsumerProtocol.CONSUMER_PROTOCOL_HEADER_SCHEMA);
    headerV100.set(ConsumerProtocol.VERSION_KEY_NAME, version);
    ByteBuffer buffer = ByteBuffer.allocate(subscriptionV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    subscriptionV100.writeTo(buffer);
    buffer.flip();
    Subscription subscription = ConsumerProtocol.deserializeSubscription(buffer);
    assertEquals(Arrays.asList("topic"), subscription.topics());
}
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.internals.PartitionAssignor.Subscription) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 2 with Field

use of org.apache.kafka.common.protocol.types.Field in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerProtocolTest method deserializeNewAssignmentVersion.

@Test
public void deserializeNewAssignmentVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema assignmentSchemaV100 = new Schema(new Field(ConsumerProtocol.TOPIC_PARTITIONS_KEY_NAME, new ArrayOf(ConsumerProtocol.TOPIC_ASSIGNMENT_V0)), new Field(ConsumerProtocol.USER_DATA_KEY_NAME, Type.BYTES), new Field("foo", Type.STRING));
    Struct assignmentV100 = new Struct(assignmentSchemaV100);
    assignmentV100.set(ConsumerProtocol.TOPIC_PARTITIONS_KEY_NAME, new Object[] { new Struct(ConsumerProtocol.TOPIC_ASSIGNMENT_V0).set(ConsumerProtocol.TOPIC_KEY_NAME, "foo").set(ConsumerProtocol.PARTITIONS_KEY_NAME, new Object[] { 1 }) });
    assignmentV100.set(ConsumerProtocol.USER_DATA_KEY_NAME, ByteBuffer.wrap(new byte[0]));
    assignmentV100.set("foo", "bar");
    Struct headerV100 = new Struct(ConsumerProtocol.CONSUMER_PROTOCOL_HEADER_SCHEMA);
    headerV100.set(ConsumerProtocol.VERSION_KEY_NAME, version);
    ByteBuffer buffer = ByteBuffer.allocate(assignmentV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    assignmentV100.writeTo(buffer);
    buffer.flip();
    PartitionAssignor.Assignment assignment = ConsumerProtocol.deserializeAssignment(buffer);
    assertEquals(toSet(Arrays.asList(new TopicPartition("foo", 1))), toSet(assignment.partitions()));
}
Also used : Field(org.apache.kafka.common.protocol.types.Field) ArrayOf(org.apache.kafka.common.protocol.types.ArrayOf) TopicPartition(org.apache.kafka.common.TopicPartition) Schema(org.apache.kafka.common.protocol.types.Schema) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 3 with Field

use of org.apache.kafka.common.protocol.types.Field in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerProtocolTest method deserializeNewSubscriptionVersion.

@Test
public void deserializeNewSubscriptionVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema subscriptionSchemaV100 = new Schema(new Field(ConsumerProtocol.TOPICS_KEY_NAME, new ArrayOf(Type.STRING)), new Field(ConsumerProtocol.USER_DATA_KEY_NAME, Type.BYTES), new Field("foo", Type.STRING));
    Struct subscriptionV100 = new Struct(subscriptionSchemaV100);
    subscriptionV100.set(ConsumerProtocol.TOPICS_KEY_NAME, new Object[] { "topic" });
    subscriptionV100.set(ConsumerProtocol.USER_DATA_KEY_NAME, ByteBuffer.wrap(new byte[0]));
    subscriptionV100.set("foo", "bar");
    Struct headerV100 = new Struct(ConsumerProtocol.CONSUMER_PROTOCOL_HEADER_SCHEMA);
    headerV100.set(ConsumerProtocol.VERSION_KEY_NAME, version);
    ByteBuffer buffer = ByteBuffer.allocate(subscriptionV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    subscriptionV100.writeTo(buffer);
    buffer.flip();
    Subscription subscription = ConsumerProtocol.deserializeSubscription(buffer);
    assertEquals(Arrays.asList("topic"), subscription.topics());
}
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.internals.PartitionAssignor.Subscription) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 4 with Field

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

the class ConsumerCoordinatorTest method subscriptionUserData.

public ByteBuffer subscriptionUserData(int generation) {
    final String generationKeyName = "generation";
    final Schema cooperativeStickyAssignorUserDataV0 = new Schema(new Field(generationKeyName, Type.INT32));
    Struct struct = new Struct(cooperativeStickyAssignorUserDataV0);
    struct.set(generationKeyName, generation);
    ByteBuffer buffer = ByteBuffer.allocate(cooperativeStickyAssignorUserDataV0.sizeOf(struct));
    cooperativeStickyAssignorUserDataV0.write(buffer, struct);
    buffer.flip();
    return buffer;
}
Also used : Field(org.apache.kafka.common.protocol.types.Field) Schema(org.apache.kafka.common.protocol.types.Schema) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 5 with Field

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

the class ConsumerProtocolTest method deserializeNewAssignmentVersion.

@Test
public void deserializeNewAssignmentVersion() {
    // verify that a new version which adds a field is still parseable
    short version = 100;
    Schema assignmentSchemaV100 = new Schema(new Field(ConsumerProtocol.TOPIC_PARTITIONS_KEY_NAME, new ArrayOf(ConsumerProtocol.TOPIC_ASSIGNMENT_V0)), new Field(ConsumerProtocol.USER_DATA_KEY_NAME, Type.BYTES), new Field("foo", Type.STRING));
    Struct assignmentV100 = new Struct(assignmentSchemaV100);
    assignmentV100.set(ConsumerProtocol.TOPIC_PARTITIONS_KEY_NAME, new Object[] { new Struct(ConsumerProtocol.TOPIC_ASSIGNMENT_V0).set(ConsumerProtocol.TOPIC_KEY_NAME, "foo").set(ConsumerProtocol.PARTITIONS_KEY_NAME, new Object[] { 1 }) });
    assignmentV100.set(ConsumerProtocol.USER_DATA_KEY_NAME, ByteBuffer.wrap(new byte[0]));
    assignmentV100.set("foo", "bar");
    Struct headerV100 = new Struct(ConsumerProtocol.CONSUMER_PROTOCOL_HEADER_SCHEMA);
    headerV100.set(ConsumerProtocol.VERSION_KEY_NAME, version);
    ByteBuffer buffer = ByteBuffer.allocate(assignmentV100.sizeOf() + headerV100.sizeOf());
    headerV100.writeTo(buffer);
    assignmentV100.writeTo(buffer);
    buffer.flip();
    PartitionAssignor.Assignment assignment = ConsumerProtocol.deserializeAssignment(buffer);
    assertEquals(toSet(Arrays.asList(new TopicPartition("foo", 1))), toSet(assignment.partitions()));
}
Also used : Field(org.apache.kafka.common.protocol.types.Field) ArrayOf(org.apache.kafka.common.protocol.types.ArrayOf) TopicPartition(org.apache.kafka.common.TopicPartition) Schema(org.apache.kafka.common.protocol.types.Schema) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.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