成都创新互联网站制作重庆分公司

利用sysschema解决一次诡异的语句hang问题

沃趣科技  罗小波


导读
1、故事背景
2、复现与剖析
3、解决方法
4、总结

1、故事背景

  • 在开始之前,先列出数据库的运行环境信息
    • 操作系统:redhat 7.2 x8_64
    • 文件系统:xfs
    • 数据库版本:MySQL 5.7.17
    • 主机配置:
      * CPU:32 vcpus
      * 内存:128 G
      * 磁盘:单盘intel SSD 320G(只存放mysql的data和binlog)
    • 主要配置参数设置:innodb_buffer_pool_size = 96G,innodb_log_file_size = 2G,innodb_flush_method = O_DIRECT,sync_binlog = 1,innodb_flush_log_at_trx_commit = 1,innodb_thread_concurrency = 32,innodb_io_capacity = 20000,innodb_read_io_threads = 4,innodb_write_io_threads = 12,transaction_isolation = READ-COMMITTED,performance_schema=ON,binlog_rows_query_log_events=ON
    • 该实例接入了高可用机制:HA心跳探测机制60S内发现实例持续探测失败时(每5秒探测一次),直接尝试关闭探测失败的MySQL实例和主机,进行高可用切换
  • 故事情节:
    • 前些天某客户反馈一个诡异的问题,一个MySQL实例一会可访问,一会不可访问,查看相关日志发现该实例反复执行高可用切换,在进行初略排查时,通过监控发现服务器故障时刻磁盘负载都较高,但是并没有完全用满。而在高可用切换之前,数据库中跑着一些insert…select语句和大量心跳检测语句,而心跳检测语句与业务表是独立的,且心跳检测语句只固定更新一行数据,怎么会被阻塞一堆心跳检测语句呢 ?经过了一番折腾总算把原因找到了,具体过程请看下文!

2、复现与剖析

  • 经过初略的分析,虽然业务SQL和心跳SQL操作的是不同的表,不会出现锁等待问题,但是从show processlist;的结果来看,业务SQL执行时间最长,而且不断有新的心跳语句被阻塞(当然,这里要先关掉HA的切换机制,否则待会复现操作时,过了60S就可能被切换了),看起来阻塞心跳SQL的很可能就是业务SQL。而且这些业务SQL操作的数据量多达3千万行(因为是insert…select语句,所以从慢日志或者innodb_trx表、sys.session视图中查看到的数据量实际上多达6KW,翻倍了)。但是两者之间有具体有什么关联无法直观地看出来。我们按照如下步骤进行了复现:

    成都创新互联公司专注于菏泽企业网站建设,响应式网站建设,购物商城网站建设。菏泽网站建设公司,为菏泽等地区提供建站服务。全流程定制制作,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

  • 2.1. 首先按照线上环境标准准备好复现环境,搭建了一台测试机

  • 2.2. 创建一张心跳表,插入一行数据

