Search in sources :

Example 76 with Struct

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

the class ConnectProtocol method deserializeAssignment.

public static Assignment deserializeAssignment(ByteBuffer buffer) {
    Struct header = CONNECT_PROTOCOL_HEADER_SCHEMA.read(buffer);
    Short version = header.getShort(VERSION_KEY_NAME);
    checkVersionCompatibility(version);
    Struct struct = ASSIGNMENT_V0.read(buffer);
    short error = struct.getShort(ERROR_KEY_NAME);
    String leader = struct.getString(LEADER_KEY_NAME);
    String leaderUrl = struct.getString(LEADER_URL_KEY_NAME);
    long offset = struct.getLong(CONFIG_OFFSET_KEY_NAME);
    List<String> connectorIds = new ArrayList<>();
    List<ConnectorTaskId> taskIds = new ArrayList<>();
    for (Object structObj : struct.getArray(ASSIGNMENT_KEY_NAME)) {
        Struct assignment = (Struct) structObj;
        String connector = assignment.getString(CONNECTOR_KEY_NAME);
        for (Object taskIdObj : assignment.getArray(TASKS_KEY_NAME)) {
            Integer taskId = (Integer) taskIdObj;
            if (taskId == CONNECTOR_TASK)
                connectorIds.add(connector);
            else
                taskIds.add(new ConnectorTaskId(connector, taskId));
        }
    }
    return new Assignment(error, leader, leaderUrl, offset, connectorIds, taskIds);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) ArrayList(java.util.ArrayList) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 77 with Struct

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

the class ConnectProtocol method serializeAssignment.

public static ByteBuffer serializeAssignment(Assignment assignment) {
    Struct struct = new Struct(ASSIGNMENT_V0);
    struct.set(ERROR_KEY_NAME, assignment.error());
    struct.set(LEADER_KEY_NAME, assignment.leader());
    struct.set(LEADER_URL_KEY_NAME, assignment.leaderUrl());
    struct.set(CONFIG_OFFSET_KEY_NAME, assignment.offset());
    List<Struct> taskAssignments = new ArrayList<>();
    for (Map.Entry<String, List<Integer>> connectorEntry : assignment.asMap().entrySet()) {
        Struct taskAssignment = new Struct(CONNECTOR_ASSIGNMENT_V0);
        taskAssignment.set(CONNECTOR_KEY_NAME, connectorEntry.getKey());
        List<Integer> tasks = connectorEntry.getValue();
        taskAssignment.set(TASKS_KEY_NAME, tasks.toArray());
        taskAssignments.add(taskAssignment);
    }
    struct.set(ASSIGNMENT_KEY_NAME, taskAssignments.toArray());
    ByteBuffer buffer = ByteBuffer.allocate(CONNECT_PROTOCOL_HEADER_V0.sizeOf() + ASSIGNMENT_V0.sizeOf(struct));
    CONNECT_PROTOCOL_HEADER_V0.writeTo(buffer);
    ASSIGNMENT_V0.write(buffer, struct);
    buffer.flip();
    return buffer;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 78 with Struct

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

the class RequestContextTest method testSerdeUnsupportedApiVersionRequest.

@Test
public void testSerdeUnsupportedApiVersionRequest() throws Exception {
    int correlationId = 23423;
    RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "", correlationId);
    RequestContext context = new RequestContext(header, "0", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, new ListenerName("ssl"), SecurityProtocol.SASL_SSL);
    assertEquals(0, context.apiVersion());
    // Write some garbage to the request buffer. This should be ignored since we will treat
    // the unknown version type as v0 which has an empty request body.
    ByteBuffer requestBuffer = ByteBuffer.allocate(8);
    requestBuffer.putInt(3709234);
    requestBuffer.putInt(29034);
    requestBuffer.flip();
    RequestAndSize requestAndSize = context.parseRequest(requestBuffer);
    assertTrue(requestAndSize.request instanceof ApiVersionsRequest);
    ApiVersionsRequest request = (ApiVersionsRequest) requestAndSize.request;
    assertTrue(request.hasUnsupportedRequestVersion());
    Send send = context.buildResponse(new ApiVersionsResponse(0, Errors.UNSUPPORTED_VERSION, Collections.<ApiVersionsResponse.ApiVersion>emptyList()));
    ByteBufferChannel channel = new ByteBufferChannel(256);
    send.writeTo(channel);
    ByteBuffer responseBuffer = channel.buffer();
    responseBuffer.flip();
    // strip off the size
    responseBuffer.getInt();
    ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
    assertEquals(correlationId, responseHeader.correlationId());
    Struct struct = ApiKeys.API_VERSIONS.parseResponse((short) 0, responseBuffer);
    ApiVersionsResponse response = (ApiVersionsResponse) AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, struct);
    assertEquals(Errors.UNSUPPORTED_VERSION, response.error());
    assertTrue(response.apiVersions().isEmpty());
}
Also used : ListenerName(org.apache.kafka.common.network.ListenerName) ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 79 with Struct

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

the class RequestHeaderTest method testRequestHeaderWithNullClientId.

@Test
public void testRequestHeaderWithNullClientId() {
    RequestHeader header = new RequestHeader(ApiKeys.FIND_COORDINATOR, (short) 1, null, 10);
    Struct headerStruct = header.toStruct();
    ByteBuffer buffer = toBuffer(headerStruct);
    RequestHeader deserialized = RequestHeader.parse(buffer);
    assertEquals(header.apiKey(), deserialized.apiKey());
    assertEquals(header.apiVersion(), deserialized.apiVersion());
    assertEquals(header.correlationId(), deserialized.correlationId());
    // null defaults to ""
    assertEquals("", deserialized.clientId());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Example 80 with Struct

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

the class RequestResponseTest method checkRequest.

private void checkRequest(AbstractRequest req) 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 request containing a HashMap
    Struct struct = req.toStruct();
    AbstractRequest deserialized = (AbstractRequest) deserialize(req, struct, req.version());
    deserialized.toStruct();
}
Also used : Struct(org.apache.kafka.common.protocol.types.Struct)

Aggregations

Struct (org.apache.kafka.common.protocol.types.Struct)252 ArrayList (java.util.ArrayList)94 ByteBuffer (java.nio.ByteBuffer)91 Map (java.util.Map)73 HashMap (java.util.HashMap)68 TopicPartition (org.apache.kafka.common.TopicPartition)37 Schema (org.apache.kafka.common.protocol.types.Schema)23 List (java.util.List)22 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)15 Test (org.junit.Test)15 Errors (org.apache.kafka.common.protocol.Errors)13 Field (org.apache.kafka.common.protocol.types.Field)13 ByteBuf (io.netty.buffer.ByteBuf)12 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)12 RequestHeader (org.apache.kafka.common.requests.RequestHeader)11 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)10 GroupTopicPartition (io.streamnative.pulsar.handlers.kop.coordinator.group.GroupMetadataManager.GroupTopicPartition)8 ByteBufferSend (org.apache.kafka.common.network.ByteBufferSend)8 LinkedHashMap (java.util.LinkedHashMap)7 Lists (com.google.common.collect.Lists)6