Search in sources :

Example 1 with ResponseHeader

use of org.apache.kafka.common.requests.ResponseHeader 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 2 with ResponseHeader

use of org.apache.kafka.common.requests.ResponseHeader in project kafka by apache.

the class NetworkClient method parseResponse.

public static AbstractResponse parseResponse(ByteBuffer responseBuffer, RequestHeader requestHeader) {
    ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
    // Always expect the response version id to be the same as the request version id
    ApiKeys apiKey = ApiKeys.forId(requestHeader.apiKey());
    Struct responseBody = apiKey.responseSchema(requestHeader.apiVersion()).read(responseBuffer);
    correlate(requestHeader, responseHeader);
    return AbstractResponse.getResponse(apiKey, responseBody);
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 3 with ResponseHeader

use of org.apache.kafka.common.requests.ResponseHeader in project kafka by apache.

the class NetworkClientTest method maybeSetExpectedApiVersionsResponse.

private void maybeSetExpectedApiVersionsResponse() {
    ByteBuffer buffer = ApiVersionsResponse.API_VERSIONS_RESPONSE.serialize((short) 0, new ResponseHeader(0));
    selector.delayedReceive(new DelayedReceive(node.idString(), new NetworkReceive(node.idString(), buffer)));
}
Also used : ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) NetworkReceive(org.apache.kafka.common.network.NetworkReceive) DelayedReceive(org.apache.kafka.test.DelayedReceive) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ResponseHeader (org.apache.kafka.common.requests.ResponseHeader)3 ByteBuffer (java.nio.ByteBuffer)2 NetworkReceive (org.apache.kafka.common.network.NetworkReceive)2 Struct (org.apache.kafka.common.protocol.types.Struct)2 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)1 DelayedReceive (org.apache.kafka.test.DelayedReceive)1