Search in sources :

Example 76 with ManagedCursor

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

the class ManagedCursorContainerTest method ordering.

@Test
void ordering() throws Exception {
    ManagedCursorContainer container = new ManagedCursorContainer();
    ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
    ManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(5, 1));
    ManagedCursor cursor3 = new MockManagedCursor(container, "test3", new PositionImpl(7, 1));
    ManagedCursor cursor4 = new MockManagedCursor(container, "test4", new PositionImpl(6, 4));
    ManagedCursor cursor5 = new MockManagedCursor(container, "test5", new PositionImpl(7, 0));
    container.add(cursor1);
    container.add(cursor2);
    container.add(cursor3);
    container.add(cursor4);
    container.add(cursor5);
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
    container.removeCursor("test2");
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
    container.removeCursor("test1");
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(6, 4));
    container.removeCursor("test4");
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 0));
    container.removeCursor("test5");
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 1));
    container.removeCursor("test3");
    assertTrue(container.isEmpty());
}
Also used : ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 77 with ManagedCursor

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

the class ManagedCursorTest method testClearBacklog.

@Test(timeOut = 20000)
void testClearBacklog() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ManagedCursor c2 = ledger.openCursor("c2");
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ManagedCursor c3 = ledger.openCursor("c3");
    ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
    assertEquals(c1.getNumberOfEntries(), 3);
    assertEquals(c1.hasMoreEntries(), true);
    c1.clearBacklog();
    c3.clearBacklog();
    assertEquals(c1.getNumberOfEntriesInBacklog(), 0);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.hasMoreEntries(), false);
    assertEquals(c2.getNumberOfEntriesInBacklog(), 2);
    assertEquals(c2.getNumberOfEntries(), 2);
    assertEquals(c2.hasMoreEntries(), true);
    assertEquals(c3.getNumberOfEntriesInBacklog(), 0);
    assertEquals(c3.getNumberOfEntries(), 0);
    assertEquals(c3.hasMoreEntries(), false);
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    c1 = ledger.openCursor("c1");
    c2 = ledger.openCursor("c2");
    c3 = ledger.openCursor("c3");
    assertEquals(c1.getNumberOfEntriesInBacklog(), 0);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.hasMoreEntries(), false);
    assertEquals(c2.getNumberOfEntriesInBacklog(), 2);
    assertEquals(c2.getNumberOfEntries(), 2);
    assertEquals(c2.hasMoreEntries(), true);
    assertEquals(c3.getNumberOfEntriesInBacklog(), 0);
    assertEquals(c3.getNumberOfEntries(), 0);
    assertEquals(c3.hasMoreEntries(), false);
    factory2.shutdown();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 78 with ManagedCursor

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

the class ManagedCursorTest method readFromEmptyLedger.

@Test(timeOut = 20000)
void readFromEmptyLedger() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    List<Entry> entries = c1.readEntries(10);
    assertEquals(entries.size(), 0);
    entries.forEach(e -> e.release());
    ledger.addEntry("test".getBytes(Encoding));
    entries = c1.readEntries(10);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    entries = c1.readEntries(10);
    assertEquals(entries.size(), 0);
    entries.forEach(e -> e.release());
    // Test string representation
    assertEquals(c1.toString(), "ManagedCursorImpl{ledger=my_test_ledger, name=c1, ackPos=3:-1, readPos=3:1}");
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 79 with ManagedCursor

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

the class ManagedCursorTest method asyncReadWithInvalidParameter.

@Test(timeOut = 20000, expectedExceptions = IllegalArgumentException.class)
void asyncReadWithInvalidParameter() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    final CountDownLatch counter = new CountDownLatch(1);
    stopBookKeeper();
    cursor.asyncReadEntries(0, new ReadEntriesCallback() {

        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            fail("async-call should have failed");
        }

        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            counter.countDown();
        }
    }, null);
    counter.await();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 80 with ManagedCursor

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

the class ManagedCursorTest method seekPosition4.

@Test(timeOut = 20000)
void seekPosition4() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    Position p1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    Position p2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    ledger.addEntry("dummy-entry-5".getBytes(Encoding));
    ledger.addEntry("dummy-entry-6".getBytes(Encoding));
    cursor.markDelete(p1);
    assertEquals(cursor.getMarkDeletedPosition(), p1);
    assertEquals(cursor.getReadPosition(), p2);
    List<Entry> entries = cursor.readEntries(2);
    entries.forEach(e -> e.release());
    cursor.seek(p2);
    assertEquals(cursor.getMarkDeletedPosition(), p1);
    assertEquals(cursor.getReadPosition(), p2);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)135 Test (org.testng.annotations.Test)126 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)102 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)66 Position (org.apache.bookkeeper.mledger.Position)59 Entry (org.apache.bookkeeper.mledger.Entry)57 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)52 CountDownLatch (java.util.concurrent.CountDownLatch)32 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 CyclicBarrier (java.util.concurrent.CyclicBarrier)15 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)12 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)12 List (java.util.List)10 BKException (org.apache.bookkeeper.client.BKException)10 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)10 Future (java.util.concurrent.Future)9 TimeUnit (java.util.concurrent.TimeUnit)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)9