mysql 线程池源码_MariaDB线程池源码分析_MySQL

MariaDB

bitsCN.com

MariaDB线程池源码分析

心中无码

0 前言

MySQL5.5的Enterprise版本以plugin的方式引入了thread pool,在并发请求数达到一定 数量的时候,性能相比社区版貌似有不少提高, 可以看下这个性能对比。

在引入线程池之前,MySQL支持的线程处理方式(thread_handling参数控制)有no-threads和one-thread-per-connection两种方式,no-threads方式是指任一时刻最多只有一个连接可以连接到server,一般用于实验性质。 one-thread-per-connection是指针对每个连接创建一个线程来处理这个连接的所有请求,直到连接断开,线程 结束。是thread_handling的默认方式。

one-thread-per-connection存在的问题就是需要为每个连接创建一个新的thread,当并发连接数达到一定 程度,性能会有明显下降,因为过多的线程会导致频繁的上下文切换,CPU cache命中率降低和锁的竞争 更加激烈。

解决one-thread-per-connection的方法就是降低线程数,这样就需要多个连接共用线程,这便引入了线程 池的概念。线程池中的线程是针对请求的,而不是针对连接的,也就是说几个连接可能使用相同的线程处理 各自的请求。

MariaDB在5.5引入了一个动态的线程池方案,可以根据当前请求的并发情况自动增加或减少线程数,还好 MariaDB完全开源,本文结合MariaDB的代码来介绍下thread pool的实现。这里使用的MariaDB 10.0的 代码树。

1 相关参数

MySQL的参数都写在sys_vars.cc文件下。static Sys_var_uint Sys_threadpool_idle_thread_timeout( "thread_pool_idle_timeout", "Timeout in seconds for an idle thread in the thread pool." "Worker thread will be shut down after timeout", GLOBAL_VAR(threadpool_idle_timeout), CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, UINT_MAX), DEFAULT(60), BLOCK_SIZE(1));static Sys_var_uint Sys_threadpool_oversubscribe( "thread_pool_oversubscribe", "How many additional active worker threads in a group are allowed.", GLOBAL_VAR(threadpool_oversubscribe), CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, 1000), DEFAULT(3), BLOCK_SIZE(1));static Sys_var_uint Sys_threadpool_size( "thread_pool_size", "Number of thread groups in the pool. " "This parameter is roughly equivalent to maximum number of concurrently " "executing threads (threads in a waiting state do not count as executing).", GLOBAL_VAR(threadpool_size), CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, MAX_THREAD_GROUPS), DEFAULT(my_getncpus()), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(0), ON_UPDATE(fix_threadpool_size));static Sys_var_uint Sys_threadpool_stall_limit( "thread_pool_stall_limit", "Maximum query execution time in milliseconds," "before an executing non-yielding thread is considered stalled." "If a worker thread is stalled, additional worker thread " "may be created to handle remaining clients.", GLOBAL_VAR(threadpool_stall_limit), CMD_LINE(REQUIRED_ARG), VALID_RANGE(10, UINT_MAX), DEFAULT(500), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(0), ON_UPDATE(fix_threadpool_stall_limit));

这几个参数都有相应的描述,这里再稍微具体介绍一下。

thread_pool_size: 线程池的分组(group)个数。MariaDB的线程池并不是说一整个大池子,而是分成了不同的group,而且是按照到来connection的顺序进行分组的,如第一个connection分配到group[0],那么第二个connection就分配到group[1],是一种Round Robin的轮询分配方式。默认值是CPU core个数。

t

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值