Search in sources :

Example 1 with DescribeConfigsOptions

use of org.apache.kafka.clients.admin.DescribeConfigsOptions in project kafka by apache.

the class TopicAdmin method describeTopicConfigs.

/**
 * Attempt to fetch the topic configurations for the given topics.
 * Apache Kafka added support for describing topic configurations in 0.11.0.0, so this method
 * works as expected with that and later versions. With brokers older than 0.11.0.0, this method
 * is unable get the topic configurations and always returns an empty set.
 *
 * <p>An entry with a null Config is placed into the resulting map for any topic that does
 * not exist on the brokers.
 *
 * @param topicNames the topics to obtain configurations
 * @return the map of topic configurations for each existing topic, or an empty map if none
 * of the topics exist
 * @throws RetriableException if a retriable error occurs, the operation takes too long, or the
 *         thread is interrupted while attempting to perform this operation
 * @throws ConnectException if a non retriable error occurs
 */
public Map<String, Config> describeTopicConfigs(String... topicNames) {
    if (topicNames == null) {
        return Collections.emptyMap();
    }
    Collection<String> topics = Arrays.stream(topicNames).filter(Objects::nonNull).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    if (topics.isEmpty()) {
        return Collections.emptyMap();
    }
    String bootstrapServers = bootstrapServers();
    String topicNameList = String.join(", ", topics);
    Collection<ConfigResource> resources = topics.stream().map(t -> new ConfigResource(ConfigResource.Type.TOPIC, t)).collect(Collectors.toList());
    Map<ConfigResource, KafkaFuture<Config>> newResults = admin.describeConfigs(resources, new DescribeConfigsOptions()).values();
    // Iterate over each future so that we can handle individual failures like when some topics don't exist
    Map<String, Config> result = new HashMap<>();
    newResults.forEach((resource, configs) -> {
        String topic = resource.name();
        try {
            result.put(topic, configs.get());
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof UnknownTopicOrPartitionException) {
                log.debug("Topic '{}' does not exist on the brokers at {}", topic, bootstrapServers);
                result.put(topic, null);
            } else if (cause instanceof ClusterAuthorizationException || cause instanceof TopicAuthorizationException) {
                log.debug("Not authorized to describe topic config for topic '{}' on brokers at {}", topic, bootstrapServers);
            } else if (cause instanceof UnsupportedVersionException) {
                log.debug("API to describe topic config for topic '{}' is unsupported on brokers at {}", topic, bootstrapServers);
            } else if (cause instanceof TimeoutException) {
                String msg = String.format("Timed out while waiting to describe topic config for topic '%s' on brokers at %s", topic, bootstrapServers);
                throw new RetriableException(msg, e);
            } else {
                String msg = String.format("Error while attempting to describe topic config for topic '%s' on brokers at %s", topic, bootstrapServers);
                throw new ConnectException(msg, e);
            }
        } catch (InterruptedException e) {
            Thread.interrupted();
            String msg = String.format("Interrupted while attempting to describe topic configs '%s'", topicNameList);
            throw new RetriableException(msg, e);
        }
    });
    return result;
}
Also used : Config(org.apache.kafka.clients.admin.Config) Arrays(java.util.Arrays) DescribeTopicsOptions(org.apache.kafka.clients.admin.DescribeTopicsOptions) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) Function(java.util.function.Function) HashSet(java.util.HashSet) ListOffsetsResult(org.apache.kafka.clients.admin.ListOffsetsResult) ConfigResource(org.apache.kafka.common.config.ConfigResource) Duration(java.time.Duration) Map(java.util.Map) Admin(org.apache.kafka.clients.admin.Admin) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) TopicConfig(org.apache.kafka.common.config.TopicConfig) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) InvalidConfigurationException(org.apache.kafka.common.errors.InvalidConfigurationException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Logger(org.slf4j.Logger) DescribeConfigsOptions(org.apache.kafka.clients.admin.DescribeConfigsOptions) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Collection(java.util.Collection) NewTopic(org.apache.kafka.clients.admin.NewTopic) Set(java.util.Set) KafkaFuture(org.apache.kafka.common.KafkaFuture) ConfigException(org.apache.kafka.common.config.ConfigException) Collectors(java.util.stream.Collectors) OffsetSpec(org.apache.kafka.clients.admin.OffsetSpec) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) RetriableException(org.apache.kafka.connect.errors.RetriableException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) ConnectException(org.apache.kafka.connect.errors.ConnectException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Optional(java.util.Optional) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) CreateTopicsOptions(org.apache.kafka.clients.admin.CreateTopicsOptions) Collections(java.util.Collections) KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) DescribeConfigsOptions(org.apache.kafka.clients.admin.DescribeConfigsOptions) Config(org.apache.kafka.clients.admin.Config) TopicConfig(org.apache.kafka.common.config.TopicConfig) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) ConfigResource(org.apache.kafka.common.config.ConfigResource) ExecutionException(java.util.concurrent.ExecutionException) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) RetriableException(org.apache.kafka.connect.errors.RetriableException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Admin (org.apache.kafka.clients.admin.Admin)1 AdminClientConfig (org.apache.kafka.clients.admin.AdminClientConfig)1 Config (org.apache.kafka.clients.admin.Config)1 ConfigEntry (org.apache.kafka.clients.admin.ConfigEntry)1 CreateTopicsOptions (org.apache.kafka.clients.admin.CreateTopicsOptions)1 DescribeConfigsOptions (org.apache.kafka.clients.admin.DescribeConfigsOptions)1 DescribeTopicsOptions (org.apache.kafka.clients.admin.DescribeTopicsOptions)1