Browse Source

HADOOP-16391 Add a prefix to the metric names for MutableRatesWithAggregation used for deferred RPC metrics to avoid collision with non-deferred metrics. Contributed by Bilwa S T.

Erik Krogen 6 years ago
parent
commit
e356e4f4b7

+ 1 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/metrics/RpcDetailedMetrics.java

@@ -61,7 +61,7 @@ public class RpcDetailedMetrics {
    */
   public void init(Class<?> protocol) {
     rates.init(protocol);
-    deferredRpcRates.init(protocol);
+    deferredRpcRates.init(protocol, "Deferred");
   }
 
   /**

+ 8 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableRatesWithAggregation.java

@@ -58,6 +58,8 @@ public class MutableRatesWithAggregation extends MutableMetric {
       weakReferenceQueue = new ConcurrentLinkedDeque<>();
   private final ThreadLocal<ConcurrentMap<String, ThreadSafeSampleStat>>
       threadLocalMetricsMap = new ThreadLocal<>();
+  // prefix for metric name
+  private String typePrefix = "";
 
   /**
    * Initialize the registry with all the methods in a protocol
@@ -148,7 +150,7 @@ public class MutableRatesWithAggregation extends MutableMetric {
   private synchronized MutableRate addMetricIfNotExists(String name) {
     MutableRate metric = globalMetrics.get(name);
     if (metric == null) {
-      metric = new MutableRate(name, name, false);
+      metric = new MutableRate(name + typePrefix, name + typePrefix, false);
       globalMetrics.put(name, metric);
     }
     return metric;
@@ -170,4 +172,9 @@ public class MutableRatesWithAggregation extends MutableMetric {
     }
   }
 
+  public void init(Class<?> protocol, String prefix) {
+    this.typePrefix = prefix;
+    init(protocol);
+  }
+
 }

+ 17 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java

@@ -274,6 +274,23 @@ public class TestMutableMetrics {
     }
   }
 
+  @Test
+  public void testDuplicateMetrics() {
+    MutableRatesWithAggregation rates = new MutableRatesWithAggregation();
+    MutableRatesWithAggregation deferredRpcRates =
+        new MutableRatesWithAggregation();
+    Class<?> protocol = Long.class;
+    rates.init(protocol);
+    deferredRpcRates.init(protocol, "Deferred");
+    MetricsRecordBuilder rb = mockMetricsRecordBuilder();
+    rates.snapshot(rb, true);
+    deferredRpcRates.snapshot(rb, true);
+    verify(rb, times(1))
+        .addCounter(info("GetLongNumOps", "Number of ops for getLong"), 0L);
+    verify(rb, times(1)).addCounter(
+        info("GetLongDeferredNumOps", "Number of ops for getLongDeferred"), 0L);
+  }
+
   /**
    * Tests that when using {@link MutableStat#add(long, long)}, even with a high
    * sample count, the mean does not lose accuracy.