点击(此处)折叠或打开

  1. root@localhost :test:33: > CREATE TABLE `xx_heartbeat` (
  2.       `server_id` int(10) unsigned NOT NULL,
  3.       `hb_time` datetime NOT NULL,
  4.       PRIMARY KEY (`server_id`)
  5.     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
  6.     root@localhost :test:33: > insert into xx_heartbeat values(@@server_id,now());
  7.     root@localhost :test:33: > select * from xx_heartbeat;
  8.     +-----------+---------------------+
  9.     | server_id | hb_time |
  10.     +-----------+---------------------+
  11.     | 3306103 | 2017-10-11 12:33:26 |
  12.     +-----------+---------------------+
  13.     2 rows in set (0.00 sec)
2.3. 打开performance_schema中的等待事件采集(为了省事,这里使用sys schema下的函数操作,而不使用UPDATE语句直接修改performance_schema的配置表)

点击(此处)折叠或打开

  1. root@localhost :test:34: > use sys
  2.     Database changed
  3.     root@localhost : sys:36: > call ps_setup_enable_instrument('wait');
  4.     +-------------------------+
  5.     | summary |
  6.     +-------------------------+
  7.     | Enabled 303 instruments |
  8.     +-------------------------+
  9.     1 row in set (0.04 sec)
  10.     Query OK, 0 rows affected (0.04 sec)
  11.     root@localhost : sys:36: > call ps_setup_enable_consumer('wait');
  12.     +---------------------+
  13.     | summary |
  14.     +---------------------+
  15.     | Enabled 3 consumers |
  16.     +---------------------+
  17.     1 row in set (0.01 sec)
  18.     Query OK, 0 rows affected (0.01 sec)
2.4. 开4个MySQL会话连接,依次对4张不同的表执行insert…select操作,表数据量都为3KW(测试数据为使用sysbench造的4张3KW数据的表)

点击(此处)折叠或打开

  1. # 会话1
  2.     ADMIN@127.0.0.1 : (none):15: > use sbtest;
  3.     Database changed
  4.     ADMIN@127.0.0.1 : sbtest:37: > insert sbtest1(k,c,pad) select k,c,pad from sbtest1;
  5.     # 会话2
  6.     ADMIN@127.0.0.1 : (none):15: > use sbtest;
  7.     Database changed
  8.     ADMIN@127.0.0.1 : sbtest:37: > insert sbtest3(k,c,pad) select k,c,pad from sbtest3;
  9.     # 会话3
  10.     ADMIN@127.0.0.1 : (none):18: > use sbtest;
  11.     Database changed
  12.     ADMIN@127.0.0.1 : sbtest:37: > insert sbtest2(k,c,pad) select k,c,pad from sbtest2;
  13.     # 会话4
  14.     ADMIN@127.0.0.1 : (none):18: > use sbtest;
  15.     Database changed
  16.     ADMIN@127.0.0.1 : sbtest:37: > insert sbtest4(k,c,pad) select k,c,pad from sbtest4
2.5. 对心跳表进行更新,持续间隔每5秒一次执行,数十分钟之后,发现心跳SQL被阻塞了,如果HA切换机制没关,阻塞心跳SQL超过60S就会发生切换了,关闭HA切换机制之后,自动心跳会停止检测,所以这里人工模拟了一条心跳SQL,使用脚本循环每5秒更新一次,脚本在发现有阻塞时也会同时打印show processlist和sys.session视图信息,脚本链接:http://5d096a11.wiz03.com/share/s/1t2mEh0a-kl_2c2NZ33kSiac2wQRmx1ykAEE23yLIT3aKVmx)

点击(此处)折叠或打开

  1. # 心跳SQL语句(可手工每5秒执行这句,不过肯定手会酸。。不过为了方便大家阅读,在复现过程中采用了用脚本持续检测,发现有语句阻塞时立即手工方式执行心跳语句,以方便截取)
  2.     ADMIN@127.0.0.1 :test:01: > update xx_heartbeat set hb_time=now() where server_id=@@server_id;
  3.     Query OK, 0 rows affected (0.00 sec)
  4.     Rows matched: 1 Changed: 0 Warnings: 0
  5.     ADMIN@127.0.0.1 :test:01: > update xx_heartbeat set hb_time=now() where server_id=@@server_id
2.6. 立即新开一个会话连接查看数据库正在做什么,可以发现4个会话的insert…select语句,以及心跳SQL update xx_heartbeat…

点击(此处)折叠或打开

  1. ADMIN@127.0.0.1 : sys:25: > show processlist;
  2.     +-----+-------+--------------------+--------+---------+------+-----------+----------------------------------------------------------------------+
  3.     | Id | User | Host | db | Command | Time | State | Info |
  4.     +-----+-------+--------------------+--------+---------+------+-----------+----------------------------------------------------------------------+
  5.     | 25 | ADMIN | 127.0.0.1:35458 | sys | Query | 0 | starting | show processlist |
  6.     | 26 | ADMIN | 127.0.0.1:35459 | sbtest | Query | 1353 | query end | insert sbtest1(k,c,pad) select k,c,pad from sbtest1 |
  7.     | 27 | ADMIN | 127.0.0.1:35460 | sbtest | Query | 1352 | query end | insert sbtest3(k,c,pad) select k,c,pad from sbtest3 |
  8.     | 30 | ADMIN | 127.0.0.1:35463 | sbtest | Query | 1352 | query end | insert sbtest2(k,c,pad) select k,c,pad from sbtest2 |
  9.     | 31 | ADMIN | 127.0.0.1:35464 | sbtest | Query | 1351 | query end | insert sbtest4(k,c,pad) select k,c,pad from sbtest4 |
  10.     | 52 | ADMIN | 127.0.0.1:35485 | test | Query | 62 | query end | update xx_heartbeat set hb_time=now() where server_id=@@server_id |
  11.     | 151 | qfha | 10.10.40.167:51328 | NULL | Query | 562 | starting | SHOW BINARY LOGS |
  12.     ......
  13.     | 160 | qfha | 10.10.40.167:51337 | NULL | Query | 22 | starting | SHOW BINARY LOGS |
  14.     +-----+-------+--------------------+--------+---------+------+-----------+----------------------------------------------------------------------+
  15.     16 rows in set (0.00 sec)
2.7. 查看服务器负载情况怎样

点击(此处)折叠或打开

  1. # top
  2.     top - 22:51:21 up 3 days, 6:54, 9 users, load average: 5.74, 3.39, 1.70
  3.     Tasks: 980 total, 1 running, 979 sleeping, 0 stopped, 0 zombie
  4.     Cpu(s): 0.4%us, 0.4%sy, 0.0%ni, 97.3%id, 1.9%wa, 0.0%hi, 0.0%si, 0.0%st
  5.     Mem: 131804428k total, 129297568k used, 2506860k free, 282052k buffers
  6.     Swap: 7974908k total, 106080k used, 7868828k free, 33743992k cached
  7.       PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  8.     22080 mysql 20 0 86.1g 71g 12m S 23.8 57.0 602:42.90 mysqld
  9.     29121 qemu 20 0 17.5g 8.2g 6160 S 6.9 6.5 140:51.21 qemu-kvm
  10.     20475 root 20 0 0 0 0 S 0.7 0.0 0:28.71 xfs-cil/dm-6
  11.     18272 root 20 0 15692 1952 924 R 0.3 0.0 0:00.33 top
  12.         1 root 20 0 19356 1408 1228 S 0.0 0.0 0:02.14 init
  13.         2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
  14.         3 root RT 0 0 0 0 S 0.0 0.0 0:00.28 migration/0
  15.     ......
  16.     # free
  17.     #free -m
  18.                 total used free shared buffers cached
  19.     Mem: 128715 121616 7099 0 275 28429
  20.     -/+ buffers/cache: 92911 35803
  21.     Swap: 7787 88 7699
  22.     # vmstat
  23.     [root@10-10-66-229 ~]# vmstat 1 10
  24.     procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
  25.     r b swpd free buff cache si so bi bo in cs us sy id wa st
  26.     2 1 90392 2484712 281996 33758712 0 0 0 266326 14803 19717 9 1 87 3 0
  27.     7 1 90392 2454332 281996 33787004 0 0 0 263063 14746 18893 10 1 87 2 0
  28.     4 10 90392 2427820 281996 33814484 0 0 0 266977 13522 18440 10 1 87 2 0
  29.     4 0 90392 2399888 281996 33840928 0 0 0 254064 14727 18820 10 1 87 2 0
  30.     4 1 90392 2373260 281996 33865532 0 0 0 255281 15479 19072 10 0 88 2 0
  31.     4 1 90392 2344072 281996 33894036 0 0 16 261650 13296 18970 10 0 87 2 0
  32.     5 0 90392 2316840 281996 33920892 0 0 0 253947 12292 18442 10 1 88 2 0
  33.     4 0 90392 2289436 281996 33947748 0 0 0 273755 13894 19790 10 1 87 3 0
  34.     4 0 90392 2258064 281996 33977544 0 0 0 265602 12351 18488 10 0 87 2 0
  35.     5 1 90392 2230940 281996 34005292 0 0 0 269967 12720 19439 9 0 88 2 0
  36.     ......
  37.     # iostat
  38.     [root@10-10-66-229 ~]# vmstat -x 1 10
  39.     ......
  40.     avg-cpu: %user %nice %system %iowait %steal %idle
  41.               10.28 0.00 0.50 2.51 0.00 86.72
  42.     Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
  43.     sdc 0.00 1031.00 0.00 6041.00 0.00 537654.00 89.00 13.20 2.19 0.16 94.10
  44.     ......
  45.     avg-cpu: %user %nice %system %iowait %steal %idle
  46.               9.83 0.00 0.53 2.54 0.00 87.10
  47.     Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
  48.     sdc 0.00 891.00 0.00 6034.00 0.00 524171.00 86.87 12.78 2.12 0.16 94.70
  49.     ......
  • 2.8. 从2.6与2.7步骤中查看到的信息来说,服务器的CPU和内存并不是瓶颈,磁盘负载虽然较高且偶尔会用到swap且有2~3%左右的IOWAIT,但是也不至于完全堵死一条单行更新的心跳SQL,而且业务SQL与心跳SQL是不同的表,也不存在表锁和事务锁相互阻塞的问题(使用sys.innodb_lock_waits视图查过,锁等待信息为空)。而从mysql的show processlist信息来看,看不出这些SQL有什么关联,只能看到这些SQL处于query end的状态时间比较长,明显不正常,另外还有一些show binary logs语句 ,该语句是监控系统监控binlog的大小使用的语句,而且这个语句不断增多,看起来像是binlog的访问出现了问题

  • 2.9. 这个时候,通常使用的排查手段已经相形见拙了,我们启用sys schema,新开一个会话连接,使用session视图来查查这些活跃会话正在执行的SQL到底正在做什么?

点击(此处)折叠或打开

  1. ADMIN@127.0.0.1 : sys:25: > select * from session where conn_id!=connection_id()\G;
  2.     *************************** 1. row ***************************
  3.                     thd_id: 60
  4.                   conn_id: 26
  5.                       user: ADMIN@127.0.0.1
  6.                         db: sbtest
  7.                   command: Query
  8.                     state: query end
  9.                       time: 6355
  10.         current_statement: insert sbtest1(k,c,pad) select k,c,pad from sbtest1 # 操作sbtest1表的业务SQL
  11.         statement_latency: 22.58 m
  12.                   progress: NULL
  13.               lock_latency: 1.25 ms
  14.             rows_examined: 60000000
  15.                 rows_sent: 0
  16.             rows_affected: 0
  17.                 tmp_tables: 1
  18.           tmp_disk_tables: 1
  19.                 full_scan: YES
  20.             last_statement: NULL
  21.     last_statement_latency: NULL
  22.             current_memory: 0 bytes
  23.                 last_wait: wait/synch/cond/sql/MYSQL_BIN_LOG::COND_done # 发现连接ID为26的会话执行insert...select语句时在等待这个事件(binlog的COND_done互斥对象)
  24.         last_wait_latency: Still Waiting #该字段值为"Still Waiting"表示这个线程目前一直在等待last_wait字段显示的事件
  25.                     source: binlog.cc:1949 #发生等待事件的源码文件和代码行数在这里
  26.               trx_latency: NULL
  27.                 trx_state: NULL
  28.             trx_autocommit: NULL
  29.                       pid: 4571
  30.               program_name: mysql
  31.     *************************** 2. row ***************************
  32.                     thd_id: 61
  33.                   conn_id: 27
  34.     ......
  35.         current_statement: insert sbtest3(k,c,pad) select k,c,pad from sbtest3
  36.         statement_latency: 22.57 m
  37.       ......
  38.                 last_wait: wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_log # 发现连接ID为27的会话执行insert...select语句时在等待这个事件(binlog的LOCK_log互斥锁)
  39.         last_wait_latency: Still Waiting # 解释同第一行信息相同字段
  40.                     source: binlog.cc:8587 # 解释同第一行信息相同字段
  41.     ......
  42.     *************************** 3. row ***************************
  43.                     thd_id: 64
  44.                   conn_id: 30
  45.     ......
  46.         current_statement: insert sbtest2(k,c,pad) select k,c,pad from sbtest2 #操作sbtest2表的业务SQL
  47.         statement_latency: 22.57 m
  48.     ......
  49.                 last_wait: wait/synch/cond/sql/MYSQL_BIN_LOG::COND_done # 发现连接ID为30的会话执行insert...select语句时在等待这个事件(binlog的COND_done对象)
  50.         last_wait_latency: Still Waiting # 解释同第一行信息相同字段
  51.                     source: binlog.cc:1949 # 解释同第一行信息相同字段
  52.     ......
  53.     *************************** 4. row ***************************
  54.                     thd_id: 65
  55.                   conn_id: 31
  56.                       user: ADMIN@127.0.0.1
  57.                         db: sbtest
  58.                   command: Query
  59.                     state: query end
  60.                       time: 6353
  61.         current_statement: insert sbtest4(k,c,pad) select k,c,pad from sbtest4 #操作sbtest4表的业务SQL
  62.         statement_latency: 22.55 m
  63.                   progress: NULL
  64.               lock_latency: 1.55 ms
  65.             rows_examined: 60000000
  66.                 rows_sent: 0
  67.             rows_affected: 0
  68.                 tmp_tables: 1
  69.           tmp_disk_tables: 1
  70.                 full_scan: YES
  71.             last_statement: NULL
  72.     last_statement_latency: NULL
  73.             current_memory: 0 bytes
  74.                 # 发现连接ID为31的会话执行insert...select语句时在等待这个事件(正在执行binlog的文件IO操作,说明在写binlog cache到binlog file,但这里只是写到文件系统的buffer,并不是sync)
  75.                 last_wait: wait/io/file/sql/binlog
  76.         last_wait_latency: Still Waiting # 解释同第一行信息相同字段
  77.                     source: mf_iocache.c:1566 # 解释同第一行信息相同字段
  78.               trx_latency: NULL
  79.                 trx_state: NULL
  80.             trx_autocommit: NULL
  81.                       pid: 4881
  82.               program_name: mysql
  83.     ......
  84.     *************************** 5. row ***************************
  85.                     thd_id: 185
  86.                   conn_id: 151
  87.                       user: qfha@10.10.40.167
  88.                         db: sys
  89.                   command: Query
  90.                     state: starting
  91.                       time: 564
  92.         current_statement: SHOW BINARY LOGS
  93.         statement_latency: 9.40 m
  94.                   progress: NULL
  95.               lock_latency: 0 ps
  96.             rows_examined: 0
  97.                 rows_sent: 0
  98.             rows_affected: 0
  99.                 tmp_tables: 0
  100.           tmp_disk_tables: 0
  101.                 full_scan: NO
  102.             last_statement: NULL
  103.     last_statement_latency: NULL
  104.             current_memory: 0 bytes
  105.                 last_wait: wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_log
  106.         last_wait_latency: Still Waiting
  107.                     source: rpl_master.cc:707
  108.               trx_latency: NULL
  109.                 trx_state: NULL
  110.             trx_autocommit: NULL
  111.                       pid: NULL
  112.               program_name: NULL
  113.     *************************** 6. row ***************************
  114.      ......
  115.     *************************** 11. row ***************************
  116.                     thd_id: 86
  117.                   conn_id: 52
  118.                       user: ADMIN@127.0.0.1
  119.                         db:test
  120.                   command: Query
  121.                     state: query end
  122.                       time: 244
  123.         current_statement: update xx_heartbeat set hb_ ... () where server_id=@@server_id #操作心跳表的SQL
  124.         statement_latency: 1.07 m
  125.                   progress: NULL
  126.               lock_latency: 219.00 us
  127.             rows_examined: 1
  128.                 rows_sent: 0
  129.             rows_affected: 0
  130.                 tmp_tables: 0
  131.           tmp_disk_tables: 0
  132.                 full_scan: NO
  133.             last_statement: NULL
  134.     last_statement_latency: NULL
  135.             current_memory: 0 bytes
  136.                 last_wait: wait/synch/cond/sql/MYSQL_BIN_LOG::COND_done # 发现连接ID为52的会话执行心跳SQL语句时在等待这个事件
  137.         last_wait_latency: Still Waiting # 解释同第一行信息相同字段
  138.                     source: binlog.cc:1949 # 解释同第一行信息相同字段
  139.               trx_latency: NULL
  140.                 trx_state: NULL
  141.             trx_autocommit: NULL
  142.                       pid: 6772
  143.               program_name: mysql
  144.     **********
    当前名称:利用sysschema解决一次诡异的语句hang问题
    文章链接:http://cxhlcq.com/article/jedepj.html

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部