Search in sources :

Example 1 with WriteCancelledException

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

the class BKAsyncLogWriter method appendCancelledFutures.

private void appendCancelledFutures(List<Future<DLSN>> futures, int numToAdd) {
    final WriteCancelledException cre = new WriteCancelledException(getStreamName());
    for (int i = 0; i < numToAdd; i++) {
        Future<DLSN> cancelledFuture = Future.exception(cre);
        futures.add(cancelledFuture);
    }
}
Also used : WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException)

Example 2 with WriteCancelledException

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

the class TestBKLogSegmentWriter method testAbortShouldNotFlush.

/**
     * Abort a segment log writer should just abort pending writes and not flush buffered data.
     *
     * @throws Exception
     */
@Test(timeout = 60000)
public void testAbortShouldNotFlush() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setOutputBufferSize(Integer.MAX_VALUE);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    ZKDistributedLock lock = createLock("/test/lock-" + runtime.getMethodName(), zkc, true);
    BKLogSegmentWriter writer = createLogSegmentWriter(confLocal, 0L, -1L, lock);
    // Use another lock to wait for writer releasing lock
    ZKDistributedLock lock0 = createLock("/test/lock-" + runtime.getMethodName(), zkc0, false);
    Future<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<Future<DLSN>> futureList = new ArrayList<Future<DLSN>>(numRecords);
    for (int i = 0; i < numRecords; i++) {
        futureList.add(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(i)));
    }
    assertEquals("Last tx id should be " + (numRecords - 1), numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should be -1", -1L, writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should be " + numRecords, 10, writer.getPositionWithinLogSegment());
    // close the writer should flush buffered data and release lock
    abortWriterAndLock(writer, lock);
    Await.result(lockFuture0);
    lock0.checkOwnership();
    assertEquals("Last tx id should still be " + (numRecords - 1), numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should still be " + (numRecords - 1), -1L, writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should still be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should still be " + numRecords, 10, writer.getPositionWithinLogSegment());
    for (int i = 0; i < numRecords; i++) {
        try {
            Await.result(futureList.get(i));
            fail("Should be aborted record " + i + " with transmit exception");
        } catch (WriteCancelledException wce) {
            assertTrue("Record " + i + " should be aborted because of ledger fenced", wce.getCause() instanceof BKTransmitException);
            BKTransmitException bkte = (BKTransmitException) wce.getCause();
            assertEquals("Record " + i + " should be aborted", BKException.Code.InterruptedException, bkte.getBKResultCode());
        }
    }
    // check no entries were written
    LedgerHandle lh = getLedgerHandle(writer);
    LedgerHandle readLh = openLedgerNoRecovery(lh);
    assertTrue("Ledger " + lh.getId() + " should not be closed", readLh.isClosed());
    assertEquals("There should be no entries in ledger " + lh.getId(), LedgerHandle.INVALID_ENTRY_ID, readLh.getLastAddConfirmed());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException) BKTransmitException(com.twitter.distributedlog.exceptions.BKTransmitException) ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) Test(org.junit.Test)

Example 3 with WriteCancelledException

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

the class TestBKLogSegmentWriter method testAbortShouldFailAllWrites.

/**
     * Abort should wait for outstanding transmits to be completed and cancel buffered data.
     *
     * @throws Exception
     */
