mysqlbinlog主要流程:
main函數:
dump_multiple_logs: 處理所有的binlog文件
注意PRINT_EVENT_INFO print_event_info; 該變量描述了mysqlbinlog打印event的方法,ev->print函數的參數
循環調用dump_single_log函數:注意最後一個binlog文件的strop_postition的結束位置
dump_local_log_entries:解析一個本地binlog文件
check_header: 讀取binlog文件的第一個事件Format_description_log_event,該事件的信息對整個binlog文件的解析有用
文件的前4個字節數值固定,校驗文件是否正確
調用Log_event::read_log_event函數(見後文分析)讀取FORMAT_DESCRIPTION_EVENT事件
根據需要打印FORMAT_DESCRIPTION_EVENT事件的內容
while循環:
調用Log_event::read_log_event函數(見後文分析)讀取事件
process_event:對每個函數進行處理
1.處理FORMAT_DESCRIPTION_EVENT事件的時候會更新全局變量glob_description_event,該變量會影響其他事件的解析處。
2.調用print函數打印事件內容
3.解析事件,根據內容修改相關的變量(eg:XID_EVENT)
dump_remote_log_entries:解析遠程的一個binlog文件,處理流程大體上一致.區別:創建一個連接,通過COM_BINLOG_DUMP或者COM_BINLOG_DUMP_GTID讀取binlog內容。
Log_event* Log_event::read_log_event(IO_CACHE* file, const Format_description_log_event *description_event, my_bool crc_check):
1.確定event_header的長度(最多讀取19個字節,舊的版本是13個字節)
2.獲取event的長度,根據event的長度讀取event的內容
3.調用Log_event* Log_event::read_log_event(const char* buf, uint event_len, const char **error, const Format_description_log_event *description_event, my_bool crc_check)
:該函數功能:1.校驗event的長度
2.獲取checksum的算法(如果是FORMAT_DESCRIPTION_EVENT事件)
3.校驗checksum
4.函數根據event的類型調用不同的event構造函數創建對應event。注意每個event都有一個構造函數根據buf中事件的內容創建event