Search in sources :

Example 6 with MarkDeleteCallback

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

the class ManagedLedgerTest method asyncAPI.

@Test(timeOut = 20000)
public void asyncAPI() throws Throwable {
    final CountDownLatch counter = new CountDownLatch(1);
    factory.asyncOpen("my_test_ledger", new ManagedLedgerConfig(), new OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            ledger.asyncOpenCursor("test-cursor", new OpenCursorCallback() {

                @Override
                public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                    ManagedLedger ledger = (ManagedLedger) ctx;
                    ledger.asyncAddEntry("test".getBytes(Encoding), new AddEntryCallback() {

                        @Override
                        public void addComplete(Position position, Object ctx) {
                            @SuppressWarnings("unchecked") Pair<ManagedLedger, ManagedCursor> pair = (Pair<ManagedLedger, ManagedCursor>) ctx;
                            ManagedLedger ledger = pair.first;
                            ManagedCursor cursor = pair.second;
                            assertEquals(ledger.getNumberOfEntries(), 1);
                            assertEquals(ledger.getTotalSize(), "test".getBytes(Encoding).length);
                            cursor.asyncReadEntries(2, new ReadEntriesCallback() {

                                @Override
                                public void readEntriesComplete(List<Entry> entries, Object ctx) {
                                    ManagedCursor cursor = (ManagedCursor) ctx;
                                    assertEquals(entries.size(), 1);
                                    Entry entry = entries.get(0);
                                    final Position position = entry.getPosition();
                                    assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
                                    log.debug("Mark-Deleting to position {}", position);
                                    cursor.asyncMarkDelete(position, new MarkDeleteCallback() {

                                        @Override
                                        public void markDeleteComplete(Object ctx) {
                                            log.debug("Mark delete complete");
                                            ManagedCursor cursor = (ManagedCursor) ctx;
                                            assertEquals(cursor.hasMoreEntries(), false);
                                            counter.countDown();
                                        }

                                        @Override
                                        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                                            fail(exception.getMessage());
                                        }
                                    }, cursor);
                                }

                                @Override
                                public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
                                    fail(exception.getMessage());
                                }
                            }, cursor);
                        }

                        @Override
                        public void addFailed(ManagedLedgerException exception, Object ctx) {
                            fail(exception.getMessage());
                        }
                    }, new Pair<ManagedLedger, ManagedCursor>(ledger, cursor));
                }

                @Override
                public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                    fail(exception.getMessage());
                }
            }, ledger);
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
    log.info("Test completed");
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) 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) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) List(java.util.List) ArrayList(java.util.ArrayList) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Pair(org.apache.bookkeeper.mledger.util.Pair) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair) Test(org.testng.annotations.Test)

Example 7 with MarkDeleteCallback

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

the class ManagedCursorImpl method asyncSkipEntries.

@Override
public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries, final SkipEntriesCallback callback, Object ctx) {
    log.info("[{}] Skipping {} entries on cursor {}", ledger.getName(), numEntriesToSkip, name);
    long numDeletedMessages = 0;
    if (deletedEntries == IndividualDeletedEntries.Exclude) {
        numDeletedMessages = getNumIndividualDeletedEntriesToSkip(numEntriesToSkip);
    }
    asyncMarkDelete(ledger.getPositionAfterN(markDeletePosition, numEntriesToSkip + numDeletedMessages, PositionBound.startExcluded), new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            callback.skipEntriesComplete(ctx);
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            if (exception.getCause() instanceof IllegalArgumentException) {
                // There could be a race condition between calling clear backlog and other mark delete
                // operations.
                // If we get an exception it means the backlog was already cleared in the meantime.
                callback.skipEntriesComplete(ctx);
            } else {
                log.error("[{}] Skip {} entries failed for cursor {}", ledger.getName(), numEntriesToSkip, name, exception);
                callback.skipEntriesFailed(exception, ctx);
            }
        }
    }, ctx);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerImpl.createManagedLedgerException(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)

Example 8 with MarkDeleteCallback

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

the class PersistentTopicTest method setupMLAsyncCallbackMocks.

