Search in sources :

Example 91 with ManagedCursor

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

the class ManagedCursorTest method randomOrderAcks.

@Test(timeOut = 20000)
void randomOrderAcks() throws Exception {
    ManagedLedger ledger = factory.open("outOfOrderAcks");
    ManagedCursor c1 = ledger.openCursor("c1");
    int N = 10;
    List<Position> positions = new ArrayList<>();
    for (int i = 0; i < N; i++) {
        positions.add(ledger.addEntry("entry".getBytes()));
    }
    assertEquals(c1.getNumberOfEntriesInBacklog(), N);
    // Randomize the ack sequence
    Collections.shuffle(positions);
    int toDelete = N;
    for (Position p : positions) {
        assertEquals(c1.getNumberOfEntriesInBacklog(), toDelete);
        c1.delete(p);
        --toDelete;
        assertEquals(c1.getNumberOfEntriesInBacklog(), toDelete);
    }
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ArrayList(java.util.ArrayList) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 92 with ManagedCursor

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

the class ManagedCursorTest method testasyncResetCursor.

@Test(timeOut = 20000)
void testasyncResetCursor() throws Exception {
    ManagedLedger ledger = factory.open("my_test_move_cursor_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10));
    ManagedCursor cursor = ledger.openCursor("tarc1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    final AtomicBoolean moveStatus = new AtomicBoolean(false);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    PositionImpl resetPosition = new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - 2);
    cursor.asyncResetCursor(resetPosition, new AsyncCallbacks.ResetCursorCallback() {

        @Override
        public void resetComplete(Object ctx) {
            moveStatus.set(true);
            countDownLatch.countDown();
        }

        @Override
        public void resetFailed(ManagedLedgerException exception, Object ctx) {
            moveStatus.set(false);
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    assertTrue(moveStatus.get());
    assertTrue(cursor.getReadPosition().equals(resetPosition));
    cursor.close();
    ledger.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 93 with ManagedCursor

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

the class ManagedCursorTest method testSkipEntries.

@Test(timeOut = 20000)
void testSkipEntries() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    Position pos;
    ManagedCursor c1 = ledger.openCursor("c1");
    // test skip on empty ledger
    pos = c1.getReadPosition();
    c1.skipEntries(1, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getReadPosition(), pos);
    pos = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    pos = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    // skip entries in same ledger
    c1.skipEntries(1, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getNumberOfEntries(), 1);
    // skip entries until end of ledger
    c1.skipEntries(1, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getReadPosition(), pos.getNext());
    assertEquals(c1.getMarkDeletedPosition(), pos);
    // skip entries across ledgers
    for (int i = 0; i < 6; i++) {
        pos = ledger.addEntry("dummy-entry".getBytes(Encoding));
    }
    c1.skipEntries(5, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getNumberOfEntries(), 1);
    // skip more than the current set of entries
    c1.skipEntries(10, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.hasMoreEntries(), false);
    assertEquals(c1.getReadPosition(), pos.getNext());
    assertEquals(c1.getMarkDeletedPosition(), pos);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 94 with ManagedCursor

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

the class ManagedCursorTest method outOfOrderAcks.

@Test(timeOut = 20000)
void outOfOrderAcks() throws Exception {
    ManagedLedger ledger = factory.open("outOfOrderAcks");
    ManagedCursor c1 = ledger.openCursor("c1");
    int N = 10;
    List<Position> positions = new ArrayList<>();
    for (int i = 0; i < N; i++) {
        positions.add(ledger.addEntry("entry".getBytes()));
    }
    assertEquals(c1.getNumberOfEntriesInBacklog(), N);
    c1.delete(positions.get(3));
    assertEquals(c1.getNumberOfEntriesInBacklog(), N - 1);
    c1.delete(positions.get(2));
    assertEquals(c1.getNumberOfEntriesInBacklog(), N - 2);
    c1.delete(positions.get(1));
    assertEquals(c1.getNumberOfEntriesInBacklog(), N - 3);
    c1.delete(positions.get(0));
    assertEquals(c1.getNumberOfEntriesInBacklog(), N - 4);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ArrayList(java.util.ArrayList) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 95 with ManagedCursor

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

the class ManagedCursorTest method testFilteringReadEntries.

@Test(timeOut = 20000)
void testFilteringReadEntries() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(3));
    ManagedCursor cursor = ledger.openCursor("c1");
    /* Position p1 = */
    ledger.addEntry("entry1".getBytes());
    /* Position p2 = */
    ledger.addEntry("entry2".getBytes());
    /* Position p3 = */
    ledger.addEntry("entry3".getBytes());
    /* Position p4 = */
    ledger.addEntry("entry4".getBytes());
    Position p5 = ledger.addEntry("entry5".getBytes());
    /* Position p6 = */
    ledger.addEntry("entry6".getBytes());
    assertEquals(cursor.getNumberOfEntries(), 6);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 6);
    List<Entry> entries = cursor.readEntries(3);
    assertEquals(entries.size(), 3);
    entries.forEach(e -> e.release());
    assertEquals(cursor.getNumberOfEntries(), 3);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 6);
    log.info("Deleting {}", p5);
    cursor.delete(p5);
    assertEquals(cursor.getNumberOfEntries(), 2);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 5);
    entries = cursor.readEntries(3);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 5);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) 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