Skip to content

Commit a6e991b

Browse files
simbadzinaGitHub AE
authored andcommitted
Merge pull request apache#27 from grid/sdzinama/backport/HADOOP-18502
HADOOP-18502. MutableStat should return 0 when there is no change (apache#5058)
2 parents b5aad20 + 4b7ef1a commit a6e991b

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
@@ -287,6 +287,27 @@ private static void snapshotMutableRatesWithAggregation(
287287
}
288288
}
289289

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

0 commit comments

Comments
 (0)