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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations