Search in sources :

Example 1 with Futures

use of org.apache.bookkeeper.mledger.util.Futures in project pulsar by yahoo.

the class ManagedLedgerImpl method asyncClose.

@Override
public synchronized void asyncClose(final CloseCallback callback, final Object ctx) {
    State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        factory.close(this);
        callback.closeFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to close a closed managed ledger", name);
        }
        callback.closeComplete(ctx);
        return;
    }
    log.info("[{}] Closing managed ledger", name);
    factory.close(this);
    STATE_UPDATER.set(this, State.Closed);
    LedgerHandle lh = currentLedger;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }
    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.closeFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
            return;
        }
        // Close all cursors in parallel
        List<CompletableFuture<Void>> futures = Lists.newArrayList();
        for (ManagedCursor cursor : cursors) {
            Futures.CloseFuture closeFuture = new Futures.CloseFuture();
            cursor.asyncClose(closeFuture, null);
            futures.add(closeFuture);
        }
        Futures.waitForAll(futures).thenRun(() -> {
            callback.closeComplete(ctx);
        }).exceptionally(exception -> {
            callback.closeFailed(new ManagedLedgerException(exception), ctx);
            return null;
        });
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CompletableFuture(java.util.concurrent.CompletableFuture) Futures(org.apache.bookkeeper.mledger.util.Futures) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 2 with Futures

use of org.apache.bookkeeper.mledger.util.Futures in project incubator-pulsar by apache.

the class ManagedLedgerImpl method closeAllCursors.

private void closeAllCursors(CloseCallback callback, final Object ctx) {
    // Close all cursors in parallel
    List<CompletableFuture<Void>> futures = Lists.newArrayList();
    for (ManagedCursor cursor : cursors) {
        Futures.CloseFuture closeFuture = new Futures.CloseFuture();
        cursor.asyncClose(closeFuture, null);
        futures.add(closeFuture);
    }
    Futures.waitForAll(futures).thenRun(() -> {
        callback.closeComplete(ctx);
    }).exceptionally(exception -> {
        callback.closeFailed(ManagedLedgerException.getManagedLedgerException(exception.getCause()), ctx);
        return null;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Futures(org.apache.bookkeeper.mledger.util.Futures) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)2 Futures (org.apache.bookkeeper.mledger.util.Futures)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 ManagedLedgerFencedException (org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)1