22
22
import java .util .Collections ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
- import java .util .concurrent .atomic .AtomicLong ;
25
+ import java .util .concurrent .atomic .LongAdder ;
26
26
import java .util .function .Consumer ;
27
27
import java .util .function .LongSupplier ;
28
28
import java .util .function .ToLongBiFunction ;
@@ -46,9 +46,9 @@ public final class EnrichCache {
46
46
47
47
private final Cache <CacheKey , CacheValue > cache ;
48
48
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 ( );
52
52
53
53
EnrichCache (long maxSize ) {
54
54
this (maxSize , System ::nanoTime );
@@ -71,7 +71,7 @@ public final class EnrichCache {
71
71
72
72
private Cache <CacheKey , CacheValue > createCache (long maxWeight , ToLongBiFunction <CacheKey , CacheValue > weigher ) {
73
73
var builder = CacheBuilder .<CacheKey , CacheValue >builder ().setMaximumWeight (maxWeight ).removalListener (notification -> {
74
- sizeInBytes .getAndAdd (-1 * notification .getValue ().sizeInBytes );
74
+ sizeInBytes .add (-1 * notification .getValue ().sizeInBytes );
75
75
});
76
76
if (weigher != null ) {
77
77
builder .weigher (weigher );
@@ -102,7 +102,7 @@ public void computeIfAbsent(
102
102
List <Map <?, ?>> response = get (cacheKey );
103
103
long cacheRequestTime = relativeNanoTimeProvider .getAsLong () - cacheStart ;
104
104
if (response != null ) {
105
- hitsTimeInNanos .addAndGet (cacheRequestTime );
105
+ hitsTimeInNanos .add (cacheRequestTime );
106
106
listener .onResponse (response );
107
107
} else {
108
108
final long retrieveStart = relativeNanoTimeProvider .getAsLong ();
@@ -111,7 +111,7 @@ public void computeIfAbsent(
111
111
put (cacheKey , cacheValue );
112
112
List <Map <?, ?>> copy = deepCopy (cacheValue .hits , false );
113
113
long databaseQueryAndCachePutTime = relativeNanoTimeProvider .getAsLong () - retrieveStart ;
114
- missesTimeInNanos .addAndGet (cacheRequestTime + databaseQueryAndCachePutTime );
114
+ missesTimeInNanos .add (cacheRequestTime + databaseQueryAndCachePutTime );
115
115
listener .onResponse (copy );
116
116
}, listener ::onFailure ));
117
117
}
@@ -130,7 +130,7 @@ public void computeIfAbsent(
130
130
// non-private for unit testing only
131
131
void put (CacheKey cacheKey , CacheValue cacheValue ) {
132
132
cache .put (cacheKey , cacheValue );
133
- sizeInBytes .addAndGet (cacheValue .sizeInBytes );
133
+ sizeInBytes .add (cacheValue .sizeInBytes );
134
134
}
135
135
136
136
public EnrichStatsAction .Response .CacheStats getStats (String localNodeId ) {
@@ -141,9 +141,9 @@ public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
141
141
cacheStats .getHits (),
142
142
cacheStats .getMisses (),
143
143
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 ()
147
147
);
148
148
}
149
149
0 commit comments