Search in sources :

Example 1 with ApiVersion

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

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

the class NodeApiVersionsTest method testVersionsToString.

@Test
public void testVersionsToString() {
    List<ApiVersion> versionList = new ArrayList<>();
    for (ApiKeys apiKey : ApiKeys.values()) {
        if (apiKey == ApiKeys.DELETE_TOPICS) {
            versionList.add(new ApiVersion().setApiKey(apiKey.id).setMinVersion((short) 10000).setMaxVersion((short) 10001));
        } else
            versionList.add(ApiVersionsResponse.toApiVersion(apiKey));
    }
    NodeApiVersions versions = new NodeApiVersions(versionList);
    StringBuilder bld = new StringBuilder();
    String prefix = "(";
    for (ApiKeys apiKey : ApiKeys.values()) {
        bld.append(prefix);
        if (apiKey == ApiKeys.DELETE_TOPICS) {
            bld.append("DeleteTopics(20): 10000 to 10001 [unusable: node too new]");
        } else {
            bld.append(apiKey.name).append("(").append(apiKey.id).append("): ");
            if (apiKey.oldestVersion() == apiKey.latestVersion()) {
                bld.append(apiKey.oldestVersion());
            } else {
                bld.append(apiKey.oldestVersion()).append(" to ").append(apiKey.latestVersion());
            }
            bld.append(" [usable: ").append(apiKey.latestVersion()).append("]");
        }
        prefix = ", ";
    }
    bld.append(")");
    assertEquals(bld.toString(), versions.toString());
}
Also used : ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ApiVersion

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

the class NodeApiVersionsTest method testUsableVersionLatestVersions.

@ParameterizedTest
@EnumSource(ApiMessageType.ListenerType.class)
public void testUsableVersionLatestVersions(ApiMessageType.ListenerType scope) {
    ApiVersionsResponse defaultResponse = ApiVersionsResponse.defaultApiVersionsResponse(scope);
    List<ApiVersion> versionList = new LinkedList<>(defaultResponse.data().apiKeys());
    // Add an API key that we don't know about.
    versionList.add(new ApiVersion().setApiKey((short) 100).setMinVersion((short) 0).setMaxVersion((short) 1));
    NodeApiVersions versions = new NodeApiVersions(versionList);
    for (ApiKeys apiKey : ApiKeys.apisForListener(scope)) {
        assertEquals(apiKey.latestVersion(), versions.latestUsableVersion(apiKey));
    }
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) LinkedList(java.util.LinkedList) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ApiVersion

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

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

the class ApiVersionsResponseTest method shouldHaveCorrectDefaultApiVersionsResponse.

@ParameterizedTest
@EnumSource(ApiMessageType.ListenerType.class)
public void shouldHaveCorrectDefaultApiVersionsResponse(ApiMessageType.ListenerType scope) {
    ApiVersionsResponse defaultResponse = ApiVersionsResponse.defaultApiVersionsResponse(scope);
    assertEquals(ApiKeys.apisForListener(scope).size(), defaultResponse.data().apiKeys().size(), "API versions for all API keys must be maintained.");
    for (ApiKeys key : ApiKeys.apisForListener(scope)) {
        ApiVersion version = defaultResponse.apiVersion(key.id);
        assertNotNull(version, "Could not find ApiVersion for API " + key.name);
        assertEquals(version.minVersion(), key.oldestVersion(), "Incorrect min version for Api " + key.name);
        assertEquals(version.maxVersion(), key.latestVersion(), "Incorrect max version for Api " + key.name);
        // Check if versions less than min version are indeed set as null, i.e., deprecated.
        for (int i = 0; i < version.minVersion(); ++i) {
            assertNull(key.messageType.requestSchemas()[i], "Request version " + i + " for API " + version.apiKey() + " must be null");
            assertNull(key.messageType.responseSchemas()[i], "Response version " + i + " for API " + version.apiKey() + " must be null");
        }
        // Check if versions between min and max versions are non null, i.e., valid.
        for (int i = version.minVersion(); i <= version.maxVersion(); ++i) {
            assertNotNull(key.messageType.requestSchemas()[i], "Request version " + i + " for API " + version.apiKey() + " must not be null");
            assertNotNull(key.messageType.responseSchemas()[i], "Response version " + i + " for API " + version.apiKey() + " must not be null");
        }
    }
    assertTrue(defaultResponse.data().supportedFeatures().isEmpty());
    assertTrue(defaultResponse.data().finalizedFeatures().isEmpty());
    assertEquals(ApiVersionsResponse.UNKNOWN_FINALIZED_FEATURES_EPOCH, defaultResponse.data().finalizedFeaturesEpoch());
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)28 Test (org.junit.jupiter.api.Test)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)7 ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)6 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)6 NodeApiVersions (org.apache.kafka.clients.NodeApiVersions)5 ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)4 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 ByteBuffer (java.nio.ByteBuffer)2 Node (org.apache.kafka.common.Node)2 DeleteGroupsResponseData (org.apache.kafka.common.message.DeleteGroupsResponseData)2 DeletableGroupResult (org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResult)2 DeletableGroupResultCollection (org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResultCollection)2 ListenerName (org.apache.kafka.common.network.ListenerName)2 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)2 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)2 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)2 DeleteGroupsResponse (org.apache.kafka.common.requests.DeleteGroupsResponse)2