MySQL的事務提交邏輯主要在函數ha_commit_trans中完成。事務的提交涉及到binlog及具體的存儲的引擎的事務提交。因此MySQL用2PC來保證的事務的完整性。MySQL的2PC過程以下:mysql
(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)。sql
(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)。app
//ha_innodb.cc函數 static性能 intui innobase_commit(spa /*============*/日誌 /* out: 0 */orm 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 */ |
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事務。
(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);
}
上面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/