Skip to content

Commit 7002e21

Browse files
authored
HADOOP-18502. MutableStat should return 0 when there is no change (apache#5058)
1 parent 7f9ca10 commit 7002e21

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
140140
if (all || changed()) {
141141
numSamples += intervalStat.numSamples();
142142
builder.addCounter(numInfo, numSamples)
143-
.addGauge(avgInfo, lastStat().mean());
143+
.addGauge(avgInfo, intervalStat.mean());
144144
if (extended) {
145-
builder.addGauge(stdevInfo, lastStat().stddev())
146-
.addGauge(iMinInfo, lastStat().min())
147-
.addGauge(iMaxInfo, lastStat().max())
145+
builder.addGauge(stdevInfo, intervalStat.stddev())
146+
.addGauge(iMinInfo, intervalStat.min())
147+
.addGauge(iMaxInfo, intervalStat.max())
148148
.addGauge(minInfo, minMax.min())
149149
.addGauge(maxInfo, minMax.max())
150-
.addGauge(iNumInfo, lastStat().numSamples());
150+
.addGauge(iNumInfo, intervalStat.numSamples());
151151
}
152152
if (changed()) {
153153
if (numSamples > 0) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,27 @@ private static void snapshotMutableRatesWithAggregation(
290290
}
291291
}
292292

293+
/**
294+
* MutableStat should output 0 instead of the previous state when there is no change.
295+
*/
296+
@Test public void testMutableWithoutChanged() {
297+
MetricsRecordBuilder builderWithChange = mockMetricsRecordBuilder();
298+
MetricsRecordBuilder builderWithoutChange = mockMetricsRecordBuilder();
299+
MetricsRegistry registry = new MetricsRegistry("test");
300+
MutableStat stat = registry.newStat("Test", "Test", "Ops", "Val", true);
301+
stat.add(1000, 1000);
302+
stat.add(1000, 2000);
303+
registry.snapshot(builderWithChange, true);
304+
305+
assertCounter("TestNumOps", 2000L, builderWithChange);
306+
assertGauge("TestINumOps", 2000L, builderWithChange);
307+
assertGauge("TestAvgVal", 1.5, builderWithChange);
308+
309+
registry.snapshot(builderWithoutChange, true);
310+
assertGauge("TestINumOps", 0L, builderWithoutChange);
311+
assertGauge("TestAvgVal", 0.0, builderWithoutChange);
312+
}
313+
293314
@Test
294315
public void testDuplicateMetrics() {
295316
MutableRatesWithAggregation rates = new MutableRatesWithAggregation();

0 commit comments

Comments
 (0)