use of org.apache.kafka.common.config.ConfigResource in project ksql by confluentinc.
the class KafkaTopicClientImpl method isTopicDeleteEnabled.
private static boolean isTopicDeleteEnabled(final AdminClient adminClient) {
try {
DescribeClusterResult describeClusterResult = adminClient.describeCluster();
Collection<Node> nodes = describeClusterResult.nodes().get();
if (nodes.isEmpty()) {
log.warn("No available broker found to fetch config info.");
throw new KsqlException("Could not fetch broker information. KSQL cannot initialize");
}
ConfigResource resource = new ConfigResource(ConfigResource.Type.BROKER, String.valueOf(nodes.iterator().next().id()));
Map<ConfigResource, Config> config = executeWithRetries(() -> adminClient.describeConfigs(Collections.singleton(resource)).all());
return config.get(resource).entries().stream().anyMatch(configEntry -> configEntry.name().equalsIgnoreCase("delete.topic.enable") && configEntry.value().equalsIgnoreCase("true"));
} catch (final Exception e) {
log.error("Failed to initialize TopicClient: {}", e.getMessage());
throw new KsqlException("Could not fetch broker information. KSQL cannot initialize", e);
}
}
use of org.apache.kafka.common.config.ConfigResource in project apache-kafka-on-k8s by banzaicloud.
the class MockAdminClient method describeConfigs.
@Override
public DescribeConfigsResult describeConfigs(Collection<ConfigResource> resources, DescribeConfigsOptions options) {
Map<ConfigResource, KafkaFuture<Config>> configescriptions = new HashMap<>();
for (ConfigResource resource : resources) {
if (resource.type() == ConfigResource.Type.TOPIC) {
Map<String, String> configs = allTopics.get(resource.name()).configs;
List<ConfigEntry> configEntries = new ArrayList<>();
for (Map.Entry<String, String> entry : configs.entrySet()) {
configEntries.add(new ConfigEntry(entry.getKey(), entry.getValue()));
}
KafkaFutureImpl<Config> future = new KafkaFutureImpl<>();
future.complete(new Config(configEntries));
configescriptions.put(resource, future);
} else {
throw new UnsupportedOperationException("Not implemented yet");
}
}
return new DescribeConfigsResult(configescriptions);
}
use of org.apache.kafka.common.config.ConfigResource in project apache-kafka-on-k8s by banzaicloud.
the class KafkaAdminClient method describeConfigs.
@Override
public DescribeConfigsResult describeConfigs(Collection<ConfigResource> configResources, final DescribeConfigsOptions options) {
final Map<ConfigResource, KafkaFutureImpl<Config>> unifiedRequestFutures = new HashMap<>();
final Map<ConfigResource, KafkaFutureImpl<Config>> brokerFutures = new HashMap<>(configResources.size());
// The BROKER resources which we want to describe. We must make a separate DescribeConfigs
// request for every BROKER resource we want to describe.
final Collection<Resource> brokerResources = new ArrayList<>();
// The non-BROKER resources which we want to describe. These resources can be described by a
// single, unified DescribeConfigs request.
final Collection<Resource> unifiedRequestResources = new ArrayList<>(configResources.size());
for (ConfigResource resource : configResources) {
if (resource.type() == ConfigResource.Type.BROKER && !resource.isDefault()) {
brokerFutures.put(resource, new KafkaFutureImpl<Config>());
brokerResources.add(configResourceToResource(resource));
} else {
unifiedRequestFutures.put(resource, new KafkaFutureImpl<Config>());
unifiedRequestResources.add(configResourceToResource(resource));
}
}
final long now = time.milliseconds();
if (!unifiedRequestResources.isEmpty()) {
runnable.call(new Call("describeConfigs", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {
@Override
AbstractRequest.Builder createRequest(int timeoutMs) {
return new DescribeConfigsRequest.Builder(unifiedRequestResources).includeSynonyms(options.includeSynonyms());
}
@Override
void handleResponse(AbstractResponse abstractResponse) {
DescribeConfigsResponse response = (DescribeConfigsResponse) abstractResponse;
for (Map.Entry<ConfigResource, KafkaFutureImpl<Config>> entry : unifiedRequestFutures.entrySet()) {
ConfigResource configResource = entry.getKey();
KafkaFutureImpl<Config> future = entry.getValue();
DescribeConfigsResponse.Config config = response.config(configResourceToResource(configResource));
if (config == null) {
future.completeExceptionally(new UnknownServerException("Malformed broker response: missing config for " + configResource));
continue;
}
if (config.error().isFailure()) {
future.completeExceptionally(config.error().exception());
continue;
}
List<ConfigEntry> configEntries = new ArrayList<>();
for (DescribeConfigsResponse.ConfigEntry configEntry : config.entries()) {
configEntries.add(new ConfigEntry(configEntry.name(), configEntry.value(), configSource(configEntry.source()), configEntry.isSensitive(), configEntry.isReadOnly(), configSynonyms(configEntry)));
}
future.complete(new Config(configEntries));
}
}
@Override
void handleFailure(Throwable throwable) {
completeAllExceptionally(unifiedRequestFutures.values(), throwable);
}
}, now);
}
for (Map.Entry<ConfigResource, KafkaFutureImpl<Config>> entry : brokerFutures.entrySet()) {
final KafkaFutureImpl<Config> brokerFuture = entry.getValue();
final Resource resource = configResourceToResource(entry.getKey());
final int nodeId = Integer.parseInt(resource.name());
runnable.call(new Call("describeBrokerConfigs", calcDeadlineMs(now, options.timeoutMs()), new ConstantNodeIdProvider(nodeId)) {
@Override
AbstractRequest.Builder createRequest(int timeoutMs) {
return new DescribeConfigsRequest.Builder(Collections.singleton(resource)).includeSynonyms(options.includeSynonyms());
}
@Override
void handleResponse(AbstractResponse abstractResponse) {
DescribeConfigsResponse response = (DescribeConfigsResponse) abstractResponse;
DescribeConfigsResponse.Config config = response.configs().get(resource);
if (config == null) {
brokerFuture.completeExceptionally(new UnknownServerException("Malformed broker response: missing config for " + resource));
return;
}
if (config.error().isFailure())
brokerFuture.completeExceptionally(config.error().exception());
else {
List<ConfigEntry> configEntries = new ArrayList<>();
for (DescribeConfigsResponse.ConfigEntry configEntry : config.entries()) {
configEntries.add(new ConfigEntry(configEntry.name(), configEntry.value(), configSource(configEntry.source()), configEntry.isSensitive(), configEntry.isReadOnly(), configSynonyms(configEntry)));
}
brokerFuture.complete(new Config(configEntries));
}
}
@Override
void handleFailure(Throwable throwable) {
brokerFuture.completeExceptionally(throwable);
}
}, now);
}
final Map<ConfigResource, KafkaFuture<Config>> allFutures = new HashMap<>();
allFutures.putAll(brokerFutures);
allFutures.putAll(unifiedRequestFutures);
return new DescribeConfigsResult(allFutures);
}
use of org.apache.kafka.common.config.ConfigResource in project apache-kafka-on-k8s by banzaicloud.
the class KafkaAdminClient method alterConfigs.
@Override
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()) {
if (resource.type() == ConfigResource.Type.BROKER && !resource.isDefault()) {
NodeProvider nodeProvider = new ConstantNodeIdProvider(Integer.parseInt(resource.name()));
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<ConfigResource, KafkaFuture<Void>>(allFutures));
}
use of org.apache.kafka.common.config.ConfigResource in project samza by apache.
the class TestKafkaSystemAdminJava method getTopicConfigFromKafkaBroker.
private static Map<String, String> getTopicConfigFromKafkaBroker(String topicName) throws Exception {
List<ConfigResource> configResourceList = ImmutableList.of(new ConfigResource(ConfigResource.Type.TOPIC, topicName));
Map<ConfigResource, org.apache.kafka.clients.admin.Config> configResourceConfigMap = adminClient().describeConfigs(configResourceList).all().get();
Map<String, String> kafkaTopicConfig = new HashMap<>();
configResourceConfigMap.values().forEach(configEntry -> {
configEntry.entries().forEach(config -> {
kafkaTopicConfig.put(config.name(), config.value());
});
});
return kafkaTopicConfig;
}
Aggregations