Search in sources :

Example 1 with ManagedLedgerException

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

the class ManagedLedgerImpl method initialize.

synchronized void initialize(final ManagedLedgerInitializeLedgerCallback callback, final Object ctx) {
    log.info("Opening managed ledger {}", name);
    // Fetch the list of existing ledgers in the managed ledger
    store.getManagedLedgerInfo(name, new MetaStoreCallback<ManagedLedgerInfo>() {

        @Override
        public void operationComplete(ManagedLedgerInfo mlInfo, Stat stat) {
            ledgersStat = stat;
            for (LedgerInfo ls : mlInfo.getLedgerInfoList()) {
                ledgers.put(ls.getLedgerId(), ls);
            }
            // Last ledger stat may be zeroed, we must update it
            if (ledgers.size() > 0) {
                final long id = ledgers.lastKey();
                OpenCallback opencb = (rc, lh, ctx1) -> {
                    executor.submitOrdered(name, safeRun(() -> {
                        mbean.endDataLedgerOpenOp();
                        if (log.isDebugEnabled()) {
                            log.debug("[{}] Opened ledger {}: ", name, id, BKException.getMessage(rc));
                        }
                        if (rc == BKException.Code.OK) {
                            LedgerInfo info = LedgerInfo.newBuilder().setLedgerId(id).setEntries(lh.getLastAddConfirmed() + 1).setSize(lh.getLength()).setTimestamp(System.currentTimeMillis()).build();
                            ledgers.put(id, info);
                            initializeBookKeeper(callback);
                        } else if (rc == BKException.Code.NoSuchLedgerExistsException) {
                            log.warn("[{}] Ledger not found: {}", name, ledgers.lastKey());
                            ledgers.remove(ledgers.lastKey());
                            initializeBookKeeper(callback);
                        } else {
                            log.error("[{}] Failed to open ledger {}: {}", name, id, BKException.getMessage(rc));
                            callback.initializeFailed(new ManagedLedgerException(BKException.getMessage(rc)));
                            return;
                        }
                    }));
                };
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Opening legder {}", name, id);
                }
                mbean.startDataLedgerOpenOp();
                bookKeeper.asyncOpenLedger(id, config.getDigestType(), config.getPassword(), opencb, null);
            } else {
                initializeBookKeeper(callback);
            }
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            callback.initializeFailed(new ManagedLedgerException(e));
        }
    });
}
Also used : ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) OpenCallback(org.apache.bookkeeper.client.AsyncCallback.OpenCallback) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo)

Example 2 with ManagedLedgerException

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

the class ManagedLedgerImpl method createComplete.

// //////////////////////////////////////////////////////////////////////
// Callbacks
@Override
public synchronized void createComplete(int rc, final LedgerHandle lh, Object ctx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] createComplete rc={} ledger={}", name, rc, lh != null ? lh.getId() : -1);
    }
    mbean.endDataLedgerCreateOp();
    if (rc != BKException.Code.OK) {
        log.error("[{}] Error creating ledger rc={} {}", name, rc, BKException.getMessage(rc));
        ManagedLedgerException status = new ManagedLedgerException(BKException.getMessage(rc));
        // Empty the list of pending requests and make all of them fail
        clearPendingAddEntries(status);
        lastLedgerCreationFailureTimestamp = System.currentTimeMillis();
        STATE_UPDATER.set(this, State.ClosedLedger);
    } else {
        log.info("[{}] Created new ledger {}", name, lh.getId());
        ledgers.put(lh.getId(), LedgerInfo.newBuilder().setLedgerId(lh.getId()).setTimestamp(0).build());
        currentLedger = lh;
        currentLedgerEntries = 0;
        currentLedgerSize = 0;
        final MetaStoreCallback<Void> cb = new MetaStoreCallback<Void>() {

            @Override
            public void operationComplete(Void v, Stat stat) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Updating of ledgers list after create complete. version={}", name, stat);
                }
                ledgersStat = stat;
                ledgersListMutex.unlock();
                updateLedgersIdsComplete(stat);
                synchronized (ManagedLedgerImpl.this) {
                    mbean.addLedgerSwitchLatencySample(System.nanoTime() - lastLedgerCreationInitiationTimestamp, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public void operationFailed(MetaStoreException e) {
                if (e instanceof BadVersionException) {
                    synchronized (ManagedLedgerImpl.this) {
                        log.error("[{}] Failed to udpate ledger list. z-node version mismatch. Closing managed ledger", name);
                        STATE_UPDATER.set(ManagedLedgerImpl.this, State.Fenced);
                        clearPendingAddEntries(e);
                        return;
                    }
                }
                log.warn("[{}] Error updating meta data with the new list of ledgers: {}", name, e.getMessage());
                // Remove the ledger, since we failed to update the list
                ledgers.remove(lh.getId());
                mbean.startDataLedgerDeleteOp();
                bookKeeper.asyncDeleteLedger(lh.getId(), (rc1, ctx1) -> {
                    mbean.endDataLedgerDeleteOp();
                    if (rc1 != BKException.Code.OK) {
                        log.warn("[{}] Failed to delete ledger {}: {}", name, lh.getId(), BKException.getMessage(rc1));
                    }
                }, null);
                ledgersListMutex.unlock();
                synchronized (ManagedLedgerImpl.this) {
                    lastLedgerCreationFailureTimestamp = System.currentTimeMillis();
                    STATE_UPDATER.set(ManagedLedgerImpl.this, State.ClosedLedger);
                    clearPendingAddEntries(e);
                }
            }
        };
        updateLedgersListAfterRollover(cb);
    }
}
Also used : MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)

