|
63 | 63 | import java.util.concurrent.TimeUnit; |
64 | 64 | import java.util.concurrent.atomic.AtomicInteger; |
65 | 65 | import java.util.concurrent.atomic.AtomicReference; |
| 66 | +import java.util.function.Supplier; |
66 | 67 |
|
67 | 68 | import static org.hamcrest.Matchers.equalTo; |
68 | 69 | import static org.hamcrest.Matchers.greaterThan; |
@@ -527,44 +528,59 @@ public void testHitsMissesAndEvictionsStats() throws Exception { |
527 | 528 | .build(); |
528 | 529 | final DocumentSubsetBitsetCache cache = newCache(settings); |
529 | 530 |
|
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(); |
537 | 543 | assertThat(cache.usageStats(), equalTo(expectedStats)); |
538 | 544 |
|
539 | 545 | runTestOnIndex((searchExecutionContext, leafContext) -> { |
540 | 546 | // first lookup - miss |
541 | 547 | final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext); |
542 | 548 | final BitSet bitSet1 = cache.getBitSet(query1, leafContext); |
543 | 549 | 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)); |
544 | 556 |
|
545 | 557 | // second same lookup - hit |
546 | 558 | final BitSet bitSet1Again = cache.getBitSet(query1, leafContext); |
547 | 559 | assertThat(bitSet1Again, sameInstance(bitSet1)); |
548 | 560 |
|
549 | 561 | 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); |
554 | 562 | assertThat(cache.usageStats(), equalTo(expectedStats)); |
555 | 563 |
|
556 | 564 | // second query - miss, should evict the first one |
557 | 565 | final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext); |
558 | 566 | final BitSet bitSet2 = cache.getBitSet(query2, leafContext); |
559 | 567 | assertThat(bitSet2, notNullValue()); |
560 | 568 |
|
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. |
562 | 570 | // so assertion is current state of the code, rather than the expected state. |
563 | 571 | // issue: https://github.com/elastic/elasticsearch/issues/132842 |
564 | 572 | expectedStats.put("misses", 3L); |
565 | 573 | expectedStats.put("evictions", 1L); |
566 | 574 | assertBusy(() -> { assertThat(cache.usageStats(), equalTo(expectedStats)); }, 200, TimeUnit.MILLISECONDS); |
567 | 575 | }); |
| 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)); |
568 | 584 | } |
569 | 585 |
|
570 | 586 | private void runTestOnIndex(CheckedBiConsumer<SearchExecutionContext, LeafReaderContext, Exception> body) throws Exception { |
|
0 commit comments