diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2019-01-31 15:11:42 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2019-01-31 15:11:42 +0100 |
commit | d092bfd8c86bc7fa66fc60ecb19226ee5bec6a36 (patch) | |
tree | 94a73cd4dcabb4457be8edd9d218ca6df899eae8 /vespaclient-container-plugin | |
parent | 827d27875c41b409d1b35281eda06c8bed2e821f (diff) |
Use Guava cache with size-based eviction
Diffstat (limited to 'vespaclient-container-plugin')
-rw-r--r-- | vespaclient-container-plugin/src/main/java/com/yahoo/documentapi/metrics/DocumentApiMetrics.java | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/documentapi/metrics/DocumentApiMetrics.java b/vespaclient-container-plugin/src/main/java/com/yahoo/documentapi/metrics/DocumentApiMetrics.java index 1d6d03655bf..4dbe1381e34 100644 --- a/vespaclient-container-plugin/src/main/java/com/yahoo/documentapi/metrics/DocumentApiMetrics.java +++ b/vespaclient-container-plugin/src/main/java/com/yahoo/documentapi/metrics/DocumentApiMetrics.java @@ -1,6 +1,8 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.documentapi.metrics; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.yahoo.metrics.simple.Counter; import com.yahoo.metrics.simple.Gauge; import com.yahoo.metrics.simple.MetricReceiver; @@ -10,7 +12,7 @@ import java.time.Duration; import java.time.Instant; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; /** @@ -24,7 +26,7 @@ public class DocumentApiMetrics { private final Gauge feedLatency; private final Counter feedRequests; private final Map<DocumentOperationStatus, Map<DocumentOperationType, Point>> points = new HashMap<>(); - private final Map<String, Point> versionPointCache = new ConcurrentHashMap<>(); + private final Cache<String, Point> versionPointCache = CacheBuilder.newBuilder().maximumSize(256).build(); public DocumentApiMetrics(MetricReceiver metricReceiver, String apiName) { Map<String, String> dimensions = new HashMap<>(); @@ -62,7 +64,12 @@ public class DocumentApiMetrics { public void reportHttpRequest(String clientVersion) { if (clientVersion != null) { - feedRequests.add(versionPointCache.computeIfAbsent(clientVersion, v -> new Point(Map.of("client-version", v)))); + try { + Point point = versionPointCache.get(clientVersion, () -> new Point(Map.of("client-version", clientVersion))); + feedRequests.add(point); + } catch (ExecutionException e) { // When Point constructor throws an exception + throw new RuntimeException(e); + } } else { feedRequests.add(); } |