【作者】

王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣。

【问题描述】

最近有一台MySQL5.6.21的服务器,在应用发布后,并发线程Threads_running迅速升高,达到2000左右,大量线程处于等待Opening tables、closing tables状态,应用端相关逻辑访问超时。

【分析过程】

1、16:10应用发布结束后,Opened_tables不断增加,如下图所示:

查看当时故障期间抓取的pt-stalk日志文件,时间点2019-01-18 16:29:37,Open_tables 的值为3430,而table_open_cache的配置值为2000。

当Open_tables值大于table_open_cache值时,每次新的session打开表,有一些无法命中table cache,而不得不重新打开表。这样反应出来的现象就是有大量的线程处于opening tables状态。

2、这个实例下的表,加上系统数据库下总计851张,远小于table_open_cache的2000,为什么会导致Open_tables达到3430呢

从官方文档中可以得到解释,

https://dev.mysql.com/doc/refman/5.6/en/table-cache.html

table_open_cache is related to max_connections. For example, for 200 concurrent running connections, specify a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute.

当时并发线程数达到1980,假设这些并发连接中有30%是访问2张表,其他都是单表,那么cache size就会达到(1980\*30%\*2+1980\*70%\*1)=2574

3、QPS在发布前后都比较平稳,从外部请求来看并没有突增的连接请求,但在发布后threads_running上升到接近2000的高位,一直持续。猜测是由于某个发布的SQL语句触发了问题。

4、查看当时抓取的processlist信息,有一句SQL并发访问很高,查询了8张物理表,SQL样本如下:

select id,name,email from table1 left join table2
union all
select id,name,email from table3 left join table4
union all
select id,name,email from table5 left join table6
union all
select id,name,email from table7 left join table8
where id in ('aaa');

5、在测试环境中创建相同的8张表,清空表缓存,单个session执行SQL前后对比,Open_tables的值会增加8,如果高并发的情况下,Open_tables的值就会大幅增加。

【问题重现】

在测试环境上模拟高并发访问的场景,并发1000个线程同时执行上面的SQL语句,复现了生产环境类似的现象,Open_tables迅速达到3800,大量进程处于Opening tables、closing tables状态。

【优化方案】

1、 定位到问题原因后,我们与开发同事沟通,建议优化该SQL,降低单句SQL查询表的数量或大幅降低该SQL的并发访问频率。

不过开发同事还没来的及优化,生产环境上故障又出现了。当时DBA排障时将table_open_cache从2000增加4000,CPU使用率上升,效果并不明显,等待Opening tables的问题依然存在。

2、 分析故障期间抓取的pstack信息,用pt-pmp聚合后,看到大量线程在open_table时等待mutex资源:

#0  0x0000003f0900e334 in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x0000003f0900960e in _L_lock_995 () from /lib64/libpthread.so.0
#2 0x0000003f09009576 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000069ce98 in open_table(THD*, TABLE_LIST*, Open_table_context*) ()
#4 0x000000000069f2ba in open_tables(THD*, TABLE_LIST**, unsigned int*, unsigned int, Prelocking_strategy*) ()
#5 0x000000000069f3df in open_normal_and_derived_tables(THD*, TABLE_LIST*, unsigned int) ()
#6 0x00000000006de821 in execute_sqlcom_select(THD*, TABLE_LIST*) ()
#7 0x00000000006e13cf in mysql_execute_command(THD*) ()
#8 0x00000000006e4d8f in mysql_parse(THD*, char*, unsigned int, Parser_state*) ()
#9 0x00000000006e62cb in dispatch_command(enum_server_command, THD*, char*, unsigned int) ()
#10 0x00000000006b304f in do_handle_one_connection(THD*) ()
#11 0x00000000006b3177 in handle_one_connection ()
#12 0x0000000000afe5ca in pfs_spawn_thread ()
#13 0x0000003f09007aa1 in start_thread () from /lib64/libpthread.so.0
#14 0x0000003f088e893d in clone () from /lib64/libc.so.6

这时table_cache_manager中的mutex冲突非常严重。

