Search in sources :

Example 31 with Struct

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

the class OffsetCommitResponse method toStruct.

@Override
public Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.OFFSET_COMMIT.responseSchema(version));
    Map<String, Map<Integer, Errors>> topicsData = CollectionUtils.groupDataByTopic(responseData);
    List<Struct> topicArray = new ArrayList<>();
    for (Map.Entry<String, Map<Integer, Errors>> entries : topicsData.entrySet()) {
        Struct topicData = struct.instance(RESPONSES_KEY_NAME);
        topicData.set(TOPIC_KEY_NAME, entries.getKey());
        List<Struct> partitionArray = new ArrayList<>();
        for (Map.Entry<Integer, Errors> partitionEntry : entries.getValue().entrySet()) {
            Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
            partitionData.set(PARTITION_KEY_NAME, partitionEntry.getKey());
            partitionData.set(ERROR_CODE_KEY_NAME, partitionEntry.getValue().code());
            partitionArray.add(partitionData);
        }
        topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
        topicArray.add(topicData);
    }
    struct.set(RESPONSES_KEY_NAME, topicArray.toArray());
    return struct;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 32 with Struct

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

the class OffsetFetchResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.OFFSET_FETCH.responseSchema(version));
    Map<String, Map<Integer, PartitionData>> topicsData = CollectionUtils.groupDataByTopic(responseData);
    List<Struct> topicArray = new ArrayList<>();
    for (Map.Entry<String, Map<Integer, PartitionData>> entries : topicsData.entrySet()) {
        Struct topicData = struct.instance(RESPONSES_KEY_NAME);
        topicData.set(TOPIC_KEY_NAME, entries.getKey());
        List<Struct> partitionArray = new ArrayList<>();
        for (Map.Entry<Integer, PartitionData> partitionEntry : entries.getValue().entrySet()) {
            PartitionData fetchPartitionData = partitionEntry.getValue();
            Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
            partitionData.set(PARTITION_KEY_NAME, partitionEntry.getKey());
            partitionData.set(COMMIT_OFFSET_KEY_NAME, fetchPartitionData.offset);
            partitionData.set(METADATA_KEY_NAME, fetchPartitionData.metadata);
            partitionData.set(ERROR_CODE_KEY_NAME, fetchPartitionData.error.code());
            partitionArray.add(partitionData);
        }
        topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
        topicArray.add(topicData);
    }
    struct.set(RESPONSES_KEY_NAME, topicArray.toArray());
    if (version > 1)
        struct.set(ERROR_CODE_KEY_NAME, this.error.code());
    return struct;
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 33 with Struct

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

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

the class NetworkClientTest method checkSimpleRequestResponse.

private void checkSimpleRequestResponse(NetworkClient networkClient) {
    // has to be before creating any request, as it may send ApiVersionsRequest and its response is mocked with correlation id 0
    awaitReady(networkClient, node);
    ProduceRequest.Builder builder = new ProduceRequest.Builder((short) 1, 1000, Collections.<TopicPartition, MemoryRecords>emptyMap());
    TestCallbackHandler handler = new TestCallbackHandler();
    ClientRequest request = networkClient.newClientRequest(node.idString(), builder, time.milliseconds(), true, handler);
    networkClient.send(request, time.milliseconds());
    networkClient.poll(1, time.milliseconds());
    assertEquals(1, networkClient.inFlightRequestCount());
    ResponseHeader respHeader = new ResponseHeader(request.correlationId());
    Struct resp = new Struct(ApiKeys.PRODUCE.responseSchema(ApiKeys.PRODUCE.latestVersion()));
    resp.set("responses", new Object[0]);
    Struct responseHeaderStruct = respHeader.toStruct();
    int size = responseHeaderStruct.sizeOf() + resp.sizeOf();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    responseHeaderStruct.writeTo(buffer);
    resp.writeTo(buffer);
    buffer.flip();
    selector.completeReceive(new NetworkReceive(node.idString(), buffer));
    List<ClientResponse> responses = networkClient.poll(1, time.milliseconds());
    assertEquals(1, responses.size());
    assertTrue("The handler should have executed.", handler.executed);
    assertTrue("Should have a response body.", handler.response.hasResponse());
    assertEquals("Should be correlated to the original request", request.correlationId(), handler.response.requestHeader().correlationId());
}
Also used : ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 35 with Struct

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

the class ConnectProtocol method serializeMetadata.

public static ByteBuffer serializeMetadata(WorkerState workerState) {
    Struct struct = new Struct(CONFIG_STATE_V0);
    struct.set(URL_KEY_NAME, workerState.url());
    struct.set(CONFIG_OFFSET_KEY_NAME, workerState.offset());
    ByteBuffer buffer = ByteBuffer.allocate(CONNECT_PROTOCOL_HEADER_V0.sizeOf() + CONFIG_STATE_V0.sizeOf(struct));
    CONNECT_PROTOCOL_HEADER_V0.writeTo(buffer);
    CONFIG_STATE_V0.write(buffer, struct);
    buffer.flip();
    return buffer;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Aggregations

Struct (org.apache.kafka.common.protocol.types.Struct)63 ArrayList (java.util.ArrayList)32 Map (java.util.Map)22 HashMap (java.util.HashMap)19 ByteBuffer (java.nio.ByteBuffer)18 TopicPartition (org.apache.kafka.common.TopicPartition)10 Test (org.junit.Test)5 List (java.util.List)4 ByteBufferSend (org.apache.kafka.common.network.ByteBufferSend)4 Errors (org.apache.kafka.common.protocol.Errors)4 LinkedHashMap (java.util.LinkedHashMap)3 Node (org.apache.kafka.common.Node)2 Send (org.apache.kafka.common.network.Send)2 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)2 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)2 Field (org.apache.kafka.common.protocol.types.Field)2 Schema (org.apache.kafka.common.protocol.types.Schema)2 ResponseHeader (org.apache.kafka.common.requests.ResponseHeader)2 Subscription (org.apache.kafka.clients.consumer.internals.PartitionAssignor.Subscription)1 MultiSend (org.apache.kafka.common.network.MultiSend)1