use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException in project pulsar by apache.
the class BrokerService method createPersistentTopic.
private void createPersistentTopic(final String topic, boolean createIfMissing, CompletableFuture<Optional<Topic>> topicFuture, Map<String, String> properties) {
final long topicCreateTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
TopicName topicName = TopicName.get(topic);
if (!pulsar.getNamespaceService().isServiceUnitActive(topicName)) {
// namespace is being unloaded
String msg = String.format("Namespace is being unloaded, cannot add topic %s", topic);
log.warn(msg);
pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture));
topicFuture.completeExceptionally(new ServiceUnitNotReadyException(msg));
return;
}
if (isTransactionSystemTopic(topicName)) {
String msg = String.format("Can not create transaction system topic %s", topic);
log.warn(msg);
pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture));
topicFuture.completeExceptionally(new NotAllowedException(msg));
return;
}
CompletableFuture<Void> maxTopicsCheck = createIfMissing ? checkMaxTopicsPerNamespace(topicName, 1) : CompletableFuture.completedFuture(null);
maxTopicsCheck.thenCompose(__ -> getManagedLedgerConfig(topicName)).thenAccept(managedLedgerConfig -> {
if (isBrokerEntryMetadataEnabled() || isBrokerPayloadProcessorEnabled()) {
// init managedLedger interceptor
Set<BrokerEntryMetadataInterceptor> interceptors = new HashSet<>();
for (BrokerEntryMetadataInterceptor interceptor : brokerEntryMetadataInterceptors) {
// add individual AppendOffsetMetadataInterceptor for each topic
if (interceptor instanceof AppendIndexMetadataInterceptor) {
interceptors.add(new AppendIndexMetadataInterceptor());
} else {
interceptors.add(interceptor);
}
}
managedLedgerConfig.setManagedLedgerInterceptor(new ManagedLedgerInterceptorImpl(interceptors, brokerEntryPayloadProcessors));
}
managedLedgerConfig.setCreateIfMissing(createIfMissing);
managedLedgerConfig.setProperties(properties);
// Once we have the configuration, we can proceed with the async open operation
managedLedgerFactory.asyncOpen(topicName.getPersistenceNamingEncoding(), managedLedgerConfig, new OpenLedgerCallback() {
@Override
public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
try {
PersistentTopic persistentTopic = isSystemTopic(topic) ? new SystemTopic(topic, ledger, BrokerService.this) : new PersistentTopic(topic, ledger, BrokerService.this);
CompletableFuture<Void> preCreateSubForCompaction = persistentTopic.preCreateSubscriptionForCompactionIfNeeded();
CompletableFuture<Void> replicationFuture = persistentTopic.initialize().thenCompose(__ -> persistentTopic.checkReplication());
CompletableFuture.allOf(preCreateSubForCompaction, replicationFuture).thenCompose(v -> {
// Also check dedup status
return persistentTopic.checkDeduplicationStatus();
}).thenRun(() -> {
log.info("Created topic {} - dedup is {}", topic, persistentTopic.isDeduplicationEnabled() ? "enabled" : "disabled");
long topicLoadLatencyMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - topicCreateTimeMs;
pulsarStats.recordTopicLoadTimeValue(topic, topicLoadLatencyMs);
if (topicFuture.isCompletedExceptionally()) {
log.warn("{} future is already completed with failure {}, closing the topic", topic, FutureUtil.getException(topicFuture));
persistentTopic.stopReplProducers().whenComplete((v, exception) -> {
topics.remove(topic, topicFuture);
});
} else {
addTopicToStatsMaps(topicName, persistentTopic);
topicFuture.complete(Optional.of(persistentTopic));
}
}).exceptionally((ex) -> {
log.warn("Replication or dedup check failed." + " Removing topic from topics list {}, {}", topic, ex);
persistentTopic.stopReplProducers().whenComplete((v, exception) -> {
topics.remove(topic, topicFuture);
topicFuture.completeExceptionally(ex);
});
return null;
});
} catch (PulsarServerException e) {
log.warn("Failed to create topic {}-{}", topic, e.getMessage());
pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture));
topicFuture.completeExceptionally(e);
}
}
@Override
public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
if (!createIfMissing && exception instanceof ManagedLedgerNotFoundException) {
// We were just trying to load a topic and the topic doesn't exist
topicFuture.complete(Optional.empty());
} else {
log.warn("Failed to create topic {}", topic, exception);
pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture));
topicFuture.completeExceptionally(new PersistenceException(exception));
}
}
}, () -> isTopicNsOwnedByBroker(topicName), null);
}).exceptionally((exception) -> {
log.warn("[{}] Failed to get topic configuration: {}", topic, exception.getMessage(), exception);
// remove topic from topics-map in different thread to avoid possible deadlock if
// createPersistentTopic-thread only tries to handle this future-result
pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture));
topicFuture.completeExceptionally(exception);
return null;
});
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException in project pulsar by apache.
the class ManagedLedgerTest method testManagedLedgerWithoutAutoCreate.
@Test
public void testManagedLedgerWithoutAutoCreate() throws Exception {
ManagedLedgerConfig config = new ManagedLedgerConfig().setCreateIfMissing(false);
try {
factory.open("testManagedLedgerWithoutAutoCreate", config);
fail("should have thrown ManagedLedgerNotFoundException");
} catch (ManagedLedgerNotFoundException e) {
// Expected
}
assertFalse(factory.getManagedLedgers().containsKey("testManagedLedgerWithoutAutoCreate"));
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException in project pulsar by apache.
the class ReadOnlyCursorTest method notFound.
@Test
void notFound() throws Exception {
try {
factory.openReadOnlyCursor("notFound", PositionImpl.EARLIEST, new ManagedLedgerConfig());
fail("Should have failed");
} catch (ManagedLedgerNotFoundException e) {
// Expected
}
factory.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException in project pulsar by yahoo.
the class ManagedLedgerTest method testManagedLedgerWithoutAutoCreate.
@Test
public void testManagedLedgerWithoutAutoCreate() throws Exception {
ManagedLedgerConfig config = new ManagedLedgerConfig().setCreateIfMissing(false);
try {
factory.open("testManagedLedgerWithoutAutoCreate", config);
fail("should have thrown ManagedLedgerNotFoundException");
} catch (ManagedLedgerNotFoundException e) {
// Expected
}
assertFalse(factory.getManagedLedgers().containsKey("testManagedLedgerWithoutAutoCreate"));
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException in project pulsar by yahoo.
the class ManagedLedgerTest method deleteWithoutOpen.
@Test
public void deleteWithoutOpen() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ledger.addEntry("dummy-entry-1".getBytes(Encoding));
assertEquals(ledger.getNumberOfEntries(), 1);
ledger.close();
factory.delete("my_test_ledger");
try {
factory.open("my_test_ledger", new ManagedLedgerConfig().setCreateIfMissing(false));
fail("Should have failed");
} catch (ManagedLedgerNotFoundException e) {
// Expected
}
}
Aggregations