diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/metrics/RpcDetailedMetrics.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/metrics/RpcDetailedMetrics.java index 67ae4cc4f9..98b9f262b8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/metrics/RpcDetailedMetrics.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/metrics/RpcDetailedMetrics.java @@ -61,7 +61,7 @@ public static RpcDetailedMetrics create(int port) { */ public void init(Class protocol) { rates.init(protocol); - deferredRpcRates.init(protocol); + deferredRpcRates.init(protocol, "Deferred"); } /** diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableRatesWithAggregation.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableRatesWithAggregation.java index 26a15063bb..aa7b759617 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableRatesWithAggregation.java +++ b/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> threadLocalMetricsMap = new ThreadLocal<>(); + // prefix for metric name + private String typePrefix = ""; /** * Initialize the registry with all the methods in a protocol @@ -148,7 +150,7 @@ Map getGlobalMetrics() { 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 @@ synchronized void snapshotInto(MutableRate metric) { } } + public void init(Class protocol, String prefix) { + this.typePrefix = prefix; + init(protocol); + } + } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java index 29d47cfab0..b5f62b1890 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java @@ -274,6 +274,23 @@ private static void snapshotMutableRatesWithAggregation( } } + @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.