Search in sources :

Example 16 with KafkaFutureImpl

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

the class MockAdminClient method createTopics.

@Override
public CreateTopicsResult createTopics(Collection<NewTopic> newTopics, CreateTopicsOptions options) {
    Map<String, KafkaFuture<Void>> createTopicResult = new HashMap<>();
    if (timeoutNextRequests > 0) {
        for (final NewTopic newTopic : newTopics) {
            String topicName = newTopic.name();
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            createTopicResult.put(topicName, future);
        }
        --timeoutNextRequests;
        return new CreateTopicsResult(createTopicResult);
    }
    for (final NewTopic newTopic : newTopics) {
        KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
        String topicName = newTopic.name();
        if (allTopics.containsKey(topicName)) {
            future.completeExceptionally(new TopicExistsException(String.format("Topic %s exists already.", topicName)));
            createTopicResult.put(topicName, future);
            continue;
        }
        int replicationFactor = newTopic.replicationFactor();
        List<Node> replicas = new ArrayList<>(replicationFactor);
        for (int i = 0; i < replicationFactor; ++i) {
            replicas.add(brokers.get(i));
        }
        int numberOfPartitions = newTopic.numPartitions();
        List<TopicPartitionInfo> partitions = new ArrayList<>(numberOfPartitions);
        for (int p = 0; p < numberOfPartitions; ++p) {
            partitions.add(new TopicPartitionInfo(p, brokers.get(0), replicas, Collections.<Node>emptyList()));
        }
        allTopics.put(topicName, new TopicMetadata(false, partitions, newTopic.configs()));
        future.complete(null);
        createTopicResult.put(topicName, future);
    }
    return new CreateTopicsResult(createTopicResult);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 17 with KafkaFutureImpl

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

the class MockAdminClient method describeTopics.

@Override
public DescribeTopicsResult describeTopics(Collection<String> topicNames, DescribeTopicsOptions options) {
    Map<String, KafkaFuture<TopicDescription>> topicDescriptions = new HashMap<>();
    if (timeoutNextRequests > 0) {
        for (String requestedTopic : topicNames) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            topicDescriptions.put(requestedTopic, future);
        }
        --timeoutNextRequests;
        return new DescribeTopicsResult(topicDescriptions);
    }
    for (String requestedTopic : topicNames) {
        for (Map.Entry<String, TopicMetadata> topicDescription : allTopics.entrySet()) {
            String topicName = topicDescription.getKey();
            if (topicName.equals(requestedTopic)) {
                TopicMetadata topicMetadata = topicDescription.getValue();
                KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
                future.complete(new TopicDescription(topicName, topicMetadata.isInternalTopic, topicMetadata.partitions));
                topicDescriptions.put(topicName, future);
                break;
            }
        }
        if (!topicDescriptions.containsKey(requestedTopic)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new UnknownTopicOrPartitionException(String.format("Topic %s unknown.", requestedTopic)));
            topicDescriptions.put(requestedTopic, future);
        }
    }
    return new DescribeTopicsResult(topicDescriptions);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 18 with KafkaFutureImpl

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

the class MockAdminClient method describeCluster.

@Override
public DescribeClusterResult describeCluster(DescribeClusterOptions options) {
    KafkaFutureImpl<Collection<Node>> nodesFuture = new KafkaFutureImpl<>();
    KafkaFutureImpl<Node> controllerFuture = new KafkaFutureImpl<>();
    KafkaFutureImpl<String> brokerIdFuture = new KafkaFutureImpl<>();
    if (timeoutNextRequests > 0) {
        nodesFuture.completeExceptionally(new TimeoutException());
        controllerFuture.completeExceptionally(new TimeoutException());
        brokerIdFuture.completeExceptionally(new TimeoutException());
        --timeoutNextRequests;
    } else {
        nodesFuture.complete(brokers);
        controllerFuture.complete(controller);
        brokerIdFuture.complete(clusterId);
    }
    return new DescribeClusterResult(nodesFuture, controllerFuture, brokerIdFuture);
}
Also used : Node(org.apache.kafka.common.Node) Collection(java.util.Collection) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 19 with KafkaFutureImpl

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

the class WorkerManager method createWorker.

