Add lock.unlock() to finally clause in Counter

It is good practice to unlock a lock in a finally clause.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132302413
This commit is contained in:
shikhman 2016-09-06 03:49:40 -07:00 committed by Ben McIlwain
parent dcb189943b
commit 93bf68039b

View file

@ -23,7 +23,6 @@ import com.google.common.util.concurrent.AtomicLongMap;
import com.google.common.util.concurrent.Striped; import com.google.common.util.concurrent.Striped;
import google.registry.monitoring.metrics.MetricSchema.Kind; import google.registry.monitoring.metrics.MetricSchema.Kind;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
@ -171,20 +170,21 @@ public final class Counter extends AbstractMetric<Long>
final void reset(Instant startTimestamp) { final void reset(Instant startTimestamp) {
// Lock the entire set of values so that all existing values will have a consistent timestamp // Lock the entire set of values so that all existing values will have a consistent timestamp
// after this call, without the possibility of interleaving with another reset() call. // after this call, without the possibility of interleaving with another reset() call.
Set<ImmutableList<String>> keys = values.asMap().keySet();
for (int i = 0; i < valueLocks.size(); i++) { for (int i = 0; i < valueLocks.size(); i++) {
valueLocks.getAt(i).lock(); valueLocks.getAt(i).lock();
} }
for (ImmutableList<String> labelValues : keys) { try {
for (ImmutableList<String> labelValues : values.asMap().keySet()) {
this.values.put(labelValues, 0); this.values.put(labelValues, 0);
this.valueStartTimestamps.put(labelValues, startTimestamp); this.valueStartTimestamps.put(labelValues, startTimestamp);
} }
} finally {
for (int i = 0; i < valueLocks.size(); i++) { for (int i = 0; i < valueLocks.size(); i++) {
valueLocks.getAt(i).unlock(); valueLocks.getAt(i).unlock();
} }
} }
}
@Override @Override
public final void reset() { public final void reset() {