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.

This commit is contained in:
Erik Krogen 2019-08-16 09:01:44 -07:00
parent 9a1d8cfaf5
commit e356e4f4b7
3 changed files with 26 additions and 2 deletions

View File

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

View File

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

View File

@ -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 * Tests that when using {@link MutableStat#add(long, long)}, even with a high
* sample count, the mean does not lose accuracy. * sample count, the mean does not lose accuracy.