public void createWorker(final String id, TaskSpec spec) throws Exception {
    try (ShutdownManager.Reference ref = shutdownManager.takeReference()) {
        final Worker worker = stateChangeExecutor.submit(new CreateWorker(id, spec, time.milliseconds())).get();
        if (worker == null) {
            log.info("{}: Ignoring request to create worker {}, because there is already " + "a worker with that id.", nodeName, id);
            return;
        }
        KafkaFutureImpl<String> haltFuture = new KafkaFutureImpl<>();
        haltFuture.thenApply(new KafkaFuture.BaseFunction<String, Void>() {

            @Override
            public Void apply(String errorString) {
                if (errorString.isEmpty()) {
                    log.info("{}: Worker {} is halting.", nodeName, id);
                } else {
                    log.info("{}: Worker {} is halting with error {}", nodeName, id, errorString);
                }
                stateChangeExecutor.submit(new HandleWorkerHalting(worker, errorString, false));
                return null;
            }
        });
        try {
            worker.taskWorker.start(platform, worker.status, haltFuture);
        } catch (Exception e) {
            stateChangeExecutor.submit(new HandleWorkerHalting(worker, "worker.start() exception: " + e.getMessage(), true));
        }
        stateChangeExecutor.submit(new FinishCreatingWorker(worker));
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) KafkaException(org.apache.kafka.common.KafkaException) TaskWorker(org.apache.kafka.trogdor.task.TaskWorker)

Example 20 with KafkaFutureImpl

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

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<DescribeReplicaLogDirsResult.ReplicaLogDirInfo>());
    }
    Map<Integer, Set<TopicPartition>> partitionsByBroker = new HashMap<>();
    for (TopicPartitionReplica replica : replicas) {
        if (!partitionsByBroker.containsKey(replica.brokerId()))
            partitionsByBroker.put(replica.brokerId(), new HashSet<TopicPartition>());
        partitionsByBroker.get(replica.brokerId()).add(new TopicPartition(replica.topic(), replica.partition()));
    }
    final long now = time.milliseconds();
    for (Map.Entry<Integer, Set<TopicPartition>> entry : partitionsByBroker.entrySet()) {
        final int brokerId = entry.getKey();
        final Set<TopicPartition> topicPartitions = entry.getValue();
        final Map<TopicPartition, ReplicaLogDirInfo> replicaDirInfoByPartition = new HashMap<>();
        for (TopicPartition topicPartition : topicPartitions) replicaDirInfoByPartition.put(topicPartition, new ReplicaLogDirInfo());
        runnable.call(new Call("describeReplicaLogDirs", 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(topicPartitions);
            }

            @Override
            public void handleResponse(AbstractResponse abstractResponse) {
                DescribeLogDirsResponse response = (DescribeLogDirsResponse) abstractResponse;
                for (Map.Entry<String, DescribeLogDirsResponse.LogDirInfo> responseEntry : response.logDirInfos().entrySet()) {
                    String logDir = responseEntry.getKey();
                    DescribeLogDirsResponse.LogDirInfo logDirInfo = responseEntry.getValue();
                    // No replica info will be provided if the log directory is offline
                    if (logDirInfo.error == Errors.KAFKA_STORAGE_ERROR)
                        continue;
                    if (logDirInfo.error != Errors.NONE)
                        handleFailure(new IllegalStateException("The error " + logDirInfo.error + " for log directory " + logDir + " in the response from broker " + brokerId + " is illegal"));
                    for (Map.Entry<TopicPartition, DescribeLogDirsResponse.ReplicaInfo> replicaInfoEntry : logDirInfo.replicaInfos.entrySet()) {
                        TopicPartition tp = replicaInfoEntry.getKey();
                        DescribeLogDirsResponse.ReplicaInfo replicaInfo = replicaInfoEntry.getValue();
                        ReplicaLogDirInfo replicaLogDirInfo = replicaDirInfoByPartition.get(tp);
                        if (replicaLogDirInfo == null) {
                            handleFailure(new IllegalStateException("The partition " + tp + " in the response from broker " + brokerId + " is not in the request"));
                        } 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<TopicPartitionReplica, KafkaFuture<ReplicaLogDirInfo>>(futures));
}
Also used : ReplicaLogDirInfo(org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ReplicaLogDirInfo(org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo) TopicPartitionReplica(org.apache.kafka.common.TopicPartitionReplica) HashSet(java.util.HashSet) KafkaFuture(org.apache.kafka.common.KafkaFuture) 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) 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