Search in sources :

Example 1 with ApiVersionsResponseData

use of org.apache.kafka.common.message.ApiVersionsResponseData in project kafka by apache.

the class NetworkClientTest method testInvalidApiVersionsRequest.

@Test
public void testInvalidApiVersionsRequest() {
    // initiate the connection
    client.ready(node, time.milliseconds());
    // handle the connection, send the ApiVersionsRequest
    client.poll(0, time.milliseconds());
    // check that the ApiVersionsRequest has been initiated
    assertTrue(client.hasInFlightRequests(node.idString()));
    // prepare response
    delayedApiVersionsResponse(0, ApiKeys.API_VERSIONS.latestVersion(), new ApiVersionsResponse(new ApiVersionsResponseData().setErrorCode(Errors.INVALID_REQUEST.code()).setThrottleTimeMs(0)));
    // handle completed receives
    client.poll(0, time.milliseconds());
    // the ApiVersionsRequest is gone
    assertFalse(client.hasInFlightRequests(node.idString()));
    // various assertions
    assertFalse(client.isReady(node, time.milliseconds()));
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) Test(org.junit.jupiter.api.Test)

Example 2 with ApiVersionsResponseData

use of org.apache.kafka.common.message.ApiVersionsResponseData in project kafka by apache.

the class NetworkClientTest method testUnsupportedApiVersionsRequestWithVersionProvidedByTheBroker.

@Test
public void testUnsupportedApiVersionsRequestWithVersionProvidedByTheBroker() {
    // initiate the connection
    client.ready(node, time.milliseconds());
    // handle the connection, initiate first ApiVersionsRequest
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest is in flight but not sent yet
    assertTrue(client.hasInFlightRequests(node.idString()));
    // completes initiated sends
    client.poll(0, time.milliseconds());
    assertEquals(1, selector.completedSends().size());
    ByteBuffer buffer = selector.completedSendBuffers().get(0).buffer();
    RequestHeader header = parseHeader(buffer);
    assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
    assertEquals(3, header.apiVersion());
    // prepare response
    ApiVersionCollection apiKeys = new ApiVersionCollection();
    apiKeys.add(new ApiVersion().setApiKey(ApiKeys.API_VERSIONS.id).setMinVersion((short) 0).setMaxVersion((short) 2));
    delayedApiVersionsResponse(0, (short) 0, new ApiVersionsResponse(new ApiVersionsResponseData().setErrorCode(Errors.UNSUPPORTED_VERSION.code()).setApiKeys(apiKeys)));
    // handle ApiVersionResponse, initiate second ApiVersionRequest
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest is in flight but not sent yet
    assertTrue(client.hasInFlightRequests(node.idString()));
    // ApiVersionsResponse has been received
    assertEquals(1, selector.completedReceives().size());
    // clean up the buffers
    selector.completedSends().clear();
    selector.completedSendBuffers().clear();
    selector.completedReceives().clear();
    // completes initiated sends
    client.poll(0, time.milliseconds());
    // ApiVersionsRequest has been sent
    assertEquals(1, selector.completedSends().size());
    buffer = selector.completedSendBuffers().get(0).buffer();
    header = parseHeader(buffer);
    assertEquals(ApiKeys.API_VERSIONS, header.apiKey());
    assertEquals(2, header.apiVersion());
    // prepare response
    delayedApiVersionsResponse(1, (short) 0, defaultApiVersionsResponse());
    // handle completed receives
    client.poll(0, time.milliseconds());
    // the ApiVersionsRequest is gone
    assertFalse(client.hasInFlightRequests(node.idString()));
    assertEquals(1, selector.completedReceives().size());
    // the client is ready
    assertTrue(client.isReady(node, time.milliseconds()));
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) RequestHeader(org.apache.kafka.common.requests.RequestHeader) ByteBuffer(java.nio.ByteBuffer) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) Test(org.junit.jupiter.api.Test)

Example 3 with ApiVersionsResponseData

use of org.apache.kafka.common.message.ApiVersionsResponseData in project kafka by apache.

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, ClientInformation.EMPTY, false);
    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.buildResponseSend(new ApiVersionsResponse(new ApiVersionsResponseData().setThrottleTimeMs(0).setErrorCode(Errors.UNSUPPORTED_VERSION.code()).setApiKeys(new ApiVersionCollection())));
    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, ApiKeys.API_VERSIONS.responseHeaderVersion(header.apiVersion()));
    assertEquals(correlationId, responseHeader.correlationId());
    ApiVersionsResponse response = (ApiVersionsResponse) AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, responseBuffer, (short) 0);
    assertEquals(Errors.UNSUPPORTED_VERSION.code(), response.data().errorCode());
    assertTrue(response.data().apiKeys().isEmpty());
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ListenerName(org.apache.kafka.common.network.ListenerName) ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) Test(org.junit.jupiter.api.Test)

Example 4 with ApiVersionsResponseData

use of org.apache.kafka.common.message.ApiVersionsResponseData in project kafka by apache.

the class RequestResponseTest method createApiVersionResponse.

private ApiVersionsResponse createApiVersionResponse() {
    ApiVersionCollection apiVersions = new ApiVersionCollection();
    apiVersions.add(new ApiVersion().setApiKey((short) 0).setMinVersion((short) 0).setMaxVersion((short) 2));
    return new ApiVersionsResponse(new ApiVersionsResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(0).setApiKeys(apiVersions));
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData)

Example 5 with ApiVersionsResponseData

use of org.apache.kafka.common.message.ApiVersionsResponseData in project kafka by apache.

the class ApiVersionsResponse method createApiVersionsResponseData.

private static ApiVersionsResponseData createApiVersionsResponseData(final int throttleTimeMs, final Errors error, final ApiVersionCollection apiKeys, final Features<SupportedVersionRange> latestSupportedFeatures, final Features<FinalizedVersionRange> finalizedFeatures, final long finalizedFeaturesEpoch) {
    final ApiVersionsResponseData data = new ApiVersionsResponseData();
    data.setThrottleTimeMs(throttleTimeMs);
    data.setErrorCode(error.code());
    data.setApiKeys(apiKeys);
    data.setSupportedFeatures(createSupportedFeatureKeys(latestSupportedFeatures));
    data.setFinalizedFeatures(createFinalizedFeatureKeys(finalizedFeatures));
    data.setFinalizedFeaturesEpoch(finalizedFeaturesEpoch);
    return data;
}
Also used : ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData)

Aggregations

ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)9 ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)6 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)5 ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)4 Test (org.junit.jupiter.api.Test)4 ByteBuffer (java.nio.ByteBuffer)3 ListenerName (org.apache.kafka.common.network.ListenerName)3 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)2 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)2 RequestHeader (org.apache.kafka.common.requests.RequestHeader)2 JaasContext (org.apache.kafka.common.security.JaasContext)2 TestSecurityConfig (org.apache.kafka.common.security.TestSecurityConfig)2 LogContext (org.apache.kafka.common.utils.LogContext)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ChannelMetadataRegistry (org.apache.kafka.common.network.ChannelMetadataRegistry)1 Send (org.apache.kafka.common.network.Send)1 TransportLayer (org.apache.kafka.common.network.TransportLayer)1