Search in sources :

Example 36 with ManagedLedgerConfig

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

the class ManagedCursorTest method testSkipEntriesWithIndividualDeletedMessages.

@Test(timeOut = 20000)
void testSkipEntriesWithIndividualDeletedMessages() throws Exception {
    ManagedLedger ledger = factory.open("testSkipEntriesWithIndividualDeletedMessages", new ManagedLedgerConfig().setMaxEntriesPerLedger(5));
    ManagedCursor c1 = ledger.openCursor("c1");
    Position pos1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    Position pos2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    Position pos3 = ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    Position pos4 = ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    Position pos5 = ledger.addEntry("dummy-entry-5".getBytes(Encoding));
    // delete individual messages
    c1.delete(pos2);
    c1.delete(pos4);
    c1.skipEntries(3, IndividualDeletedEntries.Exclude);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getReadPosition(), pos5.getNext());
    assertEquals(c1.getMarkDeletedPosition(), pos5);
    pos1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    pos2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    pos3 = ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    pos4 = ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    pos5 = ledger.addEntry("dummy-entry-5".getBytes(Encoding));
    c1.delete(pos2);
    c1.delete(pos4);
    c1.skipEntries(4, IndividualDeletedEntries.Include);
    assertEquals(c1.getNumberOfEntries(), 1);
    assertEquals(c1.getReadPosition(), pos5);
    assertEquals(c1.getMarkDeletedPosition(), pos4);
}
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 37 with ManagedLedgerConfig

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

the class ManagedLedgerTest method triggerLedgerDeletion.

@Test(timeOut = 20000)
public void triggerLedgerDeletion() 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));
    ledger.addEntry("entry-2".getBytes(Encoding));
    ledger.addEntry("entry-3".getBytes(Encoding));
    assertEquals(cursor.hasMoreEntries(), true);
    List<Entry> entries = cursor.readEntries(1);
    assertEquals(entries.size(), 1);
    assertEquals(ledger.getNumberOfEntries(), 3);
    entries.forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), true);
    entries = cursor.readEntries(1);
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.markDelete(entries.get(0).getPosition());
    entries.forEach(e -> e.release());
}
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 38 with ManagedLedgerConfig

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

the class ManagedLedgerTest method testReopenAndCleanup.

@Test(timeOut = 20000)
public void testReopenAndCleanup() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("c1");
    ledger.addEntry("data".getBytes(Encoding));
    ledger.close();
    Thread.sleep(100);
    assertEquals(bkc.getLedgers().size(), 1);
    factory.shutdown();
    factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory.open("my_test_ledger");
    ledger.openCursor("c1");
    Thread.sleep(100);
    assertEquals(bkc.getLedgers().size(), 2);
    ledger.close();
    factory.open("my_test_ledger", new ManagedLedgerConfig()).delete();
    Thread.sleep(100);
    assertEquals(bkc.getLedgers().size(), 0);
    factory.shutdown();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 39 with ManagedLedgerConfig

use of org.apache.bookkeeper.mledger.ManagedLedgerConfig 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 40 with ManagedLedgerConfig

use of org.apache.bookkeeper.mledger.ManagedLedgerConfig 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)

Aggregations

ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)82 Test (org.testng.annotations.Test)77 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)69 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)63 Position (org.apache.bookkeeper.mledger.Position)35 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)32 Entry (org.apache.bookkeeper.mledger.Entry)27 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 CyclicBarrier (java.util.concurrent.CyclicBarrier)10 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)8 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)7 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)7 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 BKException (org.apache.bookkeeper.client.BKException)6 Lists (com.google.common.collect.Lists)5 Executors (java.util.concurrent.Executors)5