Skip to content

Commit 4636f4d

Browse files
authored
Change GeoIpCache and EnrichCache to LongAdder (#132922) (#133250)
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 881b65e commit 4636f4d

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
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.ingest.geoip.stats.CacheStats;
1818

1919
import java.nio.file.Path;
20-
import java.util.concurrent.atomic.AtomicLong;
20+
import java.util.concurrent.atomic.LongAdder;
2121
import java.util.function.Function;
2222
import java.util.function.LongSupplier;
2323

@@ -44,8 +44,8 @@ public String toString() {
4444

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

5050
// package private for testing
5151
GeoIpCache(long maxSize, LongSupplier relativeNanoTimeProvider) {
@@ -80,9 +80,9 @@ <RESPONSE> RESPONSE putIfAbsent(ProjectId projectId, String ip, String databaseP
8080
// store the result or no-result in the cache
8181
cache.put(cacheKey, response);
8282
long databaseRequestAndCachePutTime = relativeNanoTimeProvider.getAsLong() - retrieveStart;
83-
missesTimeInNanos.addAndGet(cacheRequestTime + databaseRequestAndCachePutTime);
83+
missesTimeInNanos.add(cacheRequestTime + databaseRequestAndCachePutTime);
8484
} else {
85-
hitsTimeInNanos.addAndGet(cacheRequestTime);
85+
hitsTimeInNanos.add(cacheRequestTime);
8686
}
8787

8888
if (response == NO_RESULT) {
@@ -126,8 +126,8 @@ public CacheStats getCacheStats() {
126126
stats.getHits(),
127127
stats.getMisses(),
128128
stats.getEvictions(),
129-
TimeValue.nsecToMSec(hitsTimeInNanos.get()),
130-
TimeValue.nsecToMSec(missesTimeInNanos.get())
129+
TimeValue.nsecToMSec(hitsTimeInNanos.sum()),
130+
TimeValue.nsecToMSec(missesTimeInNanos.sum())
131131
);
132132
}
133133

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
@@ -23,7 +23,7 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.concurrent.atomic.AtomicLong;
26+
import java.util.concurrent.atomic.LongAdder;
2727
import java.util.function.Consumer;
2828
import java.util.function.LongSupplier;
2929
import java.util.function.ToLongBiFunction;
@@ -47,9 +47,9 @@ public final class EnrichCache {
4747

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

5454
EnrichCache(long maxSize) {
5555
this(maxSize, System::nanoTime);
@@ -72,7 +72,7 @@ public final class EnrichCache {
7272

7373
private Cache<CacheKey, CacheValue> createCache(long maxWeight, ToLongBiFunction<CacheKey, CacheValue> weigher) {
7474
var builder = CacheBuilder.<CacheKey, CacheValue>builder().setMaximumWeight(maxWeight).removalListener(notification -> {
75-
sizeInBytes.getAndAdd(-1 * notification.getValue().sizeInBytes);
75+
sizeInBytes.add(-1 * notification.getValue().sizeInBytes);
7676
});
7777
if (weigher != null) {
7878
builder.weigher(weigher);
@@ -105,7 +105,7 @@ public void computeIfAbsent(
105105
List<Map<?, ?>> response = get(cacheKey);
106106
long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart;
107107
if (response != null) {
108-
hitsTimeInNanos.addAndGet(cacheRequestTime);
108+
hitsTimeInNanos.add(cacheRequestTime);
109109
listener.onResponse(response);
110110
} else {
111111
final long retrieveStart = relativeNanoTimeProvider.getAsLong();
@@ -114,7 +114,7 @@ public void computeIfAbsent(
114114
put(cacheKey, cacheValue);
115115
List<Map<?, ?>> copy = deepCopy(cacheValue.hits, false);
116116
long databaseQueryAndCachePutTime = relativeNanoTimeProvider.getAsLong() - retrieveStart;
117-
missesTimeInNanos.addAndGet(cacheRequestTime + databaseQueryAndCachePutTime);
117+
missesTimeInNanos.add(cacheRequestTime + databaseQueryAndCachePutTime);
118118
listener.onResponse(copy);
119119
}, listener::onFailure));
120120
}
@@ -133,7 +133,7 @@ public void computeIfAbsent(
133133
// non-private for unit testing only
134134
void put(CacheKey cacheKey, CacheValue cacheValue) {
135135
cache.put(cacheKey, cacheValue);
136-
sizeInBytes.addAndGet(cacheValue.sizeInBytes);
136+
sizeInBytes.add(cacheValue.sizeInBytes);
137137
}
138138

139139
public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
@@ -144,9 +144,9 @@ public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
144144
cacheStats.getHits(),
145145
cacheStats.getMisses(),
146146
cacheStats.getEvictions(),
147-
TimeValue.nsecToMSec(hitsTimeInNanos.get()),
148-
TimeValue.nsecToMSec(missesTimeInNanos.get()),
149-
sizeInBytes.get()
147+
TimeValue.nsecToMSec(hitsTimeInNanos.sum()),
148+
TimeValue.nsecToMSec(missesTimeInNanos.sum()),
149+
sizeInBytes.sum()
150150
);
151151
}
152152

0 commit comments

Comments
 (0)