Search in sources :

Example 1 with CreatePartitionsTopicCollection

use of org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection in project kafka by apache.

the class RequestResponseTest method createCreatePartitionsRequest.

private CreatePartitionsRequest createCreatePartitionsRequest(short version) {
    CreatePartitionsTopicCollection topics = new CreatePartitionsTopicCollection();
    topics.add(new CreatePartitionsTopic().setName("my_topic").setCount(3));
    topics.add(new CreatePartitionsTopic().setName("my_other_topic").setCount(3));
    CreatePartitionsRequestData data = new CreatePartitionsRequestData().setTimeoutMs(0).setValidateOnly(false).setTopics(topics);
    return new CreatePartitionsRequest(data, version);
}
Also used : CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) CreatePartitionsRequestData(org.apache.kafka.common.message.CreatePartitionsRequestData) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic)

Example 2 with CreatePartitionsTopicCollection

use of org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection in project kafka by apache.

the class RequestResponseTest method createCreatePartitionsRequestWithAssignments.

private CreatePartitionsRequest createCreatePartitionsRequestWithAssignments(short version) {
    CreatePartitionsTopicCollection topics = new CreatePartitionsTopicCollection();
    CreatePartitionsAssignment myTopicAssignment = new CreatePartitionsAssignment().setBrokerIds(singletonList(2));
    topics.add(new CreatePartitionsTopic().setName("my_topic").setCount(3).setAssignments(singletonList(myTopicAssignment)));
    topics.add(new CreatePartitionsTopic().setName("my_other_topic").setCount(3).setAssignments(asList(new CreatePartitionsAssignment().setBrokerIds(asList(2, 3)), new CreatePartitionsAssignment().setBrokerIds(asList(3, 1)))));
    CreatePartitionsRequestData data = new CreatePartitionsRequestData().setTimeoutMs(0).setValidateOnly(false).setTopics(topics);
    return new CreatePartitionsRequest(data, version);
}
Also used : CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) CreatePartitionsAssignment(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsAssignment) CreatePartitionsRequestData(org.apache.kafka.common.message.CreatePartitionsRequestData) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic)

Example 3 with CreatePartitionsTopicCollection

use of org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection in project kafka by apache.

the class KafkaAdminClient method createPartitions.

@Override
public CreatePartitionsResult createPartitions(final Map<String, NewPartitions> newPartitions, final CreatePartitionsOptions options) {
    final Map<String, KafkaFutureImpl<Void>> futures = new HashMap<>(newPartitions.size());
    final CreatePartitionsTopicCollection topics = new CreatePartitionsTopicCollection(newPartitions.size());
    for (Map.Entry<String, NewPartitions> entry : newPartitions.entrySet()) {
        final String topic = entry.getKey();
        final NewPartitions newPartition = entry.getValue();
        List<List<Integer>> newAssignments = newPartition.assignments();
        List<CreatePartitionsAssignment> assignments = newAssignments == null ? null : newAssignments.stream().map(brokerIds -> new CreatePartitionsAssignment().setBrokerIds(brokerIds)).collect(Collectors.toList());
        topics.add(new CreatePartitionsTopic().setName(topic).setCount(newPartition.totalCount()).setAssignments(assignments));
        futures.put(topic, new KafkaFutureImpl<>());
    }
    if (!topics.isEmpty()) {
        final long now = time.milliseconds();
        final long deadline = calcDeadlineMs(now, options.timeoutMs());
        final Call call = getCreatePartitionsCall(options, futures, topics, Collections.emptyMap(), now, deadline);
        runnable.call(call, now);
    }
    return new CreatePartitionsResult(new HashMap<>(futures));
}
Also used : CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) HashMap(java.util.HashMap) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) CreatePartitionsAssignment(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsAssignment) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic)

Example 4 with CreatePartitionsTopicCollection

use of org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection in project kafka by apache.

the class KafkaAdminClient method getCreatePartitionsCall.

