diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableGaugeFloat.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableGaugeFloat.java index 6a52bf382d..126601fcbb 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableGaugeFloat.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableGaugeFloat.java @@ -69,7 +69,7 @@ private final boolean compareAndSet(float expect, float update) { private void incr(float delta) { while (true) { - float current = value.get(); + float current = Float.intBitsToFloat(value.get()); float next = current + delta; if (compareAndSet(current, next)) { setChanged(); 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 0938aa92a9..9984c9b95f 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 @@ -18,6 +18,7 @@ package org.apache.hadoop.metrics2.lib; +import static org.apache.hadoop.metrics2.impl.MsInfo.Context; import static org.apache.hadoop.metrics2.lib.Interns.info; import static org.apache.hadoop.test.MetricsAsserts.*; import static org.mockito.AdditionalMatchers.eq; @@ -500,4 +501,15 @@ public void testMutableQuantilesEmptyRollover() throws Exception { verify(mb, times(2)).addGauge( info("FooNumOps", "Number of ops for stat with 5s interval"), (long) 0); } + + /** + * Test {@link MutableGaugeFloat#incr()}. + */ + @Test(timeout = 30000) + public void testMutableGaugeFloat() { + MutableGaugeFloat mgf = new MutableGaugeFloat(Context, 3.2f); + assertEquals(3.2f, mgf.value(), 0.0); + mgf.incr(); + assertEquals(4.2f, mgf.value(), 0.0); + } }