MySQL thread pool (THREAD POOL) principle

MySQL Common (currently online use) of thread scheduling approach is one-thread-per-connection (one thread per connection), server to create a thread for each connection to the service, the connection is broken, the thread exit or enter thread_cache (depending on the number of threads thread_cache settings and the current system has the cache), the benefits of one-thread-per-connection scheduling is simple, and can ensure a smaller response time before the system does not encounter a problem, more suitable for active long connecting application scenario, but in a large amount or high concurrency short connection, one-thread-per-connection needs to be created / scheduling large number of threads, results in higher cost of the context-switch, so that the system performance

To solve this problem, Oracle and MariaDB were introduced threadpool program, currently Oracle's threadpool implemented as a plugin mode, and only added to the Enterprise version, not released the code, MariaDB threadpool introduced in version 5.5, we have been closely following the community dynamics and the first time tested the MariaDB threapool performance, and found some of these problems, such as: like to take advantage of the thread pool, you need to try to control the number of threads in the thread pool, otherwise it will degenerate into a one-thread-per-connection, If strict control of data thread pool thread, deadlocks may occur on schedule, percona further optimize the thread pool performance after transplantation MariaDB threadpool achieved by the introduction of a priority queue a good solution to this problem, tested the effect is obvious, we therefore this characteristic port to AliMySQL the
achieved profile
1, threadpool the worker thread processing unit is a statement, instead of a connection one-thread-per-connection corresponding to; when the worker thread has been processed a sql a connecting transmitted , A connection is not immediately transmitted Sql second, back to the worker thread to service other sql connected to the transmission, and therefore higher efficiency worker thread, the number of threads requires fewer
2, is essentially a producer on threadpool - consumer model, in order to reduce the competition , ThreadPool is divided into N group (n-default cpu cores), sql sent over the connection assigned to a different group according to the connection id, so that a single connection to send all sql is worker thread to handle a group of of
3, each group has two task queues, namely priority queue and a normal queue, if a transaction where sql is turned on, the task will be placed in a priority queue, or into the normal queue, worker thread priority from the priority queue take the task execution, when the priority queue is empty from the queue to perform common tasks take, this can ensure that a transaction has been opened priority to be implemented, so as to release resources acquired as soon as possible (primarily lock), can effectively reduce the response time, do not and avoid deadlock on the schedule (a and B are assigned to a different group, a is the transaction has been opened, and obtained a lock, may not be scheduled for execution immediately, B transaction relies a transaction releases lock resources, but first get to a scheduling)
4, each group for each worker thread the same position, if you encounter the task queue is empty, the thread calls take epoll_wait batch task
5, threadpool additional thread creates a timer every so often to check over all group, if found abnormal group (blockage / timeout / worker thread number is not enough), timely wake-up thread
on the MySQL line Scheduling program interface and threadpool implementation details can be referenced before I wrote two blog: mysql thread sheduler source code analysis, mariadb 5.5 threadpool source code analysis

+-------------------------------+--------------+
| Variable_name | Value |
+-------------------------------+--------------+
| thread_pool_high_prio_mode | transactions |
| thread_pool_high_prio_tickets | 4294967295 |
| thread_pool_idle_timeout | 60 |
| thread_pool_max_threads | 100000 |
| thread_pool_oversubscribe | 3 |
| thread_pool_size | 24 |
| thread_pool_stall_limit | 500 |
+-------------------------------+--------------+
7 rows in set (0.00 sec)


