Search in sources :

Example 6 with Struct

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

the class RequestResponseTest method checkResponse.

private void checkResponse(AbstractResponse response, int version) throws Exception {
    // Check that we can serialize, deserialize and serialize again
    // We don't check for equality or hashCode because it is likely to fail for any response containing a HashMap
    Struct struct = response.toStruct((short) version);
    AbstractResponse deserialized = (AbstractResponse) deserialize(response, struct, (short) version);
    deserialized.toStruct((short) version);
}
Also used : Struct(org.apache.kafka.common.protocol.types.Struct)

Example 7 with Struct

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

the class RequestResponseTest method testControlledShutdownResponse.

@Test
public void testControlledShutdownResponse() {
    ControlledShutdownResponse response = createControlledShutdownResponse();
    short version = ApiKeys.CONTROLLED_SHUTDOWN_KEY.latestVersion();
    Struct struct = response.toStruct(version);
    ByteBuffer buffer = toBuffer(struct);
    ControlledShutdownResponse deserialized = ControlledShutdownResponse.parse(buffer, version);
    assertEquals(response.error(), deserialized.error());
    assertEquals(response.partitionsRemaining(), deserialized.partitionsRemaining());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 8 with Struct

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

the class SyncGroupRequest method toStruct.

@Override
protected Struct toStruct() {
    Struct struct = new Struct(ApiKeys.SYNC_GROUP.requestSchema(version()));
    struct.set(GROUP_ID_KEY_NAME, groupId);
    struct.set(GENERATION_ID_KEY_NAME, generationId);
    struct.set(MEMBER_ID_KEY_NAME, memberId);
    List<Struct> memberArray = new ArrayList<>();
    for (Map.Entry<String, ByteBuffer> entries : groupAssignment.entrySet()) {
        Struct memberData = struct.instance(GROUP_ASSIGNMENT_KEY_NAME);
        memberData.set(MEMBER_ID_KEY_NAME, entries.getKey());
        memberData.set(MEMBER_ASSIGNMENT_KEY_NAME, entries.getValue());
        memberArray.add(memberData);
    }
    struct.set(GROUP_ASSIGNMENT_KEY_NAME, memberArray.toArray());
    return struct;
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 9 with Struct

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

the class SyncGroupResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.SYNC_GROUP.responseSchema(version));
    struct.set(ERROR_CODE_KEY_NAME, error.code());
    struct.set(MEMBER_ASSIGNMENT_KEY_NAME, memberState);
    return struct;
}
Also used : Struct(org.apache.kafka.common.protocol.types.Struct)

Example 10 with Struct

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

the class UpdateMetadataRequest method toStruct.

@Override
protected Struct toStruct() {
    short version = version();
    Struct struct = new Struct(ApiKeys.UPDATE_METADATA_KEY.requestSchema(version));
    struct.set(CONTROLLER_ID_KEY_NAME, controllerId);
    struct.set(CONTROLLER_EPOCH_KEY_NAME, controllerEpoch);
    List<Struct> partitionStatesData = new ArrayList<>(partitionStates.size());
    for (Map.Entry<TopicPartition, PartitionState> entry : partitionStates.entrySet()) {
        Struct partitionStateData = struct.instance(PARTITION_STATES_KEY_NAME);
        TopicPartition topicPartition = entry.getKey();
        partitionStateData.set(TOPIC_KEY_NAME, topicPartition.topic());
        partitionStateData.set(PARTITION_KEY_NAME, topicPartition.partition());
        PartitionState partitionState = entry.getValue();
        partitionStateData.set(CONTROLLER_EPOCH_KEY_NAME, partitionState.controllerEpoch);
        partitionStateData.set(LEADER_KEY_NAME, partitionState.leader);
        partitionStateData.set(LEADER_EPOCH_KEY_NAME, partitionState.leaderEpoch);
        partitionStateData.set(ISR_KEY_NAME, partitionState.isr.toArray());
        partitionStateData.set(ZK_VERSION_KEY_NAME, partitionState.zkVersion);
        partitionStateData.set(REPLICAS_KEY_NAME, partitionState.replicas.toArray());
        partitionStatesData.add(partitionStateData);
    }
    struct.set(PARTITION_STATES_KEY_NAME, partitionStatesData.toArray());
    List<Struct> brokersData = new ArrayList<>(liveBrokers.size());
    for (Broker broker : liveBrokers) {
        Struct brokerData = struct.instance(LIVE_BROKERS_KEY_NAME);
        brokerData.set(BROKER_ID_KEY_NAME, broker.id);
        if (version == 0) {
            EndPoint endPoint = broker.endPoints.get(0);
            brokerData.set(HOST_KEY_NAME, endPoint.host);
            brokerData.set(PORT_KEY_NAME, endPoint.port);
        } else {
            List<Struct> endPointsData = new ArrayList<>(broker.endPoints.size());
            for (EndPoint endPoint : broker.endPoints) {
                Struct endPointData = brokerData.instance(ENDPOINTS_KEY_NAME);
                endPointData.set(PORT_KEY_NAME, endPoint.port);
                endPointData.set(HOST_KEY_NAME, endPoint.host);
                endPointData.set(SECURITY_PROTOCOL_TYPE_KEY_NAME, endPoint.securityProtocol.id);
                if (version >= 3)
                    endPointData.set(LISTENER_NAME_KEY_NAME, endPoint.listenerName.value());
                endPointsData.add(endPointData);
            }
            brokerData.set(ENDPOINTS_KEY_NAME, endPointsData.toArray());
            if (version >= 2) {
                brokerData.set(RACK_KEY_NAME, broker.rack);
            }
        }
        brokersData.add(brokerData);
    }
    struct.set(LIVE_BROKERS_KEY_NAME, brokersData.toArray());
    return struct;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) 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