Search in sources :

Example 6 with BKException

use of org.apache.bookkeeper.client.BKException in project distributedlog by twitter.

the class ReadAheadWorker method closeCurrentLedgerHandle.

private boolean closeCurrentLedgerHandle() {
    if (currentLH == null) {
        return true;
    }
    boolean retVal = false;
    LedgerDescriptor ld = currentLH;
    try {
        handleCache.closeLedger(ld);
        currentLH = null;
        retVal = true;
    } catch (BKException bke) {
        LOG.debug("BK Exception during closing {} : ", ld, bke);
        handleException(ReadAheadPhase.CLOSE_LEDGER, bke.getCode());
    }
    return retVal;
}
Also used : LedgerDescriptor(com.twitter.distributedlog.LedgerDescriptor) BKException(org.apache.bookkeeper.client.BKException)

Example 7 with BKException

use of org.apache.bookkeeper.client.BKException in project distributedlog by twitter.

the class ReadUtils method getLogRecordNotLessThanTxId.

//
// Search Functions
//
/**
     * Get the log record whose transaction id is not less than provided <code>transactionId</code>.
     *
     * <p>
     * It uses a binary-search like algorithm to find the log record whose transaction id is not less than
     * provided <code>transactionId</code> within a log <code>segment</code>. You could think of a log segment
     * in terms of a sequence of records whose transaction ids are non-decreasing.
     *
     * - The sequence of records within a log segment is divided into N pieces.
     * - Find the piece of records that contains a record whose transaction id is not less than provided
     *   <code>transactionId</code>.
     *
     * N could be chosen based on trading off concurrency and latency.
     * </p>
     *
     * @param logName
     *          name of the log
     * @param segment
     *          metadata of the log segment
     * @param transactionId
     *          transaction id
     * @param executorService
     *          executor service used for processing entries
     * @param handleCache
     *          ledger handle cache
     * @param nWays
     *          how many number of entries to search in parallel
     * @return found log record. none if all transaction ids are less than provided <code>transactionId</code>.
     */
public static Future<Optional<LogRecordWithDLSN>> getLogRecordNotLessThanTxId(final String logName, final LogSegmentMetadata segment, final long transactionId, final ExecutorService executorService, final LedgerHandleCache handleCache, final int nWays) {
    if (!segment.isInProgress()) {
        if (segment.getLastTxId() < transactionId) {
            // all log records whose transaction id is less than provided transactionId
            // then return none
            Optional<LogRecordWithDLSN> noneRecord = Optional.absent();
            return Future.value(noneRecord);
        }
    }
    final Promise<Optional<LogRecordWithDLSN>> promise = new Promise<Optional<LogRecordWithDLSN>>();
    final FutureEventListener<LedgerDescriptor> openLedgerListener = new FutureEventListener<LedgerDescriptor>() {

        @Override
        public void onSuccess(final LedgerDescriptor ld) {
            promise.ensure(new AbstractFunction0<BoxedUnit>() {

                @Override
                public BoxedUnit apply() {
                    handleCache.asyncCloseLedger(ld);
                    return BoxedUnit.UNIT;
                }
            });
            long lastEntryId;
            try {
                lastEntryId = handleCache.getLastAddConfirmed(ld);
            } catch (BKException e) {
                promise.setException(e);
                return;
            }
            if (lastEntryId < 0) {
                // it means that the log segment is created but not written yet or an empty log segment.
                // it is equivalent to 'all log records whose transaction id is less than provided transactionId'
                Optional<LogRecordWithDLSN> nonRecord = Optional.absent();
                promise.setValue(nonRecord);
                return;
            }
            // all log records whose transaction id is not less than provided transactionId
            if (segment.getFirstTxId() >= transactionId) {
                final FirstTxIdNotLessThanSelector selector = new FirstTxIdNotLessThanSelector(transactionId);
                asyncReadRecordFromEntries(logName, ld, handleCache, segment, executorService, new SingleEntryScanContext(0L), selector).addEventListener(new FutureEventListener<LogRecordWithDLSN>() {

                    @Override
                    public void onSuccess(LogRecordWithDLSN value) {
                        promise.setValue(Optional.of(selector.result()));
                    }

                    @Override
                    public void onFailure(Throwable cause) {
                        promise.setException(cause);
                    }
                });
                return;
            }
            getLogRecordNotLessThanTxIdFromEntries(logName, ld, segment, transactionId, executorService, handleCache, Lists.newArrayList(0L, lastEntryId), nWays, Optional.<LogRecordWithDLSN>absent(), promise);
        }

        @Override
        public void onFailure(final Throwable cause) {
            String errMsg = "Error opening log segment [" + segment + "] for find record from " + logName;
            promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause))));
        }
    };
    handleCache.asyncOpenLedger(segment, false).addEventListener(FutureEventListenerRunnable.of(openLedgerListener, executorService));
    return promise;
}
Also used : Optional(com.google.common.base.Optional) IOException(java.io.IOException) FirstTxIdNotLessThanSelector(com.twitter.distributedlog.selector.FirstTxIdNotLessThanSelector) Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener) BKException(org.apache.bookkeeper.client.BKException) BoxedUnit(scala.runtime.BoxedUnit)

Example 8 with BKException

use of org.apache.bookkeeper.client.BKException in project pulsar by yahoo.

the class ManagedLedgerFactoryImpl method shutdown.

@Override
public void shutdown() throws InterruptedException, ManagedLedgerException {
    statsTask.cancel(true);
    int numLedgers = ledgers.size();
    final CountDownLatch latch = new CountDownLatch(numLedgers);
    log.info("Closing {} ledgers", numLedgers);
    for (CompletableFuture<ManagedLedgerImpl> ledgerFuture : ledgers.values()) {
        ManagedLedgerImpl ledger = ledgerFuture.getNow(null);
        if (ledger == null) {
            continue;
        }
        ledger.asyncClose(new AsyncCallbacks.CloseCallback() {

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

            @Override
            public void closeFailed(ManagedLedgerException exception, Object ctx) {
                log.warn("[{}] Got exception when closing managed ledger: {}", ledger.getName(), exception);
                latch.countDown();
            }
        }, null);
    }
    latch.await();
    log.info("{} ledgers closed", numLedgers);
    if (zookeeper != null) {
        zookeeper.close();
    }
    if (isBookkeeperManaged) {
        try {
            bookKeeper.close();
        } catch (BKException e) {
            throw new ManagedLedgerException(e);
        }
    }
    executor.shutdown();
    orderedExecutor.shutdown();
    entryCacheManager.clear();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) BKException(org.apache.bookkeeper.client.BKException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

BKException (org.apache.bookkeeper.client.BKException)8 IOException (java.io.IOException)4 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)3 Promise (com.twitter.util.Promise)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 Optional (com.google.common.base.Optional)1 Stopwatch (com.google.common.base.Stopwatch)1 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)1 LedgerDescriptor (com.twitter.distributedlog.LedgerDescriptor)1 LogSegmentMetadata (com.twitter.distributedlog.LogSegmentMetadata)1 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 ZKException (com.twitter.distributedlog.exceptions.ZKException)1 FirstTxIdNotLessThanSelector (com.twitter.distributedlog.selector.FirstTxIdNotLessThanSelector)1 FutureEventListenerRunnable (com.twitter.distributedlog.util.FutureUtils.FutureEventListenerRunnable)1 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)1 FutureEventListener (com.twitter.util.FutureEventListener)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)1 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)1 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)1