threadpool parameters> thread_pool_high_prio_mode
has three values: Transactions / statements / none
Transactions (default): Use a priority queue and a normal queue, the transaction has been opened for the statement, put the priority queue, the queue or put common
statements: use only priority queue
none: only a normal queue and statements essentially the same, except that a queue is
> thread_pool_high_prio_tickets
ranging from 0 to 4294967295, when (thread_pool_high_prio_mode = transactions) opened the priority queue mode, each Ci is connected Maximum thread_pool_high_prio_tickets into the priority queue, and then into the normal queue, the default is 4294967295
> thread_pool_idle_timeout
worker thread maximum idle time, in seconds, will quit after the limit, default 60
> thread_pool_max_threads
maximum number of threads in threadpool, all group in the total number of worker threads can not continue to create more threads after the limit is exceeded, default 100000
> thread_pool_oversubscribe
a number of threads in the group overload limit, when a group of ultra-thread count After the time limit, continued to create a worker thread will be delayed, default 3
> thread_pool_size
The number of Group threadpool, cpu cores default, when the server starts automatically calculated
> thread_pool_stall_limit
Timer thread detection interval, in milliseconds, default 500

-----------------------------------------------------------------------------------------------------------------------------------------------


Thread pool is a core function MySQL5.6 Enterprise Edition, for server applications, whether web apps or DB service, high concurrent requests is always not open around the topic. The application initiates a database operation, the entire application is not a small overhead, from the beginning to establish a connection, CPU division to give it a certain thread stack, followed by user authentication, establish context information, the final completion of the request, close the connection, while freeing resources that can be called on the second level of the process, when there are a large number of concurrent access requests, some with the continuous creation and release of resources, resulting in resource utilization is low, reducing the quality of service. Thread pool is a common technique, through a certain number of threads created in advance, when a request arrives, the thread pool thread to provide a distribution service, after the end of the request, the thread went to service other requests. In this way, avoid the frequent create threads and memory object and release, reducing the degree of concurrency server, reducing the competitive context switching and resources to improve the efficiency of resource use. Thread pool essence of all services are bit of resource efficiency, and achieve much the same way. This paper illustrates the principle of realization MySQL thread pool.


mysql thread pool, is the embodiment of I / O multiplexing. As for I / O multiplexing is what, please see http://blog.csdn.net/stubborn_cow/article/details/50247269


Before MySQL5.6 appearance, the way MySQL handles the connection is One-Connection-Per-Thread, that is, for each database connection, MySQL-Server creates a separate thread service, after the end of the request, destroying threads. Again a connection request, and then create a connection, after the end of destruction. In this way, high concurrency, cause frequent thread creation and release. Of course, the thread-cache, we can be cached thread, for the next use, to avoid the frequent create and release of the problem, but can not solve the problem of the high number of connections. One-Connection-Per-Thread mode with the number of connections surge, resulting in the need to create the same number of service threads, high concurrent threads means that high memory consumption, more context switching (cpu cache hit rate decreases) and more competition for resources, resulting in service jitter. With respect to the One-Thread-Per-Connection mode, a thread corresponding to a connection, Thread-Pool implementation, the minimum unit is the threading statement (statements), a thread can handle multiple requests connection. In this way, ensure the full utilization of hardware resources under (a reasonable set of thread pool size), the number of connections to avoid an instant surge server cause jitter.
Scheduling manner
MySQL-Server supports three kinds of connection management, including No-Threads, One-Thread- Per-Connection and Pool-Threads. No-Threads represents the connection using the main thread, do not create additional threads, this approach is mainly used for debugging; One-Thread-Per-Connection is to appear before the thread pool the most common way, each connection create a thread for the service; pool-threads is the thread pool mode as discussed herein. MySQL-Server through a set of pointers to functions simultaneously supports three connection management, for a particular embodiment, the function pointer is set to a specific callback function, by way of connection management thread_handling parameter control, as follows:
IF (thread_handling <= SCHEDULER_ONE_THREAD_PER_CONNECTION)
one_thread_per_connection_scheduler (thread_scheduler,
& max_connections,
& the connection_count);
the else IF (thread_handling == SCHEDULER_NO_THREADS)
one_thread_scheduler (thread_scheduler);
the else
pool_of_threads_scheduler (thread_scheduler, & max_connections, & the connection_count);
connection management process
1. port via poll monitor the mysql connection request
2. after a connection is received, the interface call accept, create a communication Socket
3. initialization thd instance, VIO object, etc.
4. set according thread_handling embodiment, scheduler instance initialization function pointers thd
5. call scheduler function specific add_connection new connections
below code shows scheduler_functions thread pool implementation templates and template callback function, this is the core of a variety of connection management.
scheduler_functions struct
{
uint max_threads;
uint * the connection_count;

ulong *max_connections;

bool (*init)(void);

bool (*init_new_connection_thread)(void);
void (*add_connection)(THD *thd);
void (*thd_wait_begin)(THD *thd, int wait_type);
void (*thd_wait_end)(THD *thd);
void (*post_kill_notification)(THD *thd);
bool (*end_thread)(THD *thd, bool cache_thread);
void (*end)(void);
};

