Search in sources :

Example 6 with PersistentTopicStats

use of org.apache.pulsar.common.policies.data.PersistentTopicStats in project incubator-pulsar by apache.

the class PulsarBoltTest method testSharedProducer.

@Test
public void testSharedProducer() throws Exception {
    PersistentTopicStats topicStats = admin.persistentTopics().getStats(topic);
    Assert.assertEquals(topicStats.publishers.size(), 1);
    PulsarBolt otherBolt = new PulsarBolt(pulsarBoltConf, new ClientConfiguration());
    MockOutputCollector otherMockCollector = new MockOutputCollector();
    OutputCollector collector = new OutputCollector(otherMockCollector);
    TopologyContext context = mock(TopologyContext.class);
    when(context.getThisComponentId()).thenReturn("test-bolt-" + methodName);
    when(context.getThisTaskId()).thenReturn(1);
    otherBolt.prepare(Maps.newHashMap(), context, collector);
    topicStats = admin.persistentTopics().getStats(topic);
    Assert.assertEquals(topicStats.publishers.size(), 1);
    otherBolt.close();
    topicStats = admin.persistentTopics().getStats(topic);
    Assert.assertEquals(topicStats.publishers.size(), 1);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) TopologyContext(org.apache.storm.task.TopologyContext) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 7 with PersistentTopicStats

use of org.apache.pulsar.common.policies.data.PersistentTopicStats in project incubator-pulsar by apache.

the class PersistentQueueE2ETest method testUnackedCountWithRedeliveries.

@Test
public void testUnackedCountWithRedeliveries() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/testUnackedCountWithRedeliveries";
    final String subName = "sub3";
    final int numMsgs = 10;
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).receiverQueueSize(10).subscriptionType(SubscriptionType.Shared);
    ConsumerImpl<byte[]> consumer1 = (ConsumerImpl<byte[]>) consumerBuilder.subscribe();
    for (int i = 0; i < numMsgs; i++) {
        producer.send(("hello-" + i).getBytes());
    }
    Set<MessageId> c1_receivedMessages = new HashSet<>();
    // C-1 gets all messages but doesn't ack
    for (int i = 0; i < numMsgs; i++) {
        c1_receivedMessages.add(consumer1.receive().getMessageId());
    }
    // C-2 will not get any message initially, since everything went to C-1 already
    Consumer<byte[]> consumer2 = consumerBuilder.subscribe();
    // Trigger C-1 to redeliver everything, half will go C-1 again and the other half to C-2
    consumer1.redeliverUnacknowledgedMessages(c1_receivedMessages);
    // Consumer 2 will also receive all message but not ack
    for (int i = 0; i < numMsgs; i++) {
        consumer2.receive();
    }
    for (MessageId msgId : c1_receivedMessages) {
        consumer1.acknowledge(msgId);
    }
    PersistentTopicStats stats = admin.persistentTopics().getStats(topicName);
    // Unacked messages count should be 0 for both consumers at this point
    SubscriptionStats subStats = stats.subscriptions.get(subName);
    assertEquals(subStats.msgBacklog, 0);
    for (ConsumerStats cs : subStats.consumers) {
        assertEquals(cs.unackedMessages, 0);
    }
    producer.close();
    consumer1.close();
    consumer2.close();
    admin.persistentTopics().delete(topicName);
}
Also used : SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) ConsumerStats(org.apache.pulsar.common.policies.data.ConsumerStats) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) ConsumerImpl(org.apache.pulsar.client.impl.ConsumerImpl) MessageId(org.apache.pulsar.client.api.MessageId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with PersistentTopicStats

use of org.apache.pulsar.common.policies.data.PersistentTopicStats in project incubator-pulsar by apache.

the class TopicReaderTest method testTopicStats.

@Test
public void testTopicStats() throws Exception {
    String topicName = "persistent://my-property/use/my-ns/my-topic1";
    Reader<byte[]> reader1 = pulsarClient.newReader().topic(topicName).startMessageId(MessageId.earliest).create();
    Reader<byte[]> reader2 = pulsarClient.newReader().topic(topicName).startMessageId(MessageId.earliest).create();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topicName);
    assertEquals(stats.subscriptions.size(), 2);
    reader1.close();
    stats = admin.persistentTopics().getStats(topicName);
    assertEquals(stats.subscriptions.size(), 1);
    reader2.close();
    stats = admin.persistentTopics().getStats(topicName);
    assertEquals(stats.subscriptions.size(), 0);
}
Also used : PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Example 9 with PersistentTopicStats

