Skip to content

Apache Lucene open-source search software

LUCENE-8213: Introduce Asynchronous Caching in LRUQueryCache (#815)

Atri Sharma0dfbf557bae6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 456843db04c..bb11aeae804 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -55,6 +55,9 @@ Improvements
* LUCENE-8984: MoreLikeThis MLT is biased for uncommon fields (Andy Hind via Anshum Gupta)
+* LUCENE-8213: LRUQueryCache#doCache now uses IndexSearcher's Executor (if present)
+ to asynchronously cache heavy queries (Atri Sharma)
+
Bug fixes
* LUCENE-8663: NRTCachingDirectory.slowFileExists may open a file while
diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
index e0f0cdf4214..3be9d346ca2 100644
--- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -181,7 +181,16 @@ public class IndexSearcher {
}
/** Runs searches for each segment separately, using the
- * provided Executor. NOTE:
+ * provided Executor. The passed in Executor will also be
+ * used by LRUQueryCache (if enabled) to perform asynchronous
+ * query caching.
+ * If a task is rejected by the host Executor, the failed task
+ * will then be executed on the caller thread. This is done to
+ * ensure that a query succeeds, albeit with a higher latency.
+ * If a user wishes to modify the said behaviour, they can either
+ * handle the exception in the provided Executor, or override
+ * the said method in a custom extension of IndexSearcher.
+ * NOTE:
* if you are using {@link NIOFSDirectory}, do not use
* the shutdownNow method of ExecutorService as this uses
* Thread.interrupt under-the-hood which can silently
@@ -841,7 +850,11 @@ public class IndexSearcher {
final QueryCache queryCache = this.queryCache;
Weight weight = query.createWeight(this, scoreMode, boost);
if (scoreMode.needsScores() == false && queryCache != null) {
- weight = queryCache.doCache(weight, queryCachingPolicy);
+ if (executor != null) {
+ weight = queryCache.doCache(weight, queryCachingPolicy, executor);
+ } else {
+ weight = queryCache.doCache(weight, queryCachingPolicy);
+ }
}
return weight;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
index ea3c158ce8c..24c5e98e723 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
@@ -22,12 +22,16 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
+import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.Executor;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
@@ -95,6 +99,11 @@ public class LRUQueryCache implements QueryCache, Accountable {
// maps queries that are contained in the cache to a singleton so that this
// cache does not store several copies of the same query
private final Map<Query, Query> uniqueQueries;
+ // Marks the inflight queries that are being asynchronously loaded into the cache
+ // This is used to ensure that multiple threads do not trigger loading
+ // of the same query in the same cache. We use a set because it is an invariant that
+ // the entries of this data structure be unique.
+ private final Set<Query> inFlightAsyncLoadQueries = new HashSet<>();
// The contract between this set and the per-leaf caches is that per-leaf caches
// are only allowed to store sub-sets of the queries that are contained in
// mostRecentlyUsedQueries. This is why write operations are performed under a lock
@@ -262,6 +271,7 @@ public class LRUQueryCache implements QueryCache, Accountable {
assert lock.isHeldByCurrentThread();
assert key instanceof BoostQuery == false;
assert key instanceof ConstantScoreQuery == false;
+
final IndexReader.CacheKey readerKey = cacheHelper.getKey();
final LeafCache leafCache = cache.get(readerKey);
if (leafCache == null) {
@@ -368,6 +378,7 @@ public class LRUQueryCache implements QueryCache, Accountable {
onEviction(singleton);
}
} finally {
+ inFlightAsyncLoadQueries.remove(query);
lock.unlock();
}
}
@@ -389,6 +400,7 @@ public class LRUQueryCache implements QueryCache, Accountable {
cache.clear();
// Note that this also clears the uniqueQueries map since mostRecentlyUsedQueries is the uniqueQueries.keySet view:
mostRecentlyUsedQueries.clear();
+ inFlightAsyncLoadQueries.clear();
onClear();
} finally {
lock.unlock();
@@ -448,13 +460,37 @@ public class LRUQueryCache implements QueryCache, Accountable {
}
}
+ // pkg-private for testing
+ // return the list of queries being loaded asynchronously
+ List<Query> inFlightQueries() {
+ return new ArrayList<>(inFlightAsyncLoadQueries);
+ }
+
@Override
public Weight doCache(Weight weight, QueryCachingPolicy policy) {
+ Weight originalWeight = getOriginalWeight(weight);
+
+ return new CachingWrapperWeight(originalWeight, policy);
+ }
+
+ // Should be used only when the user wishes to trade throughput for latency
+ // This method was not merged in the method above as to not break the existing contract
+ // advertised by QueryCache
+ @Override
+ public Weight doCache(final Weight weight, QueryCachingPolicy policy, Executor executor) {
+ assert executor != null;
+ Weight originalWeight = getOriginalWeight(weight);
+
+ return new CachingWrapperWeight(originalWeight, policy, executor);
+ }
+
+ // Get original weight from the cached weight
+ private Weight getOriginalWeight(Weight weight) {
while (weight instanceof CachingWrapperWeight) {
weight = ((CachingWrapperWeight) weight).in;
}
- return new CachingWrapperWeight(weight, policy);
+ return weight;
}
@Override
@@ -656,10 +692,21 @@ public class LRUQueryCache implements QueryCache, Accountable {
// threads when IndexSearcher is created with threads
private final AtomicBoolean used;
+ private final Executor executor;
+
CachingWrapperWeight(Weight in, QueryCachingPolicy policy) {
super(in.getQuery(), 1f);
this.in = in;
this.policy = policy;
+ this.executor = null;
+ used = new AtomicBoolean(false);
+ }
+
+ CachingWrapperWeight(Weight in, QueryCachingPolicy policy, Executor executor) {
+ super(in.getQuery(), 1f);
+ this.in = in;
+ this.policy = policy;
+ this.executor = executor;
used = new AtomicBoolean(false);
}
@@ -732,8 +779,24 @@ public class LRUQueryCache implements QueryCache, Accountable {
if (docIdSet == null) {
if (policy.shouldCache(in.getQuery())) {
- docIdSet = cache(context);
- putIfAbsent(in.getQuery(), docIdSet, cacheHelper);
+ boolean cacheSynchronously = executor == null;
+
+ // If asynchronous caching is requested, perform the same and return
+ // the uncached iterator
+ if (cacheSynchronously == false) {
+ cacheSynchronously = cacheAsynchronously(context, cacheHelper);
+
+ // If async caching failed, synchronous caching will
+ // be performed, hence do not return the uncached value
+ if (cacheSynchronously == false) {
+ return in.scorerSupplier(context);
+ }
+ }
+
+ if (cacheSynchronously) {
+ docIdSet = cache(context);
+ putIfAbsent(in.getQuery(), docIdSet, cacheHelper);
+ }
} else {
return in.scorerSupplier(context);
}
@@ -813,8 +876,23 @@ public class LRUQueryCache implements QueryCache, Accountable {
if (docIdSet == null) {
if (policy.shouldCache(in.getQuery())) {
- docIdSet = cache(context);
- putIfAbsent(in.getQuery(), docIdSet, cacheHelper);
+ boolean cacheSynchronously = executor == null;
+ // If asynchronous caching is requested, perform the same and return
+ // the uncached iterator
+ if (cacheSynchronously == false) {
+ cacheSynchronously = cacheAsynchronously(context, cacheHelper);
+
+ // If async caching failed, we will perform synchronous caching
+ // hence do not return the uncached value here
+ if (cacheSynchronously == false) {
+ return in.bulkScorer(context);
+ }
+ }
+
+ if (cacheSynchronously) {
+ docIdSet = cache(context);
+ putIfAbsent(in.getQuery(), docIdSet, cacheHelper);
+ }
} else {
return in.bulkScorer(context);
}
@@ -832,5 +910,38 @@ public class LRUQueryCache implements QueryCache, Accountable {
return new DefaultBulkScorer(new ConstantScoreScorer(this, 0f, ScoreMode.COMPLETE_NO_SCORES, disi));
}
+ // Perform a cache load asynchronously
+ // @return true if synchronous caching is needed, false otherwise
+ private boolean cacheAsynchronously(LeafReaderContext context, IndexReader.CacheHelper cacheHelper) {
+ /*
+ * If the current query is already being asynchronously cached,
+ * do not trigger another cache operation
+ */
+ if (inFlightAsyncLoadQueries.add(in.getQuery()) == false) {
+ return false;
+ }
+
+ FutureTask<Void> task = new FutureTask<>(() -> {
+ DocIdSet localDocIdSet = cache(context);
+ putIfAbsent(in.getQuery(), localDocIdSet, cacheHelper);
+
+ // Remove the key from inflight -- the key is loaded now
+ Object retValue = inFlightAsyncLoadQueries.remove(in.getQuery());
+
+ // The query should have been present in the inflight queries set before
+ // we actually loaded it -- hence the removal of the key should be successful
+ assert retValue != null;
+
+ return null;
+ });
+ try {
+ executor.execute(task);
+ } catch (RejectedExecutionException e) {
+ // Trigger synchronous caching
+ return true;
+ }
+
+ return false;
+ }
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryCache.java b/lucene/core/src/java/org/apache/lucene/search/QueryCache.java
index 94e34ee9a1e..9b829ec11e0 100644
--- a/lucene/core/src/java/org/apache/lucene/search/QueryCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/QueryCache.java
@@ -17,6 +17,8 @@
package org.apache.lucene.search;
+import java.util.concurrent.Executor;
+
/**
* A cache for queries.
*
@@ -33,4 +35,10 @@ public interface QueryCache {
*/
Weight doCache(Weight weight, QueryCachingPolicy policy);
+ /**
+ * Same as above, but allows passing in an Executor to perform caching
+ * asynchronously
+ */
+ Weight doCache(Weight weight, QueryCachingPolicy policy, Executor executor);
+
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java b/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java
index 6617c60e919..f5c58a5877b 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestIndexSearcher.java
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
@@ -189,6 +190,11 @@ public class TestIndexSearcher extends LuceneTestCase {
QueryCache dummyCache = new QueryCache() {
@Override
public Weight doCache(Weight weight, QueryCachingPolicy policy) {
+ return doCache(weight, policy, null);
+ }
+
+ @Override
+ public Weight doCache(Weight weight, QueryCachingPolicy policy, Executor executor) {
return weight;
}
};
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
index 8d8bc8c0038..4de8d3e99a8 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
@@ -30,6 +30,11 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -59,6 +64,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NamedThreadFactory;
import org.apache.lucene.util.RamUsageTester;
import org.apache.lucene.util.TestUtil;
@@ -96,6 +102,9 @@ public class TestLRUQueryCache extends LuceneTestCase {
final LRUQueryCache queryCache = new LRUQueryCache(1 + random().nextInt(20), 1 + random().nextInt(10000), context -> random().nextBoolean());
Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+ ExecutorService service = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>(),
+ new NamedThreadFactory("TestLRUQueryCache"));
final SearcherFactory searcherFactory = new SearcherFactory() {
@Override
public IndexSearcher newSearcher(IndexReader reader, IndexReader previous) throws IOException {
@@ -105,87 +114,103 @@ public class TestLRUQueryCache extends LuceneTestCase {
return searcher;
}
};
- final boolean applyDeletes = random().nextBoolean();
- final SearcherManager mgr = new SearcherManager(w.w, applyDeletes, false, searcherFactory);
- final AtomicBoolean indexing = new AtomicBoolean(true);
- final AtomicReference<Throwable> error = new AtomicReference<>();
- final int numDocs = atLeast(10000);
- Thread[] threads = new Thread[3];
- threads[0] = new Thread() {
- public void run() {
- Document doc = new Document();
- StringField f = new StringField("color", "", Store.NO);
- doc.add(f);
- for (int i = 0; indexing.get() && i < numDocs; ++i) {
- f.setStringValue(RandomPicks.randomFrom(random(), new String[] {"blue", "red", "yellow"}));
- try {
- w.addDocument(doc);
- if ((i & 63) == 0) {
- mgr.maybeRefresh();
- if (rarely()) {
- queryCache.clear();
- }
- if (rarely()) {
- final String color = RandomPicks.randomFrom(random(), new String[] {"blue", "red", "yellow"});
- w.deleteDocuments(new Term("color", color));
- }
- }
- } catch (Throwable t) {
- error.compareAndSet(null, t);
- break;
- }
- }
- indexing.set(false);
+ final SearcherFactory concurrentSearcherFactory = new SearcherFactory() {
+ @Override
+ public IndexSearcher newSearcher(IndexReader reader, IndexReader previous) throws IOException {
+ IndexSearcher searcher = new IndexSearcher(reader, service);
+ searcher.setQueryCachingPolicy(MAYBE_CACHE_POLICY);
+ searcher.setQueryCache(queryCache);
+ return searcher;
}
};
- for (int i = 1; i < threads.length; ++i) {
- threads[i] = new Thread() {
- @Override
+
+ final SearcherFactory[] searcherFactories = {searcherFactory, concurrentSearcherFactory};
+
+ for (SearcherFactory currentSearcherFactory : searcherFactories) {
+ final boolean applyDeletes = random().nextBoolean();
+ final SearcherManager mgr = new SearcherManager(w.w, applyDeletes, false, currentSearcherFactory);
+ final AtomicBoolean indexing = new AtomicBoolean(true);
+ final AtomicReference<Throwable> error = new AtomicReference<>();
+ final int numDocs = atLeast(10000);
+ Thread[] threads = new Thread[3];
+ threads[0] = new Thread() {
public void run() {
- while (indexing.get()) {
+ Document doc = new Document();
+ StringField f = new StringField("color", "", Store.NO);
+ doc.add(f);
+ for (int i = 0; indexing.get() && i < numDocs; ++i) {
+ f.setStringValue(RandomPicks.randomFrom(random(), new String[]{"blue", "red", "yellow"}));
try {
- final IndexSearcher searcher = mgr.acquire();
- try {
- final String value = RandomPicks.randomFrom(random(), new String[] {"blue", "red", "yellow", "green"});
- final Query q = new TermQuery(new Term("color", value));
- TotalHitCountCollector collector = new TotalHitCountCollector();
- searcher.search(q, collector); // will use the cache
- final int totalHits1 = collector.getTotalHits();
- TotalHitCountCollector collector2 = new TotalHitCountCollector();
- searcher.search(q, new FilterCollector(collector2) {
- public ScoreMode scoreMode() {
- return ScoreMode.COMPLETE; // will not use the cache because of scores
- }
- });
- final long totalHits2 = collector2.getTotalHits();
- assertEquals(totalHits2, totalHits1);
- } finally {
- mgr.release(searcher);
+ w.addDocument(doc);
+ if ((i & 63) == 0) {
+ mgr.maybeRefresh();
+ if (rarely()) {
+ queryCache.clear();
+ }
+ if (rarely()) {
+ final String color = RandomPicks.randomFrom(random(), new String[]{"blue", "red", "yellow"});
+ w.deleteDocuments(new Term("color", color));
+ }
}
} catch (Throwable t) {
error.compareAndSet(null, t);
+ break;
}
}
+ indexing.set(false);
}
};
- }
+ for (int i = 1; i < threads.length; ++i) {
+ threads[i] = new Thread() {
+ @Override
+ public void run() {
+ while (indexing.get()) {
+ try {
+ final IndexSearcher searcher = mgr.acquire();
+ try {
+ final String value = RandomPicks.randomFrom(random(), new String[]{"blue", "red", "yellow", "green"});
+ final Query q = new TermQuery(new Term("color", value));
+ TotalHitCountCollector collector = new TotalHitCountCollector();
+ searcher.search(q, collector); // will use the cache
+ final int totalHits1 = collector.getTotalHits();
+ TotalHitCountCollector collector2 = new TotalHitCountCollector();
+ searcher.search(q, new FilterCollector(collector2) {
+ public ScoreMode scoreMode() {
+ return ScoreMode.COMPLETE; // will not use the cache because of scores
+ }
+ });
+ final long totalHits2 = collector2.getTotalHits();
+ assertEquals(totalHits2, totalHits1);
+ } finally {
+ mgr.release(searcher);
+ }
+ } catch (Throwable t) {
+ error.compareAndSet(null, t);
+ }
+ }
+ }
+ };
+ }
- for (Thread thread : threads) {
- thread.start();
- }
+ for (Thread thread : threads) {
+ thread.start();
+ }
- for (Thread thread : threads) {
- thread.join();
- }
+ for (Thread thread : threads) {
+ thread.join();
+ }
- if (error.get() != null) {
- throw error.get();
+ if (error.get() != null) {
+ throw error.get();
+ }
+ queryCache.assertConsistent();
+ mgr.close();
}
- queryCache.assertConsistent();
- mgr.close();
+
w.close();
dir.close();
queryCache.assertConsistent();
+ service.shutdown();
}
public void testLRUEviction() throws Exception {
@@ -201,7 +226,9 @@ public class TestLRUQueryCache extends LuceneTestCase {
f.setStringValue("green");
w.addDocument(doc);
final DirectoryReader reader = w.getReader();
- final IndexSearcher searcher = newSearcher(reader);
+
+ IndexSearcher searcher = newSearcher(reader);
+
final LRUQueryCache queryCache = new LRUQueryCache(2, 100000, context -> true);
final Query blue = new TermQuery(new Term("color", "blue"));
@@ -244,6 +271,150 @@ public class TestLRUQueryCache extends LuceneTestCase {
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 LRUQueryCache queryCache = new LRUQueryCache(2, 100000, context -> true);
+
+ 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);
+
+ if (!(queryCache.cachedQueries().equals(Collections.emptyList()))) {
+ searcher.search(new ConstantScoreQuery(red), 1);
+ } else {
+ // Let the cache load be completed
+ Thread.sleep(200);
+ searcher.search(new ConstantScoreQuery(red), 1);
+ }
+
+ // Second read should hit
+ searcher.search(new ConstantScoreQuery(red), 1);
+ assertEquals(Collections.singletonList(red), queryCache.cachedQueries());
+
+ searcher.search(new ConstantScoreQuery(green), 1);
+ if (!(queryCache.cachedQueries().equals(Arrays.asList(red)))) {
+ assertEquals(Arrays.asList(red, green), queryCache.cachedQueries());
+ } else {
+ // Let the cache load be completed
+ Thread.sleep(200);
+ assertEquals(Arrays.asList(red, green), queryCache.cachedQueries());
+ }
+
+ searcher.search(new ConstantScoreQuery(red), 1);
+ assertEquals(Arrays.asList(green, red), queryCache.cachedQueries());
+
+ searcher.search(new ConstantScoreQuery(blue), 1);
+ if (!(queryCache.cachedQueries().equals(Arrays.asList(green, red)))) {
+ assertEquals(Arrays.asList(red, blue), queryCache.cachedQueries());
+ } else {
+ // Let the cache load be completed
+ Thread.sleep(200);
+ assertEquals(Arrays.asList(red, blue), queryCache.cachedQueries());
+ }
+
+ searcher.search(new ConstantScoreQuery(blue), 1);
+ assertEquals(Arrays.asList(red, blue), queryCache.cachedQueries());
+
+ searcher.search(new ConstantScoreQuery(green), 1);
+ if (!(queryCache.cachedQueries().equals(Arrays.asList(red, blue)))) {
+ assertEquals(Arrays.asList(blue, green), queryCache.cachedQueries());
+ } else {
+ // Let the cache load be completed
+ Thread.sleep(200);
+ assertEquals(Arrays.asList(blue, green), queryCache.cachedQueries());
+ }
+
+ searcher.setQueryCachingPolicy(NEVER_CACHE);
+ searcher.search(new ConstantScoreQuery(red), 1);
+ assertEquals(Arrays.asList(blue, green), queryCache.cachedQueries());
+
+ reader.close();
+ w.close();
+ dir.close();
+ service.shutdown();
+ }
+
+ public void testLRUConcurrentLoadsOfSameQuery() 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"));
+
+ ExecutorService stressService = new ThreadPoolExecutor(15, 15, 0L, TimeUnit.MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>(),
+ new NamedThreadFactory("TestLRUQueryCache2"));
+
+ IndexSearcher searcher = new IndexSearcher(reader, service);
+
+ final LRUQueryCache queryCache = new LRUQueryCache(2, 100000, context -> true);
+
+ final Query green = new TermQuery(new Term("color", "green"));
+
+ assertEquals(Collections.emptyList(), queryCache.cachedQueries());
+
+ searcher.setQueryCache(queryCache);
+ searcher.setQueryCachingPolicy(ALWAYS_CACHE);
+
+ FutureTask<Void> task = new FutureTask<>(() -> {
+ searcher.search(new ConstantScoreQuery(green), 1);
+ assertEquals(1, queryCache.inFlightQueries().size());
+ return null;
+ });
+
+ for (int i = 0; i < 5; i++) {
+ stressService.submit(task);
+ }
+
+ Thread.sleep(3000);
+ assertEquals(Arrays.asList(green), queryCache.cachedQueries());
+
+ reader.close();
+ w.close();
+ dir.close();
+ service.shutdown();
+ stressService.shutdown();
+ }
+
public void testClearFilter() throws IOException {
Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random(), dir);