Search in sources :

Example 1 with OpenCallback

use of org.apache.bookkeeper.client.AsyncCallback.OpenCallback 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)

Aggregations

OpenCallback (org.apache.bookkeeper.client.AsyncCallback.OpenCallback)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 MetaStoreException (org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException)1 Stat (org.apache.bookkeeper.mledger.impl.MetaStore.Stat)1 ManagedLedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo)1 LedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo)1