Search in sources :

Example 1 with ProducerImpl

use of org.apache.pulsar.client.impl.ProducerImpl 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 2 with ProducerImpl

use of org.apache.pulsar.client.impl.ProducerImpl 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)

Example 3 with ProducerImpl

use of org.apache.pulsar.client.impl.ProducerImpl in project incubator-pulsar by apache.

the class PersistentTopicE2ETest method testProducerQueueFullBlocking.

@Test
public void testProducerQueueFullBlocking() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic-xyzx";
    final int messages = 10;
    PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    // 1. Producer connect
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) client.newProducer().topic(topicName).maxPendingMessages(messages).blockIfQueueFull(true).sendTimeout(1, TimeUnit.SECONDS).create();
    // 2. Stop broker
    cleanup();
    // 2. producer publish messages
    long startTime = System.nanoTime();
    for (int i = 0; i < messages; i++) {
        // Should never block
        producer.sendAsync("msg".getBytes());
    }
    // Verify thread was not blocked
    long delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);
    // Next send operation must block, until all the messages in the queue expire
    startTime = System.nanoTime();
    producer.sendAsync("msg".getBytes());
    delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs > TimeUnit.MILLISECONDS.toNanos(500));
    assertTrue(delayNs < TimeUnit.MILLISECONDS.toNanos(1500));
    assertEquals(producer.getPendingQueueSize(), 1);
    // 4. producer disconnect
    producer.close();
    client.close();
    // 5. Restart broker
    setup();
}
Also used : ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 4 with ProducerImpl

use of org.apache.pulsar.client.impl.ProducerImpl in project incubator-pulsar by apache.

the class NonPersistentTopicTest method testMsgDropStat.

/**
 * Verifies msg-drop stats
 *
 * @throws Exception
 */
@Test
public void testMsgDropStat() throws Exception {
    int defaultNonPersistentMessageRate = conf.getMaxConcurrentNonPersistentMessagePerConnection();
    try {
        final String topicName = "non-persistent://my-property/use/my-ns/stats-topic";
        // restart broker with lower publish rate limit
        conf.setMaxConcurrentNonPersistentMessagePerConnection(1);
        stopBroker();
        startBroker();
        Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("subscriber-1").receiverQueueSize(1).subscribe();
        Consumer<byte[]> consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName("subscriber-2").receiverQueueSize(1).subscriptionType(SubscriptionType.Shared).subscribe();
        ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) pulsarClient.newProducer().topic(topicName).create();
        String firstTimeConnected = producer.getConnectedSince();
        ExecutorService executor = Executors.newFixedThreadPool(5);
        byte[] msgData = "testData".getBytes();
        final int totalProduceMessages = 200;
        CountDownLatch latch = new CountDownLatch(totalProduceMessages);
        for (int i = 0; i < totalProduceMessages; i++) {
            executor.submit(() -> {
                producer.sendAsync(msgData).handle((msg, e) -> {
                    latch.countDown();
                    return null;
                });
            });
        }
        latch.await();
        NonPersistentTopic topic = (NonPersistentTopic) pulsar.getBrokerService().getTopic(topicName).get();
        pulsar.getBrokerService().updateRates();
        NonPersistentTopicStats stats = topic.getStats();
        NonPersistentPublisherStats npStats = stats.getPublishers().get(0);
        NonPersistentSubscriptionStats sub1Stats = stats.getSubscriptions().get("subscriber-1");
        NonPersistentSubscriptionStats sub2Stats = stats.getSubscriptions().get("subscriber-2");
        assertTrue(npStats.msgDropRate > 0);
        assertTrue(sub1Stats.msgDropRate > 0);
        assertTrue(sub2Stats.msgDropRate > 0);
        // make sure producer connection not disconnected due to unordered ack
        assertEquals(firstTimeConnected, producer.getConnectedSince());
        producer.close();
        consumer.close();
        consumer2.close();
        executor.shutdown();
    } finally {
        conf.setMaxConcurrentNonPersistentMessagePerConnection(defaultNonPersistentMessageRate);
    }
}
Also used : ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) NonPersistentSubscriptionStats(org.apache.pulsar.common.policies.data.NonPersistentSubscriptionStats) ExecutorService(java.util.concurrent.ExecutorService) NonPersistentTopicStats(org.apache.pulsar.common.policies.data.NonPersistentTopicStats) NonPersistentPublisherStats(org.apache.pulsar.common.policies.data.NonPersistentPublisherStats) CountDownLatch(java.util.concurrent.CountDownLatch) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Example 5 with ProducerImpl

use of org.apache.pulsar.client.impl.ProducerImpl in project incubator-pulsar by apache.

the class PersistentTopicE2ETest method testProducerQueueFullNonBlocking.

@Test
public void testProducerQueueFullNonBlocking() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic-xyzx";
    final int messages = 10;
    // 1. Producer connect
    PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) client.newProducer().topic(topicName).maxPendingMessages(messages).blockIfQueueFull(false).sendTimeout(1, TimeUnit.SECONDS).create();
    // 2. Stop broker
    cleanup();
    // 2. producer publish messages
    long startTime = System.nanoTime();
    for (int i = 0; i < messages; i++) {
        // Should never block
        producer.sendAsync("msg".getBytes());
    }
    // Verify thread was not blocked
    long delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);
    // Next send operation must fail and not block
    startTime = System.nanoTime();
    try {
        producer.send("msg".getBytes());
        fail("Send should have failed");
    } catch (PulsarClientException.ProducerQueueIsFullError e) {
    // Expected
    }
    delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);
    // 4. producer disconnect
    producer.close();
    client.close();
    // 5. Restart broker
    setup();
}
Also used : ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

ProducerImpl (org.apache.pulsar.client.impl.ProducerImpl)7 Test (org.testng.annotations.Test)5 Field (java.lang.reflect.Field)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 PersistentReplicator (org.apache.pulsar.broker.service.persistent.PersistentReplicator)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2 PulsarClient (org.apache.pulsar.client.api.PulsarClient)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 ExecutorService (java.util.concurrent.ExecutorService)1 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)1 Entry (org.apache.bookkeeper.mledger.Entry)1 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 CursorAlreadyClosedException (org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException)1 NonPersistentTopic (org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic)1 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)1 NonPersistentPublisherStats (org.apache.pulsar.common.policies.data.NonPersistentPublisherStats)1 NonPersistentSubscriptionStats (org.apache.pulsar.common.policies.data.NonPersistentSubscriptionStats)1 NonPersistentTopicStats (org.apache.pulsar.common.policies.data.NonPersistentTopicStats)1 ZookeeperServerTest (org.apache.pulsar.zookeeper.ZookeeperServerTest)1