Search in sources :

Example 6 with ManagedCursor

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

the class ManagedCursorConcurrencyTest method testConcurrentIndividualDeletesWithGetNthEntry.

@Test(timeOut = 30000)
public void testConcurrentIndividualDeletesWithGetNthEntry() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(100).setThrottleMarkDelete(0.5));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final int N = 1000;
    final List<Position> addedEntries = Lists.newArrayListWithExpectedSize(N);
    for (int i = 0; i < N; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final int deleteEntries = 100;
    final CountDownLatch counter = new CountDownLatch(deleteEntries);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    final AtomicInteger iteration = new AtomicInteger(0);
    for (int i = 0; i < deleteEntries; i++) {
        executor.submit(safeRun(() -> {
            try {
                cursor.asyncDelete(addedEntries.get(iteration.getAndIncrement()), new DeleteCallback() {

                    @Override
                    public void deleteComplete(Object ctx) {
                    // Ok
                    }

                    @Override
                    public void deleteFailed(ManagedLedgerException exception, Object ctx) {
                        exception.printStackTrace();
                        gotException.set(true);
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }));
    }
    counter.await();
    final int readEntries = N - deleteEntries;
    final CountDownLatch readCounter = new CountDownLatch(readEntries);
    final AtomicInteger successReadEntries = new AtomicInteger(0);
    for (int i = 1; i <= readEntries; i++) {
        try {
            cursor.asyncGetNthEntry(i, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {

                @Override
                public void readEntryComplete(Entry entry, Object ctx) {
                    successReadEntries.getAndIncrement();
                    entry.release();
                    readCounter.countDown();
                }

                @Override
                public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
                    exception.printStackTrace();
                    gotException.set(true);
                }
            }, null);
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        }
    }
    readCounter.await();
    assertFalse(gotException.get());
    assertEquals(successReadEntries.get(), readEntries);
}
Also used : ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 7 with ManagedCursor

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

the class ManagedCursorConcurrencyTest method testCloseAndRead.

@Test
public void testCloseAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_close_and_read", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final CompletableFuture<String> closeFuture = new CompletableFuture<>();
    final String CLOSED = "closed";
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (Position position : addedEntries) {
                    cursor.markDelete(position);
                    Thread.sleep(1);
                }
            } catch (ManagedLedgerException e) {
                if (!e.getMessage().equals("Cursor was already closed")) {
                    gotException.set(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread reader = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 1000; i++) {
                    cursor.readEntries(1).forEach(e -> e.release());
                    // Thread.sleep(2,200);
                    Thread.sleep(2, 195);
                }
                cursor.asyncClose(new AsyncCallbacks.CloseCallback() {

                    @Override
                    public void closeComplete(Object ctx) {
                        log.info("Successfully closed cursor ledger");
                        closeFuture.complete(CLOSED);
                    }

                    @Override
                    public void closeFailed(ManagedLedgerException exception, Object ctx) {
                        log.error("Error closing cursor: ", exception);
                        closeFuture.completeExceptionally(new Exception(exception));
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(closeFuture.get(), CLOSED);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 8 with ManagedCursor

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

the class ManagedLedgerImpl method openCursor.

@Override
public ManagedCursor openCursor(String cursorName) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedCursor cursor = null;

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    asyncOpenCursor(cursorName, new OpenCursorCallback() {

        @Override
        public void openCursorComplete(ManagedCursor cursor, Object ctx) {
            result.cursor = cursor;
            counter.countDown();
        }

        @Override
        public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during open-cursor operation");
    }
    if (result.exception != null) {
        log.error("Error adding entry", result.exception);
        throw result.exception;
    }
    return result.cursor;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 9 with ManagedCursor

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

the class ManagedCursorContainerTest method updatingCursorOutsideContainer.

@Test
void updatingCursorOutsideContainer() throws Exception {
    ManagedCursorContainer container = new ManagedCursorContainer();
    ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
    container.add(cursor1);
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
    MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(2, 2));
    container.add(cursor2);
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
    cursor2.position = new PositionImpl(8, 8);
    // Until we don't update the container, the ordering will not change
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
    container.cursorUpdated(cursor2, cursor2.position);
    assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
}
Also used : ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 10 with ManagedCursor

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

the class ManagedCursorTest method testNumberOfEntries.

@Test(timeOut = 20000)
void testNumberOfEntries() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    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));
    ManagedCursor c4 = ledger.openCursor("c4");
    ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    ManagedCursor c5 = ledger.openCursor("c5");
    assertEquals(c1.getNumberOfEntries(), 4);
    assertEquals(c1.hasMoreEntries(), true);
    assertEquals(c2.getNumberOfEntries(), 3);
    assertEquals(c2.hasMoreEntries(), true);
    assertEquals(c3.getNumberOfEntries(), 2);
    assertEquals(c3.hasMoreEntries(), true);
    assertEquals(c4.getNumberOfEntries(), 1);
    assertEquals(c4.hasMoreEntries(), true);
    assertEquals(c5.getNumberOfEntries(), 0);
    assertEquals(c5.hasMoreEntries(), false);
    List<Entry> entries = c1.readEntries(2);
    assertEquals(entries.size(), 2);
    c1.markDelete(entries.get(1).getPosition());
    assertEquals(c1.getNumberOfEntries(), 2);
    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)

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