Search in sources :

Example 41 with ConfigResource

use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.

the class MirrorConnectorsIntegrationBaseTest method getTopicConfig.

/*
     * retrieve the config value based on the input cluster, topic and config name
     */
protected static String getTopicConfig(EmbeddedKafkaCluster cluster, String topic, String configName) throws Exception {
    try (Admin client = cluster.createAdminClient()) {
        Collection<ConfigResource> cr = Collections.singleton(new ConfigResource(ConfigResource.Type.TOPIC, topic));
        DescribeConfigsResult configsResult = client.describeConfigs(cr);
        Config allConfigs = (Config) configsResult.all().get().values().toArray()[0];
        return allConfigs.get(configName).value();
    }
}
Also used : TopicConfig(org.apache.kafka.common.config.TopicConfig) MirrorMakerConfig(org.apache.kafka.connect.mirror.MirrorMakerConfig) Config(org.apache.kafka.clients.admin.Config) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) Admin(org.apache.kafka.clients.admin.Admin) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Example 42 with ConfigResource

use of org.apache.kafka.common.config.ConfigResource 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)

Example 43 with ConfigResource

use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.

the class KafkaAdminClient method alterConfigs.

@Override
@Deprecated
public AlterConfigsResult alterConfigs(Map<ConfigResource, Config> configs, final AlterConfigsOptions options) {
    final Map<ConfigResource, KafkaFutureImpl<Void>> allFutures = new HashMap<>();
    // We must make a separate AlterConfigs request for every BROKER resource we want to alter
    // and send the request to that specific broker. Other resources are grouped together into
    // a single request that may be sent to any broker.
    final Collection<ConfigResource> unifiedRequestResources = new ArrayList<>();
    for (ConfigResource resource : configs.keySet()) {
        Integer node = nodeFor(resource);
        if (node != null) {
            NodeProvider nodeProvider = new ConstantNodeIdProvider(node);
            allFutures.putAll(alterConfigs(configs, options, Collections.singleton(resource), nodeProvider));
        } else
            unifiedRequestResources.add(resource);
    }
    if (!unifiedRequestResources.isEmpty())
        allFutures.putAll(alterConfigs(configs, options, unifiedRequestResources, new LeastLoadedNodeProvider()));
    return new AlterConfigsResult(new HashMap<>(allFutures));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Example 44 with ConfigResource

use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.

the class KafkaAdminClient method incrementalAlterConfigs.

private Map<ConfigResource, KafkaFutureImpl<Void>> incrementalAlterConfigs(Map<ConfigResource, Collection<AlterConfigOp>> configs, final AlterConfigsOptions options, Collection<ConfigResource> resources, NodeProvider nodeProvider) {
    final Map<ConfigResource, KafkaFutureImpl<Void>> futures = new HashMap<>();
    for (ConfigResource resource : resources) futures.put(resource, new KafkaFutureImpl<>());
    final long now = time.milliseconds();
    runnable.call(new Call("incrementalAlterConfigs", calcDeadlineMs(now, options.timeoutMs()), nodeProvider) {

        @Override
        public IncrementalAlterConfigsRequest.Builder createRequest(int timeoutMs) {
            return new IncrementalAlterConfigsRequest.Builder(resources, configs, options.shouldValidateOnly());
        }

        @Override
        public void handleResponse(AbstractResponse abstractResponse) {
            IncrementalAlterConfigsResponse response = (IncrementalAlterConfigsResponse) abstractResponse;
            Map<ConfigResource, ApiError> errors = IncrementalAlterConfigsResponse.fromResponseData(response.data());
            for (Map.Entry<ConfigResource, KafkaFutureImpl<Void>> entry : futures.entrySet()) {
                KafkaFutureImpl<Void> future = entry.getValue();
                ApiException exception = errors.get(entry.getKey()).exception();
                if (exception != null) {
                    future.completeExceptionally(exception);
                } else {
                    future.complete(null);
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return futures;
}
Also used : HashMap(java.util.HashMap) IncrementalAlterConfigsRequest(org.apache.kafka.common.requests.IncrementalAlterConfigsRequest) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ConfigResource(org.apache.kafka.common.config.ConfigResource) IncrementalAlterConfigsResponse(org.apache.kafka.common.requests.IncrementalAlterConfigsResponse) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ApiException(org.apache.kafka.common.errors.ApiException)

Example 45 with ConfigResource

use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.

the class KafkaAdminClientTest method callAdminClientApisAndExpectAnAuthenticationError.

private void callAdminClientApisAndExpectAnAuthenticationError(AdminClientUnitTestEnv env) throws InterruptedException {
    ExecutionException e = assertThrows(ExecutionException.class, () -> env.adminClient().createTopics(singleton(new NewTopic("myTopic", Collections.singletonMap(0, asList(0, 1, 2)))), new CreateTopicsOptions().timeoutMs(10000)).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    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))));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().createPartitions(counts).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().createAcls(asList(ACL1, ACL2)).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().describeAcls(FILTER1).values().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().deleteAcls(asList(FILTER1, FILTER2)).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().describeConfigs(singleton(new ConfigResource(ConfigResource.Type.BROKER, "0"))).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
}
Also used : AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException) HashMap(java.util.HashMap) ExecutionException(java.util.concurrent.ExecutionException) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Aggregations

ConfigResource (org.apache.kafka.common.config.ConfigResource)64 HashMap (java.util.HashMap)32 Config (org.apache.kafka.clients.admin.Config)23 Map (java.util.Map)22 KafkaFuture (org.apache.kafka.common.KafkaFuture)20 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)20 TopicConfig (org.apache.kafka.common.config.TopicConfig)18 ArrayList (java.util.ArrayList)17 ConfigEntry (org.apache.kafka.clients.admin.ConfigEntry)16 Test (org.junit.Test)15 Collection (java.util.Collection)14 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)13 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)13 Node (org.apache.kafka.common.Node)13 AdminClient (org.apache.kafka.clients.admin.AdminClient)12 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)12 Collections (java.util.Collections)11 Collectors (java.util.stream.Collectors)11 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)11 StreamsConfig (org.apache.kafka.streams.StreamsConfig)11