diff --git a/java/google/registry/monitoring/metrics/Counter.java b/java/google/registry/monitoring/metrics/Counter.java index 6f42e2378..911c35b0f 100644 --- a/java/google/registry/monitoring/metrics/Counter.java +++ b/java/google/registry/monitoring/metrics/Counter.java @@ -15,6 +15,8 @@ package google.registry.monitoring.metrics; import static com.google.common.base.Preconditions.checkArgument; +import static google.registry.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL; +import static google.registry.monitoring.metrics.MetricsUtils.newConcurrentHashMap; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; @@ -40,15 +42,6 @@ import org.joda.time.Instant; public final class Counter extends AbstractMetric implements SettableMetric, IncrementableMetric { - /** - * The below constants replicate the default initial capacity, load factor, and concurrency level - * for {@link ConcurrentHashMap} as of Java SE 7. They are hardcoded here so that the concurrency - * level in {@code valueLocks} below can be set identically. - */ - private static final int HASHMAP_INITIAL_CAPACITY = 16; - private static final float HASHMAP_LOAD_FACTOR = 0.75f; - private static final int HASHMAP_CONCURRENCY_LEVEL = 16; - /** * A map of the {@link Counter} values, with a list of label values as the keys. * @@ -63,8 +56,7 @@ public final class Counter extends AbstractMetric * implementations of {@link MetricWriter} to encode resets of monotonic counters. */ private final ConcurrentHashMap, Instant> valueStartTimestamps = - new ConcurrentHashMap<>( - HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, HASHMAP_CONCURRENCY_LEVEL); + newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL); /** * A fine-grained lock to ensure that {@code values} and {@code valueStartTimestamps} are modified @@ -73,7 +65,7 @@ public final class Counter extends AbstractMetric * * @see Striped */ - private final Striped valueLocks = Striped.lock(HASHMAP_CONCURRENCY_LEVEL); + private final Striped valueLocks = Striped.lock(DEFAULT_CONCURRENCY_LEVEL); Counter( String name, diff --git a/java/google/registry/monitoring/metrics/EventMetric.java b/java/google/registry/monitoring/metrics/EventMetric.java index 9c2160152..33dfc99dd 100644 --- a/java/google/registry/monitoring/metrics/EventMetric.java +++ b/java/google/registry/monitoring/metrics/EventMetric.java @@ -14,6 +14,9 @@ package google.registry.monitoring.metrics; +import static google.registry.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL; +import static google.registry.monitoring.metrics.MetricsUtils.newConcurrentHashMap; + import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -38,22 +41,10 @@ import org.joda.time.Instant; */ public final class EventMetric extends AbstractMetric { - /** - * The below constants replicate the default initial capacity, load factor, and concurrency level - * for {@link ConcurrentHashMap} as of Java SE 7. They are hardcoded here so that the concurrency - * level in {@code valueLocks} below can be set identically. - */ - private static final int HASHMAP_INITIAL_CAPACITY = 16; - - private static final float HASHMAP_LOAD_FACTOR = 0.75f; - private static final int HASHMAP_CONCURRENCY_LEVEL = 16; - private final ConcurrentHashMap, Instant> valueStartTimestamps = - new ConcurrentHashMap<>( - HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, HASHMAP_CONCURRENCY_LEVEL); + newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL); private final ConcurrentHashMap, MutableDistribution> values = - new ConcurrentHashMap<>( - HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, HASHMAP_CONCURRENCY_LEVEL); + newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL); private final DistributionFitter distributionFitter; @@ -64,7 +55,7 @@ public final class EventMetric extends AbstractMetric { * * @see Striped */ - private final Striped valueLocks = Striped.lock(HASHMAP_CONCURRENCY_LEVEL); + private final Striped valueLocks = Striped.lock(DEFAULT_CONCURRENCY_LEVEL); EventMetric( String name, diff --git a/java/google/registry/monitoring/metrics/MetricsUtils.java b/java/google/registry/monitoring/metrics/MetricsUtils.java index 26e4cec8e..402d81dcb 100644 --- a/java/google/registry/monitoring/metrics/MetricsUtils.java +++ b/java/google/registry/monitoring/metrics/MetricsUtils.java @@ -17,6 +17,7 @@ package google.registry.monitoring.metrics; import static com.google.common.base.Preconditions.checkArgument; import com.google.common.collect.ImmutableList; +import java.util.concurrent.ConcurrentHashMap; /** Static helper methods for the Metrics library. */ final class MetricsUtils { @@ -25,6 +26,16 @@ final class MetricsUtils { private static final String LABEL_SIZE_ERROR = "The count of labelValues must be equal to the underlying Metric's count of labels."; + /** + * The below constants replicate the default initial capacity, load factor, and concurrency level + * for {@link ConcurrentHashMap} as of Java SE 7. They are recorded here so that a {@link + * com.google.common.util.concurrent.Striped} object can be constructed with a concurrency level + * matching the default concurrency level of a {@link ConcurrentHashMap}. + */ + private static final int HASHMAP_INITIAL_CAPACITY = 16; + private static final float HASHMAP_LOAD_FACTOR = 0.75f; + static final int DEFAULT_CONCURRENCY_LEVEL = 16; + private MetricsUtils() {} /** @@ -53,4 +64,8 @@ final class MetricsUtils { !Double.isInfinite(value) && !Double.isNaN(value) && !NEGATIVE_ZERO.equals(value), "value must be finite, not NaN, and not -0.0"); } + + static ConcurrentHashMap newConcurrentHashMap(int concurrencyLevel) { + return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel); + } }