Search in sources :

Example 16 with AddEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.

the class ManagedLedgerErrorsTest method recoverLongTimeAfterMultipleWriteErrors.

@Test
public void recoverLongTimeAfterMultipleWriteErrors() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("recoverLongTimeAfterMultipleWriteErrors");
    ManagedCursor cursor = ledger.openCursor("c1");
    bkc.failNow(BKException.Code.BookieHandleNotAvailableException, BKException.Code.BookieHandleNotAvailableException);
    CountDownLatch counter = new CountDownLatch(2);
    AtomicReference<ManagedLedgerException> ex = new AtomicReference<>();
    // Write 2 entries, both should fail the first time and get re-tried internally in a new ledger
    AddEntryCallback cb = new AddEntryCallback() {

        @Override
        public void addComplete(Position position, Object ctx) {
            counter.countDown();
        }

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            log.warn("Error in write", exception);
            ex.set(exception);
            counter.countDown();
        }
    };
    ledger.asyncAddEntry("entry-1".getBytes(), cb, null);
    ledger.asyncAddEntry("entry-2".getBytes(), cb, null);
    counter.await();
    assertNull(ex.get());
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 2);
    // Ensure that we are only creating one new ledger
    // even when there are multiple (here, 2) add entry failed ops
    assertEquals(ledger.getLedgersInfoAsList().size(), 1);
    ledger.addEntry("entry-3".getBytes());
    List<Entry> entries = cursor.readEntries(10);
    assertEquals(entries.size(), 3);
    assertEquals(new String(entries.get(0).getData()), "entry-1");
    assertEquals(new String(entries.get(1).getData()), "entry-2");
    assertEquals(new String(entries.get(2).getData()), "entry-3");
    entries.forEach(e -> e.release());
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)16 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 Position (org.apache.bookkeeper.mledger.Position)10 Test (org.testng.annotations.Test)10 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)9 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)5 ByteBuf (io.netty.buffer.ByteBuf)4 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)3 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)3 Entry (org.apache.bookkeeper.mledger.Entry)3 Matchers.anyObject (org.mockito.Matchers.anyObject)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)2 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)1