Search in sources :

Example 36 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class ReplicatorTest method testCloseReplicatorStartProducer.

/**
 * It verifies that PersistentReplicator considers CursorAlreadyClosedException as non-retriable-read exception and
 * it should closed the producer as cursor is already closed because replicator is already deleted.
 *
 * @throws Exception
 */
@Test(timeOut = 15000)
public void testCloseReplicatorStartProducer() throws Exception {
    TopicName dest = TopicName.get("persistent://pulsar/global/ns1/closeCursor");
    // Producer on r1
    MessageProducer producer1 = new MessageProducer(url1, dest);
    // Consumer on r1
    MessageConsumer consumer1 = new MessageConsumer(url1, dest);
    // Consumer on r2
    MessageConsumer consumer2 = new MessageConsumer(url2, dest);
    // Replicator for r1 -> r2
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(dest.toString());
    PersistentReplicator replicator = (PersistentReplicator) topic.getPersistentReplicator("r2");
    // close the cursor
    Field cursorField = PersistentReplicator.class.getDeclaredField("cursor");
    cursorField.setAccessible(true);
    ManagedCursor cursor = (ManagedCursor) cursorField.get(replicator);
    cursor.close();
    // try to read entries
    CountDownLatch latch = new CountDownLatch(1);
    producer1.produce(10);
    cursor.asyncReadEntriesOrWait(10, new ReadEntriesCallback() {

        @Override
        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            latch.countDown();
            fail("it should have been failed");
        }

        @Override
        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
            assertTrue(exception instanceof CursorAlreadyClosedException);
        }
    }, null);
    // replicator-readException: cursorAlreadyClosed
    replicator.readEntriesFailed(new CursorAlreadyClosedException("Cursor already closed exception"), null);
    // wait replicator producer to be closed
    Thread.sleep(1000);
    // Replicator producer must be closed
    Field producerField = AbstractReplicator.class.getDeclaredField("producer");
    producerField.setAccessible(true);
    @SuppressWarnings("unchecked") ProducerImpl<byte[]> replicatorProducer = (ProducerImpl<byte[]>) producerField.get(replicator);
    assertEquals(replicatorProducer, null);
    producer1.close();
    consumer1.close();
    consumer2.close();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) CountDownLatch(java.util.concurrent.CountDownLatch) TopicName(org.apache.pulsar.common.naming.TopicName) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Field(java.lang.reflect.Field) ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) Test(org.testng.annotations.Test)

Example 37 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class ReplicatorTest method testConcurrentReplicator.

@SuppressWarnings("unchecked")
@Test(timeOut = 30000)
public void testConcurrentReplicator() throws Exception {
    log.info("--- Starting ReplicatorTest::testConcurrentReplicator ---");
    final String namespace = "pulsar/global/concurrent";
    admin1.namespaces().createNamespace(namespace);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2"));
    final TopicName topicName = TopicName.get(String.format("persistent://" + namespace + "/topic-%d", 0));
    PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    Producer<byte[]> producer = client1.newProducer().topic(topicName.toString()).create();
    producer.close();
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topicName.toString()).get();
    PulsarClientImpl pulsarClient = spy((PulsarClientImpl) pulsar1.getBrokerService().getReplicationClient("r3"));
    final Method startRepl = PersistentTopic.class.getDeclaredMethod("startReplicator", String.class);
    startRepl.setAccessible(true);
    Field replClientField = BrokerService.class.getDeclaredField("replicationClients");
    replClientField.setAccessible(true);
    ConcurrentOpenHashMap<String, PulsarClient> replicationClients = (ConcurrentOpenHashMap<String, PulsarClient>) replClientField.get(pulsar1.getBrokerService());
    replicationClients.put("r3", pulsarClient);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2", "r3"));
    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 0; i < 5; i++) {
        executor.submit(() -> {
            try {
                startRepl.invoke(topic, "r3");
            } catch (Exception e) {
                fail("setting replicator failed", e);
            }
        });
    }
    Thread.sleep(3000);
    Mockito.verify(pulsarClient, Mockito.times(1)).createProducerAsync(Mockito.any(ProducerConfigurationData.class), Mockito.any(Schema.class));
    client1.shutdown();
}
Also used : ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) Schema(org.apache.pulsar.client.api.Schema) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) TopicName(org.apache.pulsar.common.naming.TopicName) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) Field(java.lang.reflect.Field) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ExecutorService(java.util.concurrent.ExecutorService) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) Test(org.testng.annotations.Test)

