Search in sources :

Example 1 with BKTransmitException

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

the class BKLogSegmentWriter method flushIfNeeded.

// Based on transmit buffer size, immediate flush, etc., should we flush the current
// packet now.
void flushIfNeeded() throws BKTransmitException, WriteException, InvalidEnvelopedEntryException, LockingException, FlushException {
    if (outstandingBytes > transmissionThreshold) {
        // If flush delay is disabled, flush immediately, else schedule appropriately.
        if (0 == minDelayBetweenImmediateFlushMs) {
            checkStateAndTransmit();
        } else {
            scheduleFlushWithDelayIfNeeded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    checkStateAndTransmit();
                    return null;
                }
            }, transmitSchedFutureRefUpdater);
            // Timing here is not very important--the last flush failed and we should
            // indicate this to the caller. The next flush may succeed and unset the
            // scheduledFlushException in which case the next write will succeed (if the caller
            // hasn't already closed the writer).
            Exception exec = scheduledFlushExceptionUpdater.get(this);
            if (exec != null) {
                throw new FlushException("Last flush encountered an error while writing data to the backend", getLastTxId(), getLastTxIdAcknowledged(), exec);
            }
        }
    }
}
Also used : FlushException(org.apache.distributedlog.exceptions.FlushException) FlushException(org.apache.distributedlog.exceptions.FlushException) BKException(org.apache.bookkeeper.client.BKException) WriteException(org.apache.distributedlog.exceptions.WriteException) InvalidEnvelopedEntryException(org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException) LockingException(org.apache.distributedlog.exceptions.LockingException) WriteCancelledException(org.apache.distributedlog.exceptions.WriteCancelledException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) IOException(java.io.IOException) TransactionIdOutOfOrderException(org.apache.distributedlog.exceptions.TransactionIdOutOfOrderException) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException)

Example 2 with BKTransmitException

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

the class BKLogSegmentEntryStore method openRandomAccessReader.

@Override
public CompletableFuture<LogSegmentRandomAccessEntryReader> openRandomAccessReader(final LogSegmentMetadata segment, final boolean fence) {
    final BookKeeper bk;
    try {
        bk = this.bkc.get();
    } catch (IOException e) {
        return FutureUtils.exception(e);
    }
    final CompletableFuture<LogSegmentRandomAccessEntryReader> openPromise = new CompletableFuture<LogSegmentRandomAccessEntryReader>();
    AsyncCallback.OpenCallback openCallback = new AsyncCallback.OpenCallback() {

        @Override
        public void openComplete(int rc, LedgerHandle lh, Object ctx) {
            if (BKException.Code.OK != rc) {
                FutureUtils.completeExceptionally(openPromise, new BKTransmitException("Failed to open ledger handle for log segment " + segment, rc));
                return;
            }
            LogSegmentRandomAccessEntryReader reader = new BKLogSegmentRandomAccessEntryReader(segment, lh, conf);
            FutureUtils.complete(openPromise, reader);
        }
    };
    if (segment.isInProgress() && !fence) {
        bk.asyncOpenLedgerNoRecovery(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, openCallback, null);
    } else {
        bk.asyncOpenLedger(segment.getLogSegmentId(), BookKeeper.DigestType.CRC32, passwd, openCallback, null);
    }
    return openPromise;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) AsyncCallback(org.apache.bookkeeper.client.AsyncCallback) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) BookKeeper(org.apache.bookkeeper.client.BookKeeper) LogSegmentRandomAccessEntryReader(org.apache.distributedlog.logsegment.LogSegmentRandomAccessEntryReader) IOException(java.io.IOException)

Example 3 with BKTransmitException

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

the class BKLogSegmentEntryStore method openComplete.

@Override
public void openComplete(int rc, LedgerHandle lh, Object ctx) {
    OpenReaderRequest request = (OpenReaderRequest) ctx;
    if (BKException.Code.OK != rc) {
        FutureUtils.completeExceptionally(request.openPromise, new BKTransmitException("Failed to open ledger handle for log segment " + request.segment, rc));
        return;
    }
    // successfully open a ledger
    try {
        LogSegmentEntryReader reader = new BKLogSegmentEntryReader(request.segment, lh, request.startEntryId, bkc.get(), scheduler, conf, statsLogger, failureInjector);
        FutureUtils.complete(request.openPromise, reader);
    } catch (IOException e) {
        FutureUtils.completeExceptionally(request.openPromise, e);
    }
}
Also used : LogSegmentEntryReader(org.apache.distributedlog.logsegment.LogSegmentEntryReader) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) IOException(java.io.IOException)

