Fix concurrent gaugeLocks map access

Use putIfAbsent to ensure atomic creation of lock objects.

Fixes gh-1995
This commit is contained in:
Phillip Webb 2014-12-01 13:58:18 -08:00
parent ffe5348083
commit 49858a0ff1
1 changed files with 10 additions and 10 deletions

View File

@ -90,25 +90,25 @@ public class CodahaleMetricWriter implements MetricWriter {
}
else {
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
// remove causing an error in CodaHale metrics
// NOTE: CodaHale provides no way to do this atomically
synchronized (lock) {
synchronized (getGuageLock(name)) {
this.registry.remove(name);
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
public void reset(String metricName) {
this.registry.remove(metricName);