前兩天和百度的一個同學聊MySQL兩階段提交,當時自信滿滿的說了一堆,后來發現還是有些問題的理解還是比較模糊,可能是因為時間太久了,忘記了吧。這里再補一下:)
5.3.1事務提交流程
MySQL的事務提交邏輯主要在函數ha_commit_trans中完成。事務的提交涉及到binlog及具體的存儲的引擎的事務提交。所以MySQL用2PC來保證的事務的完整性。MySQL的2PC過程如下:
(1)先調用binglog_hton和innobase_hton的prepare方法完成第一階段,binlog_hton的papare方法實際上什么也沒做,innodb的prepare將事務狀態設為TRX_PREPARED,並將redo log刷磁盤 (innobase_xa_prepare à trx_prepare_for_mysql à trx_prepare_off_kernel)。
(2)如果事務涉及的所有存儲引擎的prepare都執行成功,則調用TC_LOG_BINLOG::log_xid將SQL語句寫到binlog,此時,事務已經鐵定要提交了。否則,調用ha_rollback_trans回滾事務,而SQL語句實際上也不會寫到binlog。
(3)最后,調用引擎的commit完成事務的提交。實際上binlog_hton->commit什么也不會做(因為(2)已經將binlog寫入磁盤),innobase_hton->commit則清除undo信息,刷redo日志,將事務設為TRX_NOT_STARTED狀態(innobase_commit à innobase_commit_low à trx_commit_for_mysql à trx_commit_off_kernel)。
//ha_innodb.cc static int innobase_commit( /*============*/ /* out: 0 */ THD* thd, /* in: MySQL thread handle of the user for whom the transaction should be committed */ bool all) /* in: TRUE - commit transaction FALSE - the current SQL statement ended */ { ... trx->mysql_log_file_name = mysql_bin_log.get_log_fname(); trx->mysql_log_offset = (ib_longlong)mysql_bin_log.get_log_file()->pos_in_file; ... } |
函數innobase_commit提交事務,先得到當前的binlog的位置,然后再寫入事務系統PAGE(trx_commit_off_kernel à trx_sys_update_mysql_binlog_offset)。
InnoDB將MySQL binlog的位置記錄到trx system header中:
//trx0sys.h /* The offset of the MySQL binlog offset info in the trx system header */ #define TRX_SYS_MYSQL_LOG_INFO (UNIV_PAGE_SIZE - 1000) #define TRX_SYS_MYSQL_LOG_MAGIC_N_FLD 0 /* magic number which shows if we have valid data in the MySQL binlog info; the value is ..._MAGIC_N if yes */ #define TRX_SYS_MYSQL_LOG_OFFSET_HIGH 4 /* high 4 bytes of the offset within that file */ #define TRX_SYS_MYSQL_LOG_OFFSET_LOW 8 /* low 4 bytes of the offset within that file */ #define TRX_SYS_MYSQL_LOG_NAME 12 /* MySQL log file name */ |
5.3.2 事務恢復流程
Innodb在恢復的時候,不同狀態的事務,會進行不同的處理(見trx_rollback_or_clean_all_without_sess函數):
<1>對於TRX_COMMITTED_IN_MEMORY的事務,清除回滾段,然后將事務設為TRX_NOT_STARTED;
<2>對於TRX_NOT_STARTED的事務,表示事務已經提交,跳過;
<3>對於TRX_PREPARED的事務,要根據binlog來決定事務的命運,暫時跳過;
<4>對於TRX_ACTIVE的事務,回滾。
MySQL在打開binlog時,會檢查binlog的狀態(TC_LOG_BINLOG::open)。如果binlog沒有正常關閉(LOG_EVENT_BINLOG_IN_USE_F為1),則進行恢復操作,基本流程如下:
<1>掃描binlog,讀取XID_EVENT事務,得到所有已經提交的XA事務列表(實際上事務在innodb可能處於prepare或者commit);
<2>對每個XA事務,調用handlerton::recover,檢查存儲引擎是否存在處於prepare狀態的該事務(見innobase_xa_recover),也就是檢查該XA事務在存儲引擎中的狀態;
<3>如果存在處於prepare狀態的該XA事務,則調用handlerton::commit_by_xid提交事務;
<4>否則,調用handlerton::rollback_by_xid回滾XA事務。
5.3.3 幾個參數討論
(1)sync_binlog
Mysql在提交事務時調用MYSQL_LOG::write完成寫binlog,並根據sync_binlog決定是否進行刷盤。默認值是0,即不刷盤,從而把控制權讓給OS。如果設為1,則每次提交事務,就會進行一次刷盤;這對性能有影響(5.6已經支持binlog group),所以很多人將其設置為100。
bool MYSQL_LOG::flush_and_sync()
{
int err=0, fd=log_file.file;
safe_mutex_assert_owner(&LOCK_log);
if (flush_io_cache(&log_file))
return 1;
if (++sync_binlog_counter >= sync_binlog_period && sync_binlog_period)
{
sync_binlog_counter= 0;
err=my_sync(fd, MYF(MY_WME));
}
return err;
}
(2) innodb_flush_log_at_trx_commit
該參數控制innodb在提交事務時刷redo log的行為。默認值為1,即每次提交事務,都進行刷盤操作。為了降低對性能的影響,在很多生產環境設置為2,甚至0。
If the value of innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues.
The default value of 1 is required for full ACID compliance. You can achieve better performance by setting the value different from 1, but then you can lose up to one second worth of transactions in a crash. With a value of 0, any mysqld process crash can erase the last second of transactions. With a value of 2, only an operating system crash or a power outage can erase the last second of transactions.
(3) innodb_support_xa
用於控制innodb是否支持XA事務的2PC,默認是TRUE。如果關閉,則innodb在prepare階段就什么也不做;這可能會導致binlog的順序與innodb提交的順序不一致(比如A事務比B事務先寫binlog,但是在innodb內部卻可能A事務比B事務后提交),這會導致在恢復或者slave產生不同的數據。
int
innobase_xa_prepare(
/*================*/
/* out: 0 or error number */
THD* thd, /* in: handle to the MySQL thread of the user
whose XA transaction should be prepared */
bool all) /* in: TRUE - commit transaction
FALSE - the current SQL statement ended */
{
…
if (!thd->variables.innodb_support_xa) {
return(0);
}
5.3.4 安全性/性能討論
上面3個參數不同的值會帶來不同的效果。三者都設置為1(TRUE),數據才能真正安全。sync_binlog非1,可能導致binlog丟失(OS掛掉),從而與innodb層面的數據不一致。innodb_flush_log_at_trx_commit非1,可能會導致innodb層面的數據丟失(OS掛掉),從而與binlog不一致。
關於性能分析,可以參考
http://www.mysqlperformanceblog.com/2011/03/02/what-is-innodb_support_xa/
作者:YY哥
出處:http://www.cnblogs.com/hustcat/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。