Search in sources :

Example 6 with BKTransmitException

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

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);
    CompletableFuture<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<CompletableFuture<DLSN>> futureList = new ArrayList<CompletableFuture<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);
    Utils.ioResult(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 {
            Utils.ioResult(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 : CompletableFuture(java.util.concurrent.CompletableFuture) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) WriteCancelledException(org.apache.distributedlog.exceptions.WriteCancelledException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) ArrayList(java.util.ArrayList) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock) Test(org.junit.Test)

Example 7 with BKTransmitException

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

the class TestBKLogSegmentWriter method testCloseShouldFailIfLedgerFenced.

/**
 * Close the writer when ledger is fenced: it should release the lock, fail on flushing data and throw exception.
 *
 * @throws Exception
 */
@Test(timeout = 60000)
public void testCloseShouldFailIfLedgerFenced() 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);
    CompletableFuture<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<CompletableFuture<DLSN>> futureList = new ArrayList<CompletableFuture<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());
    // fence the ledger
    fenceLedger(getLedgerHandle(writer));
    // close the writer: it should release the lock, fail on flushing data and throw exception
    try {
        closeWriterAndLock(writer, lock);
        fail("Close a log segment writer when ledger is fenced should throw exception");
    } catch (BKTransmitException bkte) {
        assertEquals("Inconsistent rc is thrown", BKException.Code.LedgerFencedException, bkte.getBKResultCode());
    }
    Utils.ioResult(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 {
            Utils.ioResult(futureList.get(i));
            fail("Should be aborted record " + i + " with transmit exception");
        } catch (BKTransmitException bkte) {
            assertEquals("Record " + i + " should be aborted", BKException.Code.LedgerFencedException, bkte.getBKResultCode());
        }
    }
    // check no entries were written
    LedgerHandle lh = getLedgerHandle(writer);
    LedgerHandle readLh = openLedgerNoRecovery(lh);
    assertTrue("Ledger " + lh.getId() + " should be closed", readLh.isClosed());
    assertEquals("There should be no entries in ledger " + lh.getId(), LedgerHandle.INVALID_ENTRY_ID, readLh.getLastAddConfirmed());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) ArrayList(java.util.ArrayList) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock) Test(org.junit.Test)

Example 8 with BKTransmitException

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

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);
    CompletableFuture<ZKDistributedLock> lockFuture0 = lock0.asyncAcquire();
    // add 10 records
    int numRecords = 10;
    List<CompletableFuture<DLSN>> futureList = new ArrayList<CompletableFuture<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());
    }
    Utils.ioResult(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 {
            Utils.ioResult(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 : CompletableFuture(java.util.concurrent.CompletableFuture) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) WriteCancelledException(org.apache.distributedlog.exceptions.WriteCancelledException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) ArrayList(java.util.ArrayList) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock)

Example 9 with BKTransmitException

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

the class TestAsyncReaderWriter method testCloseAndCompleteLogSegmentWhenStreamIsInError.

@Test(timeout = 60000)
public void testCloseAndCompleteLogSegmentWhenStreamIsInError() throws Exception {
    String name = "distrlog-close-and-complete-logsegment-when-stream-is-in-error";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    BKDistributedLogManager dlm = (BKDistributedLogManager) createNewDLM(confLocal, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
    long txId = 1L;
    for (int i = 0; i < 5; i++) {
        Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txId++)));
    }
    BKLogSegmentWriter logWriter = writer.getCachedLogWriter();
    BKNamespaceDriver driver = (BKNamespaceDriver) dlm.getNamespaceDriver();
    // fence the ledger
    driver.getReaderBKC().get().openLedger(logWriter.getLogSegmentId(), BookKeeper.DigestType.CRC32, confLocal.getBKDigestPW().getBytes(UTF_8));
    try {
        Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txId++)));
        fail("Should fail write to a fenced ledger with BKTransmitException");
    } catch (BKTransmitException bkte) {
    // expected
    }
    try {
        writer.closeAndComplete();
        fail("Should fail to complete a log segment when its ledger is fenced");
    } catch (BKTransmitException bkte) {
    // expected
    }
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(1, segments.size());
    assertTrue(segments.get(0).isInProgress());
    dlm.close();
}
Also used : DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) BKNamespaceDriver(org.apache.distributedlog.impl.BKNamespaceDriver) Test(org.junit.Test)

Example 10 with BKTransmitException

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

the class TestBKDistributedLogManager method testTwoWritersOnLockDisabled.

@Test(timeout = 60000)
public void testTwoWritersOnLockDisabled() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setWriteLockEnabled(false);
    String name = "distrlog-two-writers-lock-disabled";
    DistributedLogManager manager = createNewDLM(confLocal, name);
    AsyncLogWriter writer1 = Utils.ioResult(manager.openAsyncLogWriter());
    Utils.ioResult(writer1.write(DLMTestUtil.getLogRecordInstance(1L)));
    AsyncLogWriter writer2 = Utils.ioResult(manager.openAsyncLogWriter());
    Utils.ioResult(writer2.write(DLMTestUtil.getLogRecordInstance(2L)));
    // write a record to writer 1 again
    try {
        Utils.ioResult(writer1.write(DLMTestUtil.getLogRecordInstance(3L)));
        fail("Should fail writing record to writer 1 again as writer 2 took over the ownership");
    } catch (BKTransmitException bkte) {
        assertEquals(BKException.Code.LedgerFencedException, bkte.getBKResultCode());
    }
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) 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