1. 程式人生 > >淺談mysql的兩階段提交協議

淺談mysql的兩階段提交協議

前兩天和百度的一個同學聊MySQL兩階段提交,當時自信滿滿的說了一堆,後來發現還是有些問題的理解還是比較模糊,可能是因為時間太久了,忘記了吧。這裡再補一下:)
5.3.1事務提交流程

MySQL的事務提交邏輯主要在函式ha_commit_trans中完成。事務的提交涉及到binlog及具體的儲存的引擎的事務提交。所以MySQL用2PC來保證的事務的完整性。MySQL的2PC過程如下:

clip_image002

(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),則進行恢復操作,基本流程如下:

clip_image004

<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。

clip_image006

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不一致。

關於效能分析,可以參考


作者:YY哥
出處:http://www.cnblogs.com/hustcat/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。