Search in sources :

Example 1 with BrokerStats

use of com.yahoo.pulsar.client.admin.BrokerStats in project pulsar by yahoo.

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();
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    Producer producer = pulsarClient.createProducer(topicName);
    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 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(), 4, 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) Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) BrokerStats(com.yahoo.pulsar.client.admin.BrokerStats) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 2 with BrokerStats

use of com.yahoo.pulsar.client.admin.BrokerStats in project pulsar by yahoo.

the class BrokerServiceTest method testBrokerServiceNamespaceStats.

@SuppressWarnings("unchecked")
@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> 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.createProducer(topic1));
        String topic2 = String.format("persistent://%s/topic2", ns);
        producerList.add(pulsarClient.createProducer(topic2));
    }
    rolloverPerIntervalStats();
    JsonObject destinationStats = brokerStatsClient.getDestinations();
    assertEquals(destinationStats.size(), 2, destinationStats.toString());
    for (String ns : nsList) {
        JsonObject nsObject = destinationStats.getAsJsonObject(ns);
        List<String> topicList = admin.namespaces().getDestinations(ns);
        for (String topic : topicList) {
            NamespaceBundle bundle = (NamespaceBundle) pulsar.getNamespaceService().getBundle(DestinationName.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> destinations = admin.namespaces().getDestinations(ns);
        for (String dest : destinations) {
            admin.persistentTopics().delete(dest);
        }
        admin.namespaces().deleteNamespace(ns);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Producer(com.yahoo.pulsar.client.api.Producer) JsonObject(com.google.gson.JsonObject) BrokerStats(com.yahoo.pulsar.client.admin.BrokerStats) Test(org.testng.annotations.Test)

Aggregations

BrokerStats (com.yahoo.pulsar.client.admin.BrokerStats)2 Producer (com.yahoo.pulsar.client.api.Producer)2 Test (org.testng.annotations.Test)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Consumer (com.yahoo.pulsar.client.api.Consumer)1 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)1 Message (com.yahoo.pulsar.client.api.Message)1 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1