五、关于woker线程w2的等待
这里可能的原因有2个:
- 多线程并行的情况下,线程执行的顺序本生就是不定的,很可能线程由于丢失CPU而落后其他线程的处理,因为CPU调度的最小单位是线程。如果保证某个共享内存操作的完整性需要用到mutex、原子变量等技术。
- 如果w2中的事务本生就包含了多个DML语句,那么获取 GLOBAL READ LOCK 本身就是间歇性的,也就是每个语句结束都会释放,然后下一个语句开始的时候再次open table来获取。
我们来看看第二点,只考虑row_format格式的binlog。
我们知道一个事务可以包含多个语句,每条语句都会包含一个map Event和多个DML Event,当本Event是语句的最后一个Event的时候会使用STMT_END_F进行标记,也正是在这个时候会释放 GLOBAL READ LOCK,源码有如下:
if (get_flags(STMT_END_F)) { if((error= rows_event_stmt_cleanup(rli, thd))) 栈: #0 MDL_context::release_lock (this=0x7fffa8000a08, duration=MDL_STATEMENT, ticket=0x7fffa800ea40) at /opt/percona-server-locks-detail-5.7.22/sql/mdl.cc:4350 #1 0x0000000001464bf1 in MDL_context::release_locks_stored_before (this=0x7fffa8000a08, duration=MDL_STATEMENT, sentinel=0x0) at /opt/percona-server-locks-detail-5.7.22/sql/mdl.cc:4521 #2 0x000000000146541b in MDL_context::release_statement_locks (this=0x7fffa8000a08) at /opt/percona-server-locks-detail-5.7.22/sql/mdl.cc:4813 #3 0x0000000001865c75 in Relay_log_info::slave_close_thread_tables (this=0x341e8b0, thd=0x7fffa8000970) at /opt/percona-server-locks-detail-5.7.22/sql/rpl_rli.cc:2014 #4 0x0000000001865873 in Relay_log_info::cleanup_context (this=0x341e8b0, thd=0x7fffa8000970, error=false) at /opt/percona-server-locks-detail-5.7.22/sql/rpl_rli.cc:1886 #5 0x00000000017e8fc7 in rows_event_stmt_cleanup (rli=0x341e8b0, thd=0x7fffa8000970) at /opt/percona-server-locks-detail-5.7.22/sql/log_event.cc:11782 #6 0x00000000017e8c79 in Rows_log_event::do_apply_event (this=0x7fffa8017dc0, rli=0x341e8b0) at /opt/percona-server-locks-detail-5.7.22/sql/log_event.cc:11660 #7 0x00000000017cfdcd in Log_event::apply_event (this=0x7fffa8017dc0, rli=0x341e8b0) at /opt/percona-server-locks-detail-5.7.22/sql/log_event.cc:3570 #8 0x00000000018476dc in apply_event_and_update_pos (ptr_ev=0x7fffec14f880, thd=0x7fffa8000970, rli=0x341e8b0) at /opt/percona-server-locks-detail-5.7.22/sql/rpl_slave.cc:4766 #9 0x0000000001848d9a in exec_relay_log_event (thd=0x7fffa8000970, rli=0x341e8b0) at /opt/percona-server-locks-detail-5.7.22/sql/rpl_slave.cc:5300 #10 0x000000000184f9cc in handle_slave_sql (arg=0x33769a0) at /opt/percona-server-locks-detail-5.7.22/sql/rpl_slave.cc:7543 (gdb) p ticket->m_lock->key.mdl_namespace() $1 = MDL_key::GLOBAL (gdb) p ticket->m_type $2 = MDL_INTENTION_EXCLUSIVE (gdb) p ticket->m_duration $3 = MDL_STATEMENT
如果下一条语句开始又会重新获取GLOBAL READ LOCK,这就是我说的间歇性获取。
到这里死锁条件已经成熟,只要遇到这种情况就可能需要人为介入才能继续了。
六、关于mysqldump
社区版在如下情况下需要增加FTWRL:
- 设置了master-data
- 设置了singal-transaction和flush-logs
percona版在如下情况需要增加FTWRL:
- 设置了singal-transaction和flush-logs
我们来大概看看社区版的代码如下(代码版本8.0.21),下面是从FTWRL倒UNLOCK的过程:
if ((opt_lock_all_tables || opt_master_data || //如果设置了 master data 设置flush table with read lock
(opt_single_transaction && flush_logs)) &&//如果设置了single transaction和flush logs 设置flush table with read lockdo_flush_tables_read_lock(mysql)) //设置flush table with read lock
goto err;
/*
/*
Flush logs before starting transaction since
this causes implicit commit starting mysql-5.5.
*/
if (opt_lock_all_tables || opt_master_data ||
(opt_single_transaction && flush_logs) || opt_delete_master_logs) {
if (flush_logs || opt_delete_master_logs) {//如果设置了 flush logs 进行日志刷新
if (mysql_refresh(mysql, REFRESH_LOG)) { //进行日志刷新
DB_error(mysql, "when doing refresh");
goto err;
}
verbose_msg("-- main : logs flushed successfully!\n");
}
/ Not anymore! That would not be sensible. /
flush_logs = false;
}
if (opt_delete_master_logs) {
if (get_bin_log_name(mysql, bin_log_name, sizeof(bin_log_name))) goto err;
}
if (opt_single_transaction && start_transaction(mysql)) goto err; //开启事务 RR
/ Add 'STOP SLAVE to beginning of dump /
if (opt_slave_apply && add_stop_slave()) goto err;
/* Process opt_set_gtid_purged and add SET @@GLOBAL.GTID_PURGED if required.
*/
if (process_set_gtid_purged(mysql)) goto err; //设置GTID,如果设置了gtid_purged 这个函数会跳过
if (opt_master_data && do_show_master_status(mysql)) goto err; //获取主库binlog位置
if (opt_slave_data && do_show_slave_status(mysql)) goto err; //slave_data 设置相关 从show slave中获取
if (opt_single_transaction &&
do_unlock_tables(mysql)) / unlock but no commit! /
goto err;
percona版本中增加了判断函数 check_consistent_binlog_pos,如下(不过多描述)
:
if (opt_single_transaction && opt_master_data)
{
/*
See if we can avoid FLUSH TABLES WITH READ LOCK with Binlog_snapshot_*
variables.
*/
consistent_binlog_pos= check_consistent_binlog_pos(NULL, NULL);
}
if ((opt_lock_all_tables || (opt_master_data && !consistent_binlog_pos) ||//consistent_binlog_pos 0 需要 1 不需要
(opt_single_transaction && flush_logs)))
{
if (do_flush_tables_read_lock(mysql))
goto err;
}
七、如何解决
总结如下:
- master-data 一般备份都会增加,因此只能在低峰期进行备份,尽量减少影响。
- 考虑关闭参数 slave_preserve_commit_order。但是FTWRL的堵塞还是存在,只是不会产生死锁。
- 如果压力不大可以考虑关闭MTS。但是FTWRL的堵塞还是存在,只是不会产生死锁。
全文完。
Enjoy MySQL :)
</div>