由于MySQL5.6.21下table_open_cache_instances参数的默认值为1,想到增大table_open_cache_instances参数,增加表缓存分区,应该可以缓解争用。

3、 在测试环境上,我们调整两个参数table_open_cache_instances=32,table_open_cache=6000,同样并发1000个线程执行问题SQL,这次等待Opening tables、closing tables的线程消失了,MySQL的QPS也从12000上升到55000。

对比相同情况下,只调整table_open_cache=6000,等待Opening tables的进程数从861下降到203,问题有所缓解,有600多个进程已经从等待Opening tables变为运行状态,QPS上升到40000左右,但不能根治。

【源码分析】

查了下代码有关table_open_cache的相关逻辑:

1、Table_cache::add_used_table函数如下,当新的连接打开的表在table cache中不存在时,打开表加入到used tables list:

bool Table_cache::add_used_table(THD *thd, TABLE *table)
{
Table_cache_element *el; assert_owner(); DBUG_ASSERT(table->in_use == thd); /*
Try to get Table_cache_element representing this table in the cache
from array in the TABLE_SHARE.
*/
el= table->s->cache_element[table_cache_manager.cache_index(this)]; if (!el)
{
/*
If TABLE_SHARE doesn't have pointer to the element representing table
in this cache, the element for the table must be absent from table the
cache. Allocate new Table_cache_element object and add it to the cache
and array in TABLE_SHARE.
*/
DBUG_ASSERT(! my_hash_search(&m_cache,
(uchar*)table->s->table_cache_key.str,
table->s->table_cache_key.length)); if (!(el= new Table_cache_element(table->s)))
return true; if (my_hash_insert(&m_cache, (uchar*)el))
{
delete el;
return true;
} table->s->cache_element[table_cache_manager.cache_index(this)]= el;
} /* Add table to the used tables list */
el->used_tables.push_front(table); m_table_count++; free_unused_tables_if_necessary(thd); return false;
}

2、每次add_used_table会调用Table_cache::free_unused_tables_if_necessary函数,当满足m_table_count > table_cache_size_per_instance &&m_unused_tables时,执行remove_table,清除m_unused_tables列表中多余的cache。其中table_cache_size_per_instance= table_cache_size / table_cache_instances,MySQL5.6的默认配置是2000/1=2000,当m_table_count值大于2000并且m_unused_tables非空时就执行remove_table,将m_unused_tables中的table cache清空。这样m_table_count就是Open_tables的值正常会维持在2000上下。

void Table_cache::free_unused_tables_if_necessary(THD *thd)
{
/*
We have too many TABLE instances around let us try to get rid of them. Note that we might need to free more than one TABLE object, and thus
need the below loop, in case when table_cache_size is changed dynamically,
at server run time.
*/
if (m_table_count > table_cache_size_per_instance && m_unused_tables)
{
mysql_mutex_lock(&LOCK_open);
while (m_table_count > table_cache_size_per_instance &&
m_unused_tables)
{
TABLE *table_to_free= m_unused_tables;
remove_table(table_to_free);
intern_close_table(table_to_free);
thd->status_var.table_open_cache_overflows++;
}
mysql_mutex_unlock(&LOCK_open);
}
}

3、增大table_cache_instances为32,当Open_tables超过(2000/32=62)时,就会满足条件,加速上述逻辑中m_unused_tables的清理,使得table cache中数量进一步减少,会导致Table_open_cache_overflows升高。

4、当table_open_cache_instances从1增大到32时,1个LOCK_open锁分散到32个m_lock的mutex上,大大降低了锁的争用。

/** Acquire lock on table cache instance. */
void lock() { mysql_mutex_lock(&m_lock); }
/** Release lock on table cache instance. */
void unlock() { mysql_mutex_unlock(&m_lock); }

【解决问题】

我们生产环境同时采取下面优化措施,问题得以解决:

1、 读写分离,增加read节点,分散master库的压力;

2、 调整table_open_cache_instances=16;

3、 调整table_open_cache=6000;

【总结】

当出现Opening tables等待问题时,

1、建议找出打开表频繁的SQL语句,优化该SQL,降低单句SQL查询表的数量或大幅降低该SQL的并发访问频率。

