Search in sources :

Example 16 with DeleteCursorCallback

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

the class PersistentTopic method removeReplicator.

CompletableFuture<Void> removeReplicator(String remoteCluster) {
    log.info("[{}] Removing replicator to {}", topic, remoteCluster);
    final CompletableFuture<Void> future = new CompletableFuture<>();
    String name = PersistentReplicator.getReplicatorName(replicatorPrefix, remoteCluster);
    replicators.get(remoteCluster).disconnect().thenRun(() -> {
        ledger.asyncDeleteCursor(name, new DeleteCursorCallback() {

            @Override
            public void deleteCursorComplete(Object ctx) {
                replicators.remove(remoteCluster);
                future.complete(null);
            }

            @Override
            public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
                log.error("[{}] Failed to delete cursor {} {}", topic, name, exception.getMessage(), exception);
                future.completeExceptionally(new PersistenceException(exception));
            }
        }, null);
    }).exceptionally(e -> {
        log.error("[{}] Failed to close replication producer {} {}", topic, name, e.getMessage(), e);
        future.completeExceptionally(e);
        return null;
    });
    return future;
}
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 17 with DeleteCursorCallback

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

the class ReplicatorTest method testDeleteReplicatorFailure.

/**
     * It verifies that: if it fails while removing replicator-cluster-cursor: it should not restart the replicator and
     * it should have cleaned up from the list
     * 
     * @throws Exception
     */
@Test
public void testDeleteReplicatorFailure() throws Exception {
    log.info("--- Starting ReplicatorTest::testDeleteReplicatorFailure ---");
    final String topicName = "persistent://pulsar/global/ns/repltopicbatch";
    final DestinationName dest = DestinationName.get(topicName);
    MessageProducer producer1 = new MessageProducer(url1, dest);
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(topicName);
    final String replicatorClusterName = topic.getReplicators().keys().get(0);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) topic.getManagedLedger();
    CountDownLatch latch = new CountDownLatch(1);
    // delete cursor already : so next time if topic.removeReplicator will get exception but then it should
    // remove-replicator from the list even with failure
    ledger.asyncDeleteCursor("pulsar.repl." + replicatorClusterName, new DeleteCursorCallback() {

        @Override
        public void deleteCursorComplete(Object ctx) {
            latch.countDown();
        }

        @Override
        public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
        }
    }, null);
    latch.await();
    Method removeReplicator = PersistentTopic.class.getDeclaredMethod("removeReplicator", String.class);
    removeReplicator.setAccessible(true);
    // invoke removeReplicator : it fails as cursor is not present: but still it should remove the replicator from
    // list without restarting it
    CompletableFuture<Void> result = (CompletableFuture<Void>) removeReplicator.invoke(topic, replicatorClusterName);
    result.thenApply((v) -> {
        assertNull(topic.getPersistentReplicator(replicatorClusterName));
        return null;
    });
}
Also used : Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) Test(org.testng.annotations.Test)

Example 18 with DeleteCursorCallback

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

the class ManagedLedgerImpl method deleteCursor.

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

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    asyncDeleteCursor(name, new DeleteCursorCallback() {

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

        @Override
        public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during delete-cursors operation");
    }
    if (result.exception != null) {
        log.error("Deleting cursor", result.exception);
        throw result.exception;
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with DeleteCursorCallback

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

the class PersistentTopicTest method testAtomicReplicationRemoval.

/**
 * {@link NonPersistentReplicator.removeReplicator} doesn't remove replicator in atomic way and does in multiple step:
 * 1. disconnect replicator producer
 * <p>
 * 2. close cursor
 * <p>
 * 3. remove from replicator-list.
 * <p>
 *
 * If we try to startReplicationProducer before step-c finish then it should not avoid restarting repl-producer.
 *
 * @throws Exception
 */
@Test
public void testAtomicReplicationRemoval() throws Exception {
    final String globalTopicName = "persistent://prop/global/ns-abc/successTopic";
    String localCluster = "local";
    String remoteCluster = "remote";
    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doNothing().when(ledgerMock).asyncDeleteCursor(anyObject(), anyObject(), anyObject());
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    PersistentTopic topic = new PersistentTopic(globalTopicName, ledgerMock, brokerService);
    String remoteReplicatorName = topic.replicatorPrefix + "." + remoteCluster;
    ConcurrentOpenHashMap<String, Replicator> replicatorMap = topic.getReplicators();
    final URL brokerUrl = new URL("http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort());
    PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    ManagedCursor cursor = mock(ManagedCursorImpl.class);
    doReturn(remoteCluster).when(cursor).getName();
    brokerService.getReplicationClients().put(remoteCluster, client);
    PersistentReplicator replicator = spy(new PersistentReplicator(topic, cursor, localCluster, remoteCluster, brokerService));
    replicatorMap.put(remoteReplicatorName, replicator);
    // step-1 remove replicator : it will disconnect the producer but it will wait for callback to be completed
    Method removeMethod = PersistentTopic.class.getDeclaredMethod("removeReplicator", String.class);
    removeMethod.setAccessible(true);
    removeMethod.invoke(topic, remoteReplicatorName);
    // step-2 now, policies doesn't have removed replication cluster so, it should not invoke "startProducer" of the
    // replicator
    when(pulsar.getConfigurationCache().policiesCache().get(AdminResource.path(POLICIES, TopicName.get(globalTopicName).getNamespace()))).thenReturn(Optional.of(new Policies()));
    // try to start replicator again
    topic.startReplProducers();
    // verify: replicator.startProducer is not invoked
    verify(replicator, Mockito.times(0)).startProducer();
    // step-3 : complete the callback to remove replicator from the list
    ArgumentCaptor<DeleteCursorCallback> captor = ArgumentCaptor.forClass(DeleteCursorCallback.class);
    Mockito.verify(ledgerMock).asyncDeleteCursor(anyObject(), captor.capture(), anyObject());
    DeleteCursorCallback callback = captor.getValue();
    callback.deleteCursorComplete(null);
}
Also used : PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) NonPersistentReplicator(org.apache.pulsar.broker.service.nonpersistent.NonPersistentReplicator) Policies(org.apache.pulsar.common.policies.data.Policies) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) NonPersistentReplicator(org.apache.pulsar.broker.service.nonpersistent.NonPersistentReplicator) Matchers.anyString(org.mockito.Matchers.anyString) AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) URL(java.net.URL) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

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