前言
本文主要介绍binlog crash recovery 的过程
假设用户使用 InnoDB 引擎,sync_binlog=1
使用 MySQL 5.7.20 版本进行分析
crash recovery 过程中,binlog 需要保证:
- 所有已提交事务的binlog已存在
- 所有未提交事务的binlog不存在
两阶段提交
MySQL 使用两阶段提交解决 binlog 和 InnoDB redo log 的一致性的问题
也就是将普通事务当做内部XA事务处理,为每个事务分配一个XID,binlog作为事务的协调者
- 阶段1:InnoDB redo log 写盘,InnoDB 事务进入 prepare 状态
- 阶段2:binlog 写盘,InooDB 事务进入 commit 状态
每个事务binlog的末尾,会记录一个 XID event,标志着事务是否提交成功,也就是说,recovery 过程中,binlog 最后一个 XID event 之后的内容都应该被 purge。
InnoDB 日志可能也需要回滚或者提交,这里就不再展开。
binlog 文件的 crash recovery
1. mysqld_main
3. init_server_components
5. MYSQL_BIN_LOG::open
7. MYSQL_BIN_LOG::open_binlog
binlog recover 的主要过程在 MYSQL_BIN_LOG::open_binlog 中
1. int MYSQL_BIN_LOG::open_binlog(const char *opt_name)
2. {
4. /* 确保 index 文件初始化成功 */
5. if (!my_b_inited(&index_file))
6. {
7. /* There was a failure to open the index file, can't open the binlog */
8. cleanup();
9. return 1;
10. }
12. /* 找到 index 中第一个 binlog */
13. if ((error= find_log_pos(&log_info, NullS, true/*need_lock_index=true*/)))
15. {
16. /* 找到 index 中最后一个 binlog */
17. do
18. {
19. strmake(log_name, log_info.log_file_name, sizeof(log_name)-1);
20. } while (!(error= find_next_log(&log_info, true/*need_lock_index=true*/)));
23. /*
24. 打开最后一个binlog,会校验文件头的 magic number "\xfe\x62\x69\x6e"
25. 如果 magic number 校验失败,会直接报错退出,无法完成recovery
26. 如果确定最后一个binlog没有内容,可以删除binlog 文件再重试
27. */
28. if ((file= open_binlog_file(&log, log_name, &errmsg)) < 0)
30. /*
31. 如果 binlog 没有正常关闭,mysql server 可能crash过,
32. 我们需要调用 MYSQL_BIN_LOG::recover:
34. a) 找到最后一个 XID
35. b) 完成最后一个事务的两阶段提交(InnoDB commit)
36. c) 找到最后一个合法位点
38. 因此,我们需要遍历 binlog 文件,找到最后一个合法event集合,并 purge 无效binlog
39. */
40. if ((ev= Log_event::read_log_event(&log, 0, &fdle,
41. opt_master_verify_checksum)) &&
42. ev->get_type_code() == binary_log::FORMAT_DESCRIPTION_EVENT &&
43. (ev->common_header->flags & LOG_EVENT_BINLOG_IN_USE_F ||
44. DBUG_EVALUATE_IF("eval_force_bin_log_recovery", true, false)))
45. {
46. sql_print_information("Recovering after a crash using %s", opt_name);
48. /* 初始化合法位点 */
49. valid_pos= my_b_tell(&log);
51. /* 执行recover 过程 ,并计算出合法位点 */
52. error= recover(&log, (Format_description_log_event *)ev, &valid_pos);
53. }
54. else
55. error=0;
57. if (valid_pos > 0){
58. if (valid_pos < binlog_size)
59. {
60. /* 将 valid_pos 后面的binlog purge掉 */
61. if (my_chsize(file, valid_pos, 0, MYF(MY_WME)))
62. }
63. }
64. }
65. }
recover 函数的逻辑很简单:遍历最后一个binlog的所有 event,每次事务结尾,或者非事务event结尾更新 valid_pos(gtid event不更新)。并在一个 hash 中记录所有xid,用于引擎层 recover
1. int MYSQL_BIN_LOG::recover(IO_CACHE *log, Format_description_log_event *fdle,
2. my_off_t *valid_pos)
3. {
5. /* 初始化 XID hash,用于记录 binlog 中的 xid */
6. if (! fdle->is_valid() ||
7. my_hash_init(&xids, &my_charset_bin, TC_LOG_PAGE_SIZE/3, 0,
8. sizeof(my_xid), 0, 0, MYF(0),
9. key_memory_binlog_recover_exec))
10. goto err1;
12. /* 依次读取 binlog event */
13. while ((ev= Log_event::read_log_event(log, 0, fdle, TRUE))
14. && ev->is_valid())
15. {
16. if (ev->get_type_code() == binary_log::QUERY_EVENT &&
17. !strcmp(((Query_log_event*)ev)->query, "BEGIN"))
18. /* begin 代表事务开始 */
19. in_transaction= TRUE;
21. if (ev->get_type_code() == binary_log::QUERY_EVENT &&
22. !strcmp(((Query_log_event*)ev)->query, "COMMIT"))
23. {
24. DBUG_ASSERT(in_transaction == TRUE);
25. /* commit 代表事务结束 */
26. in_transaction= FALSE;
27. }
28. else if (ev->get_type_code() == binary_log::XID_EVENT)
29. {
30. DBUG_ASSERT(in_transaction == TRUE);
31. /* xid event 代表事务结束 */
32. in_transaction= FALSE;
33. Xid_log_event *xev=(Xid_log_event *)ev;
34. uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
35. sizeof(xev->xid));
36. /* 记录 xid */
37. if (!x || my_hash_insert(&xids, x))
38. goto err2;
39. }
41. /*
42. 如果不在事务中,且不是gtid event,则更新 valid_pos
43. 显然,如果在事务中,最后一段 event 不是一个完整事务,pos并不合法
44. */
45. if (!log->error && !in_transaction &&
46. !is_gtid_event(ev))
47. *valid_pos= my_b_tell(log);
48. }
50. /*
51. 存储引擎recover
52. 所有已经记录 XID 的事务必须在存储引擎中提交
53. 未记录 XID 的事务必须回滚
54. */
55. if (total_ha_2pc > 1 && ha_recover(&xids))
56. goto err2;
binlog index 的 crash recovery
为了保证 binlog index 的 crash safe,MySQL 引入了一个临时文件 crash_safe_index_file
新的 binlog_file_name 写入 binlog_index_file 流程如下:
- 创建临时文件 crash_safe_index_file
- 拷贝 binlog_index_file 中的内容到 crash_safe_index_file
- 新的 binlog_file_name 写入 crash_safe_index_file
- 删除 binlog_index_file
- 重命名 crash_safe_index_file 到 binlog_index_file
这个流程保证了在任何时候crash,binlog_index_file 和 crash_safe_index_file 至少有一个可用
这样再recover 时只要判断这两个文件是否可用,如果 binlog_index_file 可用则无需特殊处理,如果binlog_index_file 不可用则重命名 crash_safe_index_file 到 binlog_index_file
binlog index 的 recover 过程主要在 bool MYSQL_BIN_LOG::open_index_file 中
显然,open_indix_file 在 open_binlog 之前
1. mysqld_main
3. init_server_components
5. MYSQL_BIN_LOG::open_index_file
2. bool MYSQL_BIN_LOG::open_index_file(const char *index_file_name_arg,
3. const char *log_name, bool need_lock_index)
4. {
5. /* 拼接 index_file_name */
6. fn_format(index_file_name, index_file_name_arg, mysql_data_home,
7. ".index", opt);
9. /* 拼接 crash_safe_index_file_name */
10. if (set_crash_safe_index_file_name(index_file_name_arg))
12. /*
13. recover 主要体现在这里
14. 检查 index_file_name 和 crash_safe_index_file_name 是否存在
15. 如果 index_file_name 不存在 crash_safe_index_file_name 存在,
16. 那么将 crash_safe_index_file_name 重命名为 index_file_name
17. */
18. if (my_access(index_file_name, F_OK) &&
19. !my_access(crash_safe_index_file_name, F_OK) &&
20. my_rename(crash_safe_index_file_name, index_file_name, MYF(MY_WME)))
21. {
22. sql_print_error("MYSQL_BIN_LOG::open_index_file failed to "
23. "move crash_safe_index_file to index file.");
24. error= true;
25. goto end;
26. }
28. }
新的 binlog_file_name 写入 binlog_index_file 的过程在 MYSQL_BIN_LOG::add_log_to_index
1. int MYSQL_BIN_LOG::add_log_to_index(uchar* log_name,
2. size_t log_name_len, bool need_lock_index)
3. {
4. /* 创建 crash_safe_index_file */
5. if (open_crash_safe_index_file())
7. /* 拷贝 index_file 内容到 crash_safe_index_file */
8. if (copy_file(&index_file, &crash_safe_index_file, 0))
10. /* 写入 binlog_file_name */
11. if (my_b_write(&crash_safe_index_file, log_name, log_name_len) ||
12. my_b_write(&crash_safe_index_file, (uchar*) "\n", 1) ||
13. flush_io_cache(&crash_safe_index_file) ||
14. mysql_file_sync(crash_safe_index_file.file, MYF(MY_WME)))
16. /*
17. 函数内部先 delete binlog_index_file 再 rename crash_safe_index_file
18. 如果 delete 到 rename 之间发生 crash, crash_safe_index_file 会在 recover过程中 rename 成 binlog_index_file
19. */
20. if (move_crash_safe_index_file_to_index_file(need_lock_index))
22. }
总结
MySQL 解决了binlog crash safe 的问题,但是 relay log 依然不保证 crash safe。
relay log 结构和 binlog 一致,可以借鉴 binlog crash safe 的方式,计算出 valid_pos,将 valid_pos之后的 event 全部purge。
