Search in sources :

Example 1 with InvalidConfigurationException

use of org.apache.kafka.common.errors.InvalidConfigurationException in project kafka by apache.

the class TopicAdmin method createOrFindTopics.

/**
 * Attempt to create the topics described by the given definitions, returning all of the names of those topics that
 * were created by this request. Any existing topics with the same name are unchanged, and the names of such topics
 * are excluded from the result.
 * <p>
 * If multiple topic definitions have the same topic name, the last one with that name will be used.
 * <p>
 * Apache Kafka added support for creating topics in 0.10.1.0, so this method works as expected with that and later versions.
 * With brokers older than 0.10.1.0, this method is unable to create topics and always returns an empty set.
 *
 * @param topics the specifications of the topics
 * @return the {@link TopicCreationResponse} with the names of the newly created and existing topics;
 *         never null but possibly empty
 * @throws ConnectException if an error occurs, the operation takes too long, or the thread is interrupted while
 *                          attempting to perform this operation
 */
public TopicCreationResponse createOrFindTopics(NewTopic... topics) {
    Map<String, NewTopic> topicsByName = new HashMap<>();
    if (topics != null) {
        for (NewTopic topic : topics) {
            if (topic != null)
                topicsByName.put(topic.name(), topic);
        }
    }
    if (topicsByName.isEmpty())
        return EMPTY_CREATION;
    String bootstrapServers = bootstrapServers();
    String topicNameList = Utils.join(topicsByName.keySet(), "', '");
    // Attempt to create any missing topics
    CreateTopicsOptions args = new CreateTopicsOptions().validateOnly(false);
    Map<String, KafkaFuture<Void>> newResults = admin.createTopics(topicsByName.values(), args).values();
    // Iterate over each future so that we can handle individual failures like when some topics already exist
    Set<String> newlyCreatedTopicNames = new HashSet<>();
    Set<String> existingTopicNames = new HashSet<>();
    for (Map.Entry<String, KafkaFuture<Void>> entry : newResults.entrySet()) {
        String topic = entry.getKey();
        try {
            entry.getValue().get();
            if (logCreation) {
                log.info("Created topic {} on brokers at {}", topicsByName.get(topic), bootstrapServers);
            }
            newlyCreatedTopicNames.add(topic);
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof TopicExistsException) {
                log.debug("Found existing topic '{}' on the brokers at {}", topic, bootstrapServers);
                existingTopicNames.add(topic);
                continue;
            }
            if (cause instanceof UnsupportedVersionException) {
                log.debug("Unable to create topic(s) '{}' since the brokers at {} do not support the CreateTopics API." + " Falling back to assume topic(s) exist or will be auto-created by the broker.", topicNameList, bootstrapServers);
                return EMPTY_CREATION;
            }
            if (cause instanceof ClusterAuthorizationException) {
                log.debug("Not authorized to create topic(s) '{}' upon the brokers {}." + " Falling back to assume topic(s) exist or will be auto-created by the broker.", topicNameList, bootstrapServers);
                return EMPTY_CREATION;
            }
            if (cause instanceof TopicAuthorizationException) {
                log.debug("Not authorized to create topic(s) '{}' upon the brokers {}." + " Falling back to assume topic(s) exist or will be auto-created by the broker.", topicNameList, bootstrapServers);
                return EMPTY_CREATION;
            }
            if (cause instanceof InvalidConfigurationException) {
                throw new ConnectException("Unable to create topic(s) '" + topicNameList + "': " + cause.getMessage(), cause);
            }
            if (cause instanceof TimeoutException) {
                // Timed out waiting for the operation to complete
                throw new ConnectException("Timed out while checking for or creating topic(s) '" + topicNameList + "'." + " This could indicate a connectivity issue, unavailable topic partitions, or if" + " this is your first use of the topic it may have taken too long to create.", cause);
            }
            throw new ConnectException("Error while attempting to create/find topic(s) '" + topicNameList + "'", e);
        } catch (InterruptedException e) {
            Thread.interrupted();
            throw new ConnectException("Interrupted while attempting to create/find topic(s) '" + topicNameList + "'", e);
        }
    }
    return new TopicCreationResponse(newlyCreatedTopicNames, existingTopicNames);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) CreateTopicsOptions(org.apache.kafka.clients.admin.CreateTopicsOptions) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) HashSet(java.util.HashSet) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 2 with InvalidConfigurationException