2、设置合适的table cache,同时增大table_open_cache_instances和 table_open_cache参数的值。

MySQL大量线程处于Opening tables的问题分析的更多相关文章

  1. mysql的线程处于System lock状态下

    The thread has called mysql_lock_tables() and the thread state has not been updated since. This is a ...

  2. MySQL线程处于Waiting for table flush的分析

      最近遇到一个案例,很多查询被阻塞没有返回结果,使用show processlist查看,发现不少MySQL线程处于Waiting for table flush状态,查询语句一直被阻塞,只能通过K ...

  3. mysql processlist 线程状态

        Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ).   checking permissions 线程是检查服务器是否具有所需的权限来执行该 ...

  4. mysql查看线程详解(转载)

    如果有 SUPER 权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的 MySQL 帐户运行的线程). mysql> show processlist; +—–+——— ...

  5. MySQL服务器线程数的查看方法详解

    本文实例讲述了MySQL服务器线程数的查看方法.分享给大家供大家参考,具体如下: mysql重启命令: ? 1 /etc/init.d/mysql restart MySQL服务器的线程数需要在一个合 ...

  6. mysql explain中的 “Select tables optimized away”

    mysql explain中的 “Select tables optimized away” http://blog.chinaunix.net/uid-10449864-id-2956845.htm ...

  7. 查看MySQL的线程

    通过两张表查看MySQL的线程:information_schema.processlist 和 performance_schema.threads processlist是information_ ...

  8. Linux ulimit和动态修改MySQL最大线程数限制

    ulimit是限制进程对资源的使用但软件资源限制变化不大,特别是process/file,分别对应nproc和nofilenproc可用 ulimit -u 查询:nofile可用 ulimit -n ...

  9. mysql后台线程详解

    1.mysql后台线程 mysql后台线程主要用于维持服务器的正常运行和完成用户提交的任务,主要包括:master thread,read thread,write thread,redo log t ...

随机推荐

  1. win7安装qt5 纯记录 水文

    去qt官网下载http://www.qt.io/看见download就点进去看看吧 目前的下载地址路径是http://www.qt.io/download-open-source选择Offline I ...

  2. hive 报错 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient

    Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...

  3. Nginx的使用(反向代理,负载均衡)

    在我目前的工作内容中,接触到Nginx的用处无外乎两点: 1. 反向代理,解决前端跨域的问题 工作内容有门户的概念,就是将各个子系统集成到门户里,在门户里面访问,这样就很容易造成跨域的问题 那么解决的 ...

  4. 使用delphi 开发多层应用(二十二)使用kbmMW 的认证管理器

    从kbmmw 4.4 开始,增加了认证管理器,这个比原来的简单认证提供了更多的功能.细化了很多权限操作. 今天对这一块做个介绍. 要做一个认证管理,大概分为以下5步: 1.  定义你要保护的资源,一般 ...

  5. js,java,jstl多分隔符分割字符串

    1.js多分隔符 分割字符串 var username = “zhao,li;wang.liu”: var arr = str.split(/;|:|,|,|./);  括号里面可以写多分割符号,中英 ...

  6. UVa 11491 Erasing and Winning (贪心,单调队列或暴力)

    题意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当然采用窗口 ...

  7. 11.字符串{a,b}的幂集[回溯递归]

    我一直在想着这个事,早晨起来五六点,躺在床上冥想.突然悟解了,真如某些书上写的,大道不过三言两语,说破一文不值.还是按照老方法,把问题最大程度的精简,现在求集合A={a,b}的幂集,只有两个元素,应该 ...

  8. day11(多线程,唤醒机制,生产消费者模式,多线程的生命周期)

    A:进程: 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. B:线程: 线程是进程中的一个执行单元,负责当前进程中程序的执 ...

  9. Last Defence (2014 西安现场赛)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=94237#problem/K Last Defence Time Limit:3000MS ...

  10. Codeforces805B. 3-palindrome 2017-05-05 08:33 156人阅读 评论(0) 收藏

    B. 3-palindrome time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...