atris commented on a change in pull request #815: LUCENE-8213: Introduce 
Asynchronous Caching in LRUQueryCache
URL: https://github.com/apache/lucene-solr/pull/815#discussion_r324497573
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
 ##########
 @@ -89,12 +92,30 @@
  */
 public class LRUQueryCache implements QueryCache, Accountable {
 
+  /** Throw this error when a query is asynchronously being loaded in the cache
+   *  and a read for the same is requested
+   */
+  private static final class AsyncQueryLoadInProgressException extends 
RuntimeException {
+
+    /**
+     * Sole constructor.
+     */
+    public AsyncQueryLoadInProgressException(String query) {
+      super(query);
+    }
+  }
+
   private final int maxSize;
   private final long maxRamBytesUsed;
   private final Predicate<LeafReaderContext> leavesToCache;
   // 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 primarily 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 = 
Collections.newSetFromMap(new ConcurrentHashMap<Query,Boolean>());
 
 Review comment:
   We add to this map without a lock when triggering a caching operation, hence 
the need for a concurrent safe set. The idea is that it is a better idea to let 
two threads request an asynchronous cache load for the same query concurrently 
than syncing the entire operation under a lock. However, this means that two 
threads might want to add the same key to `inFlightAsyncLoadQueries` in the 
same duration, so we need that operation to be idempotent and thread safe.

----------------------------------------------------------------
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