mysql thread pool_MySQL Thread pool 操作过程

Thread pool 操作过程:

thread pool 包含一定数量的 thread groups,每个groups 管理一定量的client connections,当mysql建立 connection 时,thread pool会以循环的方式(round-robin fashion)将其分配到其中的一个 thread groups中。

thread_pool_size 规定了thread groups 的数量,这样也就规定了同时可以执行多少个statement 可取值为 1—64,每个thread group 的最大线程数是4096.

thread pool 将 connection 和threads 分隔开,所以connection 和thread 和没有固定的联系。

算法:

每个 thread group 有一个监听线程,该线程监听那些从connection 发送过滤的statement,当接受到a statement 的时候,thread group 会有两个选择;立即执行或者放入队列稍后执行。

当接受到的只是一个语句,而且没有其他语句处于队列,或者当前没有语句正在执行,那么就会立即执行该语句,否则的话该语句就会放入到队列中。

当立即执行该语句时,那监控线程就会来执行次任务,此时意味着在该thread group 中临时没有线程处于监听状态,如果该语句执行的很快,那么该线程还会返回来继续处于监听状态。否则的话,thread pool 就会在需要的时候创建一个新的线程来监听。 thread pool 有一个后台线程来周期的监控thread group的状态,以免因为执行SQL时thread group 被block(比如遇到disk io 产生中断等)

thread_pool_stall_limit 参数取值范围60ms ----  6s,该值代表了该语句将要结束并且要执行下一个语句的时间间隔(线程在超过thread_pool_size时,会等待thread_pool_stall_limit  ms后创建新线程,防止线程池瞬间扩展而还来不必要的线程开销)。较小的值 允许新的线程快速的启动,这样也可以防止死锁的产生。对于那些长时间执行的statement,适合较大的值,这样防止同时执行太多的statement。

thread pool 专注于并发的短时间运行的statement ???

当statement遇到disk io时,或者row lock或者table lock的时候,会造成 thread group 变的不可用,此时 thread pool 有一个回调机制来让该group 内开启一个新的thread 来执行其他的statement,当blocked thread 返回的时候,thread pool立马使用。

对于队列: high-priority queue  和 low-priority queue

一般情况下,事务里的第一个statement会被放到 低级别的队列中,其他的语句会被放到较高级别的。 thread_pool_high_priority_connection 可以将所有statement 都放到 高级别的队列中。

如果语句针对的是非事务型的存储引擎,或者存储引擎 autocommit=1 所有的语句都会被放入到 low 级别的队列中,

当thread group 选择队列中的语句开始执行的时候,先检查高优先级的队列,然后是低优先级的队列,对于发现的语句,它会从队列中移除并开始执行。

如果某个语句在低优先级的队列中时间太长,thread pool 会将其放入到高优先级的队列中,thread_pool_prio_kickup_timer 参数来控制这个时间。

thread pool 会选择重用最活跃的threads 来充分利用CPU的缓存,这个小小的调整对性能影响很大。

下面是当thread group 有多个threads 活跃时举出的实例:

1、一个线程执行一个statement,但是时间已经超过了 stalled time,thread group 会 开启一个新的thread 来执行其他的statement,设置当第一个语句还在执行过程中。。

2、一个线程正在执行一个statement,然后被 blocked并被thread pool 检测到,这个时候此时 该 thread group 允许开启一个新的线程来执行另一个statement。

3、一个线程正在执行一个statement,然后被blocked但没有被 thread pool检测到,这个时候只有等到 stalled 时间 才能开启一个新的线程来执行新的statement

在某些线程 被blocked 但不被thread pool 检测到 但也不会阻止其他语句的执行也不会造成thread pool 死锁 这些情况是很重要的。

1、长时间运行的statement,

2、Binary log dump threads

3、Statements blocked on a row lock, table lock, sleep, or any other blocking activity that has not been reported back to the thread pool by MySQL Server or a storage engine.

以上情况,为防止死锁的产生,该statement 都会被放到stalled 类别对待,

最大的线程数是the sum of max_connections and thread_pool_size

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值