我手裏維護了一個項目,其功能是用Java模擬一個MariaDB的slave庫鏈接到主庫,對從主庫傳輸過來的binlog事件進行監聽與分析mysql
碰到一個問題是:sql
若是主庫作了一個很大的修改操做(比方說直接delete了一個百萬條數據的表),那麼會產生一個很大的binlog事件,這個binlog事件會在咱們的binlog監聽器中被處理,而後組裝成一個很大的對象(含有這100萬條數據)數據庫
問題就出在這裏,這個對象太大了(佔用上g的heap絕不費力),而後咱們在對這個對象的分析過程當中,會產生更多的對象,而這些對象都是強引用,佔用的是實打實的內存。因而就OOM了網絡
我接受這個項目的時候,前人提出的方案很簡單:禁止用戶執行過大的操做。ide
很暴力的方案,並且確實也能夠解決問題。spa
可是我以爲應該有更好的解決方法code
第一反應是修改binlog監聽器模塊的代碼orm
因爲binlog事件是經過網絡傳輸,咱們也許能夠在接收binlog事件的時候,對監聽到的超大binlog事件進行拆分,將其逐步拆分爲多個等效的小binlog事件對象
可是這一塊的代碼風格不好,很難修改blog
因而在Google上搜了一下相關的關鍵詞,發現有一個叫作binlog-row-event-max-size的神奇啓動參數,其英文註釋以下
The maximum size in bytes of a row-based binary log event. Should be a multiple of 256. Minimum 256, maximum 18446744073709547520.
彷佛正好是我須要的?
可是很不幸,這個參數只在MariaDB 10.0.17以後有效
而公司的生產環境用的是MariaDB 10.0.10
對比一下二者的效果吧
MariaDB 10.0.10,delete一個百萬元素的表
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | mysql-bin.000319 | 446952048 | Gtid | 90174306 | 446952086 | BEGIN GTID 17186-90174306-429975571 | | mysql-bin.000319 | 446952086 | Table_map | 90174306 | 446952126 | table_id: 14520 (cc.t1) | | mysql-bin.000319 | 446952126 | Delete_rows_v1 | 90174306 | 455952146 | table_id: 14520 flags: STMT_END_F | | mysql-bin.000319 | 455952146 | Xid | 90174306 | 455952173 | COMMIT /* xid=489278797 */
能夠看到這個delete事件對應於一個很大的binlog事件
MariaDB 10.1.22,binlog-row-event-max-size設置爲262144,一樣delete一個百萬元素的表
mysql> SHOW BINLOG EVENTS IN 'mysqld-bin.000001'; +-------------------+---------+-------------------+-----------+-------------+-----------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+---------+-------------------+-----------+-------------+-----------------------------------------------------+ | mysqld-bin.000001 | 4 | Format_desc | 1 | 249 | Server ver: 10.1.22-MariaDB-1~jessie, Binlog ver: 4 | | mysqld-bin.000001 | 249 | Gtid_list | 1 | 274 | [] | | mysqld-bin.000001 | 274 | Binlog_checkpoint | 1 | 314 | mysqld-bin.000001 | | mysqld-bin.000001 | 314 | Gtid | 1 | 352 | BEGIN GTID 0-1-1 | | mysqld-bin.000001 | 352 | Table_map | 1 | 394 | table_id: 19 (test.t1) | | mysqld-bin.000001 | 394 | Delete_rows_v1 | 1 | 262557 | table_id: 19 | | mysqld-bin.000001 | 262557 | Delete_rows_v1 | 1 | 524720 | table_id: 19 | | mysqld-bin.000001 | 524720 | Delete_rows_v1 | 1 | 786883 | table_id: 19 | | mysqld-bin.000001 | 786883 | Delete_rows_v1 | 1 | 1049046 | table_id: 19 | | mysqld-bin.000001 | 1049046 | Delete_rows_v1 | 1 | 1311209 | table_id: 19 | | mysqld-bin.000001 | 1311209 | Delete_rows_v1 | 1 | 1573372 | table_id: 19 | | mysqld-bin.000001 | 1573372 | Delete_rows_v1 | 1 | 1835535 | table_id: 19 | | mysqld-bin.000001 | 1835535 | Delete_rows_v1 | 1 | 2097698 | table_id: 19 | | mysqld-bin.000001 | 2097698 | Delete_rows_v1 | 1 | 2359861 | table_id: 19 | | mysqld-bin.000001 | 2359861 | Delete_rows_v1 | 1 | 2622024 | table_id: 19 | | mysqld-bin.000001 | 2622024 | Delete_rows_v1 | 1 | 2884187 | table_id: 19 | | mysqld-bin.000001 | 2884187 | Delete_rows_v1 | 1 | 3146350 | table_id: 19 | | mysqld-bin.000001 | 3146350 | Delete_rows_v1 | 1 | 3408513 | table_id: 19 | | mysqld-bin.000001 | 3408513 | Delete_rows_v1 | 1 | 3670676 | table_id: 19 | | mysqld-bin.000001 | 3670676 | Delete_rows_v1 | 1 | 3932839 | table_id: 19 | | mysqld-bin.000001 | 3932839 | Delete_rows_v1 | 1 | 4195002 | table_id: 19 | | mysqld-bin.000001 | 4195002 | Delete_rows_v1 | 1 | 4457165 | table_id: 19 | | mysqld-bin.000001 | 4457165 | Delete_rows_v1 | 1 | 4719328 | table_id: 19 | | mysqld-bin.000001 | 4719328 | Delete_rows_v1 | 1 | 4981491 | table_id: 19 | | mysqld-bin.000001 | 4981491 | Delete_rows_v1 | 1 | 5243654 | table_id: 19 | | mysqld-bin.000001 | 5243654 | Delete_rows_v1 | 1 | 5505817 | table_id: 19 | | mysqld-bin.000001 | 5505817 | Delete_rows_v1 | 1 | 5767980 | table_id: 19 | | mysqld-bin.000001 | 5767980 | Delete_rows_v1 | 1 | 6030143 | table_id: 19 | | mysqld-bin.000001 | 6030143 | Delete_rows_v1 | 1 | 6292306 | table_id: 19 | | mysqld-bin.000001 | 6292306 | Delete_rows_v1 | 1 | 6554469 | table_id: 19 | | mysqld-bin.000001 | 6554469 | Delete_rows_v1 | 1 | 6816632 | table_id: 19 | | mysqld-bin.000001 | 6816632 | Delete_rows_v1 | 1 | 7078795 | table_id: 19 | | mysqld-bin.000001 | 7078795 | Delete_rows_v1 | 1 | 7340958 | table_id: 19 | | mysqld-bin.000001 | 7340958 | Delete_rows_v1 | 1 | 7603121 | table_id: 19 | | mysqld-bin.000001 | 7603121 | Delete_rows_v1 | 1 | 7865284 | table_id: 19 | | mysqld-bin.000001 | 7865284 | Delete_rows_v1 | 1 | 8127447 | table_id: 19 | | mysqld-bin.000001 | 8127447 | Delete_rows_v1 | 1 | 8389610 | table_id: 19 | | mysqld-bin.000001 | 8389610 | Delete_rows_v1 | 1 | 8651773 | table_id: 19 | | mysqld-bin.000001 | 8651773 | Delete_rows_v1 | 1 | 8913936 | table_id: 19 | | mysqld-bin.000001 | 8913936 | Delete_rows_v1 | 1 | 9001409 | table_id: 19 flags: STMT_END_F | | mysqld-bin.000001 | 9001409 | Xid | 1 | 9001436 | COMMIT /* xid=3000082 */ | +-------------------+---------+-------------------+-----------+-------------+-----------------------------------------------------+
能夠看到這個delete事件已經被拆分紅了30多個稍小的binlog事件
看來只能儘可能推進數據庫的升級工做了。