Search in sources :

Example 81 with ManagedCursor

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

the class ManagedCursorTest method testConcurrentResetCursor.

@Test(timeOut = 20000)
void testConcurrentResetCursor() throws Exception {
    ManagedLedger ledger = factory.open("my_test_concurrent_move_ledger");
    final int Messages = 100;
    final int Consumers = 5;
    List<Future<AtomicBoolean>> futures = Lists.newArrayList();
    ExecutorService executor = Executors.newCachedThreadPool();
    final CyclicBarrier barrier = new CyclicBarrier(Consumers + 1);
    for (int i = 0; i < Messages; i++) {
        ledger.addEntry("test".getBytes());
    }
    final PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    for (int i = 0; i < Consumers; i++) {
        final ManagedCursor cursor = ledger.openCursor("tcrc" + i);
        final int idx = i;
        futures.add(executor.submit(new Callable<AtomicBoolean>() {

            public AtomicBoolean call() throws Exception {
                barrier.await();
                final AtomicBoolean moveStatus = new AtomicBoolean(false);
                CountDownLatch countDownLatch = new CountDownLatch(1);
                final PositionImpl resetPosition = new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - (5 * idx));
                cursor.asyncResetCursor(resetPosition, new AsyncCallbacks.ResetCursorCallback() {

                    @Override
                    public void resetComplete(Object ctx) {
                        moveStatus.set(true);
                        PositionImpl pos = (PositionImpl) ctx;
                        log.info("move to [{}] completed for consumer [{}]", pos.toString(), idx);
                        countDownLatch.countDown();
                    }

                    @Override
                    public void resetFailed(ManagedLedgerException exception, Object ctx) {
                        moveStatus.set(false);
                        PositionImpl pos = (PositionImpl) ctx;
                        log.warn("move to [{}] failed for consumer [{}]", pos.toString(), idx);
                        countDownLatch.countDown();
                    }
                });
                countDownLatch.await();
                assertTrue(cursor.getReadPosition().equals(resetPosition));
                cursor.close();
                return moveStatus;
            }
        }));
    }
    barrier.await();
    for (Future<AtomicBoolean> f : futures) {
        assertTrue(f.get().get());
    }
    ledger.close();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Test(org.testng.annotations.Test)

Example 82 with ManagedCursor

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

the class ManagedCursorTest method markDeleteAcrossLedgers.

@Test(timeOut = 20000)
void markDeleteAcrossLedgers() throws Exception {
    ManagedLedger ml1 = factory.open("my_test_ledger");
    ManagedCursor mc1 = ml1.openCursor("c1");
    // open ledger id 3 for ml1
    // markDeletePosition for mc1 is 3:-1
    // readPosition is 3:0
    ml1.close();
    mc1.close();
    // force removal of this ledger from the cache
    factory.close(ml1);
    ManagedLedger ml2 = factory.open("my_test_ledger");
    ManagedCursor mc2 = ml2.openCursor("c1");
    // open ledger id 5 for ml2
    // this entry is written at 5:0
    Position pos = ml2.addEntry("dummy-entry-1".getBytes(Encoding));
    List<Entry> entries = mc2.readEntries(1);
    assertEquals(entries.size(), 1);
    assertEquals(new String(entries.get(0).getData(), Encoding), "dummy-entry-1");
    entries.forEach(e -> e.release());
    mc2.delete(pos);
    // verify if the markDeletePosition moves from 3:-1 to 5:0
    assertEquals(mc2.getMarkDeletedPosition(), pos);
    assertEquals(mc2.getMarkDeletedPosition().getNext(), mc2.getReadPosition());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 83 with ManagedCursor

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

the class ManagedCursorTest method seekPosition3.

@Test(timeOut = 20000)
void seekPosition3() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    PositionImpl seekPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    Position entry5 = ledger.addEntry("dummy-entry-5".getBytes(Encoding));
    Position entry6 = ledger.addEntry("dummy-entry-6".getBytes(Encoding));
    cursor.seek(new PositionImpl(seekPosition.getLedgerId(), seekPosition.getEntryId()));
    assertEquals(cursor.getReadPosition(), seekPosition);
    List<Entry> entries = cursor.readEntries(1);
    assertEquals(entries.size(), 1);
    assertEquals(new String(entries.get(0).getData(), Encoding), "dummy-entry-4");
    entries.forEach(e -> e.release());
    cursor.seek(entry5.getNext());
    assertEquals(cursor.getReadPosition(), entry6);
    entries = cursor.readEntries(1);
    assertEquals(entries.size(), 1);
    assertEquals(new String(entries.get(0).getData(), Encoding), "dummy-entry-6");
    entries.forEach(e -> e.release());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) 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 84 with ManagedCursor

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

the class ManagedCursorTest method cursorPersistenceAsyncMarkDeleteSameThread.

@Test(timeOut = 20000)
void cursorPersistenceAsyncMarkDeleteSameThread() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMetadataMaxEntriesPerLedger(5));
    final ManagedCursor c1 = ledger.openCursor("c1");
    final int N = 100;
    List<Position> positions = Lists.newArrayList();
    for (int i = 0; i < N; i++) {
        Position p = ledger.addEntry("dummy-entry".getBytes(Encoding));
        positions.add(p);
    }
    Position lastPosition = positions.get(N - 1);
    final CountDownLatch latch = new CountDownLatch(N);
    for (final Position p : positions) {
        c1.asyncMarkDelete(p, new MarkDeleteCallback() {

            public void markDeleteComplete(Object ctx) {
                latch.countDown();
            }

            public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                log.error("Failed to markdelete", exception);
                latch.countDown();
            }
        }, null);
    }
    latch.await();
    // Reopen
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger");
    ManagedCursor c2 = ledger.openCursor("c1");
    assertEquals(c2.getMarkDeletedPosition(), lastPosition);
    factory2.shutdown();
}
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) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 85 with ManagedCursor

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

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) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)135 Test (org.testng.annotations.Test)126 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)102 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)66 Position (org.apache.bookkeeper.mledger.Position)59 Entry (org.apache.bookkeeper.mledger.Entry)57 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)52 CountDownLatch (java.util.concurrent.CountDownLatch)32 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 CyclicBarrier (java.util.concurrent.CyclicBarrier)15 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)12 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)12 List (java.util.List)10 BKException (org.apache.bookkeeper.client.BKException)10 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)10 Future (java.util.concurrent.Future)9 TimeUnit (java.util.concurrent.TimeUnit)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)9