Example 38 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class ReplicatorTest method testFailures.

@Test(enabled = true, timeOut = 30000)
public void testFailures() throws Exception {
    log.info("--- Starting ReplicatorTest::testFailures ---");
    try {
        // 1. Create a consumer using the reserved consumer id prefix "pulsar.repl."
        final TopicName dest = TopicName.get(String.format("persistent://pulsar/global/ns/res-cons-id"));
        // Create another consumer using replication prefix as sub id
        MessageConsumer consumer = new MessageConsumer(url2, dest, "pulsar.repl.");
        consumer.close();
    } catch (Exception e) {
    // SUCCESS
    }
}
Also used : NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 39 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

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(timeOut = 30000)
public void testDeleteReplicatorFailure() throws Exception {
    log.info("--- Starting ReplicatorTest::testDeleteReplicatorFailure ---");
    final String topicName = "persistent://pulsar/global/ns/repltopicbatch";
    final TopicName dest = TopicName.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
    @SuppressWarnings("unchecked") CompletableFuture<Void> result = (CompletableFuture<Void>) removeReplicator.invoke(topic, replicatorClusterName);
    result.thenApply((v) -> {
        assertNull(topic.getPersistentReplicator(replicatorClusterName));
        return null;
    });
    producer1.close();
}
Also used : Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) CountDownLatch(java.util.concurrent.CountDownLatch) TopicName(org.apache.pulsar.common.naming.TopicName) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) Test(org.testng.annotations.Test)

Example 40 with TopicName

use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.

the class ReplicatorTest method testReplicatorProducerClosing.

@SuppressWarnings("unchecked")
@Test(priority = 5, timeOut = 30000)
public void testReplicatorProducerClosing() throws Exception {
    log.info("--- Starting ReplicatorTest::testDeleteReplicatorFailure ---");
    final String topicName = "persistent://pulsar/global/ns/repltopicbatch";
    final TopicName dest = TopicName.get(topicName);
    MessageProducer producer1 = new MessageProducer(url1, dest);
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(topicName);
    final String replicatorClusterName = topic.getReplicators().keys().get(0);
    Replicator replicator = topic.getPersistentReplicator(replicatorClusterName);
    pulsar2.close();
    pulsar3.close();
    replicator.disconnect(false);
    Thread.sleep(100);
    Field field = AbstractReplicator.class.getDeclaredField("producer");
    field.setAccessible(true);
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) field.get(replicator);
    assertNull(producer);
    producer1.close();
}
Also used : Field(java.lang.reflect.Field) ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Aggregations

TopicName (org.apache.pulsar.common.naming.TopicName)127 Test (org.testng.annotations.Test)54 CompletableFuture (java.util.concurrent.CompletableFuture)43 WebTarget (javax.ws.rs.client.WebTarget)32 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)23 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)23 Logger (org.slf4j.Logger)23 LoggerFactory (org.slf4j.LoggerFactory)23 List (java.util.List)22 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)22 Map (java.util.Map)20 ExecutionException (java.util.concurrent.ExecutionException)20 TimeUnit (java.util.concurrent.TimeUnit)20 NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)18 Field (java.lang.reflect.Field)17 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)17 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)17 ByteBuf (io.netty.buffer.ByteBuf)15 Set (java.util.Set)15 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)14