Skip to content

Commit 610a792

Browse files
committed
Drop the get and contains in favor of computeIfPresent
1 parent db0a286 commit 610a792

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,11 @@ public void onClose(IndexReader.CacheKey indexKey) {
151151
private void onCacheEviction(RemovalNotification<BitsetCacheKey, BitSet> notification) {
152152
final BitsetCacheKey cacheKey = notification.getKey();
153153
final IndexReader.CacheKey indexKey = cacheKey.indexKey;
154-
if (keysByIndex.getOrDefault(indexKey, Set.of()).contains(cacheKey) == false) {
155-
// If the cacheKey isn't in the lookup map, then there's nothing to synchronize
156-
return;
157-
}
158-
159-
// it's possible for the key to be back in the cache if it was immediately repopulated after it was evicted, so check
160-
if (bitsetCache.get(cacheKey) == null) {
161-
// key is no longer in the cache, make sure it is no longer in the lookup map either.
162-
keysByIndex.compute(indexKey, (ignored, keys) -> {
163-
if (keys != null) {
164-
keys.remove(cacheKey);
165-
if (keys.isEmpty()) {
166-
keys = null;
167-
}
168-
}
169-
return keys;
170-
});
171-
}
154+
// key is no longer in the cache, make sure it is no longer in the lookup map either.
155+
keysByIndex.computeIfPresent(indexKey, (ignored, keys) -> {
156+
keys.remove(cacheKey);
157+
return keys.isEmpty() ? null : keys;
158+
});
172159
}
173160

174161
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,17 @@ public void testHitsMissesAndEvictionsStats() throws Exception {
547547
final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext);
548548
final BitSet bitSet2 = cache.getBitSet(query2, leafContext);
549549
assertThat(bitSet2, notNullValue());
550-
// surprisingly, the eviction callback can call `get` on the cache (asynchronously) which causes another miss (or hit)
551-
// so this assertion is about the current state of the code, rather than the expected or desired state.
552-
// see https://github.com/elastic/elasticsearch/issues/132842
553-
expectedStats.put("misses", 3L);
550+
expectedStats.put("misses", 2L);
554551
expectedStats.put("evictions", 1L);
555552
// underlying Cache class tracks hits/misses, but timing is in DLS cache, which is why we have `2L` here,
556553
// because DLS cache is only hit once
557554
expectedStats.put("misses_time_in_millis", 2L);
558-
assertBusy(() -> { assertThat(cache.usageStats(), equalTo(expectedStats)); }, 200, TimeUnit.MILLISECONDS);
555+
assertThat(cache.usageStats(), equalTo(expectedStats));
559556
});
560557

561558
final Map<String, Object> finalStats = emptyStatsSupplier.get();
562559
finalStats.put("hits", 1L);
563-
finalStats.put("misses", 3L);
560+
finalStats.put("misses", 2L);
564561
finalStats.put("evictions", 2L);
565562
finalStats.put("hits_time_in_millis", 1L);
566563
finalStats.put("misses_time_in_millis", 2L);

0 commit comments

Comments
 (0)