This is an automated email from the ASF dual-hosted git repository. zanderxu pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/hadoop.git
The following commit(s) were added to refs/heads/trunk by this push: new 7002e214b88 HADOOP-18502. MutableStat should return 0 when there is no change (#5058) 7002e214b88 is described below commit 7002e214b8861e007a0279a8fb5dfedad3ffe3c4 Author: ted12138 <67770300+ted12...@users.noreply.github.com> AuthorDate: Wed Nov 9 10:21:43 2022 +0800 HADOOP-18502. MutableStat should return 0 when there is no change (#5058) --- .../org/apache/hadoop/metrics2/lib/MutableStat.java | 10 +++++----- .../hadoop/metrics2/lib/TestMutableMetrics.java | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableStat.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableStat.java index f2e072545ad..b130aa6ada3 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableStat.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableStat.java @@ -140,14 +140,14 @@ public class MutableStat extends MutableMetric { if (all || changed()) { numSamples += intervalStat.numSamples(); builder.addCounter(numInfo, numSamples) - .addGauge(avgInfo, lastStat().mean()); + .addGauge(avgInfo, intervalStat.mean()); if (extended) { - builder.addGauge(stdevInfo, lastStat().stddev()) - .addGauge(iMinInfo, lastStat().min()) - .addGauge(iMaxInfo, lastStat().max()) + builder.addGauge(stdevInfo, intervalStat.stddev()) + .addGauge(iMinInfo, intervalStat.min()) + .addGauge(iMaxInfo, intervalStat.max()) .addGauge(minInfo, minMax.min()) .addGauge(maxInfo, minMax.max()) - .addGauge(iNumInfo, lastStat().numSamples()); + .addGauge(iNumInfo, intervalStat.numSamples()); } if (changed()) { if (numSamples > 0) { 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 10c8057c69e..0938aa92a90 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 @@ -290,6 +290,27 @@ public class TestMutableMetrics { } } + /** + * MutableStat should output 0 instead of the previous state when there is no change. + */ + @Test public void testMutableWithoutChanged() { + MetricsRecordBuilder builderWithChange = mockMetricsRecordBuilder(); + MetricsRecordBuilder builderWithoutChange = mockMetricsRecordBuilder(); + MetricsRegistry registry = new MetricsRegistry("test"); + MutableStat stat = registry.newStat("Test", "Test", "Ops", "Val", true); + stat.add(1000, 1000); + stat.add(1000, 2000); + registry.snapshot(builderWithChange, true); + + assertCounter("TestNumOps", 2000L, builderWithChange); + assertGauge("TestINumOps", 2000L, builderWithChange); + assertGauge("TestAvgVal", 1.5, builderWithChange); + + registry.snapshot(builderWithoutChange, true); + assertGauge("TestINumOps", 0L, builderWithoutChange); + assertGauge("TestAvgVal", 0.0, builderWithoutChange); + } + @Test public void testDuplicateMetrics() { MutableRatesWithAggregation rates = new MutableRatesWithAggregation(); --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-commits-h...@hadoop.apache.org