Search in sources :

Example 1 with DeleteCursorCallback

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

the class ManagedLedgerImpl method asyncDelete.

@Override
public void asyncDelete(final DeleteLedgerCallback callback, final Object ctx) {
    // Delete the managed ledger without closing, since we are not interested in gracefully closing cursors and
    // ledgers
    STATE_UPDATER.set(this, State.Fenced);
    List<ManagedCursor> cursors = Lists.newArrayList(this.cursors);
    if (cursors.isEmpty()) {
        // No cursors to delete, proceed with next step
        deleteAllLedgers(callback, ctx);
        return;
    }
    AtomicReference<ManagedLedgerException> cursorDeleteException = new AtomicReference<>();
    AtomicInteger cursorsToDelete = new AtomicInteger(cursors.size());
    for (ManagedCursor cursor : cursors) {
        asyncDeleteCursor(cursor.getName(), new DeleteCursorCallback() {

            @Override
            public void deleteCursorComplete(Object ctx) {
                if (cursorsToDelete.decrementAndGet() == 0) {
                    if (cursorDeleteException.get() != null) {
                        // Some cursor failed to delete
                        callback.deleteLedgerFailed(cursorDeleteException.get(), ctx);
                        return;
                    }
                    // All cursors deleted, continue with deleting all ledgers
                    deleteAllLedgers(callback, ctx);
                }
            }

            @Override
            public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
                log.warn("[{}] Failed to delete cursor {}", name, cursor, exception);
                cursorDeleteException.compareAndSet(null, exception);
                if (cursorsToDelete.decrementAndGet() == 0) {
                    // Trigger callback only once
                    callback.deleteLedgerFailed(exception, ctx);
                }
            }
        }, null);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 2 with DeleteCursorCallback

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

the class PersistentTopic method unsubscribe.

/**
     * Delete the cursor ledger for a given subscription
     *
     * @param subscriptionName
     *            Subscription for which the cursor ledger is to be deleted
     * @return Completable future indicating completion of unsubscribe operation Completed exceptionally with:
     *         ManagedLedgerException if cursor ledger delete fails
     */
@Override
public CompletableFuture<Void> unsubscribe(String subscriptionName) {
    CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
    ledger.asyncDeleteCursor(Codec.encode(subscriptionName), new DeleteCursorCallback() {

        @Override
        public void deleteCursorComplete(Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Cursor deleted successfully", topic, subscriptionName);
            }
            subscriptions.remove(subscriptionName);
            unsubscribeFuture.complete(null);
            lastActive = System.nanoTime();
        }

        @Override
        public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Error deleting cursor for subscription", topic, subscriptionName, exception);
            }
            unsubscribeFuture.completeExceptionally(new PersistenceException(exception));
        }
    }, null);
    return unsubscribeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)

Example 3 with DeleteCursorCallback

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

the class ServerCnxTest method setupMLAsyncCallbackMocks.

private void setupMLAsyncCallbackMocks() {
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    // call openLedgerComplete with ledgerMock on ML factory asyncOpen
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(300);
            ((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 {
            Thread.sleep(300);
            new Thread(() -> {
                ((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerFailed(new ManagedLedgerException("Managed ledger failure"), null);
            }).start();
            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());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(300);
            ((OpenCursorCallback) invocationOnMock.getArguments()[1]).openCursorComplete(cursorMock, null);
            return null;
        }
    }).when(ledgerMock).asyncOpenCursor(matches(".*success.*"), any(OpenCursorCallback.class), anyObject());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(300);
            ((OpenCursorCallback) invocationOnMock.getArguments()[1]).openCursorFailed(new ManagedLedgerException("Managed ledger failure"), null);
            return null;
        }
    }).when(ledgerMock).asyncOpenCursor(matches(".*fail.*"), any(OpenCursorCallback.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(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((DeleteCursorCallback) invocationOnMock.getArguments()[1]).deleteCursorFailed(new ManagedLedgerException("Managed ledger failure"), null);
            return null;
        }
    }).when(ledgerMock).asyncDeleteCursor(matches(".*fail.*"), any(DeleteCursorCallback.class), anyObject());
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((CloseCallback) invocationOnMock.getArguments()[0]).closeComplete(null);
            return null;
        }
    }).when(cursorMock).asyncClose(any(CloseCallback.class), anyObject());
    doReturn(successSubName).when(cursorMock).getName();
}
Also used : PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) ByteBuf(io.netty.buffer.ByteBuf) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) 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 4 with DeleteCursorCallback

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

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

        @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()[1]).openCursorComplete(cursorMock, null);
            return null;
        }
    }).when(ledgerMock).asyncOpenCursor(matches(".*success.*"), 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());
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ByteBuf(io.netty.buffer.ByteBuf) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) 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 5 with DeleteCursorCallback

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

the class ManagedLedgerImpl method asyncDelete.

@Override
public void asyncDelete(final DeleteLedgerCallback callback, final Object ctx) {
    // Delete the managed ledger without closing, since we are not interested in gracefully closing cursors and
    // ledgers
    STATE_UPDATER.set(this, State.Fenced);
    List<ManagedCursor> cursors = Lists.newArrayList(this.cursors);
    if (cursors.isEmpty()) {
        // No cursors to delete, proceed with next step
        deleteAllLedgers(callback, ctx);
        return;
    }
    AtomicReference<ManagedLedgerException> cursorDeleteException = new AtomicReference<>();
    AtomicInteger cursorsToDelete = new AtomicInteger(cursors.size());
    for (ManagedCursor cursor : cursors) {
        asyncDeleteCursor(cursor.getName(), new DeleteCursorCallback() {

            @Override
            public void deleteCursorComplete(Object ctx) {
                if (cursorsToDelete.decrementAndGet() == 0) {
                    if (cursorDeleteException.get() != null) {
                        // Some cursor failed to delete
                        callback.deleteLedgerFailed(cursorDeleteException.get(), ctx);
                        return;
                    }
                    // All cursors deleted, continue with deleting all ledgers
                    deleteAllLedgers(callback, ctx);
                }
            }

            @Override
            public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
                log.warn("[{}] Failed to delete cursor {}", name, cursor, exception);
                cursorDeleteException.compareAndSet(null, exception);
                if (cursorsToDelete.decrementAndGet() == 0) {
                    // Trigger callback only once
                    callback.deleteLedgerFailed(exception, ctx);
                }
            }
        }, null);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Aggregations

DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)19 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)15 CompletableFuture (java.util.concurrent.CompletableFuture)9 Matchers.anyObject (org.mockito.Matchers.anyObject)8 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Test (org.testng.annotations.Test)6 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)5 ByteBuf (io.netty.buffer.ByteBuf)4 Method (java.lang.reflect.Method)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)4 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)4 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)4 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)4 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)4 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)4 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)3 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)3 BeforeMethod (org.testng.annotations.BeforeMethod)3