Search in sources :

Example 1 with DeleteTopicsResponse

use of org.apache.kafka.common.requests.DeleteTopicsResponse in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method deleteTopics.

@Override
public DeleteTopicsResult deleteTopics(final Collection<String> topicNames, DeleteTopicsOptions options) {
    final Map<String, KafkaFutureImpl<Void>> topicFutures = new HashMap<>(topicNames.size());
    for (String topicName : topicNames) {
        if (topicNameIsUnrepresentable(topicName)) {
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic name '" + topicName + "' cannot be represented in a request."));
            topicFutures.put(topicName, future);
        } else if (!topicFutures.containsKey(topicName)) {
            topicFutures.put(topicName, new KafkaFutureImpl<Void>());
        }
    }
    final long now = time.milliseconds();
    Call call = new Call("deleteTopics", calcDeadlineMs(now, options.timeoutMs()), new ControllerNodeProvider()) {

        @Override
        AbstractRequest.Builder createRequest(int timeoutMs) {
            return new DeleteTopicsRequest.Builder(new HashSet<>(topicNames), timeoutMs);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            DeleteTopicsResponse response = (DeleteTopicsResponse) abstractResponse;
            // Handle server responses for particular topics.
            for (Map.Entry<String, Errors> entry : response.errors().entrySet()) {
                KafkaFutureImpl<Void> future = topicFutures.get(entry.getKey());
                if (future == null) {
                    log.warn("Server response mentioned unknown topic {}", entry.getKey());
                } else {
                    ApiException exception = entry.getValue().exception();
                    if (exception != null) {
                        future.completeExceptionally(exception);
                    } else {
                        future.complete(null);
                    }
                }
            }
            // The server should send back a response for every topic. But do a sanity check anyway.
            for (Map.Entry<String, KafkaFutureImpl<Void>> entry : topicFutures.entrySet()) {
                KafkaFutureImpl<Void> future = entry.getValue();
                if (!future.isDone()) {
                    future.completeExceptionally(new ApiException("The server response did not " + "contain a reference to node " + entry.getKey()));
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(topicFutures.values(), throwable);
        }
    };
    if (!topicNames.isEmpty()) {
        runnable.call(call, now);
    }
    return new DeleteTopicsResult(new HashMap<String, KafkaFuture<Void>>(topicFutures));
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DeleteTopicsResponse(org.apache.kafka.common.requests.DeleteTopicsResponse) Errors(org.apache.kafka.common.protocol.Errors) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Map(java.util.Map) HashMap(java.util.HashMap) ApiException(org.apache.kafka.common.errors.ApiException)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 ApiException (org.apache.kafka.common.errors.ApiException)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 DeleteTopicsResponse (org.apache.kafka.common.requests.DeleteTopicsResponse)1