void setupMLAsyncCallbackMocks() {
    ledgerMock = mock(ManagedLedger.class);
    cursorMock = mock(ManagedCursor.class);
    final CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    doReturn("mockCursor").when(cursorMock).getName();
    // doNothing().when(cursorMock).asyncClose(new CloseCallback() {
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            // return closeFuture.get();
            return closeFuture.complete(null);
        }
    }).when(cursorMock).asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(Object ctx) {
            log.info("[{}] Successfully closed cursor ledger", "mockCursor");
            closeFuture.complete(null);
        }

        @Override
        public void closeFailed(ManagedLedgerException exception, Object ctx) {
            // isFenced.set(false);
            log.error("Error closing cursor for subscription", exception);
            closeFuture.completeExceptionally(new BrokerServiceException.PersistenceException(exception));
        }
    }, null);
    // call openLedgerComplete with ledgerMock on ML factory asyncOpen
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
            return null;
        }
    }).when(mlFactoryMock).asyncOpen(matches(".*success.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    // call openLedgerFailed on ML factory asyncOpen
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerFailed(new ManagedLedgerException("Managed ledger failure"), null);
            return null;
        }
    }).when(mlFactoryMock).asyncOpen(matches(".*fail.*"), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
    // call addComplete on ledger asyncAddEntry
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((AddEntryCallback) invocationOnMock.getArguments()[1]).addComplete(new PositionImpl(1, 1), invocationOnMock.getArguments()[2]);
            return null;
        }
    }).when(ledgerMock).asyncAddEntry(any(ByteBuf.class), any(AddEntryCallback.class), anyObject());
    // call openCursorComplete on cursor asyncOpen
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((OpenCursorCallback) invocationOnMock.getArguments()[2]).openCursorComplete(cursorMock, null);
            return null;
        }
    }).when(ledgerMock).asyncOpenCursor(matches(".*success.*"), any(InitialPosition.class), any(OpenCursorCallback.class), anyObject());
    // call deleteLedgerComplete on ledger asyncDelete
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((DeleteLedgerCallback) invocationOnMock.getArguments()[0]).deleteLedgerComplete(null);
            return null;
        }
    }).when(ledgerMock).asyncDelete(any(DeleteLedgerCallback.class), anyObject());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((DeleteCursorCallback) invocationOnMock.getArguments()[1]).deleteCursorComplete(null);
            return null;
        }
    }).when(ledgerMock).asyncDeleteCursor(matches(".*success.*"), any(DeleteCursorCallback.class), anyObject());
    doAnswer((invokactionOnMock) -> {
        ((MarkDeleteCallback) invokactionOnMock.getArguments()[2]).markDeleteComplete(invokactionOnMock.getArguments()[3]);
        return null;
    }).when(cursorMock).asyncMarkDelete(anyObject(), anyObject(), any(MarkDeleteCallback.class), anyObject());
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ByteBuf(io.netty.buffer.ByteBuf) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) DeleteLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteLedgerCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyObject(org.mockito.Matchers.anyObject) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)

Example 9 with MarkDeleteCallback

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

the class CompactorSubscription method acknowledgeMessage.

@Override
public void acknowledgeMessage(PositionImpl position, AckType ackType, Map<String, Long> properties) {
    checkArgument(ackType == AckType.Cumulative);
    checkArgument(properties.containsKey(Compactor.COMPACTED_TOPIC_LEDGER_PROPERTY));
    long compactedLedgerId = properties.get(Compactor.COMPACTED_TOPIC_LEDGER_PROPERTY);
    if (log.isDebugEnabled()) {
        log.debug("[{}][{}] Cumulative ack on compactor subscription {}", topicName, subName, position);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    cursor.asyncMarkDelete(position, properties, new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Mark deleted messages until position on compactor subscription {}", topicName, subName, position);
            }
            future.complete(null);
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            // TODO: cut consumer connection on markDeleteFailed
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Failed to mark delete for position on compactor subscription {}", topicName, subName, ctx, exception);
            }
        }
    }, null);
    if (topic.getManagedLedger().isTerminated() && cursor.getNumberOfEntriesInBacklog() == 0) {
        // Notify all consumer that the end of topic was reached
        dispatcher.getConsumers().forEach(Consumer::reachedEndOfTopic);
    }
    // Once properties have been persisted, we can notify the compacted topic to use
    // the new ledger
    future.thenAccept((v) -> compactedTopic.newCompactedLedger(position, compactedLedgerId));
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Consumer(org.apache.pulsar.broker.service.Consumer)

Example 10 with MarkDeleteCallback

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

the class MessageDeduplication method takeSnapshot.

private void takeSnapshot(PositionImpl position) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] Taking snapshot of sequence ids map", topic.getName());
    }
    Map<String, Long> snapshot = new TreeMap<>();
    highestSequencedPersisted.forEach((producerName, sequenceId) -> {
        if (snapshot.size() < maxNumberOfProducers) {
            snapshot.put(producerName, sequenceId);
        }
    });
    managedCursor.asyncMarkDelete(position, snapshot, new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Stored new deduplication snapshot at {}", topic.getName(), position);
            }
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            log.warn("[{}] Failed to store new deduplication snapshot at {}", topic.getName(), position);
        }
    }, null);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) TreeMap(java.util.TreeMap)

Aggregations

MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)19 CountDownLatch (java.util.concurrent.CountDownLatch)11 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)10 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)10 Position (org.apache.bookkeeper.mledger.Position)10 Test (org.testng.annotations.Test)9 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)8 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)6 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)5 ManagedLedgerException.getManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException)4 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)3 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 BKException (org.apache.bookkeeper.client.BKException)2