Search in sources :

Example 1 with CreateTopicsResult

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

the class KafkaTopicClientImplTest method getCreateTopicsResult.

private CreateTopicsResult getCreateTopicsResult() {
    CreateTopicsResult createTopicsResult = mock(CreateTopicsResult.class);
    expect(createTopicsResult.all()).andReturn(KafkaFuture.allOf());
    replay(createTopicsResult);
    return createTopicsResult;
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult)

Example 2 with CreateTopicsResult

use of org.apache.kafka.clients.admin.CreateTopicsResult in project apache-kafka-on-k8s by banzaicloud.

the class InternalTopicManager method makeReady.

/**
 * Prepares a set of given internal topics.
 *
 * If a topic does not exist creates a new topic.
 * If a topic with the correct number of partitions exists ignores it.
 * If a topic exists already but has different number of partitions we fail and throw exception requesting user to reset the app before restarting again.
 */
public void makeReady(final Map<String, InternalTopicConfig> topics) {
    final Map<String, Integer> existingTopicPartitions = getNumPartitions(topics.keySet());
    final Set<InternalTopicConfig> topicsToBeCreated = validateTopicPartitions(topics.values(), existingTopicPartitions);
    if (topicsToBeCreated.size() > 0) {
        final Set<NewTopic> newTopics = new HashSet<>();
        for (final InternalTopicConfig internalTopicConfig : topicsToBeCreated) {
            final Map<String, String> topicConfig = internalTopicConfig.getProperties(defaultTopicConfigs, windowChangeLogAdditionalRetention);
            log.debug("Going to create topic {} with {} partitions and config {}.", internalTopicConfig.name(), internalTopicConfig.numberOfPartitions(), topicConfig);
            newTopics.add(new NewTopic(internalTopicConfig.name(), internalTopicConfig.numberOfPartitions(), replicationFactor).configs(topicConfig));
        }
        int remainingRetries = retries;
        boolean retry;
        do {
            retry = false;
            final CreateTopicsResult createTopicsResult = adminClient.createTopics(newTopics);
            final Set<String> createTopicNames = new HashSet<>();
            for (final Map.Entry<String, KafkaFuture<Void>> createTopicResult : createTopicsResult.values().entrySet()) {
                try {
                    createTopicResult.getValue().get();
                    createTopicNames.add(createTopicResult.getKey());
                } catch (final ExecutionException couldNotCreateTopic) {
                    final Throwable cause = couldNotCreateTopic.getCause();
                    final String topicName = createTopicResult.getKey();
                    if (cause instanceof TimeoutException) {
                        retry = true;
                        log.debug("Could not get number of partitions for topic {} due to timeout. " + "Will try again (remaining retries {}).", topicName, remainingRetries - 1);
                    } else if (cause instanceof TopicExistsException) {
                        createTopicNames.add(createTopicResult.getKey());
                        log.info(String.format("Topic %s exist already: %s", topicName, couldNotCreateTopic.getMessage()));
                    } else {
                        throw new StreamsException(String.format("Could not create topic %s.", topicName), couldNotCreateTopic);
                    }
                } catch (final InterruptedException fatalException) {
                    Thread.currentThread().interrupt();
                    log.error(INTERRUPTED_ERROR_MESSAGE, fatalException);
                    throw new IllegalStateException(INTERRUPTED_ERROR_MESSAGE, fatalException);
                }
            }
            if (retry) {
                final Iterator<NewTopic> it = newTopics.iterator();
                while (it.hasNext()) {
                    if (createTopicNames.contains(it.next().name())) {
                        it.remove();
                    }
                }
                continue;
            }
            return;
        } while (remainingRetries-- > 0);
        final String timeoutAndRetryError = "Could not create topics. " + "This can happen if the Kafka cluster is temporary not available. " + "You can increase admin client config `retries` to be resilient against this error.";
        log.error(timeoutAndRetryError);
        throw new StreamsException(timeoutAndRetryError);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) StreamsException(org.apache.kafka.streams.errors.StreamsException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 3 with CreateTopicsResult

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

the class ControllerIT method createTopic.

private String createTopic(TestContext context, String topicName) throws InterruptedException, ExecutionException {
    LOGGER.info("Creating topic {}", topicName);
    // Create a topic
    String configMapName = new TopicName(topicName).asMapName().toString();
    CreateTopicsResult crt = adminClient.createTopics(singletonList(new NewTopic(topicName, 1, (short) 1)));
    crt.all().get();
    // Wait for the configmap to be created
    waitFor(context, () -> {
        ConfigMap cm = kubeClient.configMaps().inNamespace(NAMESPACE).withName(configMapName).get();
        LOGGER.info("Polled configmap {} waiting for creation", configMapName);
        return cm != null;
    }, timeout, "Expected the configmap to have been created by now");
    LOGGER.info("configmap {} has been created", configMapName);
    return configMapName;
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 4 with CreateTopicsResult

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

the class KafkaTopicClientImplTest method createTopicReturningTopicExistsException.

@SuppressWarnings("unchecked")
private CreateTopicsResult createTopicReturningTopicExistsException() {
    CreateTopicsResult createTopicsResult = niceMock(CreateTopicsResult.class);
    expect(createTopicsResult.all()).andReturn(failedFuture(new TopicExistsException("Topic already exists")));
    replay(createTopicsResult);
    return createTopicsResult;
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException)

Aggregations

CreateTopicsResult (org.apache.kafka.clients.admin.CreateTopicsResult)4 NewTopic (org.apache.kafka.clients.admin.NewTopic)2 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1