Search in sources :

Example 1 with Resource

use of org.apache.kafka.common.requests.Resource in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method describeConfigs.

@Override
public DescribeConfigsResult describeConfigs(Collection<ConfigResource> configResources, final DescribeConfigsOptions options) {
    final Map<ConfigResource, KafkaFutureImpl<Config>> unifiedRequestFutures = new HashMap<>();
    final Map<ConfigResource, KafkaFutureImpl<Config>> brokerFutures = new HashMap<>(configResources.size());
    // The BROKER resources which we want to describe.  We must make a separate DescribeConfigs
    // request for every BROKER resource we want to describe.
    final Collection<Resource> brokerResources = new ArrayList<>();
    // The non-BROKER resources which we want to describe.  These resources can be described by a
    // single, unified DescribeConfigs request.
    final Collection<Resource> unifiedRequestResources = new ArrayList<>(configResources.size());
    for (ConfigResource resource : configResources) {
        if (resource.type() == ConfigResource.Type.BROKER && !resource.isDefault()) {
            brokerFutures.put(resource, new KafkaFutureImpl<Config>());
            brokerResources.add(configResourceToResource(resource));
        } else {
            unifiedRequestFutures.put(resource, new KafkaFutureImpl<Config>());
            unifiedRequestResources.add(configResourceToResource(resource));
        }
    }
    final long now = time.milliseconds();
    if (!unifiedRequestResources.isEmpty()) {
        runnable.call(new Call("describeConfigs", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

            @Override
            AbstractRequest.Builder createRequest(int timeoutMs) {
                return new DescribeConfigsRequest.Builder(unifiedRequestResources).includeSynonyms(options.includeSynonyms());
            }

            @Override
            void handleResponse(AbstractResponse abstractResponse) {
                DescribeConfigsResponse response = (DescribeConfigsResponse) abstractResponse;
                for (Map.Entry<ConfigResource, KafkaFutureImpl<Config>> entry : unifiedRequestFutures.entrySet()) {
                    ConfigResource configResource = entry.getKey();
                    KafkaFutureImpl<Config> future = entry.getValue();
                    DescribeConfigsResponse.Config config = response.config(configResourceToResource(configResource));
                    if (config == null) {
                        future.completeExceptionally(new UnknownServerException("Malformed broker response: missing config for " + configResource));
                        continue;
                    }
                    if (config.error().isFailure()) {
                        future.completeExceptionally(config.error().exception());
                        continue;
                    }
                    List<ConfigEntry> configEntries = new ArrayList<>();
                    for (DescribeConfigsResponse.ConfigEntry configEntry : config.entries()) {
                        configEntries.add(new ConfigEntry(configEntry.name(), configEntry.value(), configSource(configEntry.source()), configEntry.isSensitive(), configEntry.isReadOnly(), configSynonyms(configEntry)));
                    }
                    future.complete(new Config(configEntries));
                }
            }

            @Override
            void handleFailure(Throwable throwable) {
                completeAllExceptionally(unifiedRequestFutures.values(), throwable);
            }
        }, now);
    }
    for (Map.Entry<ConfigResource, KafkaFutureImpl<Config>> entry : brokerFutures.entrySet()) {
        final KafkaFutureImpl<Config> brokerFuture = entry.getValue();
        final Resource resource = configResourceToResource(entry.getKey());
        final int nodeId = Integer.parseInt(resource.name());
        runnable.call(new Call("describeBrokerConfigs", calcDeadlineMs(now, options.timeoutMs()), new ConstantNodeIdProvider(nodeId)) {

            @Override
            AbstractRequest.Builder createRequest(int timeoutMs) {
                return new DescribeConfigsRequest.Builder(Collections.singleton(resource)).includeSynonyms(options.includeSynonyms());
            }

            @Override
            void handleResponse(AbstractResponse abstractResponse) {
                DescribeConfigsResponse response = (DescribeConfigsResponse) abstractResponse;
                DescribeConfigsResponse.Config config = response.configs().get(resource);
                if (config == null) {
                    brokerFuture.completeExceptionally(new UnknownServerException("Malformed broker response: missing config for " + resource));
                    return;
                }
                if (config.error().isFailure())
                    brokerFuture.completeExceptionally(config.error().exception());
                else {
                    List<ConfigEntry> configEntries = new ArrayList<>();
                    for (DescribeConfigsResponse.ConfigEntry configEntry : config.entries()) {
                        configEntries.add(new ConfigEntry(configEntry.name(), configEntry.value(), configSource(configEntry.source()), configEntry.isSensitive(), configEntry.isReadOnly(), configSynonyms(configEntry)));
                    }
                    brokerFuture.complete(new Config(configEntries));
                }
            }

            @Override
            void handleFailure(Throwable throwable) {
                brokerFuture.completeExceptionally(throwable);
            }
        }, now);
    }
    final Map<ConfigResource, KafkaFuture<Config>> allFutures = new HashMap<>();
    allFutures.putAll(brokerFutures);
    allFutures.putAll(unifiedRequestFutures);
    return new DescribeConfigsResult(allFutures);
}
Also used : HashMap(java.util.HashMap) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) KafkaFuture(org.apache.kafka.common.KafkaFuture) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ConfigResource(org.apache.kafka.common.config.ConfigResource) Resource(org.apache.kafka.common.requests.Resource) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) ConfigResource(org.apache.kafka.common.config.ConfigResource) Map(java.util.Map) HashMap(java.util.HashMap) DescribeConfigsResponse(org.apache.kafka.common.requests.DescribeConfigsResponse)

