Search in sources :

Example 1 with State

use of org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.State in project pulsar by yahoo.

the class ManagedLedgerFactoryImpl method asyncOpen.

@Override
public void asyncOpen(final String name, final ManagedLedgerConfig config, final OpenLedgerCallback callback, final Object ctx) {
    // If the ledger state is bad, remove it from the map.
    CompletableFuture<ManagedLedgerImpl> existingFuture = ledgers.get(name);
    if (existingFuture != null && existingFuture.isDone()) {
        try {
            ManagedLedgerImpl l = existingFuture.get();
            if (l.getState().equals(State.Fenced.toString()) || l.getState().equals(State.Closed.toString())) {
                // Managed ledger is in unusable state. Recreate it.
                log.warn("[{}] Attempted to open ledger in {} state. Removing from the map to recreate it", name, l.getState());
                ledgers.remove(name, existingFuture);
            }
        } catch (Exception e) {
            // Unable to get the future
            log.warn("[{}] Got exception while trying to retrieve ledger", name, e);
        }
    }
    // Ensure only one managed ledger is created and initialized
    ledgers.computeIfAbsent(name, (mlName) -> {
        // Create the managed ledger
        CompletableFuture<ManagedLedgerImpl> future = new CompletableFuture<>();
        final ManagedLedgerImpl newledger = new ManagedLedgerImpl(this, bookKeeper, store, config, executor, orderedExecutor, name);
        newledger.initialize(new ManagedLedgerInitializeLedgerCallback() {

            @Override
            public void initializeComplete() {
                future.complete(newledger);
            }

            @Override
            public void initializeFailed(ManagedLedgerException e) {
                // Clean the map if initialization fails
                ledgers.remove(name, future);
                future.completeExceptionally(e);
            }
        }, null);
        return future;
    }).thenAccept(ml -> {
        callback.openLedgerComplete(ml, ctx);
    }).exceptionally(exception -> {
        callback.openLedgerFailed((ManagedLedgerException) exception.getCause(), ctx);
        return null;
    });
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerFactoryMXBean(org.apache.bookkeeper.mledger.ManagedLedgerFactoryMXBean) State(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.State) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) States(org.apache.zookeeper.ZooKeeper.States) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Predicates(com.google.common.base.Predicates) ZooKeeper(org.apache.zookeeper.ZooKeeper) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) Logger(org.slf4j.Logger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Watcher(org.apache.zookeeper.Watcher) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Maps(com.google.common.collect.Maps) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Predicates (com.google.common.base.Predicates)1 Maps (com.google.common.collect.Maps)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 BKException (org.apache.bookkeeper.client.BKException)1 BookKeeper (org.apache.bookkeeper.client.BookKeeper)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)1 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)1 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)1 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1