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/11 14:26:15 UTC

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

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



##########
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();

Review comment:
       I wanted to make clearing the cache and task queue an atomic operation, but in fact we should not clean the task queue. So I removed this 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