Skip to content

Commit 9cec1b3

Browse files
authored
Change GeoIpCache and EnrichCache to LongAdder (#132922) (#133251)
From the javadocs: > [`LongAdder`] is usually preferable to `AtomicLong` when multiple threads update a common sum that is used for purposes such as collecting statistics, not for fine-grained synchronization control. Since we largely write stats and read very infrequently, let's avoid any lock-contention possibilities by switching to the more appropriate data type.
1 parent 97556df commit 9cec1b3

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

docs/changelog/132922.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 132922
2+
summary: Change GeoIpCache and EnrichCache to LongAdder
3+
area: Ingest Node
4+
type: bug
5+
issues: []

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.elasticsearch.ingest.geoip.stats.CacheStats;
1717

1818
import java.nio.file.Path;
19-
import java.util.concurrent.atomic.AtomicLong;
19+
import java.util.concurrent.atomic.LongAdder;
2020
import java.util.function.Function;
2121
import java.util.function.LongSupplier;
2222

@@ -43,8 +43,8 @@ public String toString() {
4343

4444
private final LongSupplier relativeNanoTimeProvider;
4545
private final Cache<CacheKey, Object> cache;
46-
private final AtomicLong hitsTimeInNanos = new AtomicLong(0);
47-
private final AtomicLong missesTimeInNanos = new AtomicLong(0);
46+
private final LongAdder hitsTimeInNanos = new LongAdder();
47+
private final LongAdder missesTimeInNanos = new LongAdder();
4848

4949
// package private for testing
5050
GeoIpCache(long maxSize, LongSupplier relativeNanoTimeProvider) {
@@ -79,9 +79,9 @@ <RESPONSE> RESPONSE putIfAbsent(String ip, String databasePath, Function<String,
7979
// store the result or no-result in the cache
8080
cache.put(cacheKey, response);
8181
long databaseRequestAndCachePutTime = relativeNanoTimeProvider.getAsLong() - retrieveStart;
82-
missesTimeInNanos.addAndGet(cacheRequestTime + databaseRequestAndCachePutTime);
82+
missesTimeInNanos.add(cacheRequestTime + databaseRequestAndCachePutTime);
8383
} else {
84-
hitsTimeInNanos.addAndGet(cacheRequestTime);
84+
hitsTimeInNanos.add(cacheRequestTime);
8585
}
8686

8787
if (response == NO_RESULT) {
@@ -125,8 +125,8 @@ public CacheStats getCacheStats() {
125125
stats.getHits(),
126126
stats.getMisses(),
127127
stats.getEvictions(),
128-
TimeValue.nsecToMSec(hitsTimeInNanos.get()),
129-
TimeValue.nsecToMSec(missesTimeInNanos.get())
128+
TimeValue.nsecToMSec(hitsTimeInNanos.sum()),
129+
TimeValue.nsecToMSec(missesTimeInNanos.sum())
130130
);
131131
}
132132

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichCache.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.concurrent.atomic.AtomicLong;
25+
import java.util.concurrent.atomic.LongAdder;
2626
import java.util.function.Consumer;
2727
import java.util.function.LongSupplier;
2828
import java.util.function.ToLongBiFunction;
@@ -46,9 +46,9 @@ public final class EnrichCache {
4646

4747
private final Cache<CacheKey, CacheValue> cache;
4848
private final LongSupplier relativeNanoTimeProvider;
49-
private final AtomicLong hitsTimeInNanos = new AtomicLong(0);
50-
private final AtomicLong missesTimeInNanos = new AtomicLong(0);
51-
private final AtomicLong sizeInBytes = new AtomicLong(0);
49+
private final LongAdder hitsTimeInNanos = new LongAdder();
50+
private final LongAdder missesTimeInNanos = new LongAdder();
51+
private final LongAdder sizeInBytes = new LongAdder();
5252

5353
EnrichCache(long maxSize) {
5454
this(maxSize, System::nanoTime);
@@ -71,7 +71,7 @@ public final class EnrichCache {
7171

7272
private Cache<CacheKey, CacheValue> createCache(long maxWeight, ToLongBiFunction<CacheKey, CacheValue> weigher) {
7373
var builder = CacheBuilder.<CacheKey, CacheValue>builder().setMaximumWeight(maxWeight).removalListener(notification -> {
74-
sizeInBytes.getAndAdd(-1 * notification.getValue().sizeInBytes);
74+
sizeInBytes.add(-1 * notification.getValue().sizeInBytes);
7575
});
7676
if (weigher != null) {
7777
builder.weigher(weigher);
@@ -102,7 +102,7 @@ public void computeIfAbsent(
102102
List<Map<?, ?>> response = get(cacheKey);
103103
long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart;
104104
if (response != null) {
105-
hitsTimeInNanos.addAndGet(cacheRequestTime);
105+
hitsTimeInNanos.add(cacheRequestTime);
106106
listener.onResponse(response);
107107
} else {
108108
final long retrieveStart = relativeNanoTimeProvider.getAsLong();
@@ -111,7 +111,7 @@ public void computeIfAbsent(
111111
put(cacheKey, cacheValue);
112112
List<Map<?, ?>> copy = deepCopy(cacheValue.hits, false);
113113
long databaseQueryAndCachePutTime = relativeNanoTimeProvider.getAsLong() - retrieveStart;
114-
missesTimeInNanos.addAndGet(cacheRequestTime + databaseQueryAndCachePutTime);
114+
missesTimeInNanos.add(cacheRequestTime + databaseQueryAndCachePutTime);
115115
listener.onResponse(copy);
116116
}, listener::onFailure));
117117
}
@@ -130,7 +130,7 @@ public void computeIfAbsent(
130130
// non-private for unit testing only
131131
void put(CacheKey cacheKey, CacheValue cacheValue) {
132132
cache.put(cacheKey, cacheValue);
133-
sizeInBytes.addAndGet(cacheValue.sizeInBytes);
133+
sizeInBytes.add(cacheValue.sizeInBytes);
134134
}
135135

136136
public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
@@ -141,9 +141,9 @@ public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
141141
cacheStats.getHits(),
142142
cacheStats.getMisses(),
143143
cacheStats.getEvictions(),
144-
TimeValue.nsecToMSec(hitsTimeInNanos.get()),
145-
TimeValue.nsecToMSec(missesTimeInNanos.get()),
146-
sizeInBytes.get()
144+
TimeValue.nsecToMSec(hitsTimeInNanos.sum()),
145+
TimeValue.nsecToMSec(missesTimeInNanos.sum()),
146+
sizeInBytes.sum()
147147
);
148148
}
149149

0 commit comments

Comments
 (0)