Feature Description

A thread pool consists of multiple thread groups and one timer thread. The number of thread groups is the number of logical CPU cores on the server by default. You can modify the number of thread groups in the configuration file or by running related commands. For details, see thread_pool_size. User connections are allocated to the corresponding thread group in polling mode. All query requests in the connections are processed by the bounded thread group. When a client connection sends an SQL statement, the thread group allocates a worker thread to the connection for processing. After the SQL statement is executed, the thread group reclaims the worker thread. The number of worker threads is controlled within a proper range by a policy to ensure high service performance.
This feature supports the following functions:
- The number of thread groups can be system-defined or user-defined.
- For better performance, priority queues and common queues process transactions, lock connections, and common query statement connections separately. For details, see thread_pool_high_prio_mode and thread_pool_high_prio_tickets.
- The number of worker threads is dynamically scaled to ensure that the number of running threads is within a proper range for efficient processing.
- Thread pool lockups or starvation are prevented.
- An extra connector is used for local Unix socket connections.
- Four status information tables are added to information_schema to monitor the thread pool status in real time.
For details about the function configuration, see Parameters.
Each thread group corresponds to:
- A pollfd, which is the poll descriptor returned by epoll_create.
- Zero or one listener thread. epoll_wait waits for network readable events.
- A common queue that stores connection objects (including TPC connection information and SQL execution context status) with network readable events to be processed by worker threads.
- A priority queue that stores connection objects that have network readable events and are in the middle of a transaction. Such connections will be processed by the worker threads first.
- Zero or more worker threads that obtain connection objects with network readable events, process login verification of connections, receive and execute SQL statements, and return results. If there is no listener thread in a thread group, the first idle worker thread that enters the sleep state becomes a listener thread.
- A waiting queue. When a worker thread has no task to process, it enters the waiting state and is placed in the waiting queue. After the thread is woken up by an external signal or is automatically woken up due to waiting timeout, the thread status is re-marked as active to process tasks or the thread just exits.
- A mutex that protects resources in a thread group from being simultaneously accessed by multiple threads.
All thread groups share one timer thread. A timer thread detects whether a task in a thread group is suspended, that is, whether no new task is generated in a period of time or no task is consumed when the task queue is not empty.