Skip to content

Commit 695e22d

Browse files
committed
joe comments
1 parent 6a35ded commit 695e22d

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.util.concurrent.TimeUnit;
6464
import java.util.concurrent.atomic.AtomicInteger;
6565
import java.util.concurrent.atomic.AtomicReference;
66+
import java.util.function.Supplier;
6667

6768
import static org.hamcrest.Matchers.equalTo;
6869
import static org.hamcrest.Matchers.greaterThan;
@@ -527,44 +528,59 @@ public void testHitsMissesAndEvictionsStats() throws Exception {
527528
.build();
528529
final DocumentSubsetBitsetCache cache = newCache(settings);
529530

530-
final Map<String, Object> expectedStats = new LinkedHashMap<>();
531-
expectedStats.put("count", 0);
532-
expectedStats.put("memory", "0b");
533-
expectedStats.put("memory_in_bytes", 0L);
534-
expectedStats.put("hits", 0L);
535-
expectedStats.put("misses", 0L);
536-
expectedStats.put("evictions", 0L);
531+
final Supplier<Map<String, Object>> emptyStatsSupplier = () -> {
532+
final Map<String, Object> stats = new LinkedHashMap<>();
533+
stats.put("count", 0);
534+
stats.put("memory", "0b");
535+
stats.put("memory_in_bytes", 0L);
536+
stats.put("hits", 0L);
537+
stats.put("misses", 0L);
538+
stats.put("evictions", 0L);
539+
return stats;
540+
};
541+
542+
final Map<String, Object> expectedStats = emptyStatsSupplier.get();
537543
assertThat(cache.usageStats(), equalTo(expectedStats));
538544

539545
runTestOnIndex((searchExecutionContext, leafContext) -> {
540546
// first lookup - miss
541547
final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext);
542548
final BitSet bitSet1 = cache.getBitSet(query1, leafContext);
543549
assertThat(bitSet1, notNullValue());
550+
expectedStats.put("count", 1);
551+
expectedStats.put("misses", 1L);
552+
expectedStats.put("memory", EXPECTED_BYTES_PER_BIT_SET + "b");
553+
expectedStats.put("memory_in_bytes", EXPECTED_BYTES_PER_BIT_SET);
554+
555+
assertThat(cache.usageStats(), equalTo(expectedStats));
544556

545557
// second same lookup - hit
546558
final BitSet bitSet1Again = cache.getBitSet(query1, leafContext);
547559
assertThat(bitSet1Again, sameInstance(bitSet1));
548560

549561
expectedStats.put("hits", 1L);
550-
expectedStats.put("misses", 1L);
551-
expectedStats.put("count", 1);
552-
expectedStats.put("memory", EXPECTED_BYTES_PER_BIT_SET + "b");
553-
expectedStats.put("memory_in_bytes", EXPECTED_BYTES_PER_BIT_SET);
554562
assertThat(cache.usageStats(), equalTo(expectedStats));
555563

556564
// second query - miss, should evict the first one
557565
final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext);
558566
final BitSet bitSet2 = cache.getBitSet(query2, leafContext);
559567
assertThat(bitSet2, notNullValue());
560568

561-
// szymon: eviction callback calls `get` on the cache, asynchronously, which updates the stats.
569+
// eviction callback calls `get` on the cache, asynchronously, which updates the stats.
562570
// so assertion is current state of the code, rather than the expected state.
563571
// issue: https://github.com/elastic/elasticsearch/issues/132842
564572
expectedStats.put("misses", 3L);
565573
expectedStats.put("evictions", 1L);
566574
assertBusy(() -> { assertThat(cache.usageStats(), equalTo(expectedStats)); }, 200, TimeUnit.MILLISECONDS);
567575
});
576+
577+
final Map<String, Object> finalStats = emptyStatsSupplier.get();
578+
// related to comment above: surprisingly last eviction doesn't increment hits nor misses, because it goes through onClose(),
579+
// and short-circuits in the eviction callback before doing a `get`.
580+
finalStats.put("hits", 1L);
581+
finalStats.put("misses", 3L);
582+
finalStats.put("evictions", 2L);
583+
assertThat(cache.usageStats(), equalTo(finalStats));
568584
}
569585

570586
private void runTestOnIndex(CheckedBiConsumer<SearchExecutionContext, LeafReaderContext, Exception> body) throws Exception {

0 commit comments

Comments
 (0)