Search in sources :

Example 41 with ManagedCursor

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

the class ManagedLedgerTest method readWithErrors1.

@Test(timeOut = 20000)
public void readWithErrors1() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    stopZooKeeper();
    stopBookKeeper();
    try {
        cursor.readEntries(10);
        fail("should have failed");
    } catch (ManagedLedgerException e) {
    // ok
    }
    try {
        ledger.addEntry("dummy-entry-3".getBytes(Encoding));
        fail("should have failed");
    } catch (ManagedLedgerException e) {
    // ok
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 42 with ManagedCursor

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

the class ManagedLedgerTest method testConcurrentOpenCursor.

@Test
public void testConcurrentOpenCursor() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testConcurrentOpenCursor");
    final AtomicReference<ManagedCursor> cursor1 = new AtomicReference<>(null);
    final AtomicReference<ManagedCursor> cursor2 = new AtomicReference<>(null);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch latch = new CountDownLatch(2);
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
        } catch (Exception e) {
        }
        ledger.asyncOpenCursor("c1", new OpenCursorCallback() {

            @Override
            public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                latch.countDown();
            }

            @Override
            public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                cursor1.set(cursor);
                latch.countDown();
            }
        }, null);
    });
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
        } catch (Exception e) {
        }
        ledger.asyncOpenCursor("c1", new OpenCursorCallback() {

            @Override
            public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                latch.countDown();
            }

            @Override
            public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                cursor2.set(cursor);
                latch.countDown();
            }
        }, null);
    });
    latch.await();
    assertNotNull(cursor1.get());
    assertNotNull(cursor2.get());
    assertEquals(cursor1.get(), cursor2.get());
    ledger.close();
}
Also used : OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) IOException(java.io.IOException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 43 with ManagedCursor

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

the class ManagedLedgerTest method discardEmptyLedgersOnClose.

@Test
public void discardEmptyLedgersOnClose() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("entry".getBytes());
    assertEquals(ledger.getLedgersInfoAsList().size(), 1);
    c1.close();
    ledger.close();
    // re-open
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    // 1 ledger with 1 entry and the current writing ledger
    assertEquals(ledger.getLedgersInfoAsList().size(), 2);
    c1.close();
    ledger.close();
    // re-open, now the previous empty ledger should have been discarded
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    // 1 ledger with 1 entry, and the current
    assertEquals(ledger.getLedgersInfoAsList().size(), 2);
// writing ledger
}
Also used : ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 44 with ManagedCursor

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

the class ManagedLedgerTest method spanningMultipleLedgersWithSize.

@Test(timeOut = 20000)
public void spanningMultipleLedgersWithSize() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1000000);
    config.setMaxSizePerLedgerMb(1);
    config.setEnsembleSize(1);
    config.setWriteQuorumSize(1).setAckQuorumSize(1);
    config.setMetadataWriteQuorumSize(1).setMetadataAckQuorumSize(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    assertEquals(ledger.getNumberOfEntries(), 0);
    assertEquals(ledger.getTotalSize(), 0);
    ManagedCursor cursor = ledger.openCursor("c1");
    byte[] content = new byte[1023 * 1024];
    for (int i = 0; i < 3; i++) ledger.addEntry(content);
    List<Entry> entries = cursor.readEntries(100);
    assertEquals(entries.size(), 3);
    assertEquals(cursor.hasMoreEntries(), false);
    PositionImpl first = (PositionImpl) entries.get(0).getPosition();
    PositionImpl last = (PositionImpl) entries.get(entries.size() - 1).getPosition();
    entries.forEach(e -> e.release());
    // Read again, from next ledger id
    entries = cursor.readEntries(100);
    assertEquals(entries.size(), 0);
    assertEquals(cursor.hasMoreEntries(), false);
    entries.forEach(e -> e.release());
    log.info("First={} Last={}", first, last);
    assertTrue(first.getLedgerId() < last.getLedgerId());
    assertEquals(first.getEntryId(), 0);
    assertEquals(last.getEntryId(), 0);
    ledger.close();
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 45 with ManagedCursor

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

the class ManagedLedgerTest method previousPosition.

@Test
public void previousPosition() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    ManagedCursor cursor = ledger.openCursor("my_cursor");
    Position p0 = cursor.getMarkDeletedPosition();
    // This is expected because p0 is already an "invalid" position (since no entry has been mark-deleted yet)
    assertEquals(ledger.getPreviousPosition((PositionImpl) p0), p0);
    // Force to close an empty ledger
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    // again
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    PositionImpl pBeforeWriting = ledger.getLastPosition();
    PositionImpl p1 = (PositionImpl) ledger.addEntry("entry".getBytes());
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    Position p2 = ledger.addEntry("entry".getBytes());
    Position p3 = ledger.addEntry("entry".getBytes());
    Position p4 = ledger.addEntry("entry".getBytes());
    assertEquals(ledger.getPreviousPosition(p1), pBeforeWriting);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p2), p1);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p3), p2);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p4), p3);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)302 Test (org.testng.annotations.Test)281 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)228 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)153 Position (org.apache.bookkeeper.mledger.Position)131 Entry (org.apache.bookkeeper.mledger.Entry)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)111 CountDownLatch (java.util.concurrent.CountDownLatch)66 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 CyclicBarrier (java.util.concurrent.CyclicBarrier)30 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)26 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)24 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 BKException (org.apache.bookkeeper.client.BKException)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 List (java.util.List)18 Future (java.util.concurrent.Future)18