最近碰到一個很蛋疼的BUG,使用merge引擎,row模式下複製,極大的機率crash備庫。
我的測試環境是5.1的主庫,5.5的備庫
test case:
- create table m1 (a int primary key, b int) engine=myisam;
- create table m2 (a int primary key, b int) engine=myisam;
- create table mm (a int primary key , b int) engine=mrg_myisam INSERT_METHOD=LAST UNION=(m1,m2);
- insert into mm values (1,2);
crash的原因非常簡單,實際上在很早之前這個bug就fix了(MySQL Bug#47103), 可能是官方疏忽,又或者是Merge儲存引擎用的人少,沒有將這個patch backport到5.5中。
以下是個簡單的介紹。
我們知道每個Binlog的行事件都有一個table_map_event。
在函數Table_map_log_event::do_apply_event中,會根據table map event的內容初始化table_list結構體,並構建table_list->m_tabledef。
這裡的table_list->m_tabledef.m_field_metadata是被分配記憶體的。然後將該table_list掛到rli->tables_to_lock上
而在函數Rows_log_event::do_apply_event中,當調用open_and_lock_tables實際開啟表時,還會開啟相依賴的子表,並將這些table_list掛到rli->tables_to_lock之後(連結指標為next_global),但要注意,這裡開啟的依賴字表時,其table_list->m_tabledef.m_field_metadata實0x0,並沒有分配空間
隨後,在開啟表後,要檢查table map裡的定義和實際開啟表的定義是否相容。
- 7663 RPL_TABLE_LIST *ptr= rli->tables_to_lock;
- 7664 for ( ; ptr ; ptr= static_cast<RPL_TABLE_LIST*>(ptr->next_global))
- 7665 {
- 7666 TABLE *conv_table;
- 7667 if (!ptr->m_tabledef.compatible_with(thd, const_cast<Relay_log_info*>(rli),
- 7668 ptr->table, &conv_table))
而在table_def::compatible_with中:
can_convert_field_to(field, type(col), field_metadata(col), rli, m_flags, &order)
其中type和field_metadata都會涉及到對m_field_metadata的引用,因此直接產生段錯誤,導致crash。
官方的fix見http://lists.mysql.com/commits/86326