diff --git a/docs/changelog/132922.yaml b/docs/changelog/132922.yaml new file mode 100644 index 0000000000000..2457e8d81eaf8 --- /dev/null +++ b/docs/changelog/132922.yaml @@ -0,0 +1,5 @@ +pr: 132922 +summary: Change GeoIpCache and EnrichCache to LongAdder +area: Ingest Node +type: bug +issues: [] diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java index d9c9c3aaf3266..f11b25fa8b746 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java @@ -16,7 +16,7 @@ import org.elasticsearch.ingest.geoip.stats.CacheStats; import java.nio.file.Path; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import java.util.function.Function; import java.util.function.LongSupplier; @@ -43,8 +43,8 @@ public String toString() { private final LongSupplier relativeNanoTimeProvider; private final Cache cache; - private final AtomicLong hitsTimeInNanos = new AtomicLong(0); - private final AtomicLong missesTimeInNanos = new AtomicLong(0); + private final LongAdder hitsTimeInNanos = new LongAdder(); + private final LongAdder missesTimeInNanos = new LongAdder(); // package private for testing GeoIpCache(long maxSize, LongSupplier relativeNanoTimeProvider) { @@ -79,9 +79,9 @@ RESPONSE putIfAbsent(String ip, String databasePath, Function cache; private final LongSupplier relativeNanoTimeProvider; - private final AtomicLong hitsTimeInNanos = new AtomicLong(0); - private final AtomicLong missesTimeInNanos = new AtomicLong(0); - private final AtomicLong sizeInBytes = new AtomicLong(0); + private final LongAdder hitsTimeInNanos = new LongAdder(); + private final LongAdder missesTimeInNanos = new LongAdder(); + private final LongAdder sizeInBytes = new LongAdder(); EnrichCache(long maxSize) { this(maxSize, System::nanoTime); @@ -71,7 +71,7 @@ public final class EnrichCache { private Cache createCache(long maxWeight, ToLongBiFunction weigher) { var builder = CacheBuilder.builder().setMaximumWeight(maxWeight).removalListener(notification -> { - sizeInBytes.getAndAdd(-1 * notification.getValue().sizeInBytes); + sizeInBytes.add(-1 * notification.getValue().sizeInBytes); }); if (weigher != null) { builder.weigher(weigher); @@ -102,7 +102,7 @@ public void computeIfAbsent( List> response = get(cacheKey); long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart; if (response != null) { - hitsTimeInNanos.addAndGet(cacheRequestTime); + hitsTimeInNanos.add(cacheRequestTime); listener.onResponse(response); } else { final long retrieveStart = relativeNanoTimeProvider.getAsLong(); @@ -111,7 +111,7 @@ public void computeIfAbsent( put(cacheKey, cacheValue); List> copy = deepCopy(cacheValue.hits, false); long databaseQueryAndCachePutTime = relativeNanoTimeProvider.getAsLong() - retrieveStart; - missesTimeInNanos.addAndGet(cacheRequestTime + databaseQueryAndCachePutTime); + missesTimeInNanos.add(cacheRequestTime + databaseQueryAndCachePutTime); listener.onResponse(copy); }, listener::onFailure)); } @@ -130,7 +130,7 @@ public void computeIfAbsent( // non-private for unit testing only void put(CacheKey cacheKey, CacheValue cacheValue) { cache.put(cacheKey, cacheValue); - sizeInBytes.addAndGet(cacheValue.sizeInBytes); + sizeInBytes.add(cacheValue.sizeInBytes); } public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) { @@ -141,9 +141,9 @@ public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) { cacheStats.getHits(), cacheStats.getMisses(), cacheStats.getEvictions(), - TimeValue.nsecToMSec(hitsTimeInNanos.get()), - TimeValue.nsecToMSec(missesTimeInNanos.get()), - sizeInBytes.get() + TimeValue.nsecToMSec(hitsTimeInNanos.sum()), + TimeValue.nsecToMSec(missesTimeInNanos.sum()), + sizeInBytes.sum() ); }