@Test(timeout = 60000)
public void testAbortShouldFailAllWrites() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setOutputBufferSize(Integer.MAX_VALUE);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    ZKDistributedLock lock = createLock("/test/lock-" + runtime.getMethodName(), zkc, true);
    BKLogSegmentWriter writer = createLogSegmentWriter(confLocal, 0L, -1L, lock);
    // Use another lock to wait for writer releasing lock
    ZKDistributedLock lock0 = createLock("/test/lock-" + runtime.getMethodName(), zkc0, false);
    Future<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<Future<DLSN>> futureList = new ArrayList<Future<DLSN>>(numRecords);
    for (int i = 0; i < numRecords; i++) {
        futureList.add(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(i)));
    }
    assertEquals("Last tx id should be " + (numRecords - 1), numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should be -1", -1L, writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should be " + numRecords, numRecords, writer.getPositionWithinLogSegment());
    final CountDownLatch deferLatch = new CountDownLatch(1);
    writer.getFuturePool().apply(new AbstractFunction0<Object>() {

        @Override
        public Object apply() {
            try {
                deferLatch.await();
            } catch (InterruptedException e) {
                LOG.warn("Interrupted on deferring completion : ", e);
            }
            return null;
        }
    });
    // transmit the buffered data
    FutureUtils.result(writer.flush());
    // add another 10 records
    List<Future<DLSN>> anotherFutureList = new ArrayList<Future<DLSN>>(numRecords);
    for (int i = numRecords; i < 2 * numRecords; i++) {
        anotherFutureList.add(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(i)));
    }
    assertEquals("Last tx id should become " + (2 * numRecords - 1), 2 * numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should become " + (numRecords - 1), (long) (numRecords - 1), writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should still be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should become " + (2 * numRecords), 2 * numRecords, writer.getPositionWithinLogSegment());
    // abort the writer: it waits for outstanding transmits and abort buffered data
    abortWriterAndLock(writer, lock);
    Await.result(lockFuture0);
    lock0.checkOwnership();
    // release defer latch so completion would go through
    deferLatch.countDown();
    List<DLSN> dlsns = Await.result(Future.collect(futureList));
    assertEquals("All first 10 records should be written", numRecords, dlsns.size());
    for (int i = 0; i < numRecords; i++) {
        DLSN dlsn = dlsns.get(i);
        assertEquals("Incorrent ledger sequence number", 0L, dlsn.getLogSegmentSequenceNo());
        assertEquals("Incorrent entry id", 0L, dlsn.getEntryId());
        assertEquals("Inconsistent slot id", i, dlsn.getSlotId());
    }
    for (int i = 0; i < numRecords; i++) {
        try {
            Await.result(anotherFutureList.get(i));
            fail("Should be aborted record " + (numRecords + i) + " with transmit exception");
        } catch (WriteCancelledException wce) {
        // writes should be cancelled.
        }
    }
    assertEquals("Last tx id should still be " + (2 * numRecords - 1), 2 * numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should be still " + (numRecords - 1), (long) (numRecords - 1), writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should become " + futureList.get(futureList.size() - 1), dlsns.get(futureList.size() - 1), writer.getLastDLSN());
    assertEquals("Position should become " + 2 * numRecords, 2 * numRecords, writer.getPositionWithinLogSegment());
    // check only 1 entry were written
    LedgerHandle lh = getLedgerHandle(writer);
    LedgerHandle readLh = openLedgerNoRecovery(lh);
    assertTrue("Ledger " + lh.getId() + " should not be closed", readLh.isClosed());
    assertEquals("Only one entry is written for ledger " + lh.getId(), 0L, lh.getLastAddPushed());
    assertEquals("Only one entry is written for ledger " + lh.getId(), 0L, readLh.getLastAddConfirmed());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException) Future(com.twitter.util.Future) Test(org.junit.Test)

Example 4 with WriteCancelledException

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

the class TestBKLogSegmentWriter method testCloseShouldNotFlushIfInErrorState.

/**
     * Close a log segment writer that is already in error state, should not flush buffered data.
     *
     * @throws Exception
     */
void testCloseShouldNotFlushIfInErrorState(int rcToFailComplete) throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setOutputBufferSize(Integer.MAX_VALUE);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    ZKDistributedLock lock = createLock("/test/lock-" + runtime.getMethodName(), zkc, true);
    BKLogSegmentWriter writer = createLogSegmentWriter(confLocal, 0L, -1L, lock);
    // Use another lock to wait for writer releasing lock
    ZKDistributedLock lock0 = createLock("/test/lock-" + runtime.getMethodName(), zkc0, false);
    Future<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<Future<DLSN>> futureList = new ArrayList<Future<DLSN>>(numRecords);
    for (int i = 0; i < numRecords; i++) {
        futureList.add(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(i)));
    }
    assertEquals("Last tx id should be " + (numRecords - 1), numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should be -1", -1L, writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should be " + numRecords, 10, writer.getPositionWithinLogSegment());
    writer.setTransmitResult(rcToFailComplete);
    // close the writer should release lock but not flush data
    try {
        closeWriterAndLock(writer, lock);
        fail("Close a log segment writer in error state should throw exception");
    } catch (BKTransmitException bkte) {
        assertEquals("Inconsistent rc is thrown", rcToFailComplete, bkte.getBKResultCode());
    }
    Await.result(lockFuture0);
    lock0.checkOwnership();
    assertEquals("Last tx id should still be " + (numRecords - 1), numRecords - 1, writer.getLastTxId());
    assertEquals("Last acked tx id should still be " + (numRecords - 1), -1L, writer.getLastTxIdAcknowledged());
    assertEquals("Last DLSN should still be " + DLSN.InvalidDLSN, DLSN.InvalidDLSN, writer.getLastDLSN());
    assertEquals("Position should still be " + numRecords, 10, writer.getPositionWithinLogSegment());
    for (int i = 0; i < numRecords; i++) {
        try {
            Await.result(futureList.get(i));
            fail("Should be aborted record " + i + " with transmit exception");
        } catch (WriteCancelledException wce) {
            assertTrue("Record " + i + " should be aborted because of ledger fenced", wce.getCause() instanceof BKTransmitException);
            BKTransmitException bkte = (BKTransmitException) wce.getCause();
            assertEquals("Record " + i + " should be aborted", rcToFailComplete, bkte.getBKResultCode());
        }
    }
    // check no entries were written
    LedgerHandle lh = getLedgerHandle(writer);
    LedgerHandle readLh = openLedgerNoRecovery(lh);
    assertFalse("Ledger " + lh.getId() + " should not be closed", readLh.isClosed());
    assertEquals("There should be no entries in ledger " + lh.getId(), LedgerHandle.INVALID_ENTRY_ID, readLh.getLastAddConfirmed());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException) BKTransmitException(com.twitter.distributedlog.exceptions.BKTransmitException) ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock)

