Apache Lucene open-source search software
fix large p99 match request latency when purgeCache is running (#12801)
Davis Cook72dbfb7cf259
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.javaindex d71e3729fa5..980e1083ce8 100644--- a/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java@@ -273,25 +273,36 @@ class WritableQueryIndex extends QueryIndex {// The purge takes the write lock when creating the register log, and then when swapping out// the old query cache. Within the second write lock guard, the contents of the register log// are added to the new query cache, and the register log itself is removed.+ // The write lock must be taken inside the commit lock in order to prevent attempting to acquire+ // the write lock while a commit is in progress. If a request to acquire the write lock was to+ // be made while a commit was in progress, it would be forced to wait for the commit to give up+ // the read lock. This is not a problem in itself, but the searches that want to acquire the+ // read lock have to wait on the request to acquire the write lock in order to prevent+ // starvation, which results in searches waiting on purge waiting on commit which is problematic+ // since commit is slow and search cannot be slow.final ConcurrentMap<String, QueryCacheEntry> newCache = new ConcurrentHashMap<>();- purgeLock.writeLock().lock();- try {- purgeCache = new ConcurrentHashMap<>();- } finally {- purgeLock.writeLock().unlock();+ synchronized (commitLock) {+ purgeLock.writeLock().lock();+ try {+ purgeCache = new ConcurrentHashMap<>();+ } finally {+ purgeLock.writeLock().unlock();+ }}populator.populateCacheWithIndex(newCache);- purgeLock.writeLock().lock();- try {- newCache.putAll(purgeCache);- purgeCache = null;- queries = newCache;- } finally {- purgeLock.writeLock().unlock();+ synchronized (commitLock) {+ purgeLock.writeLock().lock();+ try {+ newCache.putAll(purgeCache);+ purgeCache = null;+ queries = newCache;+ } finally {+ purgeLock.writeLock().unlock();+ }}}