Percona 5.6 thread pool说明及使用

本文主要简单介绍一下thread pool,因为目前网上的文章都只是针对thread pool的原理说明,没有实际的使用示范。因此本会针对thread pool的使用做一些说明。

欢迎转载,请注明作者、出处。
作者:张正
blog:http://space.itpub.net/26355921 
QQ:176036317
如有疑问,欢迎联系。


Thread pool是什么:

MySQL是每个连接上来都要创建一个线程来执行语句。这样每一个新的连接进来即会创建一个新的线程,这种动作对MySQL本身压力比较大。Threadpool是提供一种线程代理的模型执行每个连接的语句。而MySQL内部维护一个可能接受的线程总数,减少线程太多在CPU切换等方面的压力。

使用Threadpool的好处:

1.使用线程代理的模型在连接数已知可控的情况下,提前创建好过线程并利用操作系统的threadpool技术维护,能减少很多开销
2.尽可能的使用操作系统的线程方面的管理,把线程管理开销降到最低
3. 利用thread_pool_max_threads控制资源使用

使用方法:

目前支持threadpool的有MariaDB, Percona, 官方MySQL 5.5的一个收费plugin功能。percona与mariadb配置启用:

在my.cnf 中添加参数:

[mysqld]
 
#thread pool
 
thread_handling=pool-of-threads
thread_pool_max_threads=100       #设置thread pool线程的上限值(同时running的最大线程数为100)
thread_pool_size=50               #thread pool中线程数的下限值

mysql> show variables like 'thread%';
+-------------------------------+-----------------+
| Variable_name                 |      Value      |
+-------------------------------+-----------------+
| thread_cache_size             |         28      |
| thread_concurrency            |         10      |
| thread_handling               |pool-of-threads  |
| thread_pool_high_prio_mode    | transactions    |
| thread_pool_high_prio_tickets | 4294967295      |
| thread_pool_idle_timeout      |    60           |
| thread_pool_max_threads       |    120          |
| thread_pool_oversubscribe     |      3          |
| thread_pool_size              |     50          |
| thread_pool_stall_limit       |    500          |
| thread_stack                  | 262144          |
| thread_statistics             | OFF             |
+-------------------------------+-----------------+

Thread_pool_stall_limit:以10ms为单位,默认60ms范围值4-600,每个语句的时间片大小,一旦耗完则挂起将CPU交于其他线程

空闲时:
mysql> show global status like 'thread%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Threadpool_idle_threads | 51    |
| Threadpool_threads      | 52    |
| Threads_cached          | 0     |
| Threads_connected       | 22    |
| Threads_created         | 148   |
| Threads_running         | 1     |
+-------------------------+-------+
6 rows in set (0.00 sec)
系统中22个线程数(Threads_connected)为zabbix的连接,虽然目前总连接数为22,但是thread pool中的线程数(Threadpool_threads)为52。
该值的最小值决定于参数thread_pool_size 的值。
目前running线程数为1,空闲线程数为 51.


sysbench增加连接数:
使用sysbench创建1000个线程进行读写操作,同时还有22个zabbix的连接,共1022个连接:
sysbench --num-threads=1000 --test=oltp --mysql-user=root --mysql-password=root --mysql-table-engine=innodb  --init-rng=on --oltp-table-size=19390000 --max-time=$RT --max-requests=20000 --mysql-db=test --mysql-socket=/var/lib/mysql/mysql.sock run > sysbench.log

mysql> show global status like 'thread%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Threadpool_idle_threads | 46    |     -->目前thread pool中还剩余46个空闲thread
| Threadpool_threads      | 120   |     -->thread pool中允许同时running的最大线程数
| Threads_cached          | 0     |
| Threads_connected       | 1022  |     -->目前数据库一共有1022个连接
| Threads_created         | 476   |
| Threads_running         | 87    |     -->目前正在运行的线程数为87
+-------------------------+-------+
6 rows in set (0.37 sec)

数据库中 Threadpool_idle_threads(46) + Threads_running(87) 之和(133) 大致是与 Threadpool_threads(120) 的值相等的,
而 Threadpool_threads 的最大值是受限于 参数thread_pool_max_threads的值。

系统中总共连接数有1022,而running的线程数只有87,还有46空闲,是因为操作系统的CPU已经耗尽。








请使用浏览器的分享功能分享到微信等