Search in sources :

Example 1 with CreatePartitionsResponse

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

the class KafkaAdminClientTest method testCreatePartitions.

@Test
public void testCreatePartitions() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareMetadataUpdate(env.cluster(), Collections.<String>emptySet());
        env.kafkaClient().setNode(env.cluster().controller());
        Map<String, ApiError> m = new HashMap<>();
        m.put("my_topic", ApiError.NONE);
        m.put("other_topic", ApiError.fromThrowable(new InvalidTopicException("some detailed reason")));
        // Test a call where one filter has an error.
        env.kafkaClient().prepareResponse(new CreatePartitionsResponse(0, m));
        Map<String, NewPartitions> counts = new HashMap<>();
        counts.put("my_topic", NewPartitions.increaseTo(3));
        counts.put("other_topic", NewPartitions.increaseTo(3, asList(asList(2), asList(3))));
        CreatePartitionsResult results = env.adminClient().createPartitions(counts);
        Map<String, KafkaFuture<Void>> values = results.values();
        KafkaFuture<Void> myTopicResult = values.get("my_topic");
        myTopicResult.get();
        KafkaFuture<Void> otherTopicResult = values.get("other_topic");
        try {
            otherTopicResult.get();
            fail("get() should throw ExecutionException");
        } catch (ExecutionException e0) {
            assertTrue(e0.getCause() instanceof InvalidTopicException);
            InvalidTopicException e = (InvalidTopicException) e0.getCause();
            assertEquals("some detailed reason", e.getMessage());
        }
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) CreatePartitionsResponse(org.apache.kafka.common.requests.CreatePartitionsResponse) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) ApiError(org.apache.kafka.common.requests.ApiError) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with CreatePartitionsResponse

use of org.apache.kafka.common.requests.CreatePartitionsResponse 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

HashMap (java.util.HashMap)2 ApiError (org.apache.kafka.common.requests.ApiError)2 CreatePartitionsResponse (org.apache.kafka.common.requests.CreatePartitionsResponse)2 ExecutionException (java.util.concurrent.ExecutionException)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 ThrottlingQuotaExceededException (org.apache.kafka.common.errors.ThrottlingQuotaExceededException)1 CreatePartitionsRequestData (org.apache.kafka.common.message.CreatePartitionsRequestData)1 CreatePartitionsTopicCollection (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection)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 CreatePartitionsRequest (org.apache.kafka.common.requests.CreatePartitionsRequest)1 Test (org.junit.Test)1