Thread pools
Thread pools are used within Diffusion™ to optimize the use of threads.
It is important to understand balance when tuning thread usage for a system. There must be sufficient thread resources required but not so many as to starve other parts of the system. At the end of the day there are only so many threads that a system can provide.
In general, when provisioning threads, separate the blocking and non-blocking activities. While it is beneficial to have more threads than cores for blocking tasks it is detrimental to the server if more threads than cores are runnable at any given time.
There are a number of places where thread pools are used within Diffusion . For more information, see Concurrency.
Configurable properties
The following key values can be configured for a thread pool to influence its behavior and use of resource:
Property | Usage |
---|---|
Core size |
The core number of threads to have running in the thread pool. Whenever a thread is required a new one is created until this number is reached, even if there are idle threads already in the pool. After reaching this number of threads then at least this number of threads is maintained within the pool. |
Maximum size |
The maximum number of threads that can be created in the thread pool before tasks are queued. If this is specified as 0, the pool is unbounded and so the task queue size value is ignored. Generally an unbounded pool is not recommended as it can potentially consume all machine resources. |
Queue size |
The pool queue size. When the maximum pool size is reached then tasks are queued. If the value is zero, the queue is unbounded. If not zero then the value must be at least 10 (it is automatically adjusted if it is not). |
Keep-alive time |
The time limit for which threads can remain idle before being terminated. If there are more than the core number of threads currently in the pool, after waiting this amount of time without processing a task, excess threads are terminated. A value of zero (the default) causes excess threads to terminate immediately after executing tasks. |
Notification handler |
A thread pool can have a notification handler associated with it to handle certain events relating to the pool. This allows for user written actions to be performed (for example, sending an email) when certain pool events (like too much task queuing) occur. See below for more details. |
Rejection handler |
A thread pool can have a rejection handler associated with it to handle a runnable task that has been rejected. This allows user written actions to handle a runnable task that can not be executed by the thread pool. See below for more details. |
Notification handler
A thread pool notification handler can be configured to act upon certain thread pool events.
These events are:
Event | Description |
---|---|
Upper threshold reached | A specified upper threshold for the pool has been reached. This means the pool size has reached the specified size. The event is notified only once and is not notified again until the lower threshold reached event has occurred. |
Lower threshold reached | A specified lower threshold for the pool has been reached after an upper threshold reached event has been notified. This means the pool size has now shrunk the specified size. |
Task rejected | The pool has rejected a task because there are no idle threads available and the task queue has filled. What happens to the rejected task depends upon the type of pool. Typically, the task is run within the thread that passes the task to the pool, which is not desirable. This is why the thread ought to be notified when it occurs. This differs from the rejection handler in that it does not expose the runnable task. This means it can be used only for notification. |
The notification handler is a user written class which must implement the ThreadPoolNotificationHandler interface in the threads Java API . The name of such a class can be configured for in-bound or out-bound thread pools or for connector thread pools in which case an instance of the class is created (and must have a no arguments constructor) when the thread pool is created.
Rejection handler
A thread pool can have a rejection handler associated with it to handle a runnable task that has been rejected.
Two rejection handlers are provided with Diffusion . These are the ThreadService.CallerRunsRejectionPolicy and ThreadService.AbortRejectionPolicy.
The ThreadService.CallerRunsRejectionPolicy executes the runnable task in the thread that tried to pass the runnable task to the thread service. This can cause inconsistencies and out of order processing.
The ThreadService.CallerRunsRejectionPolicy does not execute the task and instead generates an exception.
The rejection handler is a user written class which must implement the ThreadPoolRejectionHandler interface in the threads Java API . The name of such a class can be configured for inbound or outbound thread pools or for connector thread pools in which case an instance of the class is created (and must have a no arguments constructor) when the thread pool is created.
Adjusting the configuration
Adjust thread pools gradually. Ideally, duplicate expected maximum loads in test environment. This environment can be used to tune the thread pools to satisfy the load. Tune the thread pools so they are just able to cope with the maximum load, increasing them beyond this might degrade overall performance.
Background thread pool:
In general, the defaults suffice for the tasks assigned to the background thread pool by Diffusion . If you assign tasks to the pool yourself, consider increasing the number of threads.
Inbound thread pool:
This pool is used to handle inbound connections and messages. Increasing the thread pool allows new connections and received messages to be handled over a greater number of threads. However, much of the behavior in this pool can involve locking the clients or parts of the topic tree. This can cause lock contention that delays processing.
Due to the underlying implementation of Java NIO sockets a high rate of threads being added/removed from the incoming thread pool will result in the allocation of off-heap byte buffers. In extreme cases this can result in an out of memory exception being thrown as the server runs out of off heap allocation space.