Search in sources :

Example 1 with StatsOutputStream

use of com.yahoo.pulsar.utils.StatsOutputStream in project pulsar by yahoo.

the class PulsarStats method updateStats.

public synchronized void updateStats(ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, PersistentTopic>>> 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));
                    topicStatsStream.startObject("persistent");
                    topics.forEach((name, topic) -> {
                        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
                        topic.getManagedLedger().checkBackloggedCursors();
                    });
                    topicStatsStream.endObject();
                    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 destination 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(com.yahoo.pulsar.utils.StatsOutputStream) ClusterReplicationMetrics(com.yahoo.pulsar.broker.stats.ClusterReplicationMetrics) Metrics(com.yahoo.pulsar.broker.stats.Metrics) BrokerOperabilityMetrics(com.yahoo.pulsar.broker.stats.BrokerOperabilityMetrics) NamespaceBundleStats(com.yahoo.pulsar.common.policies.data.loadbalancer.NamespaceBundleStats) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

BrokerOperabilityMetrics (com.yahoo.pulsar.broker.stats.BrokerOperabilityMetrics)1 ClusterReplicationMetrics (com.yahoo.pulsar.broker.stats.ClusterReplicationMetrics)1 Metrics (com.yahoo.pulsar.broker.stats.Metrics)1 NamespaceBundleStats (com.yahoo.pulsar.common.policies.data.loadbalancer.NamespaceBundleStats)1 StatsOutputStream (com.yahoo.pulsar.utils.StatsOutputStream)1 ByteBuf (io.netty.buffer.ByteBuf)1