Search in sources :

Example 6 with TopicMetadataAndConfig

use of org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig in project kafka by apache.

the class InternalTopicManagerTest method shouldThrowWhenCreateTopicsThrowsUnexpectedException.

@Test
public void shouldThrowWhenCreateTopicsThrowsUnexpectedException() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final StreamsConfig streamsConfig = new StreamsConfig(config);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, streamsConfig);
    final InternalTopicConfig internalTopicConfig = setupRepartitionTopicConfig(topic1, 1);
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFailFuture = new KafkaFutureImpl<>();
    createTopicFailFuture.completeExceptionally(new IllegalStateException("Nobody expects the Spanish inquisition"));
    final NewTopic newTopic = newTopic(topic1, internalTopicConfig, streamsConfig);
    EasyMock.expect(admin.createTopics(mkSet(newTopic))).andStubAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic1, createTopicFailFuture))));
    EasyMock.replay(admin);
    assertThrows(StreamsException.class, () -> topicManager.setup(mkMap(mkEntry(topic1, internalTopicConfig))));
}
Also used : TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 7 with TopicMetadataAndConfig

use of org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig in project kafka by apache.

the class InternalTopicManagerTest method setupCleanUpScenario.

private void setupCleanUpScenario(final AdminClient admin, final StreamsConfig streamsConfig, final InternalTopicConfig internalTopicConfig1, final InternalTopicConfig internalTopicConfig2) {
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFailFuture1 = new KafkaFutureImpl<>();
    createTopicFailFuture1.completeExceptionally(new TopicExistsException("exists"));
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFailFuture2 = new KafkaFutureImpl<>();
    createTopicFailFuture2.completeExceptionally(new IllegalStateException("Nobody expects the Spanish inquisition"));
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicSuccessfulFuture = new KafkaFutureImpl<>();
    createTopicSuccessfulFuture.complete(new TopicMetadataAndConfig(Uuid.randomUuid(), 1, 1, new Config(Collections.emptyList())));
    final NewTopic newTopic1 = newTopic(topic1, internalTopicConfig1, streamsConfig);
    final NewTopic newTopic2 = newTopic(topic2, internalTopicConfig2, streamsConfig);
    EasyMock.expect(admin.createTopics(mkSet(newTopic1, newTopic2))).andAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic1, createTopicSuccessfulFuture), mkEntry(topic2, createTopicFailFuture1))));
    EasyMock.expect(admin.createTopics(mkSet(newTopic2))).andAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic2, createTopicFailFuture2))));
}
Also used : TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) TopicConfig(org.apache.kafka.common.config.TopicConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Config(org.apache.kafka.clients.admin.Config) StreamsConfig(org.apache.kafka.streams.StreamsConfig) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException)

Example 8 with TopicMetadataAndConfig

use of org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig in project kafka by apache.

the class InternalTopicManagerTest method shouldCleanUpWhenCreateTopicsTimesOut.

@Test
public void shouldCleanUpWhenCreateTopicsTimesOut() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final StreamsConfig streamsConfig = new StreamsConfig(config);
    final MockTime time = new MockTime((Integer) config.get(StreamsConfig.consumerPrefix(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG)) / 3);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, streamsConfig);
    final InternalTopicConfig internalTopicConfig1 = setupRepartitionTopicConfig(topic1, 1);
    final InternalTopicConfig internalTopicConfig2 = setupRepartitionTopicConfig(topic2, 1);
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFailFuture1 = new KafkaFutureImpl<>();
    createTopicFailFuture1.completeExceptionally(new TopicExistsException("exists"));
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicSuccessfulFuture = new KafkaFutureImpl<>();
    createTopicSuccessfulFuture.complete(new TopicMetadataAndConfig(Uuid.randomUuid(), 1, 1, new Config(Collections.emptyList())));
    final NewTopic newTopic1 = newTopic(topic1, internalTopicConfig1, streamsConfig);
    final NewTopic newTopic2 = newTopic(topic2, internalTopicConfig2, streamsConfig);
    EasyMock.expect(admin.createTopics(mkSet(newTopic1, newTopic2))).andAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic1, createTopicSuccessfulFuture), mkEntry(topic2, createTopicFailFuture1))));
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFutureThatNeverCompletes = new KafkaFutureImpl<>();
    EasyMock.expect(admin.createTopics(mkSet(newTopic2))).andStubAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic2, createTopicFutureThatNeverCompletes))));
    final KafkaFutureImpl<Void> deleteTopicSuccessfulFuture = new KafkaFutureImpl<>();
    deleteTopicSuccessfulFuture.complete(null);
    EasyMock.expect(admin.deleteTopics(mkSet(topic1))).andAnswer(() -> new MockDeleteTopicsResult(mkMap(mkEntry(topic1, deleteTopicSuccessfulFuture))));
    EasyMock.replay(admin);
    assertThrows(TimeoutException.class, () -> topicManager.setup(mkMap(mkEntry(topic1, internalTopicConfig1), mkEntry(topic2, internalTopicConfig2))));
    EasyMock.verify(admin);
}
Also used : TopicConfig(org.apache.kafka.common.config.TopicConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Config(org.apache.kafka.clients.admin.Config) StreamsConfig(org.apache.kafka.streams.StreamsConfig) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockTime(org.apache.kafka.common.utils.MockTime) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 9 with TopicMetadataAndConfig

use of org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig in project kafka by apache.

the class InternalTopicManagerTest method shouldThrowTimeoutExceptionWhenFuturesNeverCompleteDuringSetup.

@Test
public void shouldThrowTimeoutExceptionWhenFuturesNeverCompleteDuringSetup() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final MockTime time = new MockTime((Integer) config.get(StreamsConfig.consumerPrefix(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG)) / 3);
    final StreamsConfig streamsConfig = new StreamsConfig(config);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, streamsConfig);
    final KafkaFutureImpl<TopicMetadataAndConfig> createTopicFutureThatNeverCompletes = new KafkaFutureImpl<>();
    final InternalTopicConfig internalTopicConfig = setupRepartitionTopicConfig(topic1, 1);
    final NewTopic newTopic = newTopic(topic1, internalTopicConfig, streamsConfig);
    EasyMock.expect(admin.createTopics(mkSet(newTopic))).andStubAnswer(() -> new MockCreateTopicsResult(mkMap(mkEntry(topic1, createTopicFutureThatNeverCompletes))));
    EasyMock.replay(admin);
    assertThrows(TimeoutException.class, () -> topicManager.setup(Collections.singletonMap(topic1, internalTopicConfig)));
}
Also used : TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MockTime(org.apache.kafka.common.utils.MockTime) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 10 with TopicMetadataAndConfig

