Search in sources :

Example 46 with ManagedLedgerConfig

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

the class ManagedCursorTest method testOutOfOrderDeletePersistenceIntoZkWithClose.

/**
 * Close Cursor without MaxUnackedRangesToPersistInZK: It should store individually unack range into Zk
 *
 * @throws Exception
 */
@Test(timeOut = 20000)
public void testOutOfOrderDeletePersistenceIntoZkWithClose() throws Exception {
    final int totalAddEntries = 100;
    String ledgerName = "my_test_ledger_zk";
    String cursorName = "c1";
    ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open(ledgerName, managedLedgerConfig);
    ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor(cursorName);
    List<Position> addedPositions = new ArrayList<>();
    for (int i = 0; i < totalAddEntries; i++) {
        Position p = ledger.addEntry(("dummy-entry-" + i).getBytes(Encoding));
        addedPositions.add(p);
        if (i % 2 == 0) {
            // Acknowledge alternative message to create totalEntries/2 holes
            c1.delete(addedPositions.get(i));
        }
    }
    assertEquals(c1.getNumberOfEntriesInBacklog(), totalAddEntries / 2);
    // Close ledger to persist individual-deleted positions into cursor-ledger
    ledger.close();
    // verify cursor-ledgerId is updated as -1 into cursor-metaStore
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger individualDeletedMessagesCount = new AtomicInteger(0);
    ledger.getStore().asyncGetCursorInfo(ledger.getName(), cursorName, new MetaStoreCallback<ManagedCursorInfo>() {

        @Override
        public void operationComplete(ManagedCursorInfo result, Stat stat) {
            individualDeletedMessagesCount.set(result.getIndividualDeletedMessagesCount());
            latch.countDown();
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            latch.countDown();
        }
    });
    latch.await();
    assertEquals(individualDeletedMessagesCount.get(), totalAddEntries / 2 - 1);
    // Re-Open
    factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = (ManagedLedgerImpl) factory.open(ledgerName, managedLedgerConfig);
    c1 = (ManagedCursorImpl) ledger.openCursor(cursorName);
    // verify cursor has been recovered
    assertEquals(c1.getNumberOfEntriesInBacklog(), totalAddEntries / 2);
    // try to read entries which should only read non-deleted positions
    List<Entry> entries = c1.readEntries(totalAddEntries);
    assertEquals(entries.size(), totalAddEntries / 2);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedCursorInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 47 with ManagedLedgerConfig

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

the class ManagedCursorTest method testMarkDeleteTwice.

@Test(timeOut = 20000)
void testMarkDeleteTwice() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    ManagedCursor cursor = ledger.openCursor("c1");
    Position p1 = ledger.addEntry("entry1".getBytes());
    cursor.markDelete(p1);
    cursor.markDelete(p1);
    assertEquals(cursor.getMarkDeletedPosition(), p1);
}
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 48 with ManagedLedgerConfig

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

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 49 with ManagedLedgerConfig

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

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

Aggregations

ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)194 Test (org.testng.annotations.Test)182 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)159 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)147 Position (org.apache.bookkeeper.mledger.Position)85 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)77 Entry (org.apache.bookkeeper.mledger.Entry)63 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)54 CountDownLatch (java.util.concurrent.CountDownLatch)52 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)33 CyclicBarrier (java.util.concurrent.CyclicBarrier)20 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)19 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)18 List (java.util.List)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)16 TimeUnit (java.util.concurrent.TimeUnit)15 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)15 BKException (org.apache.bookkeeper.client.BKException)14 Lists (com.google.common.collect.Lists)13