Example 5 with WriteCancelledException

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

the class BKLogSegmentWriter method addCompleteDeferredProcessing.

private void addCompleteDeferredProcessing(final BKTransmitPacket transmitPacket, final long entryId, final int rc) {
    boolean cancelPendingPromises = false;
    EntryBuffer recordSet = transmitPacket.getRecordSet();
    synchronized (this) {
        if (transmitResult.compareAndSet(BKException.Code.OK, rc)) {
            // If this is the first time we are setting an error code in the transmitResult then
            // we must cancel pending promises; once this error has been set, more records will not
            // be enqueued; they will be failed with WriteException
            cancelPendingPromises = (BKException.Code.OK != rc);
        } else {
            LOG.warn("Log segment {} entryId {}: Tried to set transmit result to ({}) but is already ({})", new Object[] { fullyQualifiedLogSegment, entryId, rc, transmitResult.get() });
        }
        if (transmitResult.get() != BKException.Code.OK) {
            if (recordSet.hasUserRecords()) {
                transmitDataPacketSize.registerFailedEvent(recordSet.getNumBytes());
            }
        } else {
            // visible by advancing the lastAck
            if (recordSet.hasUserRecords()) {
                transmitDataPacketSize.registerSuccessfulEvent(recordSet.getNumBytes());
                controlFlushNeeded = true;
                if (immediateFlushEnabled) {
                    if (0 == minDelayBetweenImmediateFlushMs) {
                        backgroundFlush(true);
                    } else {
                        scheduleFlushWithDelayIfNeeded(new Callable<Void>() {

                            @Override
                            public Void call() throws Exception {
                                backgroundFlush(true);
                                return null;
                            }
                        }, immFlushSchedFutureRef);
                    }
                }
            }
        }
        // update last dlsn before satisifying future
        if (BKException.Code.OK == transmitResult.get()) {
            DLSN lastDLSNInPacket = recordSet.finalizeTransmit(logSegmentSequenceNumber, entryId);
            if (recordSet.hasUserRecords()) {
                if (null != lastDLSNInPacket && lastDLSN.compareTo(lastDLSNInPacket) < 0) {
                    lastDLSN = lastDLSNInPacket;
                }
            }
        }
    }
    if (BKException.Code.OK == transmitResult.get()) {
        recordSet.completeTransmit(logSegmentSequenceNumber, entryId);
    } else {
        recordSet.abortTransmit(FutureUtils.transmitException(transmitResult.get()));
    }
    if (cancelPendingPromises) {
        // Since the writer is in a bad state no more packets will be tramsitted, and its safe to
        // assign a new empty packet. This is to avoid a race with closeInternal which may also
        // try to cancel the current packet;
        final BKTransmitPacket packetCurrentSaved;
        synchronized (this) {
            packetCurrentSaved = new BKTransmitPacket(recordSetWriter);
            recordSetWriter = newRecordSetWriter();
        }
        packetCurrentSaved.getRecordSet().abortTransmit(new WriteCancelledException(streamName, FutureUtils.transmitException(transmitResult.get())));
    }
}
Also used : WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException) EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) BKException(org.apache.bookkeeper.client.BKException) BKTransmitException(com.twitter.distributedlog.exceptions.BKTransmitException) TransactionIdOutOfOrderException(com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException) WriteCancelledException(com.twitter.distributedlog.exceptions.WriteCancelledException) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException) LockingException(com.twitter.distributedlog.exceptions.LockingException) IOException(java.io.IOException) InvalidEnvelopedEntryException(com.twitter.distributedlog.exceptions.InvalidEnvelopedEntryException) WriteException(com.twitter.distributedlog.exceptions.WriteException) FlushException(com.twitter.distributedlog.exceptions.FlushException)

Aggregations

WriteCancelledException (com.twitter.distributedlog.exceptions.WriteCancelledException)6 BKTransmitException (com.twitter.distributedlog.exceptions.BKTransmitException)3 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)3 Future (com.twitter.util.Future)3 ArrayList (java.util.ArrayList)3 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)3 Test (org.junit.Test)2 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)1 FlushException (com.twitter.distributedlog.exceptions.FlushException)1 InvalidEnvelopedEntryException (com.twitter.distributedlog.exceptions.InvalidEnvelopedEntryException)1 LockingException (com.twitter.distributedlog.exceptions.LockingException)1 LogRecordTooLongException (com.twitter.distributedlog.exceptions.LogRecordTooLongException)1 TransactionIdOutOfOrderException (com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException)1 WriteException (com.twitter.distributedlog.exceptions.WriteException)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BKException (org.apache.bookkeeper.client.BKException)1