Search in sources :

Example 76 with ManagedLedgerException

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

the class ManagedLedgerImpl method addEntry.

@Override
public Position addEntry(byte[] data, int offset, int length) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    // position
    class Result {

        ManagedLedgerException status = null;

        Position position = null;
    }
    final Result result = new Result();
    asyncAddEntry(data, offset, length, new AddEntryCallback() {

        @Override
        public void addComplete(Position position, Object ctx) {
            result.position = position;
            counter.countDown();
        }

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            result.status = exception;
            counter.countDown();
        }
    }, null);
    counter.await();
    if (result.status != null) {
        log.error("[{}] Error adding entry", name, result.status);
        throw result.status;
    }
    return result.position;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)

Example 77 with ManagedLedgerException

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

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() {

            public void addFailed(ManagedLedgerException exception, Object ctx) {
            }

            public void addComplete(Position position, Object ctx) {
                lastPosition.set(position);
                c1.asyncMarkDelete(position, new MarkDeleteCallback() {

                    public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                    }

                    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)

Example 78 with ManagedLedgerException

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

the class ManagedCursorTest method markDeleteWithErrors.

@Test(timeOut = 20000)
void markDeleteWithErrors() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    List<Entry> entries = cursor.readEntries(100);
    stopBookKeeper();
    assertEquals(entries.size(), 1);
    try {
        cursor.markDelete(entries.get(0).getPosition());
        fail("call should have failed");
    } catch (ManagedLedgerException e) {
    // ok
    }
    entries.forEach(e -> e.release());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 79 with ManagedLedgerException

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

the class ManagedCursorTest method asyncReadWithInvalidParameter.

@Test(timeOut = 20000, expectedExceptions = IllegalArgumentException.class)
void asyncReadWithInvalidParameter() throws Exception {
    ManagedLedger ledger = 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(0, new ReadEntriesCallback() {

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

        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            counter.countDown();
        }
    }, null);
    counter.await();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 80 with ManagedLedgerException

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

Aggregations

ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)133 Test (org.testng.annotations.Test)70 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)65 CountDownLatch (java.util.concurrent.CountDownLatch)63 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)46 Entry (org.apache.bookkeeper.mledger.Entry)36 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)33 Position (org.apache.bookkeeper.mledger.Position)33 CompletableFuture (java.util.concurrent.CompletableFuture)23 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 List (java.util.List)18 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)18 BKException (org.apache.bookkeeper.client.BKException)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)13 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)13 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13