use of org.apache.kafka.common.errors.TopicExistsException in project kafka by apache.
the class InternalTopicManager method processCreateTopicResults.
private void processCreateTopicResults(final CreateTopicsResult createTopicsResult, final Set<String> topicStillToCreate, final Set<String> createdTopics, final long deadline) {
final Map<String, Throwable> lastErrorsSeenForTopic = new HashMap<>();
final Map<String, KafkaFuture<Void>> createResultForTopic = createTopicsResult.values();
while (!createResultForTopic.isEmpty()) {
for (final String topicName : new HashSet<>(topicStillToCreate)) {
if (!createResultForTopic.containsKey(topicName)) {
cleanUpCreatedTopics(createdTopics);
throw new IllegalStateException("Create topic results do not contain internal topic " + topicName + " to setup. " + BUG_ERROR_MESSAGE);
}
final KafkaFuture<Void> createResult = createResultForTopic.get(topicName);
if (createResult.isDone()) {
try {
createResult.get();
createdTopics.add(topicName);
topicStillToCreate.remove(topicName);
} catch (final ExecutionException executionException) {
final Throwable cause = executionException.getCause();
if (cause instanceof TopicExistsException) {
lastErrorsSeenForTopic.put(topicName, cause);
log.info("Internal topic {} already exists. Topic is probably marked for deletion. " + "Will retry to create this topic later (to let broker complete async delete operation first)", topicName);
} else if (cause instanceof TimeoutException) {
lastErrorsSeenForTopic.put(topicName, cause);
log.info("Creating internal topic {} timed out.", topicName);
} else {
cleanUpCreatedTopics(createdTopics);
log.error("Unexpected error during creation of internal topic: ", cause);
throw new StreamsException(String.format("Could not create internal topic %s for the following reason: ", topicName), cause);
}
} catch (final InterruptedException interruptedException) {
throw new InterruptException(interruptedException);
} finally {
createResultForTopic.remove(topicName);
}
}
}
maybeThrowTimeoutExceptionDuringSetup(topicStillToCreate, createdTopics, lastErrorsSeenForTopic, deadline);
if (!createResultForTopic.isEmpty()) {
Utils.sleep(100);
}
}
}
use of org.apache.kafka.common.errors.TopicExistsException in project zipkin by openzipkin.
the class KafkaExtension method prepareTopics.
void prepareTopics(String topics, int partitions) {
Properties config = new Properties();
config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer());
List<NewTopic> newTopics = new ArrayList<>();
for (String topic : topics.split(",")) {
if ("".equals(topic))
continue;
newTopics.add(new NewTopic(topic, partitions, (short) 1));
}
try (AdminClient adminClient = AdminClient.create(config)) {
adminClient.createTopics(newTopics).all().get();
} catch (InterruptedException | ExecutionException e) {
if (e.getCause() != null && e.getCause() instanceof TopicExistsException)
return;
throw new TestAbortedException("Topics could not be created " + newTopics + ": " + e.getMessage(), e);
}
}
use of org.apache.kafka.common.errors.TopicExistsException in project hono by eclipse.
the class KafkaBasedInternalCommandConsumer method createTopic.
private Future<Void> createTopic() {
final Promise<Void> promise = Promise.promise();
final String topicName = getTopicName();
// create topic with unspecified replication factor - broker "default.replication.factor" should be used
final NewTopic newTopic = new NewTopic(topicName, Optional.of(NUM_PARTITIONS), Optional.empty());
adminClient.createTopics(List.of(newTopic)).all().whenComplete((v, ex) -> {
context.runOnContext(v1 -> Optional.ofNullable(ex).filter(e -> !(e instanceof TopicExistsException)).ifPresentOrElse(promise::fail, promise::complete));
});
return promise.future().onSuccess(v -> LOG.debug("created topic [{}]", topicName)).onFailure(thr -> LOG.error("error creating topic [{}]", topicName, thr));
}
use of org.apache.kafka.common.errors.TopicExistsException in project nakadi by zalando.
the class KafkaTopicRepository method createTopic.
private void createTopic(final String topic, final int partitionsNum, final int replicaFactor, final long retentionMs, final long rotationMs) throws TopicCreationException {
try {
doWithZkUtils(zkUtils -> {
final Properties topicConfig = new Properties();
topicConfig.setProperty("retention.ms", Long.toString(retentionMs));
topicConfig.setProperty("segment.ms", Long.toString(rotationMs));
AdminUtils.createTopic(zkUtils, topic, partitionsNum, replicaFactor, topicConfig, RackAwareMode.Safe$.MODULE$);
});
} catch (final TopicExistsException e) {
throw new TopicCreationException("Topic with name " + topic + " already exists (or wasn't completely removed yet)", e);
} catch (final Exception e) {
throw new TopicCreationException("Unable to create topic " + topic, e);
}
// Next step is to wait for topic initialization. On can not skip this task, cause kafka instances may not
// receive information about topic creation, which in turn will block publishing.
// This kind of behavior was observed during tests, but may also present on highly loaded event types.
final long timeoutMillis = TimeUnit.SECONDS.toMillis(5);
final Boolean allowsConsumption = Retryer.executeWithRetry(() -> {
try (Consumer<byte[], byte[]> consumer = kafkaFactory.getConsumer()) {
return null != consumer.partitionsFor(topic);
}
}, new RetryForSpecifiedTimeStrategy<Boolean>(timeoutMillis).withWaitBetweenEachTry(100L).withResultsThatForceRetry(Boolean.FALSE));
if (!Boolean.TRUE.equals(allowsConsumption)) {
throw new TopicCreationException("Failed to confirm topic creation within " + timeoutMillis + " millis");
}
}
use of org.apache.kafka.common.errors.TopicExistsException in project ksql by confluentinc.
the class KafkaTopicClientImplTest method createTopicReturningTopicExistsException.
@SuppressWarnings("unchecked")
private CreateTopicsResult createTopicReturningTopicExistsException() {
CreateTopicsResult createTopicsResult = niceMock(CreateTopicsResult.class);
expect(createTopicsResult.all()).andReturn(failedFuture(new TopicExistsException("Topic already exists")));
replay(createTopicsResult);
return createTopicsResult;
}
Aggregations