use of org.apache.pulsar.common.policies.data.PersistentTopicStats in project incubator-pulsar by apache.

the class BacklogQuotaManagerTest method testConcurrentAckAndEviction.

@Test
public void testConcurrentAckAndEviction() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/ns-quota"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction));
    final String topic1 = "persistent://prop/usc/ns-quota/topic12";
    final String subName1 = "c12";
    final String subName2 = "c22";
    final int numMsgs = 20;
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    Consumer<byte[]> consumer1 = client2.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    Consumer<byte[]> consumer2 = client2.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
    Thread producerThread = new Thread() {

        public void run() {
            try {
                barrier.await();
                org.apache.pulsar.client.api.Producer<byte[]> producer = client.newProducer().topic(topic1).create();
                byte[] content = new byte[1024];
                for (int i = 0; i < numMsgs; i++) {
                    producer.send(content);
                }
                producer.close();
            } catch (Exception e) {
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread ConsumerThread = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < numMsgs; i++) {
                    // only one consumer acknowledges the message
                    consumer1.acknowledge(consumer1.receive());
                    consumer2.receive();
                }
            } catch (Exception e) {
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    producerThread.start();
    ConsumerThread.start();
    // test hangs without timeout since there is nothing to consume due to eviction
    counter.await(20, TimeUnit.SECONDS);
    assertTrue(!gotException.get());
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    Assert.assertTrue(stats.storageSize <= 10 * 1024, "Storage size is [" + stats.storageSize + "]");
    client.close();
    client2.close();
}
Also used : BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 10 with PersistentTopicStats

use of org.apache.pulsar.common.policies.data.PersistentTopicStats in project incubator-pulsar by apache.

the class BacklogQuotaManagerTest method testProducerExceptionAndThenUnblock.

@Test
public void testProducerExceptionAndThenUnblock() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/usc/quotahold/exceptandunblock";
    final String subName1 = "c1except";
    boolean gotException = false;
    Consumer<byte[]> consumer = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException ce) {
        Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
        gotException = true;
    }
    Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
    // now remove backlog and ensure that producer is unblockedrolloverStats();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    int backlog = (int) stats.subscriptions.get(subName1).msgBacklog;
    for (int i = 0; i < backlog; i++) {
        Message<?> msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    // publish should work now
    Exception sendException = null;
    gotException = false;
    try {
        for (int i = 0; i < 5; i++) {
            producer.send(content);
        }
    } catch (Exception e) {
        gotException = true;
        sendException = e;
    }
    Assert.assertFalse(gotException, "unable to publish due to " + sendException);
    client.close();
}
Also used : BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

PersistentTopicStats (org.apache.pulsar.common.policies.data.PersistentTopicStats)27 Test (org.testng.annotations.Test)22 PulsarClient (org.apache.pulsar.client.api.PulsarClient)12 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)6 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)6 SubscriptionStats (org.apache.pulsar.common.policies.data.SubscriptionStats)6 URL (java.net.URL)5 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)5 PartitionedTopicStats (org.apache.pulsar.common.policies.data.PartitionedTopicStats)4 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)3 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)3 ClientConfiguration (org.apache.pulsar.client.api.ClientConfiguration)3 TopologyContext (org.apache.storm.task.TopologyContext)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 WebTarget (javax.ws.rs.client.WebTarget)2 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)2