Search in sources :

Example 1 with DescribeLogDirsRequestData

use of org.apache.kafka.common.message.DescribeLogDirsRequestData in project kafka by apache.

the class KafkaAdminClient method describeReplicaLogDirs.

@Override
public DescribeReplicaLogDirsResult describeReplicaLogDirs(Collection<TopicPartitionReplica> replicas, DescribeReplicaLogDirsOptions options) {
    final Map<TopicPartitionReplica, KafkaFutureImpl<DescribeReplicaLogDirsResult.ReplicaLogDirInfo>> futures = new HashMap<>(replicas.size());
    for (TopicPartitionReplica replica : replicas) {
        futures.put(replica, new KafkaFutureImpl<>());
    }
    Map<Integer, DescribeLogDirsRequestData> partitionsByBroker = new HashMap<>();
    for (TopicPartitionReplica replica : replicas) {
        DescribeLogDirsRequestData requestData = partitionsByBroker.computeIfAbsent(replica.brokerId(), brokerId -> new DescribeLogDirsRequestData());
        DescribableLogDirTopic describableLogDirTopic = requestData.topics().find(replica.topic());
        if (describableLogDirTopic == null) {
            List<Integer> partitions = new ArrayList<>();
            partitions.add(replica.partition());
            describableLogDirTopic = new DescribableLogDirTopic().setTopic(replica.topic()).setPartitions(partitions);
            requestData.topics().add(describableLogDirTopic);
        } else {
            describableLogDirTopic.partitions().add(replica.partition());
        }
    }
    final long now = time.milliseconds();
    for (Map.Entry<Integer, DescribeLogDirsRequestData> entry : partitionsByBroker.entrySet()) {
        final int brokerId = entry.getKey();
        final DescribeLogDirsRequestData topicPartitions = entry.getValue();
        final Map<TopicPartition, ReplicaLogDirInfo> replicaDirInfoByPartition = new HashMap<>();
        for (DescribableLogDirTopic topicPartition : topicPartitions.topics()) {
            for (Integer partitionId : topicPartition.partitions()) {
                replicaDirInfoByPartition.put(new TopicPartition(topicPartition.topic(), partitionId), new ReplicaLogDirInfo());
            }
        }
        runnable.call(new Call("describeReplicaLogDirs", calcDeadlineMs(now, options.timeoutMs()), new ConstantNodeIdProvider(brokerId)) {

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

            @Override
            public void handleResponse(AbstractResponse abstractResponse) {
                DescribeLogDirsResponse response = (DescribeLogDirsResponse) abstractResponse;
                for (Map.Entry<String, LogDirDescription> responseEntry : logDirDescriptions(response).entrySet()) {
                    String logDir = responseEntry.getKey();
                    LogDirDescription logDirInfo = responseEntry.getValue();
                    // No replica info will be provided if the log directory is offline
                    if (logDirInfo.error() instanceof KafkaStorageException)
                        continue;
                    if (logDirInfo.error() != null)
                        handleFailure(new IllegalStateException("The error " + logDirInfo.error().getClass().getName() + " for log directory " + logDir + " in the response from broker " + brokerId + " is illegal"));
                    for (Map.Entry<TopicPartition, ReplicaInfo> replicaInfoEntry : logDirInfo.replicaInfos().entrySet()) {
                        TopicPartition tp = replicaInfoEntry.getKey();
                        ReplicaInfo replicaInfo = replicaInfoEntry.getValue();
                        ReplicaLogDirInfo replicaLogDirInfo = replicaDirInfoByPartition.get(tp);
                        if (replicaLogDirInfo == null) {
                            log.warn("Server response from broker {} mentioned unknown partition {}", brokerId, tp);
                        } else if (replicaInfo.isFuture()) {
                            replicaDirInfoByPartition.put(tp, new ReplicaLogDirInfo(replicaLogDirInfo.getCurrentReplicaLogDir(), replicaLogDirInfo.getCurrentReplicaOffsetLag(), logDir, replicaInfo.offsetLag()));
                        } else {
                            replicaDirInfoByPartition.put(tp, new ReplicaLogDirInfo(logDir, replicaInfo.offsetLag(), replicaLogDirInfo.getFutureReplicaLogDir(), replicaLogDirInfo.getFutureReplicaOffsetLag()));
                        }
                    }
                }
                for (Map.Entry<TopicPartition, ReplicaLogDirInfo> entry : replicaDirInfoByPartition.entrySet()) {
                    TopicPartition tp = entry.getKey();
                    KafkaFutureImpl<ReplicaLogDirInfo> future = futures.get(new TopicPartitionReplica(tp.topic(), tp.partition(), brokerId));
                    future.complete(entry.getValue());
                }
            }

            @Override
            void handleFailure(Throwable throwable) {
                completeAllExceptionally(futures.values(), throwable);
            }
        }, now);
    }
    return new DescribeReplicaLogDirsResult(new HashMap<>(futures));
}
Also used : ReplicaLogDirInfo(org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo) HashMap(java.util.HashMap) DescribeLogDirsRequestData(org.apache.kafka.common.message.DescribeLogDirsRequestData) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) TopicPartitionReplica(org.apache.kafka.common.TopicPartitionReplica) KafkaStorageException(org.apache.kafka.common.errors.KafkaStorageException) DescribableLogDirTopic(org.apache.kafka.common.message.DescribeLogDirsRequestData.DescribableLogDirTopic) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicPartition(org.apache.kafka.common.TopicPartition) DescribeLogDirsRequest(org.apache.kafka.common.requests.DescribeLogDirsRequest) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 2 with DescribeLogDirsRequestData

