Search in sources :

Example 1 with CloseCallback

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

the class ManagedLedgerImpl method close.

@Override
public void close() throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(Object ctx) {
            counter.countDown();
        }

        @Override
        public void closeFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during managed ledger close");
    }
    if (result.exception != null) {
        log.error("[{}] Error closing managed ledger", name, result.exception);
        throw result.exception;
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CloseCallback

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

the class PersistentTopic method close.

/**
     * Close this topic - close all producers and subscriptions associated with this topic
     *
     * @return Completable future indicating completion of close operation
     */
@Override
public CompletableFuture<Void> close() {
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    lock.writeLock().lock();
    try {
        if (!isFenced) {
            isFenced = true;
        } else {
            log.warn("[{}] Topic is already being closed or deleted", topic);
            closeFuture.completeExceptionally(new TopicFencedException("Topic is already fenced"));
            return closeFuture;
        }
    } finally {
        lock.writeLock().unlock();
    }
    List<CompletableFuture<Void>> futures = Lists.newArrayList();
    replicators.forEach((cluster, replicator) -> futures.add(replicator.disconnect()));
    producers.forEach(producer -> futures.add(producer.disconnect()));
    subscriptions.forEach((s, sub) -> futures.add(sub.disconnect()));
    FutureUtil.waitForAll(futures).thenRun(() -> {
        // After having disconnected all producers/consumers, close the managed ledger
        ledger.asyncClose(new CloseCallback() {

            @Override
            public void closeComplete(Object ctx) {
                // Everything is now closed, remove the topic from map
                brokerService.removeTopicFromCache(topic);
                log.info("[{}] Topic closed", topic);
                closeFuture.complete(null);
            }

            @Override
            public void closeFailed(ManagedLedgerException exception, Object ctx) {
                log.error("[{}] Failed to close managed ledger, proceeding anyway.", topic, exception);
                brokerService.removeTopicFromCache(topic);
                closeFuture.complete(null);
            }
        }, null);
    }).exceptionally(exception -> {
        log.error("[{}] Error closing topic", topic, exception);
        isFenced = false;
        closeFuture.completeExceptionally(exception);
        return null;
    });
    return closeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) TopicFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicFencedException) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)

Example 3 with CloseCallback

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

the class PersistentSubscription method close.

/**
     * Close the cursor ledger for this subscription. Requires that there are no active consumers on the dispatcher
     *
     * @return CompletableFuture indicating the completion of delete operation
     */
@Override
public CompletableFuture<Void> close() {
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    synchronized (this) {
        if (dispatcher != null && dispatcher.isConsumerConnected()) {
            closeFuture.completeExceptionally(new SubscriptionBusyException("Subscription has active consumers"));
            return closeFuture;
        }
        IS_FENCED_UPDATER.set(this, TRUE);
        log.info("[{}][{}] Successfully fenced cursor ledger [{}]", topicName, subName, cursor);
    }
    cursor.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Successfully closed cursor ledger", topicName, subName);
            }
            closeFuture.complete(null);
        }

        @Override
        public void closeFailed(ManagedLedgerException exception, Object ctx) {
            IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
            log.error("[{}][{}] Error closing cursor for subscription", topicName, subName, exception);
            closeFuture.completeExceptionally(new PersistenceException(exception));
        }
    }, null);
    return closeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)

Example 4 with CloseCallback

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

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

Aggregations

CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)7 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)7 CompletableFuture (java.util.concurrent.CompletableFuture)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)3 ByteBuf (io.netty.buffer.ByteBuf)2 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)2 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)2 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)2 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)2 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Test (org.testng.annotations.Test)2 PersistenceException (com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException)1 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)1 TopicFencedException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicFencedException)1 DeleteLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteLedgerCallback)1 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)1