use of io.confluent.ksql.exception.KafkaResponseGetFailedException in project ksql by confluentinc.
the class KafkaTopicClientImpl method createTopic.
@Override
public void createTopic(final String topic, final int numPartitions, final short replicationFactor, final Map<String, ?> configs) {
if (isTopicExists(topic)) {
validateTopicProperties(topic, numPartitions, replicationFactor);
return;
}
final NewTopic newTopic = new NewTopic(topic, numPartitions, replicationFactor);
newTopic.configs(toStringConfigs(configs));
try {
log.info("Creating topic '{}'", topic);
executeWithRetries(() -> adminClient.createTopics(Collections.singleton(newTopic)).all());
} catch (final InterruptedException e) {
throw new KafkaResponseGetFailedException("Failed to guarantee existence of topic " + topic, e);
} catch (final TopicExistsException e) {
// if the topic already exists, it is most likely because another node just created it.
// ensure that it matches the partition count and replication factor before returning
// success
validateTopicProperties(topic, numPartitions, replicationFactor);
} catch (final Exception e) {
throw new KafkaResponseGetFailedException("Failed to guarantee existence of topic " + topic, e);
}
}
use of io.confluent.ksql.exception.KafkaResponseGetFailedException in project ksql by confluentinc.
the class KafkaTopicClientImpl method addTopicConfig.
@Override
public boolean addTopicConfig(final String topicName, final Map<String, ?> overrides) {
final ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
try {
final Map<String, String> existingConfig = topicConfig(topicName, false);
final boolean changed = overrides.entrySet().stream().anyMatch(e -> !Objects.equals(existingConfig.get(e.getKey()), e.getValue()));
if (!changed) {
return false;
}
existingConfig.putAll(toStringConfigs(overrides));
final Set<ConfigEntry> entries = existingConfig.entrySet().stream().map(e -> new ConfigEntry(e.getKey(), e.getValue())).collect(Collectors.toSet());
final Map<ConfigResource, Config> request = Collections.singletonMap(resource, new Config(entries));
executeWithRetries(() -> adminClient.alterConfigs(request).all());
return true;
} catch (final Exception e) {
throw new KafkaResponseGetFailedException("Failed to set config for Kafka Topic " + topicName, e);
}
}
use of io.confluent.ksql.exception.KafkaResponseGetFailedException in project ksql by confluentinc.
the class KafkaTopicClientImpl method topicConfig.
private Map<String, String> topicConfig(final String topicName, final boolean includeDefaults) {
final ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
final List<ConfigResource> request = Collections.singletonList(resource);
try {
final Config config = executeWithRetries(() -> adminClient.describeConfigs(request).all()).get(resource);
return config.entries().stream().filter(e -> includeDefaults || e.source().equals(ConfigEntry.ConfigSource.DYNAMIC_TOPIC_CONFIG)).collect(Collectors.toMap(ConfigEntry::name, ConfigEntry::value));
} catch (final Exception e) {
throw new KafkaResponseGetFailedException("Failed to get config for Kafka Topic " + topicName, e);
}
}
Aggregations