|
43 | 43 | import org.elasticsearch.test.ESTestCase; |
44 | 44 | import org.elasticsearch.test.IndexSettingsModule; |
45 | 45 | import org.elasticsearch.test.MockLog; |
46 | | -import org.mockito.Mockito; |
47 | 46 |
|
48 | 47 | import java.io.Closeable; |
49 | 48 | import java.io.IOException; |
|
72 | 71 | import static org.hamcrest.Matchers.notNullValue; |
73 | 72 | import static org.hamcrest.Matchers.nullValue; |
74 | 73 | import static org.hamcrest.Matchers.sameInstance; |
75 | | -import static org.mockito.ArgumentMatchers.any; |
76 | 74 | import static org.mockito.Mockito.mock; |
77 | 75 | import static org.mockito.Mockito.when; |
78 | 76 |
|
@@ -377,61 +375,37 @@ public void testCacheUnderConcurrentAccess() throws Exception { |
377 | 375 | cache.verifyInternalConsistencyKeysToCache(); |
378 | 376 | } |
379 | 377 |
|
380 | | - @AwaitsFix(bugUrl = "todo") |
381 | | - public void testCleanupWorksWhenIndexIsClosing() throws Exception { |
| 378 | + public void testCleanupWorksWhenIndexIsClosed() throws Exception { |
382 | 379 | // Enough to hold slightly more than 1 bit-set in the cache |
383 | 380 | final long maxCacheBytes = EXPECTED_BYTES_PER_BIT_SET + EXPECTED_BYTES_PER_BIT_SET / 2; |
384 | 381 | final Settings settings = Settings.builder() |
385 | 382 | .put(DocumentSubsetBitsetCache.CACHE_SIZE_SETTING.getKey(), maxCacheBytes + "b") |
386 | 383 | .build(); |
387 | | - final ExecutorService threads = Executors.newFixedThreadPool(1); |
388 | | - final ExecutorService cleanupExecutor = Mockito.mock(ExecutorService.class); |
389 | | - final CountDownLatch cleanupReadyLatch = new CountDownLatch(1); |
390 | | - final CountDownLatch cleanupCompleteLatch = new CountDownLatch(1); |
391 | | - final CountDownLatch indexCloseLatch = new CountDownLatch(1); |
392 | | - final AtomicReference<Throwable> cleanupException = new AtomicReference<>(); |
393 | | - when(cleanupExecutor.submit(any(Runnable.class))).thenAnswer(inv -> { |
394 | | - final Runnable runnable = (Runnable) inv.getArguments()[0]; |
395 | | - return threads.submit(() -> { |
396 | | - try { |
397 | | - cleanupReadyLatch.countDown(); |
398 | | - assertTrue("index close did not completed in expected time", indexCloseLatch.await(1, TimeUnit.SECONDS)); |
399 | | - runnable.run(); |
400 | | - } catch (Throwable e) { |
401 | | - logger.warn("caught error in cleanup thread", e); |
402 | | - cleanupException.compareAndSet(null, e); |
403 | | - } finally { |
404 | | - cleanupCompleteLatch.countDown(); |
405 | | - } |
406 | | - return null; |
407 | | - }); |
408 | | - }); |
409 | 384 |
|
410 | 385 | final DocumentSubsetBitsetCache cache = new DocumentSubsetBitsetCache(settings); |
411 | 386 | assertThat(cache.entryCount(), equalTo(0)); |
412 | 387 | assertThat(cache.ramBytesUsed(), equalTo(0L)); |
413 | 388 |
|
414 | | - try { |
415 | | - runTestOnIndex((searchExecutionContext, leafContext) -> { |
416 | | - final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext); |
417 | | - final BitSet bitSet1 = cache.getBitSet(query1, leafContext); |
418 | | - assertThat(bitSet1, notNullValue()); |
| 389 | + runTestOnIndex((searchExecutionContext, leafContext) -> { |
| 390 | + final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext); |
| 391 | + final BitSet bitSet1 = cache.getBitSet(query1, leafContext); |
| 392 | + assertThat(bitSet1, notNullValue()); |
| 393 | + cache.verifyInternalConsistency(); |
419 | 394 |
|
420 | | - // Second query should trigger a cache eviction |
421 | | - final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext); |
422 | | - final BitSet bitSet2 = cache.getBitSet(query2, leafContext); |
423 | | - assertThat(bitSet2, notNullValue()); |
| 395 | + // Second query should trigger a cache eviction |
| 396 | + final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext); |
| 397 | + final BitSet bitSet2 = cache.getBitSet(query2, leafContext); |
| 398 | + assertThat(bitSet2, notNullValue()); |
| 399 | + cache.verifyInternalConsistency(); |
424 | 400 |
|
425 | | - final IndexReader.CacheKey indexKey = leafContext.reader().getCoreCacheHelper().getKey(); |
426 | | - assertTrue("cleanup did not trigger in expected time", cleanupReadyLatch.await(1, TimeUnit.SECONDS)); |
427 | | - cache.onClose(indexKey); |
428 | | - indexCloseLatch.countDown(); |
429 | | - assertTrue("cleanup did not complete in expected time", cleanupCompleteLatch.await(1, TimeUnit.SECONDS)); |
430 | | - assertThat("caught error in cleanup thread: " + cleanupException.get(), cleanupException.get(), nullValue()); |
431 | | - }); |
432 | | - } finally { |
433 | | - threads.shutdown(); |
434 | | - } |
| 401 | + final IndexReader.CacheKey indexKey = leafContext.reader().getCoreCacheHelper().getKey(); |
| 402 | + cache.onClose(indexKey); |
| 403 | + cache.verifyInternalConsistency(); |
| 404 | + |
| 405 | + // closing an index results in the associated entries being removed from the cache (at least when single threaded) |
| 406 | + assertThat(cache.entryCount(), equalTo(0)); |
| 407 | + assertThat(cache.ramBytesUsed(), equalTo(0L)); |
| 408 | + }); |
435 | 409 | } |
436 | 410 |
|
437 | 411 | public void testCacheIsPerIndex() throws Exception { |
|
0 commit comments