use of org.apache.kafka.common.errors.InvalidConfigurationException in project kafka by apache.

the class ChannelBuilders method createPrincipalBuilder.

public static KafkaPrincipalBuilder createPrincipalBuilder(Map<String, ?> configs, KerberosShortNamer kerberosShortNamer, SslPrincipalMapper sslPrincipalMapper) {
    Class<?> principalBuilderClass = (Class<?>) configs.get(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG);
    final KafkaPrincipalBuilder builder;
    if (principalBuilderClass == null || principalBuilderClass == DefaultKafkaPrincipalBuilder.class) {
        builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer, sslPrincipalMapper);
    } else if (KafkaPrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        builder = (KafkaPrincipalBuilder) Utils.newInstance(principalBuilderClass);
    } else {
        throw new InvalidConfigurationException("Type " + principalBuilderClass.getName() + " is not " + "an instance of " + KafkaPrincipalBuilder.class.getName());
    }
    if (builder instanceof Configurable)
        ((Configurable) builder).configure(configs);
    return builder;
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) Configurable(org.apache.kafka.common.Configurable) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of org.apache.kafka.common.errors.InvalidConfigurationException in project apache-kafka-on-k8s by banzaicloud.

the class ChannelBuilders method createPrincipalBuilder.

@SuppressWarnings("deprecation")
public static KafkaPrincipalBuilder createPrincipalBuilder(Map<String, ?> configs, TransportLayer transportLayer, Authenticator authenticator, KerberosShortNamer kerberosShortNamer) {
    Class<?> principalBuilderClass = (Class<?>) configs.get(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG);
    final KafkaPrincipalBuilder builder;
    if (principalBuilderClass == null || principalBuilderClass == DefaultKafkaPrincipalBuilder.class) {
        builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer);
    } else if (KafkaPrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        builder = (KafkaPrincipalBuilder) Utils.newInstance(principalBuilderClass);
    } else if (org.apache.kafka.common.security.auth.PrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
        org.apache.kafka.common.security.auth.PrincipalBuilder oldPrincipalBuilder = createPrincipalBuilder(principalBuilderClass, configs);
        builder = DefaultKafkaPrincipalBuilder.fromOldPrincipalBuilder(authenticator, transportLayer, oldPrincipalBuilder, kerberosShortNamer);
    } else {
        throw new InvalidConfigurationException("Type " + principalBuilderClass.getName() + " is not " + "an instance of " + org.apache.kafka.common.security.auth.PrincipalBuilder.class.getName() + " or " + KafkaPrincipalBuilder.class.getName());
    }
    if (builder instanceof Configurable)
        ((Configurable) builder).configure(configs);
    return builder;
}
Also used : DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) KafkaPrincipalBuilder(org.apache.kafka.common.security.auth.KafkaPrincipalBuilder) DefaultKafkaPrincipalBuilder(org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder) Configurable(org.apache.kafka.common.Configurable) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException)

Example 4 with InvalidConfigurationException

use of org.apache.kafka.common.errors.InvalidConfigurationException in project kafka by apache.

the class ConsumerConfig method maybeOverrideEnableAutoCommit.

boolean maybeOverrideEnableAutoCommit() {
    Optional<String> groupId = Optional.ofNullable(getString(CommonClientConfigs.GROUP_ID_CONFIG));
    boolean enableAutoCommit = getBoolean(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG);
    if (!groupId.isPresent()) {
        // overwrite in case of default group id where the config is not explicitly provided
        if (!originals().containsKey(ENABLE_AUTO_COMMIT_CONFIG)) {
            enableAutoCommit = false;
        } else if (enableAutoCommit) {
            throw new InvalidConfigurationException(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG + " cannot be set to true when default group id (null) is used.");
        }
    }
    return enableAutoCommit;
}
Also used : InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException)

Aggregations

InvalidConfigurationException (org.apache.kafka.common.errors.InvalidConfigurationException)4 Configurable (org.apache.kafka.common.Configurable)2 KafkaPrincipalBuilder (org.apache.kafka.common.security.auth.KafkaPrincipalBuilder)2 DefaultKafkaPrincipalBuilder (org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 CreateTopicsOptions (org.apache.kafka.clients.admin.CreateTopicsOptions)1 NewTopic (org.apache.kafka.clients.admin.NewTopic)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)1 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 ConnectException (org.apache.kafka.connect.errors.ConnectException)1