static scheduler_functions tp_scheduler_functions=
{
0, // max_threads
NULL,
NULL,
tp_init, // init
NULL, // init_new_connection_thread
tp_add_connection, // add_connection
tp_wait_begin, // thd_wait_begin
tp_wait_end, // thd_wait_end
tp_post_kill_notification, post_kill_notification //
NULL, // end_thread
tp_end // End
};
parameters thread pool
1.thread_handling: indicates thread pool model.
2.thread_pool_size: indicates the number of thread pool group is generally set to the current number of CPU cores. Ideally, a group of an active worker, achieve full utilization of the CPU.
3.thread_pool_stall_limit: a timer thread periodically checks whether the group "stagnation" parameter indicates the interval detection.
4.thread_pool_idle_timeout: When a worker will automatically exit after an idle period to ensure worker threads in the thread pool in case satisfy the request, maintaining a relatively low level.
5.thread_pool_oversubscribe: This parameter is used to control the number of threads "OC" on the CPU core. The parameter settings free listen thread count.
6.threadpool_high_prio_mode: priority mode represents queue.
Thread pool to achieve
the above described how Mysql-Server connection management, implementation framework described in this section focuses on the thread pool, as well as key interfaces. Figure 1

Each box represents a group of green, the number of group is determined by thread_pool_size parameters. Each group contains a priority queue and a normal queue, a listener thread and comprising a plurality of worker threads, listener thread and the worker thread can be dynamically converted, determined by the number of worker thread workloads while affected thread_pool_oversubscribe provided. In addition, there is a whole thread pool thread timer monitoring group, to prevent the group "stagnation."
Key Interface
1. tp_add_connection [Processing new connection]
1) create a connection object
2) thread_id% group_count group allocated to which connection is determined according to
3) corresponding to the connection into a queue group
4) If the number of currently active threads is 0, is created a worker thread
2. worker_main [worker]
1) acquisition request call get_event
2) If there is a request, the call processing handle_event
3) otherwise, indicates that the queue has been no request to exit end.
3. get_event [acquiring request]
1) obtaining a connection request
2), if present, returns immediately, ending
3) If there is no listener within Group case, the thread is converted to listener thread blocked waiting
4) if the listener exists, then threads are waiting for the head of the queue
5) thread to sleep a specified time (thread_pool_idle_timeout)
6) If you still can not wake up, is timed out, the end of the thread, the end of the exit
7) Otherwise, it indicates the queue connection request comes in, jump 1
Note: before getting a connection request, determines whether the current number of active threads than
thread_pool_oversubscribe + 1, if exceeded, will thread into hibernation.
4. handle_event [Processing Request]
1) determines whether the login authentication connection, if not, then the login authentication
2) information related thd Example
3) acquires network packets, the analysis request
4) call processing request do_command function loops
5) Get thd examples of the socket handle, the handle is determined whether the listener in the list epoll
6) if not, the call association epoll_ctl
7) end
5.listener [listener thread]
1) calls for a socket to listen for group epoll_wait associated blocking wait
2) if the request comes, the recovery from blocking
3) according to the priority level of the connection, is determined into a normal queue or priority queue
4) determines whether the task queue is empty
5) if the queue is empty, then the listener is converted to a worker thread
6) If there is no active threads within the group, then wake up a thread
NOTE: this epoll_wait listen to all socket connections within the group, and then listen to a connection
request to push queue, worker thread gets the task from the queue, and then executed.
6. timer_thread [monitoring thread]
1) If there is no listener threads, and recent events have not io_event
2) create a wake or create a worker thread
3) If the processing request is not the most recent group, and there are queues request,
4) represents a group has stall, the wake-up or create a thread
NOTE: the role of the timer is to avoid the stall state in a group
7.tp_wait_begin [process enters a wait state]
1) the number of active threads minus one, wait a few threads, plus one
2) If the number of active threads is zero, and the task queue is not empty, or there is no listening thread, then
create a thread 3) wake or
Note: waiting_threads there's a thread is idle thread, the thread is not waiting for the so-called empty
idle threads are threads ready to handle the task, and it is because the waiting thread waiting on a lock, or wait for io operations
threads for tasks such as can not handle.
8.tp_wait_end [standby state End Process]
1) connection waiting state is set to false
2) the number of active threads, plus 1, minus number of threads waiting a
thread pool and connection pool
Connection pooling is usually implemented in the Client-side, it refers to the application (?? client) to create a certain pre-created connections using these connection services to end customers all DB requests. If a certain moment the number of connections, number of requests is less than the idle DB, the request needs to be queued, waiting for free connection processing. Can be connected by a multiplexed connection pool, created to avoid frequent connection and release, thereby reducing the average response time of a request, and the request is busy, the request is queued, the application can buffer the impact of the DB. Thread pool implementation the server side, the service threads DB request to create a certain amount with respect to embodiment one-conection-per-thread of a thread serving a connection, the smallest unit of the thread pool and services that statement, i.e., one thread may correspond to multiple two active connections. By the thread pool, the number of service threads server-side can be controlled to a certain extent, reduce the consumption of system resources competition and thread context switching brings, but also to avoid high concurrency problems arise due to the high number of connections. Connection pooling and thread pools complement each other, can be reduced by connecting pool creation and release of connections, increase the average response time of the request, and can be well controlled application of a number of DB connections, but can not control the number of connections to the size of the entire application cluster, thus resulting in a high number of connections, it can well cope with the high number connected by a thread pool, to ensure the server side to provide a stable service. Shown, each web-server terminal 2 maintains the connection pool of three, each for connecting a worker exclusively db-server is not actually connected to the pool, but may be shared with other connections. It is assumed that only three db-server group, each group only a worker, worker handling each request for connection of the two.

