You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/02/10 16:29:50 UTC

[GitHub] [ignite-3] ygerzhedovich commented on a change in pull request #652: IGNITE-16502 fixed deadlock caused by query plan cache

ygerzhedovich commented on a change in pull request #652:
URL: https://github.com/apache/ignite-3/pull/652#discussion_r803862434



##########
File path: modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/QueryPlanCacheImpl.java
##########
@@ -18,50 +18,99 @@
 package org.apache.ignite.internal.sql.engine.prepare;
 
 import com.github.benmanes.caffeine.cache.Caffeine;
-import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.function.Supplier;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
 
 /**
  * Implementation of {@link QueryPlanCache} that simply wraps a {@link Caffeine} cache.
  */
 public class QueryPlanCacheImpl implements QueryPlanCache {
-    private final ConcurrentMap<CacheKey, QueryPlan> cache;
+    private static final long THREAD_TIMEOUT_MS = 60_000;
+
+    private final ConcurrentMap<CacheKey, CompletableFuture<QueryPlan>> cache;
+
+    private final ThreadPoolExecutor planningPool;
+
+    private final BlockingQueue<Runnable> planningTasks = new LinkedBlockingQueue<>();
+
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
 
     /**
      * Creates a plan cache of provided size.
      *
      * @param cacheSize Desired cache size.
      */
-    public QueryPlanCacheImpl(int cacheSize) {
+    public QueryPlanCacheImpl(String nodeName, int cacheSize) {
+        planningPool = new ThreadPoolExecutor(
+                4,
+                4,
+                THREAD_TIMEOUT_MS,
+                TimeUnit.MILLISECONDS,
+                planningTasks,
+                new NamedThreadFactory(NamedThreadFactory.threadPrefix(nodeName, "sqlPlan"))
+        );
+
+        planningPool.allowCoreThreadTimeOut(true);
+
         cache = Caffeine.newBuilder()
                 .maximumSize(cacheSize)
-                .<CacheKey, QueryPlan>build()
+                .<CacheKey, CompletableFuture<QueryPlan>>build()
                 .asMap();
     }
 
     /** {@inheritDoc} */
     @Override
     public QueryPlan queryPlan(CacheKey key, Supplier<QueryPlan> planSupplier) {
-        Map<CacheKey, QueryPlan> cache = this.cache;
-        QueryPlan plan = cache.computeIfAbsent(key, k -> planSupplier.get());
+        lock.readLock().lock();
 
-        return plan.copy();
+        CompletableFuture<QueryPlan> planFut;
+        try {
+            planFut = cache.computeIfAbsent(key, k -> CompletableFuture.supplyAsync(planSupplier, planningPool));
+        } finally {
+            lock.readLock().unlock();
+        }
+
+        return planFut.join().copy();
     }
 
     /** {@inheritDoc} */
     @Override
     public QueryPlan queryPlan(CacheKey key) {
-        Map<CacheKey, QueryPlan> cache = this.cache;
-        QueryPlan plan = cache.get(key);
+        lock.readLock().lock();

Review comment:
       why not write lock?




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

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org