Search in sources :

Example 11 with DLIllegalStateException

use of org.apache.distributedlog.exceptions.DLIllegalStateException in project bookkeeper by apache.

the class BKLogSegmentEntryReader method readEntriesFromReadAheadCache.

private void readEntriesFromReadAheadCache(PendingReadRequest nextRequest) {
    while (!nextRequest.hasReadEnoughEntries()) {
        CacheEntry entry;
        boolean hitEndOfLogSegment;
        synchronized (this) {
            entry = readAheadEntries.peek();
            hitEndOfLogSegment = (null == entry) && isEndOfLogSegment();
        }
        // reach end of log segment
        if (hitEndOfLogSegment) {
            completeExceptionally(new EndOfLogSegmentException(getSegment().getZNodeName()), false);
            return;
        }
        if (null == entry) {
            return;
        }
        // entry is not complete yet.
        if (!entry.isDone()) {
            // we already reached end of the log segment
            if (isEndOfLogSegment(entry.getEntryId())) {
                completeExceptionally(new EndOfLogSegmentException(getSegment().getZNodeName()), false);
            }
            return;
        }
        if (entry.isSuccess()) {
            CacheEntry removedEntry = readAheadEntries.poll();
            try {
                if (entry != removedEntry) {
                    DLIllegalStateException ise = new DLIllegalStateException("Unexpected condition at reading from " + getSegment());
                    completeExceptionally(ise, false);
                    return;
                }
                try {
                    // the reference is retained on `entry.getEntry()`.
                    // Entry.Reader is responsible for releasing it.
                    nextRequest.addEntry(processReadEntry(entry.getEntry()));
                } catch (IOException e) {
                    completeExceptionally(e, false);
                    return;
                }
            } finally {
                removedEntry.release();
            }
        } else if (skipBrokenEntries && BKException.Code.DigestMatchException == entry.getRc()) {
            // skip this entry and move forward
            skippedBrokenEntriesCounter.inc();
            CacheEntry removedEntry = readAheadEntries.poll();
            removedEntry.release();
            continue;
        } else {
            completeExceptionally(new BKTransmitException("Encountered issue on reading entry " + entry.getEntryId() + " @ log segment " + getSegment(), entry.getRc()), false);
            return;
        }
    }
}
Also used : EndOfLogSegmentException(org.apache.distributedlog.exceptions.EndOfLogSegmentException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) DLIllegalStateException(org.apache.distributedlog.exceptions.DLIllegalStateException) IOException(java.io.IOException)

Example 12 with DLIllegalStateException

use of org.apache.distributedlog.exceptions.DLIllegalStateException in project bookkeeper by apache.

the class BKLogSegmentEntryReader method safeRun.

/**
 * The core function to propagate fetched entries to read requests.
 */
@Override
public void safeRun() {
    long scheduleCountLocal = scheduleCountUpdater.get(this);
    while (true) {
        PendingReadRequest nextRequest = null;
        synchronized (readQueue) {
            nextRequest = readQueue.peek();
        }
        // if read queue is empty, nothing to read, return
        if (null == nextRequest) {
            scheduleCountUpdater.set(this, 0L);
            return;
        }
        // we don't know the last consumed read
        if (null == lastExceptionUpdater.get(this)) {
            if (nextRequest.getPromise().isCancelled()) {
                completeExceptionally(new DLInterruptedException("Interrupted on reading log segment " + getSegment() + " : " + nextRequest.getPromise().isCancelled()), false);
            }
        }
        // if the reader is in error state, stop read
        if (checkClosedOrInError()) {
            return;
        }
        // read entries from readahead cache to satisfy next read request
        readEntriesFromReadAheadCache(nextRequest);
        // check if we can satisfy the read request
        if (nextRequest.hasReadEntries()) {
            PendingReadRequest request;
            synchronized (readQueue) {
                request = readQueue.poll();
            }
            if (null != request && nextRequest == request) {
                request.complete();
            } else {
                DLIllegalStateException ise = new DLIllegalStateException("Unexpected condition at reading from " + getSegment());
                nextRequest.completeExceptionally(ise);
                if (null != request) {
                    request.completeExceptionally(ise);
                }
                completeExceptionally(ise, false);
            }
        } else {
            if (0 == scheduleCountLocal) {
                return;
            }
            scheduleCountLocal = scheduleCountUpdater.decrementAndGet(this);
        }
    }
}
Also used : DLIllegalStateException(org.apache.distributedlog.exceptions.DLIllegalStateException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException)

Aggregations

DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)12 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)2 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)2 LogNotFoundException (org.apache.distributedlog.exceptions.LogNotFoundException)2 Stopwatch (com.google.common.base.Stopwatch)1 URI (java.net.URI)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)1 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)1 LogReader (org.apache.distributedlog.api.LogReader)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)1 BKTransmitException (org.apache.distributedlog.exceptions.BKTransmitException)1 EndOfLogSegmentException (org.apache.distributedlog.exceptions.EndOfLogSegmentException)1 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 LogReadException (org.apache.distributedlog.exceptions.LogReadException)1 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)1