From ab4c0276d0e001abc071100f9563f268c460e6f9 Mon Sep 17 00:00:00 2001 From: Joe Gallo Date: Tue, 17 Sep 2024 21:09:37 -0400 Subject: [PATCH] Make the GeoIpCache more generic (#113053) --- .../elasticsearch/ingest/geoip/GeoIpCache.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 061fedf79ce75..c5c7c6175c5f3 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 @@ -9,7 +9,6 @@ package org.elasticsearch.ingest.geoip; import com.maxmind.db.NodeCache; -import com.maxmind.geoip2.model.AbstractResponse; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; @@ -35,15 +34,15 @@ final class GeoIpCache { * something not being in the cache because the data doesn't exist in the database. */ // visible for testing - static final AbstractResponse NO_RESULT = new AbstractResponse() { + static final Object NO_RESULT = new Object() { @Override public String toString() { - return "AbstractResponse[NO_RESULT]"; + return "NO_RESULT"; } }; private final LongSupplier relativeNanoTimeProvider; - private final Cache cache; + private final Cache cache; private final AtomicLong hitsTimeInNanos = new AtomicLong(0); private final AtomicLong missesTimeInNanos = new AtomicLong(0); @@ -53,7 +52,7 @@ public String toString() { throw new IllegalArgumentException("geoip max cache size must be 0 or greater"); } this.relativeNanoTimeProvider = relativeNanoTimeProvider; - this.cache = CacheBuilder.builder().setMaximumWeight(maxSize).build(); + this.cache = CacheBuilder.builder().setMaximumWeight(maxSize).build(); } GeoIpCache(long maxSize) { @@ -61,12 +60,12 @@ public String toString() { } @SuppressWarnings("unchecked") - T putIfAbsent(String ip, String databasePath, Function retrieveFunction) { + T putIfAbsent(String ip, String databasePath, Function retrieveFunction) { // can't use cache.computeIfAbsent due to the elevated permissions for the jackson (run via the cache loader) CacheKey cacheKey = new CacheKey(ip, databasePath); long cacheStart = relativeNanoTimeProvider.getAsLong(); // intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition. - AbstractResponse response = cache.get(cacheKey); + Object response = cache.get(cacheKey); long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart; // populate the cache for this key, if necessary @@ -93,7 +92,7 @@ T putIfAbsent(String ip, String databasePath, Funct } // only useful for testing - AbstractResponse get(String ip, String databasePath) { + Object get(String ip, String databasePath) { CacheKey cacheKey = new CacheKey(ip, databasePath); return cache.get(cacheKey); }