Search in sources :

Example 11 with PersistentTopicStats

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

the class BacklogQuotaManagerTest method testConsumerBacklogEviction.

@Test
public void testConsumerBacklogEviction() 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));
    PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/usc/ns-quota/topic1";
    final String subName1 = "c1";
    final String subName2 = "c2";
    final int numMsgs = 20;
    Consumer<byte[]> consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    Consumer<byte[]> consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
    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);
        consumer1.receive();
        consumer2.receive();
    }
    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();
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Example 12 with PersistentTopicStats

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

the class BrokerServiceTest method testBrokerServicePersistentTopicStats.

@Test
public void testBrokerServicePersistentTopicStats() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/successTopic";
    final String subName = "successSub";
    PersistentTopicStats stats;
    SubscriptionStats subStats;
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    rolloverPerIntervalStats();
    stats = topicRef.getStats();
    subStats = stats.subscriptions.values().iterator().next();
    // subscription stats
    assertEquals(stats.subscriptions.keySet().size(), 1);
    assertEquals(subStats.msgBacklog, 0);
    assertEquals(subStats.consumers.size(), 1);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    rolloverPerIntervalStats();
    stats = topicRef.getStats();
    subStats = stats.subscriptions.values().iterator().next();
    // publisher stats
    assertEquals(subStats.msgBacklog, 10);
    assertEquals(stats.publishers.size(), 1);
    assertTrue(stats.publishers.get(0).msgRateIn > 0.0);
    assertTrue(stats.publishers.get(0).msgThroughputIn > 0.0);
    assertTrue(stats.publishers.get(0).averageMsgSize > 0.0);
    assertNotNull(stats.publishers.get(0).clientVersion);
    // aggregated publish stats
    assertEquals(stats.msgRateIn, stats.publishers.get(0).msgRateIn);
    assertEquals(stats.msgThroughputIn, stats.publishers.get(0).msgThroughputIn);
    double diff = stats.averageMsgSize - stats.publishers.get(0).averageMsgSize;
    assertTrue(Math.abs(diff) < 0.000001);
    // consumer stats
    assertTrue(subStats.consumers.get(0).msgRateOut > 0.0);
    assertTrue(subStats.consumers.get(0).msgThroughputOut > 0.0);
    // aggregated consumer stats
    assertEquals(subStats.msgRateOut, subStats.consumers.get(0).msgRateOut);
    assertEquals(subStats.msgThroughputOut, subStats.consumers.get(0).msgThroughputOut);
    assertEquals(stats.msgRateOut, subStats.consumers.get(0).msgRateOut);
    assertEquals(stats.msgThroughputOut, subStats.consumers.get(0).msgThroughputOut);
    assertNotNull(subStats.consumers.get(0).clientVersion);
    Message<byte[]> msg;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    consumer.close();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    rolloverPerIntervalStats();
    stats = topicRef.getStats();
    subStats = stats.subscriptions.values().iterator().next();
    assertEquals(subStats.msgBacklog, 0);
}
Also used : SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Example 13 with PersistentTopicStats

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

the class AdminApiTest method persistentTopics.

@Test(dataProvider = "topicName")
public void persistentTopics(String topicName) throws Exception {
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
    final String persistentTopicName = "persistent://prop-xyz/use/ns1/" + topicName;
    // Force to create a topic
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 0);
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/" + topicName));
    // create consumer and subscription
    URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
    PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    Consumer<byte[]> consumer = client.newConsumer().topic(persistentTopicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
    assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList("my-sub"));
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 10);
    PersistentTopicStats topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
    assertEquals(topicStats.subscriptions.get("my-sub").consumers.size(), 1);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10);
    assertEquals(topicStats.publishers.size(), 0);
    PersistentTopicInternalStats internalStats = admin.persistentTopics().getInternalStats(persistentTopicName);
    assertEquals(internalStats.cursors.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
    List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 3);
    assertEquals(messages.size(), 3);
    for (int i = 0; i < 3; i++) {
        String expectedMessage = "message-" + i;
        assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
    }
    messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 15);
    assertEquals(messages.size(), 10);
    for (int i = 0; i < 10; i++) {
        String expectedMessage = "message-" + i;
        assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
    }
    admin.persistentTopics().skipMessages(persistentTopicName, "my-sub", 5);
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 5);
    admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 0);
    consumer.close();
    client.close();
    admin.persistentTopics().deleteSubscription(persistentTopicName, "my-sub");
    assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList());
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet());
    assertEquals(topicStats.publishers.size(), 0);
    try {
        admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
    } catch (NotFoundException e) {
    }
    admin.persistentTopics().delete(persistentTopicName);
    try {
        admin.persistentTopics().delete(persistentTopicName);
        fail("Should have received 404");
    } catch (NotFoundException e) {
    }
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
}
Also used : Message(org.apache.pulsar.client.api.Message) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 14 with PersistentTopicStats

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

