mysql 建表时at line 1_MySQL的STOP SLAVE语句堵塞源码剖析

本文分析了在MySQL中执行STOP SLAVE语句导致的阻塞问题,通过堆栈分析发现线程在等待信号量,可能由于SQL线程未正确关闭。在循环中,线程每2秒发送一个SIGUSR1信号,直到达到rpl_stop_slave_timeout(默认1年)才会退出。解决方案是调整slave_checkpoint_period以确保在条件变量等待时间内能被虚假唤醒,降低阻塞风险。
摘要由CSDN通过智能技术生成

背景

维护MySQL不可避免要经常使用stop slave语句,然而总有那么些意外,导致了hang,死的莫名奇妙。情况好点,也就等个好几十秒,情况差点直接就死锁了。对于金融级MySQL来说,几十秒的业务受损时间也是相当严重的。刚好,这几天又发生了一起hang,在同事的一次凌晨切换演习中,执行stop slave阻塞了60~80s,恰巧同事也在阻塞恢复前30s左右保留了现场的pstack。于是,又开启了心血来潮的探索。

堆栈分析

  • debug日志分析

首先对测试环境中stop slave语句的debug日志进行采集分析,大致内容如下所示:

1223 T@16: | | | | | >stop_slave(THD)
1224 T@16: | | | | | | >Multisource_info::get_num_instances
1225 T@16: | | | | | | <Multisource_info::get_num_instances 254
1226 T@16: | | | | | | >stop_slave(THD, Master_info, bool, bool
...
1234 T@16: | | | | | | | THD::enter_stage: 'Killing slave' /data/mysql-5.7.24/sql/rpl_slave.cc:10200
...
1241 T@16: | | | | | | | >terminate_slave_threads
1242 T@16: | | | | | | | | info: Terminating SQL thread
1243 T@16: | | | | | | | | >terminate_slave_thread
...
1251 T@16: | | | | | | | | info: Flushing relay-log info file.
1252 T@16: | | | | | | | | THD::enter_stage: 'Flushing relay-log info file.' /data/mysql-5.7.24/sql/rpl_slave.cc:1685
...
1276 T@16: | | | | | | | | | | | | | | >open_and_process_table
1277 T@16: | | | | | | | | | | | | | | | tcache: opening table: 'mysql'.'slave_relay_log_info'  item: 0x7f1e183cbb70
...
1721 T@16: | | | | | | | | <Relay_log_info::flush_info 2533
1722 T@16: | | | | | | | | info: Terminating IO thread
...
1734 T@16: | | | | | | | | THD::enter_stage: 'Flushing relay log and master info repository.' /data/mysql-5.7.24/sql/rpl_slave.cc:1726
...
1761 T@16: | | | | | | | | | | | | | | >open_and_process_table
1762 T@16: | | | | | | | | | | | | | | | tcache: opening table: 'mysql'.'slave_master_info'  item: 0x7f1e183cbb60
...
2772 T@16: | | | | | | | <terminate_slave_threads 1749
2773 T@16: | | | | | | | >unlock_slave_threads
2774 T@16: | | | | | | | <unlock_slave_threads 341
2775 T@16: | | | | | | | info: 0x60bfee0.unlock()
2776 T@16: | | | | | | <stop_slave(THD, Master_info, bool, bool 10272

总结:

  1. 进入Killing slave状态
  2. 进入Flushing relay-log info file状态,更新mysql.slave_relay_log_info表,关闭SQL线程
  3. 进入Flushing relay log and master info repository状态,更新mysql.slave_master_info表,关闭IO线程
  • 堆栈分析
执行 stop slave的用户线程
# python pstack_analytic.py -i termin -f stop_slave_hang_pstack.log   
Thread: 28
#0  0x00007f96ff3d5cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x0000000000f0eff3 in native_cond_timedwait (abstime=0x7f8cab9fc3f0, mutex=0x93baa528, cond=0x93baa628) at /export/home/pb2/build/sb_0-33648028-1555164244.06/mysql-5.7.26/include/thr_cond.h:129
#2  my_cond_timedwait (abstime=0x7f8cab9fc3f0, mp=0x93baa528, cond=0x93baa628) at /export/home/pb2/build/sb_0-33648028-1555164244.06/mysql-5.7.26/include/thr_cond.h:182
#3  inline_mysql_cond_timedw
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值