Search in sources :

Example 1 with ApiVersionCollection

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection 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 2 with ApiVersionCollection

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

the class NodeApiVersionsTest method testUnsupportedVersionsToString.

@Test
public void testUnsupportedVersionsToString() {
    NodeApiVersions versions = new NodeApiVersions(new ApiVersionCollection());
    StringBuilder bld = new StringBuilder();
    String prefix = "(";
    for (ApiKeys apiKey : ApiKeys.zkBrokerApis()) {
        bld.append(prefix).append(apiKey.name).append("(").append(apiKey.id).append("): UNSUPPORTED");
        prefix = ", ";
    }
    bld.append(")");
    assertEquals(bld.toString(), versions.toString());
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ApiVersionCollection

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

the class ApiVersionsResponseTest method shouldHaveCommonlyAgreedApiVersionResponseWithControllerOnForwardableAPIs.

@Test
public void shouldHaveCommonlyAgreedApiVersionResponseWithControllerOnForwardableAPIs() {
    final ApiKeys forwardableAPIKey = ApiKeys.CREATE_ACLS;
    final ApiKeys nonForwardableAPIKey = ApiKeys.JOIN_GROUP;
    final short minVersion = 0;
    final short maxVersion = 1;
    Map<ApiKeys, ApiVersion> activeControllerApiVersions = Utils.mkMap(Utils.mkEntry(forwardableAPIKey, new ApiVersion().setApiKey(forwardableAPIKey.id).setMinVersion(minVersion).setMaxVersion(maxVersion)), Utils.mkEntry(nonForwardableAPIKey, new ApiVersion().setApiKey(nonForwardableAPIKey.id).setMinVersion(minVersion).setMaxVersion(maxVersion)));
    ApiVersionCollection commonResponse = ApiVersionsResponse.intersectForwardableApis(ApiMessageType.ListenerType.ZK_BROKER, RecordVersion.current(), activeControllerApiVersions);
    verifyVersions(forwardableAPIKey.id, minVersion, maxVersion, commonResponse);
    verifyVersions(nonForwardableAPIKey.id, ApiKeys.JOIN_GROUP.oldestVersion(), ApiKeys.JOIN_GROUP.latestVersion(), commonResponse);
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ApiVersionCollection

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection 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 5 with ApiVersionCollection

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection 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)

Aggregations

ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)10 ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)6 ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)6 Test (org.junit.jupiter.api.Test)5 ListenerName (org.apache.kafka.common.network.ListenerName)3 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)3 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ByteBuffer (java.nio.ByteBuffer)2 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)2 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)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 RequestHeader (org.apache.kafka.common.requests.RequestHeader)1