Search in sources :

Example 1 with NonPersistentTopic

use of org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic in project incubator-pulsar by apache.

the class NonPersistentTopics method getStats.

@GET
@Path("{property}/{cluster}/{namespace}/{topic}/stats")
@ApiOperation(hidden = true, value = "Get the stats for the topic.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Topic does not exist") })
public NonPersistentTopicStats getStats(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateTopicName(property, cluster, namespace, encodedTopic);
    validateAdminOperationOnTopic(authoritative);
    Topic topic = getTopicReference(topicName);
    return ((NonPersistentTopic) topic).getStats();
}
Also used : Topic(org.apache.pulsar.broker.service.Topic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with NonPersistentTopic

use of org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic in project incubator-pulsar by apache.

the class PulsarStats method updateStats.

public synchronized void updateStats(ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, Topic>>> topicsMap) {
    StatsOutputStream topicStatsStream = new StatsOutputStream(tempTopicStatsBuf);
    try {
        tempMetricsCollection.clear();
        bundleStats.clear();
        brokerOperabilityMetrics.reset();
        // Json begin
        topicStatsStream.startObject();
        topicsMap.forEach((namespaceName, bundles) -> {
            if (bundles.isEmpty()) {
                return;
            }
            try {
                topicStatsStream.startObject(namespaceName);
                nsStats.reset();
                bundles.forEach((bundle, topics) -> {
                    NamespaceBundleStats currentBundleStats = bundleStats.computeIfAbsent(bundle, k -> new NamespaceBundleStats());
                    currentBundleStats.reset();
                    currentBundleStats.topics = topics.size();
                    topicStatsStream.startObject(NamespaceBundle.getBundleRange(bundle));
                    tempNonPersistentTopics.clear();
                    // start persistent topic
                    topicStatsStream.startObject("persistent");
                    topics.forEach((name, topic) -> {
                        if (topic instanceof PersistentTopic) {
                            try {
                                topic.updateRates(nsStats, currentBundleStats, topicStatsStream, clusterReplicationMetrics, namespaceName);
                            } catch (Exception e) {
                                log.error("Failed to generate topic stats for topic {}: {}", name, e.getMessage(), e);
                            }
                            // this task: helps to activate inactive-backlog-cursors which have caught up and
                            // connected, also deactivate active-backlog-cursors which has backlog
                            ((PersistentTopic) topic).getManagedLedger().checkBackloggedCursors();
                        } else if (topic instanceof NonPersistentTopic) {
                            tempNonPersistentTopics.add((NonPersistentTopic) topic);
                        } else {
                            log.warn("Unsupported type of topic {}", topic.getClass().getName());
                        }
                    });
                    // end persistent topics section
                    topicStatsStream.endObject();
                    if (!tempNonPersistentTopics.isEmpty()) {
                        // start non-persistent topic
                        topicStatsStream.startObject("non-persistent");
                        tempNonPersistentTopics.forEach(topic -> {
                            try {
                                topic.updateRates(nsStats, currentBundleStats, topicStatsStream, clusterReplicationMetrics, namespaceName);
                            } catch (Exception e) {
                                log.error("Failed to generate topic stats for topic {}: {}", topic.getName(), e.getMessage(), e);
                            }
                        });
                        // end non-persistent topics section
                        topicStatsStream.endObject();
                    }
                    // end namespace-bundle section
                    topicStatsStream.endObject();
                });
                topicStatsStream.endObject();
                // Update metricsCollection with namespace stats
                tempMetricsCollection.add(nsStats.add(namespaceName));
            } catch (Exception e) {
                log.error("Failed to generate namespace stats for namespace {}: {}", namespaceName, e.getMessage(), e);
            }
        });
        if (clusterReplicationMetrics.isMetricsEnabled()) {
            clusterReplicationMetrics.get().forEach(clusterMetric -> tempMetricsCollection.add(clusterMetric));
            clusterReplicationMetrics.reset();
        }
        brokerOperabilityMetrics.getMetrics().forEach(brokerOperabilityMetric -> tempMetricsCollection.add(brokerOperabilityMetric));
        // json end
        topicStatsStream.endObject();
    } catch (Exception e) {
        log.error("Unable to update topic stats", e);
    }
    // swap metricsCollection and tempMetricsCollection
    List<Metrics> tempRefMetrics = metricsCollection;
    metricsCollection = tempMetricsCollection;
    tempMetricsCollection = tempRefMetrics;
    bufferLock.writeLock().lock();
    try {
        ByteBuf tmp = topicStatsBuf;
        topicStatsBuf = tempTopicStatsBuf;
        tempTopicStatsBuf = tmp;
        tempTopicStatsBuf.clear();
    } finally {
        bufferLock.writeLock().unlock();
    }
}
Also used : StatsOutputStream(org.apache.pulsar.utils.StatsOutputStream) Metrics(org.apache.pulsar.common.stats.Metrics) BrokerOperabilityMetrics(org.apache.pulsar.broker.stats.BrokerOperabilityMetrics) ClusterReplicationMetrics(org.apache.pulsar.broker.stats.ClusterReplicationMetrics) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with NonPersistentTopic

use of org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic in project incubator-pulsar by apache.

the class NonPersistentTopics method getStats.

@GET
@Path("{property}/{namespace}/{topic}/stats")
@ApiOperation(value = "Get the stats for the topic.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Topic does not exist") })
public NonPersistentTopicStats getStats(@PathParam("property") String property, @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String encodedTopic, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateTopicName(property, namespace, encodedTopic);
    validateAdminOperationOnTopic(topicName, authoritative);
    Topic topic = getTopicReference(topicName);
    return ((NonPersistentTopic) topic).getStats();
}
Also used : Topic(org.apache.pulsar.broker.service.Topic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with NonPersistentTopic

use of org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic 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 NonPersistentTopic

use of org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic in project incubator-pulsar by apache.

the class NonPersistentTopicTest method testTopicStats.

/**
 * verifies that broker is capturing topic stats correctly
 */
@Test
public void testTopicStats() throws Exception {
    final String topicName = "non-persistent://my-property/use/my-ns/unacked-topic";
    final String subName = "non-persistent";
    final int timeWaitToSync = 100;
    NonPersistentTopicStats stats;
    SubscriptionStats subStats;
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionType(SubscriptionType.Shared).subscriptionName(subName).subscribe();
    Thread.sleep(timeWaitToSync);
    NonPersistentTopic topicRef = (NonPersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    rolloverPerIntervalStats(pulsar);
    stats = topicRef.getStats();
    subStats = stats.getSubscriptions().values().iterator().next();
    // subscription stats
    assertEquals(stats.getSubscriptions().keySet().size(), 1);
    assertEquals(subStats.consumers.size(), 1);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    Thread.sleep(timeWaitToSync);
    int totalProducedMessages = 100;
    for (int i = 0; i < totalProducedMessages; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Thread.sleep(timeWaitToSync);
    rolloverPerIntervalStats(pulsar);
    stats = topicRef.getStats();
    subStats = stats.getSubscriptions().values().iterator().next();
    assertTrue(subStats.msgRateOut > 0);
    assertEquals(subStats.consumers.size(), 1);
    assertTrue(subStats.msgThroughputOut > 0);
    // consumer stats
    assertTrue(subStats.consumers.get(0).msgRateOut > 0.0);
    assertTrue(subStats.consumers.get(0).msgThroughputOut > 0.0);
    assertEquals(subStats.msgRateRedeliver, 0.0);
    producer.close();
    consumer.close();
}
Also used : SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) NonPersistentSubscriptionStats(org.apache.pulsar.common.policies.data.NonPersistentSubscriptionStats) NonPersistentTopicStats(org.apache.pulsar.common.policies.data.NonPersistentTopicStats) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Aggregations

NonPersistentTopic (org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic)10 ZookeeperServerTest (org.apache.pulsar.zookeeper.ZookeeperServerTest)6 Test (org.testng.annotations.Test)6 NonPersistentSubscriptionStats (org.apache.pulsar.common.policies.data.NonPersistentSubscriptionStats)3 NonPersistentTopicStats (org.apache.pulsar.common.policies.data.NonPersistentTopicStats)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Field (java.lang.reflect.Field)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 LoadManager (org.apache.pulsar.broker.loadbalance.LoadManager)2 ResourceUnit (org.apache.pulsar.broker.loadbalance.ResourceUnit)2 Topic (org.apache.pulsar.broker.service.Topic)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)2 SubscriptionStats (org.apache.pulsar.common.policies.data.SubscriptionStats)2 ByteBuf (io.netty.buffer.ByteBuf)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1