the class PersistentTopicStatsTest method testPersistentTopicStats.

@Test
public void testPersistentTopicStats() {
    PersistentTopicStats persistentTopicStats = new PersistentTopicStats();
    persistentTopicStats.msgRateIn = 1;
    persistentTopicStats.msgThroughputIn = 1;
    persistentTopicStats.msgRateOut = 1;
    persistentTopicStats.msgThroughputOut = 1;
    persistentTopicStats.averageMsgSize = 1;
    persistentTopicStats.storageSize = 1;
    persistentTopicStats.publishers.add(new PublisherStats());
    persistentTopicStats.subscriptions.put("test_ns", new SubscriptionStats());
    persistentTopicStats.replication.put("test_ns", new ReplicatorStats());
    PersistentTopicStats target = new PersistentTopicStats();
    target.add(persistentTopicStats);
    assertEquals(persistentTopicStats.msgRateIn, 1.0);
    assertEquals(persistentTopicStats.msgThroughputIn, 1.0);
    assertEquals(persistentTopicStats.msgRateOut, 1.0);
    assertEquals(persistentTopicStats.msgThroughputOut, 1.0);
    assertEquals(persistentTopicStats.averageMsgSize, 1.0);
    assertEquals(persistentTopicStats.storageSize, 1);
    assertEquals(persistentTopicStats.publishers.size(), 1);
    assertEquals(persistentTopicStats.subscriptions.size(), 1);
    assertEquals(persistentTopicStats.replication.size(), 1);
    persistentTopicStats.reset();
    assertEquals(persistentTopicStats.msgRateIn, 0.0);
    assertEquals(persistentTopicStats.msgThroughputIn, 0.0);
    assertEquals(persistentTopicStats.msgRateOut, 0.0);
    assertEquals(persistentTopicStats.msgThroughputOut, 0.0);
    assertEquals(persistentTopicStats.averageMsgSize, 0.0);
    assertEquals(persistentTopicStats.storageSize, 0);
    assertEquals(persistentTopicStats.publishers.size(), 0);
    assertEquals(persistentTopicStats.subscriptions.size(), 0);
    assertEquals(persistentTopicStats.replication.size(), 0);
}
Also used : SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) PublisherStats(org.apache.pulsar.common.policies.data.PublisherStats) ReplicatorStats(org.apache.pulsar.common.policies.data.ReplicatorStats) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Example 15 with PersistentTopicStats

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

the class PersistentTopicsBase method internalGetPartitionedStats.

protected PartitionedTopicStats internalGetPartitionedStats(boolean authoritative) {
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions == 0) {
        throw new RestException(Status.NOT_FOUND, "Partitioned Topic not found");
    }
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicStats stats = new PartitionedTopicStats(partitionMetadata);
    try {
        for (int i = 0; i < partitionMetadata.partitions; i++) {
            PersistentTopicStats partitionStats = pulsar().getAdminClient().persistentTopics().getStats(topicName.getPartition(i).toString());
            stats.add(partitionStats);
            stats.partitions.put(topicName.getPartition(i).toString(), partitionStats);
        }
    } catch (Exception e) {
        throw new RestException(e);
    }
    return stats;
}
Also used : PartitionedTopicStats(org.apache.pulsar.common.policies.data.PartitionedTopicStats) RestException(org.apache.pulsar.broker.web.RestException) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

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