Search in sources :

Example 36 with ManagedCursor

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

the class ManagedLedgerTest method testActiveDeactiveCursorWithDiscardEntriesFromCache.

/**
     * Validations:
     *
     * 1. openCursor : activates cursor 2. EntryCache keeps entries: till entry will be read by all active cursors a.
     * active cursor1 reads entry b. EntryCache keeps entry till cursor2 reads c. active cursor2 reads entry d.
     * EntryCache deletes all read entries by cursor1 and cursor2 3. EntryCache discard entries: deactivate slower
     * cursor a. active cursor1 read all entries b. EntryCache keeps entry till cursor2 reads c. deactivate cursor2 d.
     * EntryCache deletes all read entries by cursor1
     *
     * @throws Exception
     */
@Test
public void testActiveDeactiveCursorWithDiscardEntriesFromCache() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("cache_eviction_ledger");
    // Open Cursor also adds cursor into activeCursor-container
    ManagedCursor cursor1 = ledger.openCursor("c1");
    ManagedCursor cursor2 = ledger.openCursor("c2");
    Set<ManagedCursor> activeCursors = Sets.newHashSet();
    activeCursors.add(cursor1);
    activeCursors.add(cursor2);
    Field cacheField = ManagedLedgerImpl.class.getDeclaredField("entryCache");
    cacheField.setAccessible(true);
    EntryCacheImpl entryCache = (EntryCacheImpl) cacheField.get(ledger);
    Iterator<ManagedCursor> activeCursor = ledger.getActiveCursors().iterator();
    // (1) validate cursors are part of activeCursorContainer
    activeCursors.remove(activeCursor.next());
    activeCursors.remove(activeCursor.next());
    assertTrue(activeCursors.isEmpty());
    assertFalse(activeCursor.hasNext());
    final int totalInsertedEntries = 50;
    for (int i = 0; i < totalInsertedEntries; i++) {
        // 5 bytes
        String content = "entry";
        ledger.addEntry(content.getBytes());
    }
    // (2) Validate: as ledger has active cursors: all entries have been cached
    assertEquals((5 * totalInsertedEntries), entryCache.getSize());
    // read 20 entries
    final int readEntries = 20;
    List<Entry> entries1 = cursor1.readEntries(readEntries);
    for (Entry entry : entries1) {
        log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
        entry.release();
    }
    // Acknowledge only on last entry
    cursor1.markDelete(entries1.get(entries1.size() - 1).getPosition());
    // read after a second: as RateLimiter limits triggering of removing cache
    Thread.sleep(1000);
    List<Entry> entries2 = cursor2.readEntries(readEntries);
    for (Entry entry : entries2) {
        log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
        entry.release();
    }
    // Acknowledge only on last entry
    cursor2.markDelete((entries2.get(entries2.size() - 1)).getPosition());
    // (3) Validate: cache should remove all entries read by both active cursors
    log.info("expected, found : {}, {}", (5 * (totalInsertedEntries - readEntries)), entryCache.getSize());
    assertEquals((5 * (totalInsertedEntries - readEntries)), entryCache.getSize());
    final int remainingEntries = totalInsertedEntries - readEntries;
    entries1 = cursor1.readEntries(remainingEntries);
    for (Entry entry : entries1) {
        log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
        entry.release();
    }
    // Acknowledge only on last entry
    cursor1.markDelete(entries1.get(entries1.size() - 1).getPosition());
    // (4) Validate: cursor2 is active cursor and has not read these entries yet: so, cache should not remove these
    // entries
    assertEquals((5 * (remainingEntries)), entryCache.getSize());
    ledger.deactivateCursor(cursor2);
    // (5) Validate: cursor2 is not active cursor now: cache should have removed all entries read by active cursor1
    assertEquals(0, entryCache.getSize());
    log.info("Finished reading entries");
    ledger.close();
}
Also used : Field(java.lang.reflect.Field) Entry(org.apache.bookkeeper.mledger.Entry) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 37 with ManagedCursor

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

the class ManagedLedgerTest method getCursors.

@Test
public void getCursors() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ManagedCursor c2 = ledger.openCursor("c2");
    assertEquals(Sets.newHashSet(ledger.getCursors()), Sets.newHashSet(c1, c2));
    c1.close();
    ledger.deleteCursor("c1");
    assertEquals(Sets.newHashSet(ledger.getCursors()), Sets.newHashSet(c2));
    c2.close();
    ledger.deleteCursor("c2");
    assertEquals(Sets.newHashSet(ledger.getCursors()), Sets.newHashSet());
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 38 with ManagedCursor

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

the class ManagedLedgerTest method cursorReadsWithDiscardedEmptyLedgers.

@Test
public void cursorReadsWithDiscardedEmptyLedgers() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    Position p1 = c1.getReadPosition();
    c1.close();
    ledger.close();
    // re-open
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.hasMoreEntries(), false);
    ledger.addEntry("entry".getBytes());
    assertEquals(c1.getNumberOfEntries(), 1);
    assertEquals(c1.hasMoreEntries(), true);
    assertEquals(ledger.getLedgersInfoAsList().size(), 1);
    List<Entry> entries = c1.readEntries(1);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    assertEquals(c1.hasMoreEntries(), false);
    assertEquals(c1.readEntries(1).size(), 0);
    c1.seek(p1);
    assertEquals(c1.hasMoreEntries(), true);
    assertEquals(c1.getNumberOfEntries(), 1);
    entries = c1.readEntries(1);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    assertEquals(c1.readEntries(1).size(), 0);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 39 with ManagedCursor

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

the class ManagedLedgerTest method simple.

@Test(timeOut = 20000)
public void simple() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    assertEquals(ledger.getNumberOfEntries(), 0);
    assertEquals(ledger.getNumberOfActiveEntries(), 0);
    assertEquals(ledger.getTotalSize(), 0);
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    assertEquals(ledger.getNumberOfEntries(), 1);
    assertEquals(ledger.getNumberOfActiveEntries(), 0);
    assertEquals(ledger.getTotalSize(), "dummy-entry-1".getBytes(Encoding).length);
    ManagedCursor cursor = ledger.openCursor("c1");
    assertEquals(cursor.hasMoreEntries(), false);
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 0);
    assertEquals(cursor.readEntries(100), new ArrayList<Entry>());
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    assertEquals(cursor.hasMoreEntries(), true);
    assertEquals(cursor.getNumberOfEntries(), 1);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 1);
    assertEquals(ledger.getNumberOfActiveEntries(), 1);
    List<Entry> entries = cursor.readEntries(100);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    entries = cursor.readEntries(100);
    assertEquals(entries.size(), 0);
    ledger.close();
    factory.shutdown();
}
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 40 with ManagedCursor

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

the class ManagedLedgerTest method invalidReadEntriesArg1.

@Test(expectedExceptions = IllegalArgumentException.class)
public void invalidReadEntriesArg1() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("entry".getBytes());
    cursor.readEntries(-1);
    fail("Should have thrown an exception in the above line");
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) 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