Search in sources :

Example 6 with LogEmptyException

use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.

the class BKLogHandler method asyncGetLastLogRecord.

private void asyncGetLastLogRecord(final Iterator<LogSegmentMetadata> ledgerIter, final Promise<LogRecordWithDLSN> promise, final boolean fence, final boolean includeControlRecord, final boolean includeEndOfStream) {
    if (ledgerIter.hasNext()) {
        LogSegmentMetadata metadata = ledgerIter.next();
        asyncReadLastRecord(metadata, fence, includeControlRecord, includeEndOfStream).addEventListener(new FutureEventListener<LogRecordWithDLSN>() {

            @Override
            public void onSuccess(LogRecordWithDLSN record) {
                if (null == record) {
                    asyncGetLastLogRecord(ledgerIter, promise, fence, includeControlRecord, includeEndOfStream);
                } else {
                    promise.setValue(record);
                }
            }

            @Override
            public void onFailure(Throwable cause) {
                promise.setException(cause);
            }
        });
    } else {
        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
    }
}
Also used : LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException)

Example 7 with LogEmptyException

use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.

the class BKLogHandler method forceGetLedgerList.

/**
     * Get a list of all segments in the journal.
     */
protected List<LogSegmentMetadata> forceGetLedgerList(final Comparator<LogSegmentMetadata> comparator, final LogSegmentFilter segmentFilter, boolean throwOnEmpty) throws IOException {
    final List<LogSegmentMetadata> ledgers = new ArrayList<LogSegmentMetadata>();
    final AtomicInteger result = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Stopwatch stopwatch = Stopwatch.createStarted();
    asyncGetLedgerListInternal(comparator, segmentFilter, null, new GenericCallback<List<LogSegmentMetadata>>() {

        @Override
        public void operationComplete(int rc, List<LogSegmentMetadata> logSegmentMetadatas) {
            result.set(rc);
            if (KeeperException.Code.OK.intValue() == rc) {
                ledgers.addAll(logSegmentMetadatas);
            } else {
                LOG.error("Failed to get ledger list for {} : with error {}", getFullyQualifiedName(), rc);
            }
            latch.countDown();
        }
    }, new AtomicInteger(conf.getZKNumRetries()), new AtomicLong(conf.getZKRetryBackoffStartMillis()));
    try {
        latch.await();
    } catch (InterruptedException e) {
        forceGetListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        throw new DLInterruptedException("Interrupted on reading ledger list from zkfor " + getFullyQualifiedName(), e);
    }
    long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
    KeeperException.Code rc = KeeperException.Code.get(result.get());
    if (rc == KeeperException.Code.OK) {
        forceGetListStat.registerSuccessfulEvent(elapsedMicros);
    } else {
        forceGetListStat.registerFailedEvent(elapsedMicros);
        if (KeeperException.Code.NONODE == rc) {
            throw new LogNotFoundException("Log " + getFullyQualifiedName() + " is not found");
        } else {
            throw new IOException("ZK Exception " + rc + " reading ledger list for " + getFullyQualifiedName());
        }
    }
    if (throwOnEmpty && ledgers.isEmpty()) {
        throw new LogEmptyException("Log " + getFullyQualifiedName() + " is empty");
    }
    return ledgers;
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with LogEmptyException

use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.

the class BKLogHandler method asyncForceGetLedgerList.

protected Future<List<LogSegmentMetadata>> asyncForceGetLedgerList(final Comparator<LogSegmentMetadata> comparator, final LogSegmentFilter segmentFilter, final boolean throwOnEmpty) {
    final Promise<List<LogSegmentMetadata>> promise = new Promise<List<LogSegmentMetadata>>();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    asyncGetLedgerListWithRetries(comparator, segmentFilter, null).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

        @Override
        public void onSuccess(List<LogSegmentMetadata> ledgers) {
            forceGetListStat.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
            if (ledgers.isEmpty() && throwOnEmpty) {
                promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " is empty"));
            } else {
                promise.setValue(ledgers);
            }
        }

        @Override
        public void onFailure(Throwable cause) {
            forceGetListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) Stopwatch(com.google.common.base.Stopwatch) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

LogEmptyException (com.twitter.distributedlog.exceptions.LogEmptyException)8 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)3 Promise (com.twitter.util.Promise)3 URI (java.net.URI)3 Stopwatch (com.google.common.base.Stopwatch)2 FutureEventListener (com.twitter.util.FutureEventListener)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 TransformedRecord (com.twitter.distributedlog.thrift.messaging.TransformedRecord)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 TException (org.apache.thrift.TException)1 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)1 KeeperException (org.apache.zookeeper.KeeperException)1