Search in sources :

Example 41 with Position

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

the class ManagedCursorTest method errorRecoveringCursor3.

@Test(timeOut = 20000)
void errorRecoveringCursor3() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    Position p1 = ledger.addEntry("entry".getBytes());
    ledger.addEntry("entry".getBytes());
    ManagedCursor c1 = ledger.openCursor("c1");
    Position p3 = ledger.addEntry("entry".getBytes());
    assertEquals(c1.getReadPosition(), p3);
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    bkc.failAfter(4, BKException.Code.ReadException);
    ledger = factory2.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    // Verify the ManagedCursor was rewind back to the snapshotted position
    assertEquals(c1.getReadPosition(), p3);
    factory2.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 42 with Position

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

the class ManagedCursorTest method testGetEntryAfterNWithIndividualDeletedMessages.

@Test(timeOut = 20000)
void testGetEntryAfterNWithIndividualDeletedMessages() throws Exception {
    ManagedLedger ledger = factory.open("testGetEnteryAfterNWithIndividualDeletedMessages");
    ManagedCursor c1 = ledger.openCursor("c1");
    Position pos1 = ledger.addEntry("msg1".getBytes());
    Position pos2 = ledger.addEntry("msg2".getBytes());
    Position pos3 = ledger.addEntry("msg3".getBytes());
    Position pos4 = ledger.addEntry("msg4".getBytes());
    Position pos5 = ledger.addEntry("msg5".getBytes());
    c1.delete(pos3);
    c1.delete(pos4);
    Entry e = c1.getNthEntry(3, IndividualDeletedEntries.Exclude);
    assertEquals(e.getDataAndRelease(), "msg5".getBytes());
    e = c1.getNthEntry(3, IndividualDeletedEntries.Include);
    assertEquals(e.getDataAndRelease(), "msg3".getBytes());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 43 with Position

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

the class ManagedCursorTest method testFindNewestMatchingEven4.

@Test(timeOut = 20000)
void testFindNewestMatchingEven4() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1");
    ledger.addEntry("expired".getBytes(Encoding));
    ledger.addEntry("expired".getBytes(Encoding));
    ledger.addEntry("expired".getBytes(Encoding));
    Position p4 = ledger.addEntry("expired".getBytes(Encoding));
    assertEquals(c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p4);
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Test(org.testng.annotations.Test) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) ManagedCursorInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo) Future(java.util.concurrent.Future) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CyclicBarrier(java.util.concurrent.CyclicBarrier) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Code(org.apache.zookeeper.KeeperException.Code) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) Assert(org.testng.Assert) Charset(java.nio.charset.Charset) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) ExecutorService(java.util.concurrent.ExecutorService) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) PositionInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) VoidCallback(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.VoidCallback) Collections(java.util.Collections) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Test(org.testng.annotations.Test)

Example 44 with Position

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

the class ManagedCursorTest method unorderedAsyncMarkDelete.

@Test(timeOut = 20000)
void unorderedAsyncMarkDelete() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    final ManagedCursor c1 = ledger.openCursor("c1");
    Position p1 = ledger.addEntry("entry-1".getBytes(Encoding));
    Position p2 = ledger.addEntry("entry-2".getBytes(Encoding));
    final CountDownLatch latch = new CountDownLatch(2);
    c1.asyncMarkDelete(p2, new MarkDeleteCallback() {

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

        @Override
        public void markDeleteComplete(Object ctx) {
            latch.countDown();
        }
    }, null);
    c1.asyncMarkDelete(p1, new MarkDeleteCallback() {

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
        }

        @Override
        public void markDeleteComplete(Object ctx) {
            fail();
        }
    }, null);
    latch.await();
    assertEquals(c1.getMarkDeletedPosition(), p2);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 45 with Position

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

Aggregations

Position (org.apache.bookkeeper.mledger.Position)201 Test (org.testng.annotations.Test)169 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)168 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)167 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)115 Entry (org.apache.bookkeeper.mledger.Entry)104 CountDownLatch (java.util.concurrent.CountDownLatch)97 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)72 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)68 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 BKException (org.apache.bookkeeper.client.BKException)56 List (java.util.List)55 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)53 AtomicReference (java.util.concurrent.atomic.AtomicReference)52 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)52 Logger (org.slf4j.Logger)52 LoggerFactory (org.slf4j.LoggerFactory)52 Lists (com.google.common.collect.Lists)51 TimeUnit (java.util.concurrent.TimeUnit)51