Package org.apache.cassandra.concurrent
Class DebuggableThreadPoolExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- org.apache.cassandra.concurrent.DebuggableThreadPoolExecutor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
,LocalAwareExecutorService
,ResizableThreadPool
- Direct Known Subclasses:
JMXEnabledThreadPoolExecutor
public class DebuggableThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor implements LocalAwareExecutorService
This class encorporates some Executor best practices for Cassandra. Most of the executors in the system should use or extend this. There are two main improvements over a vanilla TPE: - If a task throws an exception, the default uncaught exception handler will be invoked; if there is no such handler, the exception will be logged. - MaximumPoolSize is not supported. Here is what that means (quoting TPE javadoc): If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing. If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread. If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected. We don't want this last stage of creating new threads if the queue is full; it makes it needlessly difficult to reason about the system's behavior. In other words, if DebuggableTPE has allocated our maximum number of (core) threads and the queue is full, we want the enqueuer to block. But to allow the number of threads to drop if a stage is less busy, core thread timeout is enabled.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
-
Nested classes/interfaces inherited from interface org.apache.cassandra.concurrent.LocalAwareExecutorService
LocalAwareExecutorService.MaximumPoolSizeListener
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.RejectedExecutionHandler
blockingExecutionHandler
protected static org.slf4j.Logger
logger
-
Constructor Summary
Constructors Constructor Description DebuggableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)
DebuggableThreadPoolExecutor(int corePoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.util.concurrent.ThreadFactory factory)
DebuggableThreadPoolExecutor(java.lang.String threadPoolName, int priority)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterExecute(java.lang.Runnable r, java.lang.Throwable t)
protected void
beforeExecute(java.lang.Thread t, java.lang.Runnable r)
static DebuggableThreadPoolExecutor
createCachedThreadpoolWithMaxSize(java.lang.String threadPoolName)
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.static DebuggableThreadPoolExecutor
createWithFixedPoolSize(java.lang.String threadPoolName, int size)
Returns a ThreadPoolExecutor with a fixed number of threads.static DebuggableThreadPoolExecutor
createWithMaximumPoolSize(java.lang.String threadPoolName, int size, int keepAliveTime, java.util.concurrent.TimeUnit unit)
Returns a ThreadPoolExecutor with a fixed maximum number of threads, but whose threads are terminated when idle for too long.void
execute(java.lang.Runnable command)
void
execute(java.lang.Runnable command, ExecutorLocals locals)
static java.lang.Throwable
extractThrowable(java.lang.Runnable runnable)
int
getActiveTaskCount()
Returns the approximate number of threads that are actively executing tasks.int
getPendingTaskCount()
Returns the approximate total of tasks waiting to be executed.static void
handleOrLog(java.lang.Throwable t)
Send @param t to the default uncaught exception handler, or log it if none such is set upstatic void
logExceptionsAfterExecute(java.lang.Runnable r, java.lang.Throwable t)
Send @param t and any exception wrapped by @param r to the default uncaught exception handler, or log them if none such is set upvoid
maybeExecuteImmediately(java.lang.Runnable command)
protected static void
maybeResetLocalSessionWrapper(java.lang.Runnable r)
protected <T> java.util.concurrent.RunnableFuture<T>
newTaskFor(java.lang.Runnable runnable, T result)
protected <T> java.util.concurrent.RunnableFuture<T>
newTaskFor(java.util.concurrent.Callable<T> callable)
protected void
onFinalAccept(java.lang.Runnable task)
protected void
onFinalRejection(java.lang.Runnable task)
protected void
onInitialRejection(java.lang.Runnable task)
-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toString
-
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
-
Methods inherited from interface org.apache.cassandra.concurrent.LocalAwareExecutorService
getCompletedTaskCount, getMaxTasksQueued
-
Methods inherited from interface org.apache.cassandra.concurrent.ResizableThreadPool
getCorePoolSize, getMaximumPoolSize, setCorePoolSize, setMaximumPoolSize
-
-
-
-
Constructor Detail
-
DebuggableThreadPoolExecutor
public DebuggableThreadPoolExecutor(java.lang.String threadPoolName, int priority)
-
DebuggableThreadPoolExecutor
public DebuggableThreadPoolExecutor(int corePoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.util.concurrent.ThreadFactory factory)
-
DebuggableThreadPoolExecutor
public DebuggableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)
-
-
Method Detail
-
createCachedThreadpoolWithMaxSize
public static DebuggableThreadPoolExecutor createCachedThreadpoolWithMaxSize(java.lang.String threadPoolName)
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.- Parameters:
threadPoolName
- the name of the threads created by this executor- Returns:
- The new DebuggableThreadPoolExecutor
-
createWithFixedPoolSize
public static DebuggableThreadPoolExecutor createWithFixedPoolSize(java.lang.String threadPoolName, int size)
Returns a ThreadPoolExecutor with a fixed number of threads. When all threads are actively executing tasks, new tasks are queued. If (most) threads are expected to be idle most of the time, prefer createWithMaxSize() instead.- Parameters:
threadPoolName
- the name of the threads created by this executorsize
- the fixed number of threads for this executor- Returns:
- the new DebuggableThreadPoolExecutor
-
createWithMaximumPoolSize
public static DebuggableThreadPoolExecutor createWithMaximumPoolSize(java.lang.String threadPoolName, int size, int keepAliveTime, java.util.concurrent.TimeUnit unit)
Returns a ThreadPoolExecutor with a fixed maximum number of threads, but whose threads are terminated when idle for too long. When all threads are actively executing tasks, new tasks are queued.- Parameters:
threadPoolName
- the name of the threads created by this executorsize
- the maximum number of threads for this executorkeepAliveTime
- the time an idle thread is kept alive before being terminatedunit
- tht time unit forkeepAliveTime
- Returns:
- the new DebuggableThreadPoolExecutor
-
onInitialRejection
protected void onInitialRejection(java.lang.Runnable task)
-
onFinalAccept
protected void onFinalAccept(java.lang.Runnable task)
-
onFinalRejection
protected void onFinalRejection(java.lang.Runnable task)
-
execute
public void execute(java.lang.Runnable command, ExecutorLocals locals)
- Specified by:
execute
in interfaceLocalAwareExecutorService
-
maybeExecuteImmediately
public void maybeExecuteImmediately(java.lang.Runnable command)
- Specified by:
maybeExecuteImmediately
in interfaceLocalAwareExecutorService
-
execute
public void execute(java.lang.Runnable command)
- Specified by:
execute
in interfacejava.util.concurrent.Executor
- Overrides:
execute
in classjava.util.concurrent.ThreadPoolExecutor
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.lang.Runnable runnable, T result)
- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.util.concurrent.Callable<T> callable)
- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
-
afterExecute
protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)
- Overrides:
afterExecute
in classjava.util.concurrent.ThreadPoolExecutor
-
maybeResetLocalSessionWrapper
protected static void maybeResetLocalSessionWrapper(java.lang.Runnable r)
-
beforeExecute
protected void beforeExecute(java.lang.Thread t, java.lang.Runnable r)
- Overrides:
beforeExecute
in classjava.util.concurrent.ThreadPoolExecutor
-
getActiveTaskCount
public int getActiveTaskCount()
Description copied from interface:LocalAwareExecutorService
Returns the approximate number of threads that are actively executing tasks.- Specified by:
getActiveTaskCount
in interfaceLocalAwareExecutorService
- Returns:
- the number of threads
-
getPendingTaskCount
public int getPendingTaskCount()
Description copied from interface:LocalAwareExecutorService
Returns the approximate total of tasks waiting to be executed. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.- Specified by:
getPendingTaskCount
in interfaceLocalAwareExecutorService
- Returns:
- the number of tasks
-
logExceptionsAfterExecute
public static void logExceptionsAfterExecute(java.lang.Runnable r, java.lang.Throwable t)
Send @param t and any exception wrapped by @param r to the default uncaught exception handler, or log them if none such is set up
-
handleOrLog
public static void handleOrLog(java.lang.Throwable t)
Send @param t to the default uncaught exception handler, or log it if none such is set up
-
extractThrowable
public static java.lang.Throwable extractThrowable(java.lang.Runnable runnable)
- Returns:
- any exception wrapped by @param runnable, i.e., if it is a FutureTask
-
-