use of org.apache.kafka.common.message.DescribeLogDirsRequestData in project kafka by apache.

the class KafkaAdminClient method describeLogDirs.

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

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

            @Override
            public void handleResponse(AbstractResponse abstractResponse) {
                DescribeLogDirsResponse response = (DescribeLogDirsResponse) abstractResponse;
                Map<String, LogDirDescription> descriptions = logDirDescriptions(response);
                if (descriptions.size() > 0) {
                    future.complete(descriptions);
                } else {
                    // Up to v3 DescribeLogDirsResponse did not have an error code field, hence it defaults to None
                    Errors error = response.data().errorCode() == Errors.NONE.code() ? Errors.CLUSTER_AUTHORIZATION_FAILED : Errors.forCode(response.data().errorCode());
                    future.completeExceptionally(error.exception());
                }
            }

            @Override
            void handleFailure(Throwable throwable) {
                future.completeExceptionally(throwable);
            }
        }, now);
    }
    return new DescribeLogDirsResult(new HashMap<>(futures));
}
Also used : HashMap(java.util.HashMap) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) DescribeLogDirsRequestData(org.apache.kafka.common.message.DescribeLogDirsRequestData) 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) Errors(org.apache.kafka.common.protocol.Errors) DescribeLogDirsRequest(org.apache.kafka.common.requests.DescribeLogDirsRequest) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 3 with DescribeLogDirsRequestData

use of org.apache.kafka.common.message.DescribeLogDirsRequestData in project kafka by apache.

the class RequestResponseTest method createDescribeLogDirsRequest.

private DescribeLogDirsRequest createDescribeLogDirsRequest(short version) {
    DescribeLogDirsRequestData.DescribableLogDirTopicCollection topics = new DescribeLogDirsRequestData.DescribableLogDirTopicCollection();
    topics.add(new DescribeLogDirsRequestData.DescribableLogDirTopic().setPartitions(asList(0, 1, 2)).setTopic("topic1"));
    DescribeLogDirsRequestData data = new DescribeLogDirsRequestData().setTopics(topics);
    return new DescribeLogDirsRequest.Builder(data).build(version);
}
Also used : DescribeLogDirsRequestData(org.apache.kafka.common.message.DescribeLogDirsRequestData)

Aggregations

DescribeLogDirsRequestData (org.apache.kafka.common.message.DescribeLogDirsRequestData)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)2 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)2 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)2 DescribeLogDirsRequest (org.apache.kafka.common.requests.DescribeLogDirsRequest)2 DescribeLogDirsResponse (org.apache.kafka.common.requests.DescribeLogDirsResponse)2 ArrayList (java.util.ArrayList)1 ReplicaLogDirInfo (org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 TopicPartitionReplica (org.apache.kafka.common.TopicPartitionReplica)1 KafkaStorageException (org.apache.kafka.common.errors.KafkaStorageException)1 DescribableLogDirTopic (org.apache.kafka.common.message.DescribeLogDirsRequestData.DescribableLogDirTopic)1 Errors (org.apache.kafka.common.protocol.Errors)1