Search in sources :

Example 86 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger in project incubator-pulsar by apache.

the class ManagedCursorTest method testIndividuallyDeletedMessages2.

@Test(timeOut = 20000)
void testIndividuallyDeletedMessages2() throws Exception {
    ManagedLedger ledger = factory.open("testIndividuallyDeletedMessages2");
    ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1");
    ledger.addEntry("entry-0".getBytes(Encoding));
    ledger.addEntry("entry-1".getBytes(Encoding));
    ledger.addEntry("entry-2".getBytes(Encoding));
    ledger.addEntry("entry-3".getBytes(Encoding));
    ledger.addEntry("entry-4".getBytes(Encoding));
    List<Entry> entries = c1.readEntries(4);
    c1.delete(entries.get(1).getPosition());
    c1.delete(entries.get(2).getPosition());
    c1.delete(entries.get(0).getPosition());
    entries.forEach(e -> e.release());
    assertTrue(c1.isIndividuallyDeletedEntriesEmpty());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Test(org.testng.annotations.Test)

Example 87 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger in project incubator-pulsar by apache.

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 88 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger in project incubator-pulsar by apache.

the class ManagedCursorTest method asyncMarkDeleteBlocking.

@Test(timeOut = 20000)
public void asyncMarkDeleteBlocking() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setMaxEntriesPerLedger(10);
    config.setMetadataMaxEntriesPerLedger(5);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    final ManagedCursor c1 = ledger.openCursor("c1");
    final AtomicReference<Position> lastPosition = new AtomicReference<Position>();
    final int N = 100;
    final CountDownLatch latch = new CountDownLatch(N);
    for (int i = 0; i < N; i++) {
        ledger.asyncAddEntry("entry".getBytes(Encoding), new AddEntryCallback() {

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
            }

            @Override
            public void addComplete(Position position, Object ctx) {
                lastPosition.set(position);
                c1.asyncMarkDelete(position, new MarkDeleteCallback() {

                    @Override
                    public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                    }

                    @Override
                    public void markDeleteComplete(Object ctx) {
                        latch.countDown();
                    }
                }, null);
            }
        }, null);
    }
    latch.await();
    assertEquals(c1.getNumberOfEntries(), 0);
    // Reopen
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger");
    ManagedCursor c2 = ledger.openCursor("c1");
    assertEquals(c2.getMarkDeletedPosition(), lastPosition.get());
    factory2.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 89 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger in project incubator-pulsar by apache.

the class ManagedCursorTest method testResetCursor.

@Test(timeOut = 20000)
void testResetCursor() throws Exception {
    ManagedLedger ledger = factory.open("my_test_move_cursor_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10));
    ManagedCursor cursor = ledger.openCursor("trc1");
    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);
    PositionImpl resetPosition = new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - 2);
    try {
        cursor.resetCursor(resetPosition);
        moveStatus.set(true);
    } catch (Exception e) {
        log.warn("error in reset cursor", e.getCause());
    }
    assertTrue(moveStatus.get());
    assertTrue(cursor.getReadPosition().equals(resetPosition));
    cursor.close();
    ledger.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 90 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger in project incubator-pulsar by apache.

the class ManagedLedgerBkTest method testConcurrentMarkDelete.

@Test
public void testConcurrentMarkDelete() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ManagedLedgerConfig mlConfig = new ManagedLedgerConfig();
    mlConfig.setEnsembleSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1).setMetadataAckQuorumSize(1);
    // set the data ledger size
    mlConfig.setMaxEntriesPerLedger(100);
    // set the metadata ledger size to 1 to kick off many ledger switching cases
    mlConfig.setMetadataMaxEntriesPerLedger(10);
    ManagedLedger ledger = factory.open("ml-markdelete-ledger", mlConfig);
    final List<Position> addedEntries = Lists.newArrayList();
    int numCursors = 10;
    final CyclicBarrier barrier = new CyclicBarrier(numCursors);
    List<ManagedCursor> cursors = Lists.newArrayList();
    for (int i = 0; i < numCursors; i++) {
        cursors.add(ledger.openCursor(String.format("c%d", i)));
    }
    for (int i = 0; i < 50; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    List<Future<?>> futures = Lists.newArrayList();
    for (ManagedCursor cursor : cursors) {
        futures.add(executor.submit(() -> {
            barrier.await();
            for (Position position : addedEntries) {
                cursor.markDelete(position);
            }
            return null;
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    // Since in this test we roll-over the cursor ledger every 10 entries acknowledged, the background roll back
    // might still be happening when the futures are completed.
    Thread.sleep(1000);
    factory.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) Future(java.util.concurrent.Future) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)363 Test (org.testng.annotations.Test)354 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)262 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)198 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)180 Position (org.apache.bookkeeper.mledger.Position)167 Entry (org.apache.bookkeeper.mledger.Entry)151 CountDownLatch (java.util.concurrent.CountDownLatch)117 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)98 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)74 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)68 CyclicBarrier (java.util.concurrent.CyclicBarrier)66 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)59 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)58 ArrayList (java.util.ArrayList)57 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)57 Future (java.util.concurrent.Future)56 AtomicReference (java.util.concurrent.atomic.AtomicReference)55 ExecutorService (java.util.concurrent.ExecutorService)54 BKException (org.apache.bookkeeper.client.BKException)54