HADOOP-18502. MutableStat should return 0 when there is no change (#5058)

This commit is contained in:
ted12138 2022-11-09 10:21:43 +08:00 committed by GitHub
parent 7f9ca101e2
commit 7002e214b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -140,14 +140,14 @@ public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
if (all || changed()) { if (all || changed()) {
numSamples += intervalStat.numSamples(); numSamples += intervalStat.numSamples();
builder.addCounter(numInfo, numSamples) builder.addCounter(numInfo, numSamples)
.addGauge(avgInfo, lastStat().mean()); .addGauge(avgInfo, intervalStat.mean());
if (extended) { if (extended) {
builder.addGauge(stdevInfo, lastStat().stddev()) builder.addGauge(stdevInfo, intervalStat.stddev())
.addGauge(iMinInfo, lastStat().min()) .addGauge(iMinInfo, intervalStat.min())
.addGauge(iMaxInfo, lastStat().max()) .addGauge(iMaxInfo, intervalStat.max())
.addGauge(minInfo, minMax.min()) .addGauge(minInfo, minMax.min())
.addGauge(maxInfo, minMax.max()) .addGauge(maxInfo, minMax.max())
.addGauge(iNumInfo, lastStat().numSamples()); .addGauge(iNumInfo, intervalStat.numSamples());
} }
if (changed()) { if (changed()) {
if (numSamples > 0) { if (numSamples > 0) {

View File

@ -290,6 +290,27 @@ private static void snapshotMutableRatesWithAggregation(
} }
} }
/**
* 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 @Test
public void testDuplicateMetrics() { public void testDuplicateMetrics() {
MutableRatesWithAggregation rates = new MutableRatesWithAggregation(); MutableRatesWithAggregation rates = new MutableRatesWithAggregation();