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 16:52:59 UTC

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

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



##########
File path: modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/QueryPlanCacheImpl.java
##########
@@ -18,44 +18,93 @@
 package org.apache.ignite.internal.sql.engine.prepare;
 
 import com.github.benmanes.caffeine.cache.Caffeine;
-import java.util.Map;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.IgniteInternalException;
 
 /**
  * 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;
 
     /**
      * 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,
+                new LinkedBlockingQueue<>(),
+                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());
+        CompletableFuture<QueryPlan> planFut = cache.computeIfAbsent(key, k -> CompletableFuture.supplyAsync(planSupplier, planningPool));
+
+        try {
+            return planFut.join().copy();
+        } catch (CancellationException | CompletionException ex) {

Review comment:
       Duplicate conversion exception code. 
   `CompletableFuture` is used a lot at the current project. May be we introduce `ExceptionsUtils` (aka X class) to convert the most used exception types?




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