cassandra-pr mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [cassandra] aweisberg commented on a change in pull request #283: CASSANDRA-14459: DynamicEndpointSnitch should never prefer latent replicas
Date Fri, 22 Mar 2019 23:29:59 GMT
aweisberg commented on a change in pull request #283: CASSANDRA-14459: DynamicEndpointSnitch
should never prefer latent replicas
URL: https://github.com/apache/cassandra/pull/283#discussion_r268302086
 
 

 ##########
 File path: src/java/org/apache/cassandra/concurrent/ScheduledExecutors.java
 ##########
 @@ -17,43 +17,111 @@
  */
 package org.apache.cassandra.concurrent;
 
-import java.util.concurrent.ExecutorService;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 
 import com.google.common.annotations.VisibleForTesting;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.utils.Pair;
+
 /**
  * Centralized location for shared executors
  */
 public class ScheduledExecutors
 {
+    private static final Logger logger = LoggerFactory.getLogger(ScheduledExecutors.class);
+
+    /**
+     * Holds all shared executors keyed by name and how long we should wait for them on shutdown
+     */
+    private static Map<Pair<String, Integer>, DebuggableScheduledThreadPoolExecutor>
executors = new ConcurrentHashMap<>();
+
+    /**
+     * Either retrieves an existing named DebuggabledScheduledThreadPoolExecutor (DSTPE),
or creates a new one if it
+     * does not exist. Note that this method is thread safe, and will create a single unique
executor per unique
+     * name passed.
+     *
+     * Use this instead of manually constructing DSTPE instances so that when Cassandra needs
to properly shutdown
+     * threadpools it can from this central location. These pools will be waited on for one
minute by default to
+     * shutdown. If you need them to be waited on longer or have more fine graind control
over construction, use
+     * {@link ScheduledExecutors#getOrCreateSharedExecutor(String, int, Function)}
+     *
+     * @param name The name of the DSTPE to get or create
+     * @return Either a freshly constructed or previously cached DebuggableScheduledThreadPoolExecutor
+     */
+    public static DebuggableScheduledThreadPoolExecutor getOrCreateSharedExecutor(String
name)
+    {
+        return getOrCreateSharedExecutor(name, 60, DebuggableScheduledThreadPoolExecutor::new);
+    }
+
+    /**
+     * Either retrieves an existing named DebuggabledScheduledThreadPoolExecutor (DSTPE),
or creates a new one if it
+     * does not exist. Note that this method is thread safe, and will create a single unique
executor per unique
+     * (name, shutdownWaitTimeInSeconds) combination passed.
+     *
+     * Use this instead of manually constructing DSTPE instances so that when Cassandra needs
to properly shutdown
+     * threadpools it can from this central location.
+     *
+     * @param name The name of the DSTPE to get or create
+     * @param shutdownWaitTimeInSeconds The number of seconds that Cassandra shutdown should
wait on this thread pool.
+     * @param create The constructor you want to use to construct the DSTPE
+     * @return Either a freshly constructed or previously cached DebuggableScheduledThreadPoolExecutor
+     */
+    public static DebuggableScheduledThreadPoolExecutor getOrCreateSharedExecutor(String
name, int shutdownWaitTimeInSeconds,
+                                                                                  Function<String,
DebuggableScheduledThreadPoolExecutor> create)
+    {
+        Pair<String, Integer> key = Pair.create(name, shutdownWaitTimeInSeconds);
+        executors.putIfAbsent(key, create.apply(name));
+        return executors.get(key);
+    }
+
     /**
      * This pool is used for periodic fast (sub-microsecond) tasks.
      */
-    public static final DebuggableScheduledThreadPoolExecutor scheduledFastTasks = new DebuggableScheduledThreadPoolExecutor("ScheduledFastTasks");
+    public static final DebuggableScheduledThreadPoolExecutor scheduledFastTasks = getOrCreateSharedExecutor("ScheduledFastTasks");
 
     /**
      * This pool is used for periodic short (sub-second) tasks.
      */
-     public static final DebuggableScheduledThreadPoolExecutor scheduledTasks = new DebuggableScheduledThreadPoolExecutor("ScheduledTasks");
+     public static final DebuggableScheduledThreadPoolExecutor scheduledTasks = getOrCreateSharedExecutor("ScheduledTasks");
 
     /**
      * This executor is used for tasks that can have longer execution times, and usually
are non periodic.
      */
-    public static final DebuggableScheduledThreadPoolExecutor nonPeriodicTasks = new DebuggableScheduledThreadPoolExecutor("NonPeriodicTasks");
+    public static final DebuggableScheduledThreadPoolExecutor nonPeriodicTasks = getOrCreateSharedExecutor("NonPeriodicTasks");
 
     /**
      * This executor is used for tasks that do not need to be waited for on shutdown/drain.
      */
-    public static final DebuggableScheduledThreadPoolExecutor optionalTasks = new DebuggableScheduledThreadPoolExecutor("OptionalTasks");
+    public static final DebuggableScheduledThreadPoolExecutor optionalTasks = getOrCreateSharedExecutor("OptionalTasks");
 
     @VisibleForTesting
     public static void shutdownAndWait() throws InterruptedException
     {
-        ExecutorService[] executors = new ExecutorService[] { scheduledFastTasks, scheduledTasks,
nonPeriodicTasks, optionalTasks };
-        for (ExecutorService executor : executors)
-            executor.shutdownNow();
-        for (ExecutorService executor : executors)
-            executor.awaitTermination(60, TimeUnit.SECONDS);
+        List<Pair<String, Integer>> executorsToWaitFor = new ArrayList<>();
 
 Review comment:
   Is this going to leave behind the shut down executor preventing it from being recreated
in the same process?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Mime
View raw message