Example 2 with Resource

use of org.apache.kafka.common.requests.Resource in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method alterConfigs.

private Map<ConfigResource, KafkaFutureImpl<Void>> alterConfigs(Map<ConfigResource, Config> configs, final AlterConfigsOptions options, Collection<ConfigResource> resources, NodeProvider nodeProvider) {
    final Map<ConfigResource, KafkaFutureImpl<Void>> futures = new HashMap<>();
    final Map<Resource, AlterConfigsRequest.Config> requestMap = new HashMap<>(resources.size());
    for (ConfigResource resource : resources) {
        List<AlterConfigsRequest.ConfigEntry> configEntries = new ArrayList<>();
        for (ConfigEntry configEntry : configs.get(resource).entries()) configEntries.add(new AlterConfigsRequest.ConfigEntry(configEntry.name(), configEntry.value()));
        requestMap.put(configResourceToResource(resource), new AlterConfigsRequest.Config(configEntries));
        futures.put(resource, new KafkaFutureImpl<Void>());
    }
    final long now = time.milliseconds();
    runnable.call(new Call("alterConfigs", calcDeadlineMs(now, options.timeoutMs()), nodeProvider) {

        @Override
        public AbstractRequest.Builder createRequest(int timeoutMs) {
            return new AlterConfigsRequest.Builder(requestMap, options.shouldValidateOnly());
        }

        @Override
        public void handleResponse(AbstractResponse abstractResponse) {
            AlterConfigsResponse response = (AlterConfigsResponse) abstractResponse;
            for (Map.Entry<ConfigResource, KafkaFutureImpl<Void>> entry : futures.entrySet()) {
                KafkaFutureImpl<Void> future = entry.getValue();
                ApiException exception = response.errors().get(configResourceToResource(entry.getKey())).exception();
                if (exception != null) {
                    future.completeExceptionally(exception);
                } else {
                    future.complete(null);
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return futures;
}
Also used : HashMap(java.util.HashMap) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) AlterConfigsRequest(org.apache.kafka.common.requests.AlterConfigsRequest) AlterConfigsResponse(org.apache.kafka.common.requests.AlterConfigsResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ConfigResource(org.apache.kafka.common.config.ConfigResource) Resource(org.apache.kafka.common.requests.Resource) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ConfigResource(org.apache.kafka.common.config.ConfigResource) ApiException(org.apache.kafka.common.errors.ApiException)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConfigResource (org.apache.kafka.common.config.ConfigResource)2 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)2 MetricConfig (org.apache.kafka.common.metrics.MetricConfig)2 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)2 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)2 Resource (org.apache.kafka.common.requests.Resource)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 ApiException (org.apache.kafka.common.errors.ApiException)1 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)1 AlterConfigsRequest (org.apache.kafka.common.requests.AlterConfigsRequest)1 AlterConfigsResponse (org.apache.kafka.common.requests.AlterConfigsResponse)1 DescribeConfigsResponse (org.apache.kafka.common.requests.DescribeConfigsResponse)1