Search in sources :

Example 6 with AddEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback 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 7 with AddEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback 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 8 with AddEntryCallback

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

the class OpAddEntry method closeComplete.

@Override
public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
    checkArgument(ledger.getId() == lh.getId());
    if (rc == BKException.Code.OK) {
        log.debug("Successfuly closed ledger {}", lh.getId());
    } else {
        log.warn("Error when closing ledger {}. Status={}", lh.getId(), BKException.getMessage(rc));
    }
    ml.ledgerClosed(lh);
    updateLatency();
    AddEntryCallback cb = callbackUpdater.getAndSet(this, null);
    if (cb != null) {
        cb.addComplete(PositionImpl.get(lh.getId(), entryId), ctx);
        ml.notifyCursors();
        this.recycle();
    }
}
Also used : AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)

Example 9 with AddEntryCallback

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

the class OpAddEntry method failed.

public void failed(ManagedLedgerException e) {
    AddEntryCallback cb = callbackUpdater.getAndSet(this, null);
    if (cb != null) {
        cb.addFailed(e, ctx);
        ml.mbean.recordAddEntryError();
    }
}
Also used : AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)

Example 10 with AddEntryCallback

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

the class PersistentTopicTest method testPublishMessageMLFailure.

@Test
public void testPublishMessageMLFailure() throws Exception {
    final String successTopicName = "persistent://prop/use/ns-abc/successTopic";
    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    MessageMetadata.Builder messageMetadata = MessageMetadata.newBuilder();
    messageMetadata.setPublishTime(System.currentTimeMillis());
    messageMetadata.setProducerName("prod-name");
    messageMetadata.setSequenceId(1);
    ByteBuf payload = Unpooled.wrappedBuffer("content".getBytes());
    final CountDownLatch latch = new CountDownLatch(1);
    // override asyncAddEntry callback to return error
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((AddEntryCallback) invocationOnMock.getArguments()[1]).addFailed(new ManagedLedgerException("Managed ledger failure"), invocationOnMock.getArguments()[2]);
            return null;
        }
    }).when(ledgerMock).asyncAddEntry(any(ByteBuf.class), any(AddEntryCallback.class), anyObject());
    topic.publishMessage(payload, (exception, ledgerId, entryId) -> {
        if (exception == null) {
            fail("publish should have failed");
        } else {
            latch.countDown();
        }
    });
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Matchers.anyString(org.mockito.Matchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) MessageMetadata(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Aggregations

AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)16 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 Position (org.apache.bookkeeper.mledger.Position)10 Test (org.testng.annotations.Test)10 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)9 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)5 ByteBuf (io.netty.buffer.ByteBuf)4 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)3 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)3 Entry (org.apache.bookkeeper.mledger.Entry)3 Matchers.anyObject (org.mockito.Matchers.anyObject)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)2 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)1