use of org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig in project kafka by apache.

the class KafkaAdminClient method getCreateTopicsCall.

private Call getCreateTopicsCall(final CreateTopicsOptions options, final Map<String, KafkaFutureImpl<TopicMetadataAndConfig>> futures, final CreatableTopicCollection topics, final Map<String, ThrottlingQuotaExceededException> quotaExceededExceptions, final long now, final long deadline) {
    return new Call("createTopics", deadline, new ControllerNodeProvider()) {

        @Override
        public CreateTopicsRequest.Builder createRequest(int timeoutMs) {
            return new CreateTopicsRequest.Builder(new CreateTopicsRequestData().setTopics(topics).setTimeoutMs(timeoutMs).setValidateOnly(options.shouldValidateOnly()));
        }

        @Override
        public void handleResponse(AbstractResponse abstractResponse) {
            // Check for controller change
            handleNotControllerError(abstractResponse);
            // Handle server responses for particular topics.
            final CreateTopicsResponse response = (CreateTopicsResponse) abstractResponse;
            final CreatableTopicCollection retryTopics = new CreatableTopicCollection();
            final Map<String, ThrottlingQuotaExceededException> retryTopicQuotaExceededExceptions = new HashMap<>();
            for (CreatableTopicResult result : response.data().topics()) {
                KafkaFutureImpl<TopicMetadataAndConfig> 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 {
                        TopicMetadataAndConfig topicMetadataAndConfig;
                        if (result.topicConfigErrorCode() != Errors.NONE.code()) {
                            topicMetadataAndConfig = new TopicMetadataAndConfig(Errors.forCode(result.topicConfigErrorCode()).exception());
                        } else if (result.numPartitions() == CreateTopicsResult.UNKNOWN) {
                            topicMetadataAndConfig = new TopicMetadataAndConfig(new UnsupportedVersionException("Topic metadata and configs in CreateTopics response not supported"));
                        } else {
                            List<CreatableTopicConfigs> configs = result.configs();
                            Config topicConfig = new Config(configs.stream().map(this::configEntry).collect(Collectors.toSet()));
                            topicMetadataAndConfig = new TopicMetadataAndConfig(result.topicId(), result.numPartitions(), result.replicationFactor(), topicConfig);
                        }
                        future.complete(topicMetadataAndConfig);
                    }
                }
            }
            // 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 = getCreateTopicsCall(options, futures, retryTopics, retryTopicQuotaExceededExceptions, now, deadline);
                runnable.call(call, now);
            }
        }

        private ConfigEntry configEntry(CreatableTopicConfigs config) {
            return new ConfigEntry(config.name(), config.value(), configSource(DescribeConfigsResponse.ConfigSource.forId(config.configSource())), config.isSensitive(), config.readOnly(), Collections.emptyList(), null, null);
        }

        @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 : CreateTopicsResponse(org.apache.kafka.common.requests.CreateTopicsResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) HashMap(java.util.HashMap) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ThrottlingQuotaExceededException(org.apache.kafka.common.errors.ThrottlingQuotaExceededException) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreatableTopicConfigs(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicConfigs) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) List(java.util.List) ApiError(org.apache.kafka.common.requests.ApiError) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Aggregations

TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)10 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)9 NewTopic (org.apache.kafka.clients.admin.NewTopic)8 StreamsConfig (org.apache.kafka.streams.StreamsConfig)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)7 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)7 Test (org.junit.Test)6 Config (org.apache.kafka.clients.admin.Config)5 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)5 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)5 TopicConfig (org.apache.kafka.common.config.TopicConfig)5 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)4 MockTime (org.apache.kafka.common.utils.MockTime)3 HashMap (java.util.HashMap)2 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 ThrottlingQuotaExceededException (org.apache.kafka.common.errors.ThrottlingQuotaExceededException)1