Skip to content

Commit e356e4f

Browse files
committed
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.
1 parent 9a1d8cf commit e356e4f

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static RpcDetailedMetrics create(int port) {
6161
*/
6262
public void init(Class<?> protocol) {
6363
rates.init(protocol);
64-
deferredRpcRates.init(protocol);
64+
deferredRpcRates.init(protocol, "Deferred");
6565
}
6666

6767
/**

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class MutableRatesWithAggregation extends MutableMetric {
5858
weakReferenceQueue = new ConcurrentLinkedDeque<>();
5959
private final ThreadLocal<ConcurrentMap<String, ThreadSafeSampleStat>>
6060
threadLocalMetricsMap = new ThreadLocal<>();
61+
// prefix for metric name
62+
private String typePrefix = "";
6163

6264
/**
6365
* Initialize the registry with all the methods in a protocol
@@ -148,7 +150,7 @@ Map<String, MutableRate> getGlobalMetrics() {
148150
private synchronized MutableRate addMetricIfNotExists(String name) {
149151
MutableRate metric = globalMetrics.get(name);
150152
if (metric == null) {
151-
metric = new MutableRate(name, name, false);
153+
metric = new MutableRate(name + typePrefix, name + typePrefix, false);
152154
globalMetrics.put(name, metric);
153155
}
154156
return metric;
@@ -170,4 +172,9 @@ synchronized void snapshotInto(MutableRate metric) {
170172
}
171173
}
172174

175+
public void init(Class<?> protocol, String prefix) {
176+
this.typePrefix = prefix;
177+
init(protocol);
178+
}
179+
173180
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ private static void snapshotMutableRatesWithAggregation(
274274
}
275275
}
276276

277+
@Test
278+
public void testDuplicateMetrics() {
279+
MutableRatesWithAggregation rates = new MutableRatesWithAggregation();
280+
MutableRatesWithAggregation deferredRpcRates =
281+
new MutableRatesWithAggregation();
282+
Class<?> protocol = Long.class;
283+
rates.init(protocol);
284+
deferredRpcRates.init(protocol, "Deferred");
285+
MetricsRecordBuilder rb = mockMetricsRecordBuilder();
286+
rates.snapshot(rb, true);
287+
deferredRpcRates.snapshot(rb, true);
288+
verify(rb, times(1))
289+
.addCounter(info("GetLongNumOps", "Number of ops for getLong"), 0L);
290+
verify(rb, times(1)).addCounter(
291+
info("GetLongDeferredNumOps", "Number of ops for getLongDeferred"), 0L);
292+
}
293+
277294
/**
278295
* Tests that when using {@link MutableStat#add(long, long)}, even with a high
279296
* sample count, the mean does not lose accuracy.

0 commit comments

Comments
 (0)