天天看点

MySQL内核月报 2015.02-MySQL · 捉虫动态· 变量修改导致binlog错误

<b>背景</b>

last_sql_errno: 1594 last_sql_error: relay log read failure: could not parse relay log event entry. the possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's mysql code. if you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'show slave status' on this slave.

<b>bug 分析</b>

binlog event 结构

为啥会出现这种一个event前后不一致的情况呢,代码编写不严谨!

在写 rows_log_event(write/update/delete) 过程中,有2次用到 log_bin_use_v1_row_events 这个全局变量,一次是在构造函数处,一次是在写postheader时 rows_log_event::write_data_header(),2次都是直接使用,如果正好在这2次中间,我们执行 set global log_bin_use_v1_row_events = 0|1,改变原来的值,就会导致前后逻辑判断结果不一致。如果主库有频繁的更新操作,每次更新又比较大,只要修改这个值,就很容易触发这个bug。

<b>bug修复</b>

修复很简单,把2次引用全局变量改成一次就好了,在rows_log_event::write_data_header函数里直接使用已经保存的m_type,改法如下

这样改之后,就只会在构造函数中才用到全局变量。

继续阅读