Example 3 with ManagedLedgerException

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

the class ManagedLedgerImpl method asyncDelete.

@Override
public void asyncDelete(final DeleteLedgerCallback callback, final Object ctx) {
    // Delete the managed ledger without closing, since we are not interested in gracefully closing cursors and
    // ledgers
    STATE_UPDATER.set(this, State.Fenced);
    List<ManagedCursor> cursors = Lists.newArrayList(this.cursors);
    if (cursors.isEmpty()) {
        // No cursors to delete, proceed with next step
        deleteAllLedgers(callback, ctx);
        return;
    }
    AtomicReference<ManagedLedgerException> cursorDeleteException = new AtomicReference<>();
    AtomicInteger cursorsToDelete = new AtomicInteger(cursors.size());
    for (ManagedCursor cursor : cursors) {
        asyncDeleteCursor(cursor.getName(), new DeleteCursorCallback() {

            @Override
            public void deleteCursorComplete(Object ctx) {
                if (cursorsToDelete.decrementAndGet() == 0) {
                    if (cursorDeleteException.get() != null) {
                        // Some cursor failed to delete
                        callback.deleteLedgerFailed(cursorDeleteException.get(), ctx);
                        return;
                    }
                    // All cursors deleted, continue with deleting all ledgers
                    deleteAllLedgers(callback, ctx);
                }
            }

            @Override
            public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
                log.warn("[{}] Failed to delete cursor {}", name, cursor, exception);
                cursorDeleteException.compareAndSet(null, exception);
                if (cursorsToDelete.decrementAndGet() == 0) {
                    // Trigger callback only once
                    callback.deleteLedgerFailed(exception, ctx);
                }
            }
        }, null);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 4 with ManagedLedgerException

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

the class ManagedLedgerImpl method deleteAllLedgers.

private void deleteAllLedgers(DeleteLedgerCallback callback, Object ctx) {
    List<LedgerInfo> ledgers = Lists.newArrayList(ManagedLedgerImpl.this.ledgers.values());
    AtomicInteger ledgersToDelete = new AtomicInteger(ledgers.size());
    if (ledgers.isEmpty()) {
        // No ledgers to delete, proceed with deleting metadata
        deleteMetadata(callback, ctx);
        return;
    }
    for (LedgerInfo ls : ledgers) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Deleting ledger {}", name, ls);
        }
        bookKeeper.asyncDeleteLedger(ls.getLedgerId(), (rc, ctx1) -> {
            switch(rc) {
                case BKException.Code.NoSuchLedgerExistsException:
                    log.warn("[{}] Ledger {} not found when deleting it", name, ls.getLedgerId());
                case BKException.Code.OK:
                    if (ledgersToDelete.decrementAndGet() == 0) {
                        // All ledgers deleted, now remove ML metadata
                        deleteMetadata(callback, ctx);
                    }
                    break;
                default:
                    // Handle error
                    log.warn("[{}] Failed to delete ledger {} -- {}", name, ls.getLedgerId(), BKException.getMessage(rc));
                    int toDelete = ledgersToDelete.get();
                    if (toDelete != -1 && ledgersToDelete.compareAndSet(toDelete, -1)) {
                        // Trigger callback only once
                        callback.deleteLedgerFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
                    }
            }
        }, null);
    }
}
Also used : ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 5 with ManagedLedgerException

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

the class ManagedLedgerImpl method close.

@Override
public void close() throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(Object ctx) {
            counter.countDown();
        }

        @Override
        public void closeFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during managed ledger close");
    }
    if (result.exception != null) {
        log.error("[{}] Error closing managed ledger", name, result.exception);
        throw result.exception;
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)133 Test (org.testng.annotations.Test)70 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)65 CountDownLatch (java.util.concurrent.CountDownLatch)63 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)46 Entry (org.apache.bookkeeper.mledger.Entry)36 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)33 Position (org.apache.bookkeeper.mledger.Position)33 CompletableFuture (java.util.concurrent.CompletableFuture)23 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 List (java.util.List)18 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)18 BKException (org.apache.bookkeeper.client.BKException)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)13 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)13 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13