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

发布时间:2020-08-09 16:27:39 作者:沃趣科技
来源:ITPUB博客 阅读:261
沃趣科技  罗小波


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

1、故事背景

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.     ......

点击(此处)折叠或打开

  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.     *************************** 12. row ***************************
  145.     ......
  146.     15 rows in set (0.15 sec)
2.10. 从2.9步骤中的信息来看,基本可以确定是因为写binlog不能及时响应的问题导致的,但是具体是binlog的什么问题呢?跟心跳SQL被阻塞又有什么关系?这个时候我们在以上所有SQL正常执行完成之后(等待时间大约几分钟,超过了60S的高可用切换时间),解析了包含以上业务SQL和心跳SQL的所有binlog文件进行分析(binlog大约有22G),查看这些语句的提交先后顺序,发现都是在同一个group 里,并且心跳SQL排在了业务SQL最后面,详情如下:

点击(此处)折叠或打开

  1. [root@10-10-66-229 binlog]# mysqlbinlog -vv mysql-bin.000175|grep -E 'insert sbtest|insert into sbtest|last_committed|update xx_heartbeat|GTID_NEXT' > a.sql
  2.     [root@10-10-66-229 binlog]# cat a.sql
  3.     ......
  4.     #171011 15:01:42 server id 3306103 end_log_pos 29975 CRC32 0x85a80516 GTID last_committed=76 sequence_number=77
  5.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90292'/*!*/;
  6.     # update xx_heartbeat set hb_time=now() where server_id=@@server_id
  7.     #171011 15:01:43 server id 3306103 end_log_pos 30366 CRC32 0x14c9d915 GTID last_committed=77 sequence_number=78
  8.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90293'/*!*/;
  9.     # update xx_heartbeat set hb_time=now() where server_id=@@server_id
  10.     # 以下就是我们要找的关键信息,留意记录group 信息的event的时间,从这里可以看到insert...select语句发生的时间顺序按照表名排序\
  11.     # 依次sbtest1、sbtest3、sbtest2、sbtest4,xx_heartbeat表(与在客户端敲下SQL的时间顺序相同),从信息"last_committed=78"我们可以知道这些SQL都被放进了\
  12.     # 同一个队列里进行binlog提交,从信息"sequence_number=79"我们可以得知,操作表sbtest4的线程在binlog提交队列中的顺序最靠前,也就是说它是leader,其他的为follower
  13.     #171011 14:39:56 server id 3306103 end_log_pos 30757 CRC32 0xb2c44222 GTID last_committed=78 sequence_number=79
  14.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90294'/*!*/;
  15.     # insert sbtest4(k,c,pad) select k,c,pad from sbtest4 # 业务SQL
  16.     #171011 14:39:55 server id 3306103 end_log_pos 1429482395 CRC32 0x00c7685d GTID last_committed=78 sequence_number=80
  17.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90295'/*!*/;
  18.     # insert sbtest3(k,c,pad) select k,c,pad from sbtest3 # 业务SQL
  19.     #171011 14:39:55 server id 3306103 end_log_pos 2858934033 CRC32 0xb0e9f488 GTID last_committed=78 sequence_number=81
  20.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90296'/*!*/;
  21.     # insert sbtest2(k,c,pad) select k,c,pad from sbtest2 # 业务SQL
  22.     #171011 14:39:54 server id 3306103 end_log_pos 4288385671 CRC32 0x52cf3dfa GTID last_committed=78 sequence_number=82
  23.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90297'/*!*/;
  24.     # insert sbtest1(k,c,pad) select k,c,pad from sbtest1 # 业务SQL
  25.     #171011 16:21:45 server id 3306103 end_log_pos 1422870013 CRC32 0xd3caff89 GTID last_committed=78 sequence_number=83
  26.     SET @@SESSION.GTID_NEXT= '6d362bfe-ae21-11e7-a22f-fabb9a69be00:90298'/*!*/;
  27.     # 下面一句SQL即为被阻塞的心跳SQL,可以看到排在了前面几个业务SQL的后面,而前面几个业务SQL产生了大量的binlog数据,这些数据写到binlog文件中消耗了大量的时间\
  28.     # 而因为这句心跳SQL与前面几个业务SQL在同一个group中,必须等待前面几个业务SQL的binlog数据写完之后才能写binlog,所以,从这里就可以知道,\
  29.     # 心跳SQL就是因为在提交binlog阶段被业务SQL阻塞了,所以最终导致心跳SQL的事务无法提交
  30.     # update xx_heartbeat set hb_time=now() where server_id=@@server_id
  31.     SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/

3、解决方法

点击(此处)折叠或打开

  1. set sql_log_bin=0;update xx_heartbeat set hb_time=now() where server_id=@@server_id

4、总结

推荐阅读:
  1. 带你认识MySQL sys schema
  2. 利用Galera Cluster解决主主复制单点问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hang schema 解决

上一篇:Oracle数据库(RAC)巡检报告

下一篇:oracle数值类型漫谈

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》