Search in sources :

Example 16 with ApiVersion

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

the class KafkaAdminClientTest method testDeleteMultipleConsumerGroupsWithOlderBroker.

@Test
public void testDeleteMultipleConsumerGroupsWithOlderBroker() throws Exception {
    final List<String> groupIds = asList("group1", "group2");
    ApiVersion findCoordinatorV3 = new ApiVersion().setApiKey(ApiKeys.FIND_COORDINATOR.id).setMinVersion((short) 0).setMaxVersion((short) 3);
    ApiVersion describeGroups = new ApiVersion().setApiKey(ApiKeys.DESCRIBE_GROUPS.id).setMinVersion((short) 0).setMaxVersion(ApiKeys.DELETE_GROUPS.latestVersion());
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create(Arrays.asList(findCoordinatorV3, describeGroups)));
        // Dummy response for MockClient to handle the UnsupportedVersionException correctly to switch from batched to un-batched
        env.kafkaClient().prepareResponse(null);
        // Retriable FindCoordinatorResponse errors should be retried
        for (int i = 0; i < groupIds.size(); i++) {
            env.kafkaClient().prepareResponse(prepareOldFindCoordinatorResponse(Errors.COORDINATOR_NOT_AVAILABLE, Node.noNode()));
        }
        for (int i = 0; i < groupIds.size(); i++) {
            env.kafkaClient().prepareResponse(prepareOldFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        }
        final DeletableGroupResultCollection validResponse = new DeletableGroupResultCollection();
        validResponse.add(new DeletableGroupResult().setGroupId("group1").setErrorCode(Errors.NONE.code()));
        validResponse.add(new DeletableGroupResult().setGroupId("group2").setErrorCode(Errors.NONE.code()));
        env.kafkaClient().prepareResponse(new DeleteGroupsResponse(new DeleteGroupsResponseData().setResults(validResponse)));
        final DeleteConsumerGroupsResult result = env.adminClient().deleteConsumerGroups(groupIds);
        final KafkaFuture<Void> results = result.deletedGroups().get("group1");
        assertNull(results.get(5, TimeUnit.SECONDS));
    }
}
Also used : ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) DeletableGroupResult(org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResult) DeleteGroupsResponseData(org.apache.kafka.common.message.DeleteGroupsResponseData) DeleteGroupsResponse(org.apache.kafka.common.requests.DeleteGroupsResponse) DeletableGroupResultCollection(org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResultCollection) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 17 with ApiVersion

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

the class KafkaAdminClientTest method testListConsumerGroupsWithStatesOlderBrokerVersion.

@Test
public void testListConsumerGroupsWithStatesOlderBrokerVersion() throws Exception {
    ApiVersion listGroupV3 = new ApiVersion().setApiKey(ApiKeys.LIST_GROUPS.id).setMinVersion((short) 0).setMaxVersion((short) 3);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create(Collections.singletonList(listGroupV3)));
        env.kafkaClient().prepareResponse(prepareMetadataResponse(env.cluster(), Errors.NONE));
        // Check we can list groups with older broker if we don't specify states
        env.kafkaClient().prepareResponseFrom(new ListGroupsResponse(new ListGroupsResponseData().setErrorCode(Errors.NONE.code()).setGroups(Collections.singletonList(new ListGroupsResponseData.ListedGroup().setGroupId("group-1").setProtocolType(ConsumerProtocol.PROTOCOL_TYPE)))), env.cluster().nodeById(0));
        ListConsumerGroupsOptions options = new ListConsumerGroupsOptions();
        ListConsumerGroupsResult result = env.adminClient().listConsumerGroups(options);
        Collection<ConsumerGroupListing> listing = result.all().get();
        assertEquals(1, listing.size());
        List<ConsumerGroupListing> expected = Collections.singletonList(new ConsumerGroupListing("group-1", false, Optional.empty()));
        assertEquals(expected, listing);
        // But we cannot set a state filter with older broker
        env.kafkaClient().prepareResponse(prepareMetadataResponse(env.cluster(), Errors.NONE));
        env.kafkaClient().prepareUnsupportedVersionResponse(body -> body instanceof ListGroupsRequest);
        options = new ListConsumerGroupsOptions().inStates(singleton(ConsumerGroupState.STABLE));
        result = env.adminClient().listConsumerGroups(options);
        TestUtils.assertFutureThrows(result.all(), UnsupportedVersionException.class);
    }
}
Also used : ListGroupsRequest(org.apache.kafka.common.requests.ListGroupsRequest) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) ListGroupsResponse(org.apache.kafka.common.requests.ListGroupsResponse) ListGroupsResponseData(org.apache.kafka.common.message.ListGroupsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 18 with ApiVersion

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

the class NodeApiVersions method latestUsableVersion.

/**
 * Get the latest version supported by the broker within an allowed range of versions
 */
public short latestUsableVersion(ApiKeys apiKey, short oldestAllowedVersion, short latestAllowedVersion) {
    if (!supportedVersions.containsKey(apiKey))
        throw new UnsupportedVersionException("The broker does not support " + apiKey);
    ApiVersion supportedVersion = supportedVersions.get(apiKey);
    Optional<ApiVersion> intersectVersion = ApiVersionsResponse.intersect(supportedVersion, new ApiVersion().setApiKey(apiKey.id).setMinVersion(oldestAllowedVersion).setMaxVersion(latestAllowedVersion));
    if (intersectVersion.isPresent())
        return intersectVersion.get().maxVersion();
    else
        throw new UnsupportedVersionException("The broker does not support " + apiKey + " with version in range [" + oldestAllowedVersion + "," + latestAllowedVersion + "]. The supported" + " range is [" + supportedVersion.minVersion() + "," + supportedVersion.maxVersion() + "].");
}
Also used : ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 19 with ApiVersion

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

the class SaslClientAuthenticator method setSaslAuthenticateAndHandshakeVersions.

// Visible to override for testing
protected void setSaslAuthenticateAndHandshakeVersions(ApiVersionsResponse apiVersionsResponse) {
    ApiVersion authenticateVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_AUTHENTICATE.id);
    if (authenticateVersion != null) {
        this.saslAuthenticateVersion = (short) Math.min(authenticateVersion.maxVersion(), ApiKeys.SASL_AUTHENTICATE.latestVersion());
    }
    ApiVersion handshakeVersion = apiVersionsResponse.apiVersion(ApiKeys.SASL_HANDSHAKE.id);
    if (handshakeVersion != null) {
        this.saslHandshakeVersion = (short) Math.min(handshakeVersion.maxVersion(), ApiKeys.SASL_HANDSHAKE.latestVersion());
    }
}
Also used : ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)

Example 20 with ApiVersion

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

the class NodeApiVersionsTest method testConstructionFromApiVersionsResponse.

@ParameterizedTest
@EnumSource(ApiMessageType.ListenerType.class)
public void testConstructionFromApiVersionsResponse(ApiMessageType.ListenerType scope) {
    ApiVersionsResponse apiVersionsResponse = ApiVersionsResponse.defaultApiVersionsResponse(scope);
    NodeApiVersions versions = new NodeApiVersions(apiVersionsResponse.data().apiKeys());
    for (ApiVersion apiVersionKey : apiVersionsResponse.data().apiKeys()) {
        ApiVersion apiVersion = versions.apiVersion(ApiKeys.forId(apiVersionKey.apiKey()));
        assertEquals(apiVersionKey.apiKey(), apiVersion.apiKey());
        assertEquals(apiVersionKey.minVersion(), apiVersion.minVersion());
        assertEquals(apiVersionKey.maxVersion(), apiVersion.maxVersion());
    }
}
Also used : ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) 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