Search in sources :

Example 56 with ManagedCursor

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

the class ManagedLedgerTest method acknowledge1.

@Test(timeOut = 20000)
public void acknowledge1() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    assertEquals(cursor.hasMoreEntries(), true);
    List<Entry> entries = cursor.readEntries(2);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 2);
    assertEquals(cursor.hasMoreEntries(), false);
    assertEquals(ledger.getNumberOfEntries(), 2);
    assertEquals(ledger.getNumberOfActiveEntries(), 2);
    cursor.markDelete(entries.get(0).getPosition());
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 1);
    assertEquals(cursor.hasMoreEntries(), false);
    assertEquals(ledger.getNumberOfActiveEntries(), 1);
    ledger.close();
    // / Reopen the same managed-ledger
    ledger = factory.open("my_test_ledger");
    cursor = ledger.openCursor("c1");
    assertEquals(ledger.getNumberOfEntries(), 2);
    assertEquals(ledger.getTotalSize(), "dummy-entry-1".getBytes(Encoding).length * 2);
    assertEquals(cursor.getNumberOfEntries(), 1);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 1);
    assertEquals(cursor.hasMoreEntries(), true);
    entries = cursor.readEntries(100);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    ledger.close();
}
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 57 with ManagedCursor

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

the class ManagedLedgerTest method testOpenRaceCondition.

/**
     * Reproduce a race condition between opening cursors and concurrent mark delete operations
     */
@Test(timeOut = 20000)
public void testOpenRaceCondition() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    final ManagedLedger ledger = factory.open("my-ledger", config);
    final ManagedCursor c1 = ledger.openCursor("c1");
    final int N = 1000;
    final Position position = ledger.addEntry("entry-0".getBytes());
    Executor executor = Executors.newCachedThreadPool();
    final CountDownLatch counter = new CountDownLatch(2);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < N; i++) {
                    c1.markDelete(position);
                }
                counter.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    executor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < N; i++) {
                    ledger.openCursor("cursor-" + i);
                }
                counter.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    // If there is the race condition, this method will not complete triggering the test timeout
    counter.await();
}
Also used : Executor(java.util.concurrent.Executor) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) 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) Test(org.testng.annotations.Test)

Example 58 with ManagedCursor

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

the class ManagedLedgerTest method spanningMultipleLedgers.

@Test(timeOut = 20000)
public void spanningMultipleLedgers() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(10);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    assertEquals(ledger.getNumberOfEntries(), 0);
    assertEquals(ledger.getTotalSize(), 0);
    ManagedCursor cursor = ledger.openCursor("c1");
    for (int i = 0; i < 11; i++) ledger.addEntry(("dummy-entry-" + i).getBytes(Encoding));
    List<Entry> entries = cursor.readEntries(100);
    assertEquals(entries.size(), 11);
    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());
    log.info("First={} Last={}", first, last);
    assertTrue(first.getLedgerId() < last.getLedgerId());
    assertEquals(first.getEntryId(), 0);
    assertEquals(last.getEntryId(), 0);
    // Read again, from next ledger id
    entries = cursor.readEntries(100);
    assertEquals(entries.size(), 0);
    assertEquals(cursor.hasMoreEntries(), false);
    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 59 with ManagedCursor

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

the class ManagedLedgerTest method moveCursorToNextLedger.

@Test(timeOut = 20000)
public void moveCursorToNextLedger() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    ManagedCursor cursor = ledger.openCursor("test");
    ledger.addEntry("entry-1".getBytes(Encoding));
    log.debug("Added 1st message");
    List<Entry> entries = cursor.readEntries(1);
    log.debug("read message ok");
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    ledger.addEntry("entry-2".getBytes(Encoding));
    log.debug("Added 2nd message");
    ledger.addEntry("entry-3".getBytes(Encoding));
    log.debug("Added 3nd message");
    assertEquals(cursor.hasMoreEntries(), true);
    assertEquals(cursor.getNumberOfEntries(), 2);
    entries = cursor.readEntries(2);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    entries = cursor.readEntries(2);
    assertEquals(entries.size(), 0);
    entries = cursor.readEntries(2);
    assertEquals(entries.size(), 0);
}
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 60 with ManagedCursor

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

the class ManagedLedgerErrorsTest method removingCursor.

@Test
public void removingCursor() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    assertEquals(zkc.exists("/managed-ledgers/my_test_ledger/c1", false) != null, true);
    zkc.failNow(Code.BADVERSION);
    try {
        c1.close();
        fail("should fail");
    } catch (ManagedLedgerException e) {
    // ok
    }
    bkc.failNow(BKException.Code.NoSuchLedgerExistsException);
    // Cursor ledger deletion will fail, but that should not prevent the deleteCursor to fail
    ledger.deleteCursor("c1");
    assertEquals(zkc.exists("/managed-ledgers/my_test_ledger/c1", false) != null, false);
    assertEquals(bkc.getLedgers().size(), 2);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) 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