Search in sources :

Example 1 with DescribeClusterResponse

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

the class KafkaAdminClientTest method testDescribeClusterHandleError.

@Test
public void testDescribeClusterHandleError() {
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(4, 0), AdminClientConfig.RETRIES_CONFIG, "2")) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        // Prepare the describe cluster response used for the first describe cluster
        String errorMessage = "my error";
        env.kafkaClient().prepareResponse(new DescribeClusterResponse(new DescribeClusterResponseData().setErrorCode(Errors.INVALID_REQUEST.code()).setErrorMessage(errorMessage)));
        final DescribeClusterResult result = env.adminClient().describeCluster();
        TestUtils.assertFutureThrows(result.clusterId(), InvalidRequestException.class, errorMessage);
        TestUtils.assertFutureThrows(result.controller(), InvalidRequestException.class, errorMessage);
        TestUtils.assertFutureThrows(result.nodes(), InvalidRequestException.class, errorMessage);
        TestUtils.assertFutureThrows(result.authorizedOperations(), InvalidRequestException.class, errorMessage);
    }
}
Also used : DescribeClusterResponseData(org.apache.kafka.common.message.DescribeClusterResponseData) DescribeClusterResponse(org.apache.kafka.common.requests.DescribeClusterResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with DescribeClusterResponse

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

the class KafkaAdminClientTest method prepareDescribeClusterResponse.

private static DescribeClusterResponse prepareDescribeClusterResponse(int throttleTimeMs, Collection<Node> brokers, String clusterId, int controllerId, int clusterAuthorizedOperations) {
    DescribeClusterResponseData data = new DescribeClusterResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(throttleTimeMs).setControllerId(controllerId).setClusterId(clusterId).setClusterAuthorizedOperations(clusterAuthorizedOperations);
    brokers.forEach(broker -> data.brokers().add(new DescribeClusterBroker().setHost(broker.host()).setPort(broker.port()).setBrokerId(broker.id()).setRack(broker.rack())));
    return new DescribeClusterResponse(data);
}
Also used : DescribeClusterResponseData(org.apache.kafka.common.message.DescribeClusterResponseData) DescribeClusterResponse(org.apache.kafka.common.requests.DescribeClusterResponse) DescribeClusterBroker(org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBroker)

Example 3 with DescribeClusterResponse

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

the class KafkaAdminClient method describeCluster.

@Override
public DescribeClusterResult describeCluster(DescribeClusterOptions options) {
    final KafkaFutureImpl<Collection<Node>> describeClusterFuture = new KafkaFutureImpl<>();
    final KafkaFutureImpl<Node> controllerFuture = new KafkaFutureImpl<>();
    final KafkaFutureImpl<String> clusterIdFuture = new KafkaFutureImpl<>();
    final KafkaFutureImpl<Set<AclOperation>> authorizedOperationsFuture = new KafkaFutureImpl<>();
    final long now = time.milliseconds();
    runnable.call(new Call("listNodes", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        private boolean useMetadataRequest = false;

        @Override
        AbstractRequest.Builder createRequest(int timeoutMs) {
            if (!useMetadataRequest) {
                return new DescribeClusterRequest.Builder(new DescribeClusterRequestData().setIncludeClusterAuthorizedOperations(options.includeAuthorizedOperations()));
            } else {
                // simplifies communication with older brokers)
                return new MetadataRequest.Builder(new MetadataRequestData().setTopics(Collections.emptyList()).setAllowAutoTopicCreation(true).setIncludeClusterAuthorizedOperations(options.includeAuthorizedOperations()));
            }
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            if (!useMetadataRequest) {
                DescribeClusterResponse response = (DescribeClusterResponse) abstractResponse;
                Errors error = Errors.forCode(response.data().errorCode());
                if (error != Errors.NONE) {
                    ApiError apiError = new ApiError(error, response.data().errorMessage());
                    handleFailure(apiError.exception());
                    return;
                }
                Map<Integer, Node> nodes = response.nodes();
                describeClusterFuture.complete(nodes.values());
                // Controller is null if controller id is equal to NO_CONTROLLER_ID
                controllerFuture.complete(nodes.get(response.data().controllerId()));
                clusterIdFuture.complete(response.data().clusterId());
                authorizedOperationsFuture.complete(validAclOperations(response.data().clusterAuthorizedOperations()));
            } else {
                MetadataResponse response = (MetadataResponse) abstractResponse;
                describeClusterFuture.complete(response.brokers());
                controllerFuture.complete(controller(response));
                clusterIdFuture.complete(response.clusterId());
                authorizedOperationsFuture.complete(validAclOperations(response.clusterAuthorizedOperations()));
            }
        }

        private Node controller(MetadataResponse response) {
            if (response.controller() == null || response.controller().id() == MetadataResponse.NO_CONTROLLER_ID)
                return null;
            return response.controller();
        }

        @Override
        void handleFailure(Throwable throwable) {
            describeClusterFuture.completeExceptionally(throwable);
            controllerFuture.completeExceptionally(throwable);
            clusterIdFuture.completeExceptionally(throwable);
            authorizedOperationsFuture.completeExceptionally(throwable);
        }

        @Override
        boolean handleUnsupportedVersionException(final UnsupportedVersionException exception) {
            if (useMetadataRequest) {
                return false;
            }
            useMetadataRequest = true;
            return true;
        }
    }, now);
    return new DescribeClusterResult(describeClusterFuture, controllerFuture, clusterIdFuture, authorizedOperationsFuture);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Node(org.apache.kafka.common.Node) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) DescribeClusterRequest(org.apache.kafka.common.requests.DescribeClusterRequest) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) DescribeClusterRequestData(org.apache.kafka.common.message.DescribeClusterRequestData) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) DescribeClusterResponse(org.apache.kafka.common.requests.DescribeClusterResponse) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) Errors(org.apache.kafka.common.protocol.Errors) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) TopicNameCollection(org.apache.kafka.common.TopicCollection.TopicNameCollection) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) TopicIdCollection(org.apache.kafka.common.TopicCollection.TopicIdCollection) Collection(java.util.Collection) TopicCollection(org.apache.kafka.common.TopicCollection) CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) ApiError(org.apache.kafka.common.requests.ApiError) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Aggregations

DescribeClusterResponse (org.apache.kafka.common.requests.DescribeClusterResponse)3 DescribeClusterResponseData (org.apache.kafka.common.message.DescribeClusterResponseData)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 Node (org.apache.kafka.common.Node)1 TopicCollection (org.apache.kafka.common.TopicCollection)1 TopicIdCollection (org.apache.kafka.common.TopicCollection.TopicIdCollection)1 TopicNameCollection (org.apache.kafka.common.TopicCollection.TopicNameCollection)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 CreatePartitionsTopicCollection (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection)1 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)1 DescribeClusterRequestData (org.apache.kafka.common.message.DescribeClusterRequestData)1 DescribeClusterBroker (org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBroker)1 MetadataRequestData (org.apache.kafka.common.message.MetadataRequestData)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1