Search in sources :

Example 31 with AddEntryCallback

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

the class ManagedLedgerTest method asyncAddEntryWithError.

@Test(timeOut = 20000)
public void asyncAddEntryWithError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    final CountDownLatch counter = new CountDownLatch(1);
    stopBookKeeper();
    stopZooKeeper();
    ledger.asyncAddEntry("dummy-entry-1".getBytes(Encoding), new AddEntryCallback() {

        @Override
        public void addComplete(Position position, Object ctx) {
            fail("Should have failed");
        }

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            counter.countDown();
        }
    }, null);
    counter.await();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 32 with AddEntryCallback

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

the class ManagedLedgerTest method doubleAsyncAddEntryWithoutError.

@Test(timeOut = 20000)
public void doubleAsyncAddEntryWithoutError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    final CountDownLatch done = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final String content = "dummy-entry-" + i;
        ledger.asyncAddEntry(content.getBytes(Encoding), new AddEntryCallback() {

            @Override
            public void addComplete(Position position, Object ctx) {
                assertNotNull(ctx);
                log.info("Successfully added {}", content);
                done.countDown();
            }

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
                fail(exception.getMessage());
            }
        }, this);
    }
    done.await();
    assertEquals(ledger.getNumberOfEntries(), 10);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 33 with AddEntryCallback

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

the class ManagedLedgerTest method asyncAddEntryWithoutError.

@Test(timeOut = 20000)
public void asyncAddEntryWithoutError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    final CountDownLatch counter = new CountDownLatch(1);
    ledger.asyncAddEntry("dummy-entry-1".getBytes(Encoding), new AddEntryCallback() {

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

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
    assertEquals(ledger.getNumberOfEntries(), 1);
    assertEquals(ledger.getTotalSize(), "dummy-entry-1".getBytes(Encoding).length);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 34 with AddEntryCallback

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

the class NonDurableCursorTest 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() {

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

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

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

                    @Override
                    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 35 with AddEntryCallback

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

the class ManagedLedgerBkTest method managedLedgerClosed.

@Test(timeOut = 30000)
public void managedLedgerClosed() throws Exception {
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedgerImpl ledger1 = (ManagedLedgerImpl) factory.open("my_test_ledger", config);
    int N = 100;
    AtomicReference<ManagedLedgerException> res = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(N);
    for (int i = 0; i < N; i++) {
        ledger1.asyncAddEntry(("entry-" + i).getBytes(), new AddEntryCallback() {

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

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
                res.compareAndSet(null, exception);
                latch.countDown();
            }
        }, null);
        if (i == 1) {
            ledger1.close();
        }
    }
    // Ensures all the callback must have been invoked
    latch.await();
    assertNotNull(res.get());
    assertEquals(res.get().getClass(), ManagedLedgerAlreadyClosedException.class);
    factory.shutdown();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) AtomicReference(java.util.concurrent.atomic.AtomicReference) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Aggregations

AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)35 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)29 CountDownLatch (java.util.concurrent.CountDownLatch)24 Position (org.apache.bookkeeper.mledger.Position)23 Test (org.testng.annotations.Test)22 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)20 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)15 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)11 ByteBuf (io.netty.buffer.ByteBuf)9 InitialPosition (org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition)9 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)6 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)6 Entry (org.apache.bookkeeper.mledger.Entry)6 Matchers.anyObject (org.mockito.Matchers.anyObject)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)4 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)4 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)4