Example 4 with BKTransmitException

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

the class BKLogSegmentRandomAccessEntryReader method readComplete.

@SuppressWarnings("unchecked")
@Override
public void readComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> entries, Object ctx) {
    CompletableFuture<List<Entry.Reader>> promise = (CompletableFuture<List<Entry.Reader>>) ctx;
    if (BKException.Code.OK == rc) {
        List<Entry.Reader> entryList = Lists.newArrayList();
        while (entries.hasMoreElements()) {
            LedgerEntry entry = entries.nextElement();
            try {
                entryList.add(processReadEntry(entry));
            } catch (IOException ioe) {
                // release the buffers
                while (entries.hasMoreElements()) {
                    LedgerEntry le = entries.nextElement();
                    le.getEntryBuffer().release();
                }
                FutureUtils.completeExceptionally(promise, ioe);
                return;
            } finally {
                entry.getEntryBuffer().release();
            }
        }
        FutureUtils.complete(promise, entryList);
    } else {
        FutureUtils.completeExceptionally(promise, new BKTransmitException("Failed to read entries :", rc));
    }
}
Also used : LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Entry(org.apache.distributedlog.Entry) CompletableFuture(java.util.concurrent.CompletableFuture) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) LogSegmentRandomAccessEntryReader(org.apache.distributedlog.logsegment.LogSegmentRandomAccessEntryReader) List(java.util.List) IOException(java.io.IOException)

Example 5 with BKTransmitException

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

the class TestBKLogSegmentWriter method testNondurableWriteAfterLedgerIsFenced.

/**
 * Non durable write should fail if the log segment is fenced.
 *
 * @throws Exception
 */
@Test(timeout = 60000)
public void testNondurableWriteAfterLedgerIsFenced() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setOutputBufferSize(Integer.MAX_VALUE);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setDurableWriteEnabled(false);
    ZKDistributedLock lock = createLock("/test/lock-" + runtime.getMethodName(), zkc, true);
    BKLogSegmentWriter writer = createLogSegmentWriter(confLocal, 0L, -1L, lock);
    // fence the ledger
    fenceLedger(getLedgerHandle(writer));
    LogRecord record = DLMTestUtil.getLogRecordInstance(1);
    record.setControl();
    try {
        Utils.ioResult(writer.asyncWrite(record));
        fail("Should fail the writer if the log segment is already fenced");
    } catch (BKTransmitException bkte) {
        // expected
        assertEquals(BKException.Code.LedgerFencedException, bkte.getBKResultCode());
    }
    try {
        Utils.ioResult(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(2)));
        fail("Should fail the writer if the log segment is already fenced");
    } catch (WriteException we) {
    // expected
    }
    abortWriterAndLock(writer, lock);
}
Also used : WriteException(org.apache.distributedlog.exceptions.WriteException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock) Test(org.junit.Test)

Aggregations

BKTransmitException (org.apache.distributedlog.exceptions.BKTransmitException)12 IOException (java.io.IOException)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Test (org.junit.Test)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)4 ZKDistributedLock (org.apache.distributedlog.lock.ZKDistributedLock)4 ArrayList (java.util.ArrayList)3 WriteCancelledException (org.apache.distributedlog.exceptions.WriteCancelledException)3 WriteException (org.apache.distributedlog.exceptions.WriteException)3 InvalidEnvelopedEntryException (org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException)2 LogSegmentRandomAccessEntryReader (org.apache.distributedlog.logsegment.LogSegmentRandomAccessEntryReader)2 ByteBuf (io.netty.buffer.ByteBuf)1 List (java.util.List)1 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)1 BKException (org.apache.bookkeeper.client.BKException)1 BookKeeper (org.apache.bookkeeper.client.BookKeeper)1 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 Entry (org.apache.distributedlog.Entry)1 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)1