FIG 2 (thread connection pooling FIG pool frame)
thread pool optimization
1. priority queue
since a group will also handle multiple connections, but not connected to the plurality of peers. For example, some are connected to the first transmission request; and some connected to the corresponding transaction has been opened, and a holding section lock resources. In order to reduce lock contention for resources, should the latter than the former is clearly a priority, in order to achieve the release of a lock resource as soon as possible. Therefore, in the group inside, you can add a priority queue, already holds the lock of the connection request initiated in the priority queue, work queue threads get first priority task execution.
2. Large Query Processing
assume a scenario, which is connected to a group are big query, then the number of worker threads inside the group will soon reach thread_pool_oversubscribe parameter values for subsequent connection request, it will not respond in time ( no more connection to handle), this time the group took place stall. By the previous analysis know, timer thread will periodically check the situation and create a new worker thread to handle the request. If the query comes from a long service request at this time all group are facing this problem, when the host may be due to overload, lead to hang live. This case the thread pool itself powerless, because the source may be complicated by bad SQL, SQL or did not take the lead on the implementation plan, through other methods, such as SQL or SQL water level limiting filter means may be emergency treatment. However, there is another situation in which dump task. Many downstream depends on the original data in the database, usually by dump command data pulled downstream, and this dump task usually takes longer, so it can be considered a big query. If the dump task concentrated in a group, and lead to other normal service request can not respond immediately, this can not be tolerated, because the database and no pressure, just because the use of the thread pool policy was a result of not timely respond to the request, in order to to solve this problem, we will group the task of threading dump thread_pool_oversubscribe not included in the cumulative value, to avoid this problem.

Guess you like

Origin www.cnblogs.com/dbalightyear/p/11237073.html