Search in sources :

Example 11 with ManagedCursor

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

the class ManagedCursorTest method testOutOfOrderDeletePersistenceAfterCrash.

@Test(timeOut = 20000)
public void testOutOfOrderDeletePersistenceAfterCrash() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig());
    ManagedCursor c1 = ledger.openCursor("c1");
    List<Position> addedPositions = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Position p = ledger.addEntry(("dummy-entry-" + i).getBytes(Encoding));
        addedPositions.add(p);
    }
    // Acknowledge few messages leaving holes
    c1.delete(addedPositions.get(2));
    c1.delete(addedPositions.get(5));
    c1.delete(addedPositions.get(7));
    c1.delete(addedPositions.get(8));
    c1.delete(addedPositions.get(9));
    assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
    // Re-Open
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger", new ManagedLedgerConfig());
    c1 = ledger.openCursor("c1");
    assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
    List<Entry> entries = c1.readEntries(20);
    assertEquals(entries.size(), 20 - 5);
    List<String> entriesStr = entries.stream().map(e -> new String(e.getDataAndRelease(), Encoding)).collect(Collectors.toList());
    assertEquals(entriesStr.get(0), "dummy-entry-0");
    assertEquals(entriesStr.get(1), "dummy-entry-1");
    // Entry-2 was deleted
    assertEquals(entriesStr.get(2), "dummy-entry-3");
    assertEquals(entriesStr.get(3), "dummy-entry-4");
    // Entry-6 was deleted
    assertEquals(entriesStr.get(4), "dummy-entry-6");
    assertFalse(c1.hasMoreEntries());
    factory2.shutdown();
}
Also used : ZNodeProtobufFormat(org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.ZNodeProtobufFormat) Arrays(java.util.Arrays) Assert.assertNull(org.testng.Assert.assertNull) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) ExecutorService(java.util.concurrent.ExecutorService) Charsets(com.google.common.base.Charsets) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) CyclicBarrier(java.util.concurrent.CyclicBarrier) Logger(org.slf4j.Logger) Factory(org.testng.annotations.Factory) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) Sets(com.google.common.collect.Sets) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ArrayList(java.util.ArrayList) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 12 with ManagedCursor

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

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 13 with ManagedCursor

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

the class ManagedCursorTest method asyncReadWithErrors.

@Test(timeOut = 20000)
void asyncReadWithErrors() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    final CountDownLatch counter = new CountDownLatch(1);
    stopBookKeeper();
    cursor.asyncReadEntries(100, new ReadEntriesCallback() {

        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            entries.forEach(e -> e.release());
            counter.countDown();
        }

        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            fail("async-call should not have failed");
        }
    }, null);
    counter.await();
    cursor.rewind();
    // Clear the cache to force reading from BK
    ledger.entryCache.clear();
    final CountDownLatch counter2 = new CountDownLatch(1);
    cursor.asyncReadEntries(100, new ReadEntriesCallback() {

        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            fail("async-call should have failed");
        }

        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            counter2.countDown();
        }
    }, null);
    counter2.await();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) ZNodeProtobufFormat(org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.ZNodeProtobufFormat) Arrays(java.util.Arrays) Assert.assertNull(org.testng.Assert.assertNull) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) ExecutorService(java.util.concurrent.ExecutorService) Charsets(com.google.common.base.Charsets) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) CyclicBarrier(java.util.concurrent.CyclicBarrier) Logger(org.slf4j.Logger) Factory(org.testng.annotations.Factory) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) Sets(com.google.common.collect.Sets) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 14 with ManagedCursor

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

the class ManagedCursorTest method readTwice.

@Test(timeOut = 20000)
void readTwice() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    ManagedCursor c1 = ledger.openCursor("c1");
    ManagedCursor c2 = ledger.openCursor("c2");
    ledger.addEntry("entry-1".getBytes(Encoding));
    ledger.addEntry("entry-2".getBytes(Encoding));
    List<Entry> entries = c1.readEntries(2);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    entries = c1.readEntries(2);
    assertEquals(entries.size(), 0);
    entries.forEach(e -> e.release());
    entries = c2.readEntries(2);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    entries = c2.readEntries(2);
    assertEquals(entries.size(), 0);
    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 15 with ManagedCursor

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

the class ManagedCursorTest method deleteCursor.

@Test(timeOut = 20000)
void deleteCursor() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("entry-1".getBytes(Encoding));
    Position p2 = ledger.addEntry("entry-2".getBytes(Encoding));
    assertEquals(c1.getNumberOfEntries(), 2);
    // Remove and recreate the same cursor
    ledger.deleteCursor("c1");
    try {
        c1.readEntries(10);
        fail("must fail, the cursor should be closed");
    } catch (ManagedLedgerException e) {
    // ok
    }
    try {
        c1.markDelete(p2);
        fail("must fail, the cursor should be closed");
    } catch (ManagedLedgerException e) {
    // ok
    }
    c1 = ledger.openCursor("c1");
    assertEquals(c1.getNumberOfEntries(), 0);
    c1.close();
    try {
        c1.readEntries(10);
        fail("must fail, the cursor should be closed");
    } catch (ManagedLedgerException e) {
    // ok
    }
    c1.close();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)302 Test (org.testng.annotations.Test)281 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)228 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)153 Position (org.apache.bookkeeper.mledger.Position)131 Entry (org.apache.bookkeeper.mledger.Entry)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)111 CountDownLatch (java.util.concurrent.CountDownLatch)66 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 CyclicBarrier (java.util.concurrent.CyclicBarrier)30 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)26 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)24 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 BKException (org.apache.bookkeeper.client.BKException)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 List (java.util.List)18 Future (java.util.concurrent.Future)18