From 55816db46966c1adc4fddd5d0cb441bf0fd0d18d Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Fri, 4 Jan 2019 21:08:11 +0800 Subject: [PATCH] fix bug: the metrics of caches is lost Change-Id: If99b20ea1d37cbaca8f844d7796b11bb9cfbd601 --- .../baidu/hugegraph/core/GraphManager.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 0bc3f8a33c..90ef5426d4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -54,7 +54,6 @@ import com.baidu.hugegraph.task.TaskManager; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; -import com.codahale.metrics.MetricRegistry; public final class GraphManager { @@ -226,15 +225,16 @@ private void addMetrics(HugeConfig config) { return maxWriteThreads; }); - // Add metrics for cache + // Add metrics for caches Map caches = CacheManager.instance().caches(); - final AtomicInteger lastCaches = new AtomicInteger(caches.size()); + registerCacheMetrics(caches); + final AtomicInteger lastCachesSize = new AtomicInteger(caches.size()); MetricsUtil.registerGauge(Cache.class, "instances", () -> { int count = caches.size(); - if (count != lastCaches.get()) { - registerCacheMetrics(); - } else { - lastCaches.set(count); + if (count != lastCachesSize.get()) { + // Update if caches changed (effect in the next report period) + registerCacheMetrics(caches); + lastCachesSize.set(count); } return count; }); @@ -248,10 +248,8 @@ private void addMetrics(HugeConfig config) { }); } - private void registerCacheMetrics() { - Map caches = CacheManager.instance().caches(); - final MetricRegistry registry = MetricManager.INSTANCE.getRegistry(); - Set names = registry.getNames(); + private static void registerCacheMetrics(Map caches) { + Set names = MetricManager.INSTANCE.getRegistry().getNames(); for (Map.Entry entry : caches.entrySet()) { String key = entry.getKey(); Cache cache = entry.getValue();