Search in sources :

Example 71 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class EmbeddedKafkaCluster method describeTopics.

/**
 * Get the topic descriptions of the named topics. The value of the map entry will be empty
 * if the topic does not exist.
 *
 * @param topicNames the names of the topics to describe
 * @return the map of optional {@link TopicDescription} keyed by the topic name
 */
public Map<String, Optional<TopicDescription>> describeTopics(Set<String> topicNames) {
    Map<String, Optional<TopicDescription>> results = new HashMap<>();
    log.info("Describing topics {}", topicNames);
    try (Admin admin = createAdminClient()) {
        DescribeTopicsResult result = admin.describeTopics(topicNames);
        Map<String, KafkaFuture<TopicDescription>> byName = result.topicNameValues();
        for (Map.Entry<String, KafkaFuture<TopicDescription>> entry : byName.entrySet()) {
            String topicName = entry.getKey();
            try {
                TopicDescription desc = entry.getValue().get();
                results.put(topicName, Optional.of(desc));
                log.info("Found topic {} : {}", topicName, desc);
            } catch (ExecutionException e) {
                Throwable cause = e.getCause();
                if (cause instanceof UnknownTopicOrPartitionException) {
                    results.put(topicName, Optional.empty());
                    log.info("Found non-existant topic {}", topicName);
                    continue;
                }
                throw new AssertionError("Could not describe topic(s)" + topicNames, e);
            }
        }
    } catch (Exception e) {
        throw new AssertionError("Could not describe topic(s) " + topicNames, e);
    }
    log.info("Found topics {}", results);
    return results;
}
Also used : Optional(java.util.Optional) KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Admin(org.apache.kafka.clients.admin.Admin) KafkaException(org.apache.kafka.common.KafkaException) InvalidReplicationFactorException(org.apache.kafka.common.errors.InvalidReplicationFactorException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 72 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method describeConfigs.

@Override
public synchronized DescribeConfigsResult describeConfigs(Collection<ConfigResource> resources, DescribeConfigsOptions options) {
    if (timeoutNextRequests > 0) {
        Map<ConfigResource, KafkaFuture<Config>> configs = new HashMap<>();
        for (ConfigResource requestedResource : resources) {
            KafkaFutureImpl<Config> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            configs.put(requestedResource, future);
        }
        --timeoutNextRequests;
        return new DescribeConfigsResult(configs);
    }
    Map<ConfigResource, KafkaFuture<Config>> results = new HashMap<>();
    for (ConfigResource resource : resources) {
        KafkaFutureImpl<Config> future = new KafkaFutureImpl<>();
        results.put(resource, future);
        try {
            future.complete(getResourceDescription(resource));
        } catch (Throwable e) {
            future.completeExceptionally(e);
        }
    }
    return new DescribeConfigsResult(results);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ConfigResource(org.apache.kafka.common.config.ConfigResource) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 73 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method listOffsets.

@Override
public synchronized ListOffsetsResult listOffsets(Map<TopicPartition, OffsetSpec> topicPartitionOffsets, ListOffsetsOptions options) {
    Map<TopicPartition, KafkaFuture<ListOffsetsResult.ListOffsetsResultInfo>> futures = new HashMap<>();
    for (Map.Entry<TopicPartition, OffsetSpec> entry : topicPartitionOffsets.entrySet()) {
        TopicPartition tp = entry.getKey();
        OffsetSpec spec = entry.getValue();
        KafkaFutureImpl<ListOffsetsResult.ListOffsetsResultInfo> future = new KafkaFutureImpl<>();
        if (spec instanceof OffsetSpec.TimestampSpec)
            throw new UnsupportedOperationException("Not implement yet");
        else if (spec instanceof OffsetSpec.EarliestSpec)
            future.complete(new ListOffsetsResult.ListOffsetsResultInfo(beginningOffsets.get(tp), -1, Optional.empty()));
        else
            future.complete(new ListOffsetsResult.ListOffsetsResultInfo(endOffsets.get(tp), -1, Optional.empty()));
        futures.put(tp, future);
    }
    return new ListOffsetsResult(futures);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 74 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method incrementalAlterConfigs.

@Override
public synchronized AlterConfigsResult incrementalAlterConfigs(Map<ConfigResource, Collection<AlterConfigOp>> configs, AlterConfigsOptions options) {
    Map<ConfigResource, KafkaFuture<Void>> futures = new HashMap<>();
    for (Map.Entry<ConfigResource, Collection<AlterConfigOp>> entry : configs.entrySet()) {
        ConfigResource resource = entry.getKey();
        KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
        futures.put(resource, future);
        Throwable throwable = handleIncrementalResourceAlteration(resource, entry.getValue());
        if (throwable == null) {
            future.complete(null);
        } else {
            future.completeExceptionally(throwable);
        }
    }
    return new AlterConfigsResult(futures);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) TopicCollection(org.apache.kafka.common.TopicCollection) TopicNameCollection(org.apache.kafka.common.TopicCollection.TopicNameCollection) TopicIdCollection(org.apache.kafka.common.TopicCollection.TopicIdCollection) Collection(java.util.Collection) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) HashMap(java.util.HashMap) Map(java.util.Map) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Example 75 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method alterReplicaLogDirs.

@Override
public synchronized AlterReplicaLogDirsResult alterReplicaLogDirs(Map<TopicPartitionReplica, String> replicaAssignment, AlterReplicaLogDirsOptions options) {
    Map<TopicPartitionReplica, KafkaFuture<Void>> results = new HashMap<>();
    for (Map.Entry<TopicPartitionReplica, String> entry : replicaAssignment.entrySet()) {
        TopicPartitionReplica replica = entry.getKey();
        String newLogDir = entry.getValue();
        KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
        results.put(replica, future);
        List<String> dirs = brokerLogDirs.get(replica.brokerId());
        if (dirs == null) {
            future.completeExceptionally(new ReplicaNotAvailableException("Can't find " + replica));
        } else if (!dirs.contains(newLogDir)) {
            future.completeExceptionally(new KafkaStorageException("Log directory " + newLogDir + " is offline"));
        } else {
            TopicMetadata metadata = allTopics.get(replica.topic());
            if (metadata == null || metadata.partitions.size() <= replica.partition()) {
                future.completeExceptionally(new ReplicaNotAvailableException("Can't find " + replica));
            } else {
                String currentLogDir = metadata.partitionLogDirs.get(replica.partition());
                replicaMoves.put(replica, new ReplicaLogDirInfo(currentLogDir, 0, newLogDir, 0));
                future.complete(null);
            }
        }
    }
    return new AlterReplicaLogDirsResult(results);
}
Also used : ReplicaLogDirInfo(org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo) KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) ReplicaNotAvailableException(org.apache.kafka.common.errors.ReplicaNotAvailableException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicPartitionReplica(org.apache.kafka.common.TopicPartitionReplica) HashMap(java.util.HashMap) Map(java.util.Map) KafkaStorageException(org.apache.kafka.common.errors.KafkaStorageException)

Aggregations

KafkaFuture (org.apache.kafka.common.KafkaFuture)84 HashMap (java.util.HashMap)59 Map (java.util.Map)43 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)31 ExecutionException (java.util.concurrent.ExecutionException)30 TimeoutException (org.apache.kafka.common.errors.TimeoutException)21 ArrayList (java.util.ArrayList)16 TopicPartition (org.apache.kafka.common.TopicPartition)16 ConfigResource (org.apache.kafka.common.config.ConfigResource)16 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)15 Test (org.junit.jupiter.api.Test)15 HashSet (java.util.HashSet)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Test (org.junit.Test)12 TopicPartitionReplica (org.apache.kafka.common.TopicPartitionReplica)10 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)10 NewTopic (org.apache.kafka.clients.admin.NewTopic)8 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)7 ReplicaLogDirInfo (org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult.ReplicaLogDirInfo)7