Search in sources :

Example 1 with BrokerStats

use of org.apache.pulsar.client.admin.BrokerStats in project incubator-pulsar by apache.

the class BrokerServiceTest method testBrokerStatsMetrics.

@Test
public void testBrokerStatsMetrics() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/newTopic";
    final String subName = "newSub";
    BrokerStats brokerStatsClient = admin.brokerStats();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    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);
    Message<byte[]> msg = null;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    consumer.close();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    JsonArray metrics = brokerStatsClient.getMetrics();
    assertEquals(metrics.size(), 5, metrics.toString());
    // these metrics seem to be arriving in different order at different times...
    // is the order really relevant here?
    boolean namespaceDimensionFound = false;
    boolean topicLoadTimesDimensionFound = false;
    for (int i = 0; i < metrics.size(); i++) {
        try {
            String data = metrics.get(i).getAsJsonObject().get("dimensions").toString();
            if (!namespaceDimensionFound && data.contains("prop/use/ns-abc")) {
                namespaceDimensionFound = true;
            }
            if (!topicLoadTimesDimensionFound && data.contains("prop/use/ns-abc")) {
                topicLoadTimesDimensionFound = true;
            }
        } catch (Exception e) {
        /* it's possible there's no dimensions */
        }
    }
    assertTrue(namespaceDimensionFound && topicLoadTimesDimensionFound);
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
}
Also used : JsonArray(com.google.gson.JsonArray) BrokerStats(org.apache.pulsar.client.admin.BrokerStats) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) IOException(java.io.IOException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 2 with BrokerStats

use of org.apache.pulsar.client.admin.BrokerStats in project incubator-pulsar by apache.

the class PulsarAdminToolTest method brokerStats.

@Test
void brokerStats() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    BrokerStats mockBrokerStats = mock(BrokerStats.class);
    doReturn(mockBrokerStats).when(admin).brokerStats();
    CmdBrokerStats brokerStats = new CmdBrokerStats(admin);
    brokerStats.run(split("topics"));
    verify(mockBrokerStats).getTopics();
    brokerStats.run(split("load-report"));
    verify(mockBrokerStats).getLoadReport();
    brokerStats.run(split("mbeans"));
    verify(mockBrokerStats).getMBeans();
    brokerStats.run(split("monitoring-metrics"));
    verify(mockBrokerStats).getMetrics();
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) BrokerStats(org.apache.pulsar.client.admin.BrokerStats) Test(org.testng.annotations.Test)

Example 3 with BrokerStats

use of org.apache.pulsar.client.admin.BrokerStats in project incubator-pulsar by apache.

the class BrokerServiceTest method testBrokerServiceNamespaceStats.

@Test
public void testBrokerServiceNamespaceStats() throws Exception {
    final int numBundles = 4;
    final String ns1 = "prop/use/stats1";
    final String ns2 = "prop/use/stats2";
    List<String> nsList = Lists.newArrayList(ns1, ns2);
    List<Producer<byte[]>> producerList = Lists.newArrayList();
    BrokerStats brokerStatsClient = admin.brokerStats();
    for (String ns : nsList) {
        admin.namespaces().createNamespace(ns, numBundles);
        String topic1 = String.format("persistent://%s/topic1", ns);
        producerList.add(pulsarClient.newProducer().topic(topic1).create());
        String topic2 = String.format("persistent://%s/topic2", ns);
        producerList.add(pulsarClient.newProducer().topic(topic2).create());
    }
    rolloverPerIntervalStats();
    JsonObject topicStats = brokerStatsClient.getTopics();
    assertEquals(topicStats.size(), 2, topicStats.toString());
    for (String ns : nsList) {
        JsonObject nsObject = topicStats.getAsJsonObject(ns);
        List<String> topicList = admin.namespaces().getTopics(ns);
        for (String topic : topicList) {
            NamespaceBundle bundle = (NamespaceBundle) pulsar.getNamespaceService().getBundle(TopicName.get(topic));
            JsonObject bundleObject = nsObject.getAsJsonObject(bundle.getBundleRange());
            JsonObject topicObject = bundleObject.getAsJsonObject("persistent");
            AtomicBoolean topicPresent = new AtomicBoolean();
            topicObject.entrySet().iterator().forEachRemaining(persistentTopic -> {
                if (persistentTopic.getKey().equals(topic)) {
                    topicPresent.set(true);
                }
            });
            assertTrue(topicPresent.get());
        }
    }
    for (Producer<?> producer : producerList) {
        producer.close();
    }
    for (String ns : nsList) {
        List<String> topics = admin.namespaces().getTopics(ns);
        for (String dest : topics) {
            admin.persistentTopics().delete(dest);
        }
        admin.namespaces().deleteNamespace(ns);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Producer(org.apache.pulsar.client.api.Producer) JsonObject(com.google.gson.JsonObject) BrokerStats(org.apache.pulsar.client.admin.BrokerStats) Test(org.testng.annotations.Test)

Aggregations

BrokerStats (org.apache.pulsar.client.admin.BrokerStats)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 PersistenceException (org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException)1 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)1 Producer (org.apache.pulsar.client.api.Producer)1 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)1