private Call getCreatePartitionsCall(final CreatePartitionsOptions options, final Map<String, KafkaFutureImpl<Void>> futures, final CreatePartitionsTopicCollection topics, final Map<String, ThrottlingQuotaExceededException> quotaExceededExceptions, final long now, final long deadline) {
    return new Call("createPartitions", deadline, new ControllerNodeProvider()) {

        @Override
        public CreatePartitionsRequest.Builder createRequest(int timeoutMs) {
            return new CreatePartitionsRequest.Builder(new CreatePartitionsRequestData().setTopics(topics).setValidateOnly(options.validateOnly()).setTimeoutMs(timeoutMs));
        }

        @Override
        public void handleResponse(AbstractResponse abstractResponse) {
            // Check for controller change
            handleNotControllerError(abstractResponse);
            // Handle server responses for particular topics.
            final CreatePartitionsResponse response = (CreatePartitionsResponse) abstractResponse;
            final CreatePartitionsTopicCollection retryTopics = new CreatePartitionsTopicCollection();
            final Map<String, ThrottlingQuotaExceededException> retryTopicQuotaExceededExceptions = new HashMap<>();
            for (CreatePartitionsTopicResult result : response.data().results()) {
                KafkaFutureImpl<Void> future = futures.get(result.name());
                if (future == null) {
                    log.warn("Server response mentioned unknown topic {}", result.name());
                } else {
                    ApiError error = new ApiError(result.errorCode(), result.errorMessage());
                    if (error.isFailure()) {
                        if (error.is(Errors.THROTTLING_QUOTA_EXCEEDED)) {
                            ThrottlingQuotaExceededException quotaExceededException = new ThrottlingQuotaExceededException(response.throttleTimeMs(), error.messageWithFallback());
                            if (options.shouldRetryOnQuotaViolation()) {
                                retryTopics.add(topics.find(result.name()).duplicate());
                                retryTopicQuotaExceededExceptions.put(result.name(), quotaExceededException);
                            } else {
                                future.completeExceptionally(quotaExceededException);
                            }
                        } else {
                            future.completeExceptionally(error.exception());
                        }
                    } else {
                        future.complete(null);
                    }
                }
            }
            // If there are topics to retry, retry them; complete unrealized futures otherwise.
            if (retryTopics.isEmpty()) {
                // The server should send back a response for every topic. But do a sanity check anyway.
                completeUnrealizedFutures(futures.entrySet().stream(), topic -> "The controller response did not contain a result for topic " + topic);
            } else {
                final long now = time.milliseconds();
                final Call call = getCreatePartitionsCall(options, futures, retryTopics, retryTopicQuotaExceededExceptions, now, deadline);
                runnable.call(call, now);
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            // If there were any topics retries due to a quota exceeded exception, we propagate
            // the initial error back to the caller if the request timed out.
            maybeCompleteQuotaExceededException(options.shouldRetryOnQuotaViolation(), throwable, futures, quotaExceededExceptions, (int) (time.milliseconds() - now));
            // Fail all the other remaining futures
            completeAllExceptionally(futures.values(), throwable);
        }
    };
}
Also used : CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) ThrottlingQuotaExceededException(org.apache.kafka.common.errors.ThrottlingQuotaExceededException) CreatePartitionsResponse(org.apache.kafka.common.requests.CreatePartitionsResponse) CreatePartitionsRequest(org.apache.kafka.common.requests.CreatePartitionsRequest) CreatePartitionsRequestData(org.apache.kafka.common.message.CreatePartitionsRequestData) ApiError(org.apache.kafka.common.requests.ApiError)

Aggregations

CreatePartitionsTopicCollection (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection)4 CreatePartitionsRequestData (org.apache.kafka.common.message.CreatePartitionsRequestData)3 CreatePartitionsTopic (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic)3 HashMap (java.util.HashMap)2 CreatePartitionsAssignment (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsAssignment)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ThrottlingQuotaExceededException (org.apache.kafka.common.errors.ThrottlingQuotaExceededException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 CreatePartitionsTopicResult (org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiError (org.apache.kafka.common.requests.ApiError)1 CreatePartitionsRequest (org.apache.kafka.common.requests.CreatePartitionsRequest)1 CreatePartitionsResponse (org.apache.kafka.common.requests.CreatePartitionsResponse)1