Commit 49858a0f authored by Phillip Webb's avatar Phillip Webb

Fix concurrent gaugeLocks map access

Use putIfAbsent to ensure atomic creation of lock objects.

Fixes gh-1995
parent ffe53480
...@@ -90,25 +90,25 @@ public class CodahaleMetricWriter implements MetricWriter { ...@@ -90,25 +90,25 @@ public class CodahaleMetricWriter implements MetricWriter {
} }
else { else {
final double gauge = value.getValue().doubleValue(); final double gauge = value.getValue().doubleValue();
Object lock = null;
if (this.gaugeLocks.containsKey(name)) {
lock = this.gaugeLocks.get(name);
}
else {
this.gaugeLocks.putIfAbsent(name, new Object());
lock = this.gaugeLocks.get(name);
}
// Ensure we synchronize to avoid another thread pre-empting this thread after // Ensure we synchronize to avoid another thread pre-empting this thread after
// remove causing an error in CodaHale metrics // remove causing an error in CodaHale metrics
// NOTE: CodaHale provides no way to do this atomically // NOTE: CodaHale provides no way to do this atomically
synchronized (lock) { synchronized (getGuageLock(name)) {
this.registry.remove(name); this.registry.remove(name);
this.registry.register(name, new SimpleGauge(gauge)); this.registry.register(name, new SimpleGauge(gauge));
} }
} }
} }
private Object getGuageLock(String name) {
Object lock = this.gaugeLocks.get(name);
if (lock == null) {
this.gaugeLocks.putIfAbsent(name, new Object());
lock = this.gaugeLocks.get(name);
}
return lock;
}
@Override @Override
public void reset(String metricName) { public void reset(String metricName) {
this.registry.remove(metricName); this.registry.remove(metricName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment