Search in sources :

Example 1 with DescribeClusterRequestData

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

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 MetadataRequestData (org.apache.kafka.common.message.MetadataRequestData)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiError (org.apache.kafka.common.requests.ApiError)1