Apache Lucene open-source search software
Fix concurrent modification in LRUQueryCache.cleanUp() (#16212)
Prithvi S372b1db88a56
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex bf53046f9b2..c17c33c1935 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -257,6 +257,8 @@ Bug Fixes* GITHUB#15939: Fix thread-safety issues with NFARunAutomaton. (Dimitris Rempapis)+* GITHUB#16212: Fix concurrent modification in LRUQueryCache.cleanUp(). (Prithvi S)+* GITHUB#16278: Refactor NativeFSLockFactory lock acquisition to cleanly separate locking and file system logic. (Bharathi-Kanna)* GITHUB#16303: Add suppressed exception details when lock acquisition fails in NativeFSLockFactory. (Bharathi-Kanna)diff --git a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.javaindex cc35845f7d8..b923f3c2c81 100644--- a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java+++ b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java@@ -953,8 +953,13 @@ public class LRUQueryCache implements QueryCache, Accountable, Closeable {}// Package private for testing- Iterable<QueryCacheKey> keys() {- return cache.keySet();+ Set<QueryCacheKey> keys() {+ readLock.lock();+ try {+ return new HashSet<>(cache.keySet());+ } finally {+ readLock.unlock();+ }}Map<QueryCacheKey, QueryMetadata> getUniqueCacheKeys() {diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.javaindex 6700e6e1829..01832e7b13e 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java@@ -245,6 +245,105 @@ public class TestLRUQueryCache extends LuceneTestCase {}}+ public void testCleanUpDoesNotRaceWithCacheInserts() throws Exception {+ try (Directory dir = newDirectory();+ RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {+ // Add a document so that queries actually match and get cached.+ Document doc = new Document();+ doc.add(new StringField("color", "red", Store.NO));+ w.addDocument(doc);++ try (DirectoryReader reader = w.getReader();+ LRUQueryCache queryCache =+ new LRUQueryCache(+ Integer.MAX_VALUE, Long.MAX_VALUE, _ -> true, Float.POSITIVE_INFINITY, 4, null)) {+ IndexSearcher searcher = newSearcher(reader);+ searcher.setQueryCache(queryCache);+ searcher.setQueryCachingPolicy(ALWAYS_CACHE);++ final int numIterations = 200;+ final int numQueryThreads = 4;+ final CountDownLatch startLatch = new CountDownLatch(1);+ final CountDownLatch queryDoneLatch = new CountDownLatch(numQueryThreads);+ final AtomicReference<Throwable> error = new AtomicReference<>();+ final AtomicBoolean running = new AtomicBoolean(true);++ // Query threads: repeatedly cache queries and mark some for cleanup.+ // clearQuery() populates queriesToClean so that cleanUp() actually+ // scans cache entries instead of taking the early-return path.+ Thread[] queryThreads = new Thread[numQueryThreads];+ for (int t = 0; t < numQueryThreads; t++) {+ final int threadId = t;+ queryThreads[t] =+ new Thread("query-thread-" + threadId) {+ @Override+ public void run() {+ try {+ startLatch.await();+ for (int i = 0; i < numIterations; i++) {+ int queryId = threadId * numIterations + i;+ Query q = new TermQuery(new Term("color", "q" + queryId));+ searcher.count(q);+ if ((i & 3) == 0 && i > 0) {+ Query toClear =+ new TermQuery(new Term("color", "q" + (queryId - numQueryThreads)));+ queryCache.clearQuery(toClear);+ }+ }+ } catch (Throwable ex) {+ error.compareAndSet(null, ex);+ } finally {+ queryDoneLatch.countDown();+ }+ }+ };+ queryThreads[t].start();+ }++ // Cleanup thread: hammer cleanUp() in a tight loop while query threads run.+ Thread cleanupThread =+ new Thread("cleanup-thread") {+ @Override+ public void run() {+ try {+ startLatch.await();+ while (running.get()) {+ queryCache.cleanUp();+ }+ } catch (Throwable ex) {+ error.compareAndSet(null, ex);+ }+ }+ };+ cleanupThread.start();++ startLatch.countDown();+ assertTrue(queryDoneLatch.await(60, TimeUnit.SECONDS));+ running.set(false);+ cleanupThread.join(5000);+ assertFalse("cleanup thread should have stopped", cleanupThread.isAlive());++ for (Thread thread : queryThreads) {+ thread.join(5000);+ assertFalse("query thread should have stopped", thread.isAlive());+ }++ Throwable ex = error.get();+ if (ex != null) {+ if (ex instanceof Exception) {+ throw (Exception) ex;+ } else if (ex instanceof Error) {+ throw (Error) ex;+ } else {+ throw new RuntimeException(ex);+ }+ }++ queryCache.assertConsistent();+ }+ }+ }+@SuppressForbidden(reason = "Thread sleep")public void testLRUEviction() throws Exception {Directory dir = newDirectory();