korlov42 commented on a change in pull request #652:
URL: https://github.com/apache/ignite-3/pull/652#discussion_r805710995
##########
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:
Deduplicate the exception handling. But generic ExceptionUtils is
outside of the scope of this issue, I believe.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]