HADOOP-18429. fix infinite loop in MutableGaugeFloat#incr(float) (#4823)

This commit is contained in:
Hu Xinqiu 2022-11-17 17:50:39 +08:00 committed by GitHub
parent eccd2d0492
commit 7d39abd799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -69,7 +69,7 @@ public class MutableGaugeFloat extends MutableGauge {
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();

View File

@ -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 class TestMutableMetrics {
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);
}
}