Search in sources :

Example 21 with KafkaFutureImpl

use of org.apache.kafka.common.internals.KafkaFutureImpl 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)

Example 22 with KafkaFutureImpl

use of org.apache.kafka.common.internals.KafkaFutureImpl in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method describeLogDirs.

@Override
public DescribeLogDirsResult describeLogDirs(Collection<Integer> brokers, DescribeLogDirsOptions options) {
    final Map<Integer, KafkaFutureImpl<Map<String, DescribeLogDirsResponse.LogDirInfo>>> futures = new HashMap<>(brokers.size());
    for (Integer brokerId : brokers) {
        futures.put(brokerId, new KafkaFutureImpl<Map<String, DescribeLogDirsResponse.LogDirInfo>>());
    }
    final long now = time.milliseconds();
    for (final Integer brokerId : brokers) {
        runnable.call(new Call("describeLogDirs", calcDeadlineMs(now, options.timeoutMs()), new ConstantNodeIdProvider(brokerId)) {

            @Override
            public AbstractRequest.Builder createRequest(int timeoutMs) {
                // Query selected partitions in all log directories
                return new DescribeLogDirsRequest.Builder(null);
            }

            @Override
            public void handleResponse(AbstractResponse abstractResponse) {
                DescribeLogDirsResponse response = (DescribeLogDirsResponse) abstractResponse;
                KafkaFutureImpl<Map<String, DescribeLogDirsResponse.LogDirInfo>> future = futures.get(brokerId);
                if (response.logDirInfos().size() > 0) {
                    future.complete(response.logDirInfos());
                } else {
                    // response.logDirInfos() will be empty if and only if the user is not authorized to describe clsuter resource.
                    future.completeExceptionally(Errors.CLUSTER_AUTHORIZATION_FAILED.exception());
                }
            }

            @Override
            void handleFailure(Throwable throwable) {
                completeAllExceptionally(futures.values(), throwable);
            }
        }, now);
    }
    return new DescribeLogDirsResult(new HashMap<Integer, KafkaFuture<Map<String, DescribeLogDirsResponse.LogDirInfo>>>(futures));
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ReplicaLogDirInfo(org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DescribeLogDirsRequest(org.apache.kafka.common.requests.DescribeLogDirsRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)22 KafkaFuture (org.apache.kafka.common.KafkaFuture)18 HashMap (java.util.HashMap)17 Map (java.util.Map)11 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)11 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)10 ArrayList (java.util.ArrayList)9 TopicPartition (org.apache.kafka.common.TopicPartition)6 TimeoutException (org.apache.kafka.common.errors.TimeoutException)5 ConfigResource (org.apache.kafka.common.config.ConfigResource)4 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DeleteRecordsResult (org.apache.kafka.clients.admin.DeleteRecordsResult)3 DeletedRecords (org.apache.kafka.clients.admin.DeletedRecords)3 RecordsToDelete (org.apache.kafka.clients.admin.RecordsToDelete)3 Node (org.apache.kafka.common.Node)3 ApiException (org.apache.kafka.common.errors.ApiException)3 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)3