jpountz commented on a change in pull request #916: LUCENE-8213: Asynchronous 
Caching in LRUQueryCache
URL: https://github.com/apache/lucene-solr/pull/916#discussion_r333898028
 
 

 ##########
 File path: lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
 ##########
 @@ -244,6 +275,213 @@ public void testLRUEviction() throws Exception {
     dir.close();
   }
 
+  public void testLRUConcurrentLoadAndEviction() throws Exception {
+    Directory dir = newDirectory();
+    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+    Document doc = new Document();
+    StringField f = new StringField("color", "blue", Store.NO);
+    doc.add(f);
+    w.addDocument(doc);
+    f.setStringValue("red");
+    w.addDocument(doc);
+    f.setStringValue("green");
+    w.addDocument(doc);
+    final DirectoryReader reader = w.getReader();
+    ExecutorService service = new ThreadPoolExecutor(4, 4, 0L, 
TimeUnit.MILLISECONDS,
+        new LinkedBlockingQueue<Runnable>(),
+        new NamedThreadFactory("TestLRUQueryCache"));
+
+    IndexSearcher searcher = new IndexSearcher(reader, service);
+
+    final CountDownLatch[] latch = {new CountDownLatch(1)};
+
+    final LRUQueryCache queryCache = new LRUQueryCache(2, 100000, context -> 
true) {
+      @Override
+      protected void onDocIdSetCache(Object readerCoreKey, long ramBytesUsed) {
+        super.onDocIdSetCache(readerCoreKey, ramBytesUsed);
+        latch[0].countDown();
+      }
+    };
+
+    final Query blue = new TermQuery(new Term("color", "blue"));
+    final Query red = new TermQuery(new Term("color", "red"));
+    final Query green = new TermQuery(new Term("color", "green"));
+
+    assertEquals(Collections.emptyList(), queryCache.cachedQueries());
+
+    searcher.setQueryCache(queryCache);
+    // the filter is not cached on any segment: no changes
+    searcher.setQueryCachingPolicy(NEVER_CACHE);
+    searcher.search(new ConstantScoreQuery(green), 1);
+    assertEquals(Collections.emptyList(), queryCache.cachedQueries());
+
+    searcher.setQueryCachingPolicy(ALWAYS_CACHE);
+
+    // First read should miss
+    searcher.search(new ConstantScoreQuery(red), 1);
+
+
+    // Let the cache load be completed
+    latch[0].await();
+    searcher.search(new ConstantScoreQuery(red), 1);
+
+    // Second read should hit
+    searcher.search(new ConstantScoreQuery(red), 1);
+    assertEquals(Collections.singletonList(red), queryCache.cachedQueries());
+
+    latch[0] = new CountDownLatch(1);
+    searcher.search(new ConstantScoreQuery(green), 1);
+
+    // Let the cache load be completed
+    latch[0].await();
+    assertEquals(Arrays.asList(red, green), queryCache.cachedQueries());
+
+    searcher.search(new ConstantScoreQuery(red), 1);
+    assertEquals(Arrays.asList(green, red), queryCache.cachedQueries());
+
+    latch[0] = new CountDownLatch(1);
+
+    searcher.search(new ConstantScoreQuery(blue), 1);
+
+    // Let the cache load be completed
+    latch[0].await();
+    assertEquals(Arrays.asList(red, blue), queryCache.cachedQueries());
+
+    searcher.search(new ConstantScoreQuery(blue), 1);
+    assertEquals(Arrays.asList(red, blue), queryCache.cachedQueries());
+
+    latch[0] = new CountDownLatch(1);
+
+    searcher.search(new ConstantScoreQuery(green), 1);
+
+    // Let the cache load be completed
+    latch[0].await();
+    assertEquals(Arrays.asList(blue, green), queryCache.cachedQueries());
+
+    searcher.setQueryCachingPolicy(NEVER_CACHE);
+    searcher.search(new ConstantScoreQuery(red), 1);
+    assertEquals(Arrays.asList(blue, green), queryCache.cachedQueries());
 
 Review comment:
   maybe move the call to service.shutdown() above this line and also call 
`awaitTermination` to make sure that any ongoing cache operation are done so 
that the assertion doesn't succeed only because we are lucky with timing?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to