<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        MySQLbinlog組提交與XA(兩階段提交)_MySQL

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-09 20:10:05
        文檔

        MySQLbinlog組提交與XA(兩階段提交)_MySQL

        MySQLbinlog組提交與XA(兩階段提交)_MySQL:1. XA-2PC (two phase commit, 兩階段提交 ) XA是由X/Open組織提出的分布式事務(wù)的規(guī)范(X代表transaction; A代表accordant?)。XA規(guī)范主要定義了(全局)事務(wù)管理器(TM: Transaction Manager)和(局部)資源管理器(RM: Resource
        推薦度:
        導(dǎo)讀MySQLbinlog組提交與XA(兩階段提交)_MySQL:1. XA-2PC (two phase commit, 兩階段提交 ) XA是由X/Open組織提出的分布式事務(wù)的規(guī)范(X代表transaction; A代表accordant?)。XA規(guī)范主要定義了(全局)事務(wù)管理器(TM: Transaction Manager)和(局部)資源管理器(RM: Resource

        1. XA-2PC (two phase commit, 兩階段提交 )

        XA是由X/Open組織提出的分布式事務(wù)的規(guī)范(X代表transaction; A代表accordant?)。XA規(guī)范主要定義了(全局)事務(wù)管理器(TM: Transaction Manager)和(局部)資源管理器(RM: Resource Manager)之間的接口。XA為了實現(xiàn)分布式事務(wù),將事務(wù)的提交分成了兩個階段:也就是2PC (tow phase commit),XA協(xié)議就是通過將事務(wù)的提交分為兩個階段來實現(xiàn)分布式事務(wù)。

        1.1 prepare 階段:

        第一階段,事務(wù)管理器向所有涉及到的數(shù)據(jù)庫服務(wù)器發(fā)出prepare"準備提交"請求,數(shù)據(jù)庫收到請求后執(zhí)行數(shù)據(jù)修改和日志記錄等處理,處理完成后只是把事務(wù)的狀態(tài)改成"可以提交",然后把結(jié)果返回給事務(wù)管理器。

        1.2 commit 階段:

        事務(wù)管理器收到回應(yīng)后進入第二階段,如果在第一階段內(nèi)有任何一個數(shù)據(jù)庫的操作發(fā)生了錯誤,或者事務(wù)管理器收不到某個數(shù)據(jù)庫的回應(yīng),則認為事務(wù)失敗,回撤所有數(shù)據(jù)庫的事務(wù)。數(shù)據(jù)庫服務(wù)器收不到第二階段的確認提交請求,也會把"可以提交"的事務(wù)回撤。如果第一階段中所有數(shù)據(jù)庫都提交成功,那么事務(wù)管理器向數(shù)據(jù)庫服務(wù)器發(fā)出"確認提交"請求,數(shù)據(jù)庫服務(wù)器把事務(wù)的"可以提交"狀態(tài)改為"提交完成"狀態(tài),然后返回應(yīng)答。

        2. MySQL 中的XA實現(xiàn)

        Support for XA transactions is available for the InnoDB storage engine. The MySQL XA implementation is based on the X/Open CAE document Distributed Transaction Processing: The XA Specification.

        Currently, among the MySQL Connectors, MySQL Connector/J 5.0.0 and higher supports XA directly, by means of a class interface that handles the XA SQL statement interface for you.

        XA supports distributed transactions, that is, the ability to permit multiple separate transactional resources to participate in a global transaction. Transactional resources often are RDBMSs but may be other kinds of resources.

        A global transaction involves several actions that are transactional in themselves, but that all must either complete successfully as a group, or all be rolled back as a group. In essence, this extends ACID properties “up a level” so that multiple ACID transactions can be executed in concert as components of a global operation that also has ACID properties. (However, for a distributed transaction, you must use the SERIALIZABLE isolation level to achieve ACID properties. It is enough to use REPEATABLE READ for a nondistributed transaction, but not for a distributed transaction.)

        最重要的一點:使用MySQL中的XA實現(xiàn)分布式事務(wù)時必須使用serializable隔離級別。

        The MySQL implementation of XA MySQL enables a MySQL server to act as a Resource Manager that handles XA transactions within a global transaction. A client program that connects to the MySQL server acts as the Transaction Manager.

        The process for executing a global transaction uses two-phase commit (2PC). This takes place after the actions performed by the branches of the global transaction have been executed.

        In the first phase, all branches are prepared. That is, they are told by the TM to get ready to commit. Typically, this means each RM that manages a branch records the actions for the branch in stable storage. The branches indicate whether they are able to do this, and these results are used for the second phase.

        In the second phase, the TM tells the RMs whether to commit or roll back. If all branches indicated when they were prepared that they will be able to commit, all branches are told to commit. If any branch indicated when it was prepared that it will not be able to commit, all branches are told to roll back.

        第一階段:為prepare階段,TM向RM發(fā)出prepare指令,RM進行操作,然后返回成功與否的信息給TM;

        第二階段:為事務(wù)提交或者回滾階段,如果TM收到所有RM的成功消息,則TM向RM發(fā)出提交指令;不然則發(fā)出回滾指令;

        XA transaction support is limited to the InnoDB storage engine.(只有innodb支持XA分布式事務(wù))

        For "external XA" a MySQL server acts as a Resource Manager and client programs act as Transaction Managers. For "Internal XA", storage engines within a MySQL server act as RMs, and the server itself acts as a TM. Internal XA support is limited by the capabilities of individual storage engines. Internal XA is required for handling XA transactions that involve more than one storage engine. The implementation of internal XA requires that a storage engine support two-phase commit at the table handler level, and currently this is true only for InnoDB.

        MySQL中的XA實現(xiàn)分為:外部XA和內(nèi)部XA;前者是指我們通常意義上的分布式事務(wù)實現(xiàn);后者是指單臺MySQL服務(wù)器中,Server層作為TM(事務(wù)協(xié)調(diào)者),而服務(wù)器中的多個數(shù)據(jù)庫實例作為RM,而進行的一種分布式事務(wù),也就是MySQL跨庫事務(wù);也就是一個事務(wù)涉及到同一條MySQL服務(wù)器中的兩個innodb數(shù)據(jù)庫(因為其它引擎不支持XA)。

        3. 內(nèi)部XA的額外功能

        XA 將事務(wù)的提交分為兩個階段,而這種實現(xiàn),解決了 binlog 和 redo log的一致性問題,這就是MySQL內(nèi)部XA的第三種功能。

        MySQL為了兼容其它非事物引擎的復(fù)制,在server層面引入了 binlog, 它可以記錄所有引擎中的修改操作,因而可以對所有的引擎使用復(fù)制功能;MySQL在4.x 的時候放棄redo的復(fù)制策略而引入binlog的復(fù)制(淘寶丁奇)。

        但是引入了binlog,會導(dǎo)致一個問題——binlog和redo log的一致性問題:一個事務(wù)的提交必須寫redo log和binlog,那么二者如何協(xié)調(diào)一致呢?事務(wù)的提交以哪一個log為標準?如何判斷事務(wù)提交?事務(wù)崩潰恢復(fù)如何進行?

        MySQL通過兩階段提交(內(nèi)部XA的兩階段提交)很好地解決了這一問題:

        第一階段:InnoDB prepare,持有prepare_commit_mutex,并且write/sync redo log; 將回滾段設(shè)置為Prepared狀態(tài),binlog不作任何操作;

        第二階段:包含兩步,1> write/sync Binlog; 2> InnoDB commit (寫入COMMIT標記后釋放prepare_commit_mutex);

        以 binlog 的寫入與否作為事務(wù)提交成功與否的標志,innodb commit標志并不是事務(wù)成功與否的標志。因為此時的事務(wù)崩潰恢復(fù)過程如下:

        1> 崩潰恢復(fù)時,掃描最后一個Binlog文件,提取其中的xid;

        2> InnoDB維持了狀態(tài)為Prepare的事務(wù)鏈表,將這些事務(wù)的xid和Binlog中記錄的xid做比較,如果在Binlog中存在,則提交,否則回滾事務(wù)。

        通過這種方式,可以讓InnoDB和Binlog中的事務(wù)狀態(tài)保持一致。如果在寫入innodb commit標志時崩潰,則恢復(fù)時,會重新對commit標志進行寫入;

        在prepare階段崩潰,則會回滾,在write/sync binlog階段崩潰,也會回滾。這種事務(wù)提交的實現(xiàn)是MySQL5.6之前的實現(xiàn)。

        4. binlog 組提交

        上面的事務(wù)的兩階段提交過程是5.6之前版本中的實現(xiàn),有嚴重的缺陷。當(dāng)sync_binlog=1時,很明顯上述的第二階段中的 write/sync binlog會成為瓶頸,而且還是持有全局大鎖(prepare_commit_mutex: prepare 和 commit共用一把鎖),這會導(dǎo)致性能急劇下降。解決辦法就是MySQL5.6中的 binlog組提交。

        4.1 MySQL5.6中的binlog group commit:

        將Binlog Group Commit的過程拆分成了三個階段:

        1> flush stage 將各個線程的binlog從cache寫到文件中;

        2> sync stage 對binlog做fsync操作(如果需要的話;最重要的就是這一步,對多個線程的binlog合并寫入磁盤);

        3> commit stage 為各個線程做引擎層的事務(wù)commit(這里不用寫redo log,在prepare階段已寫)。每個stage同時只有一個線程在操作。(分成三個階段,每個階段的任務(wù)分配給一個專門的線程,這是典型的并發(fā)優(yōu)化)

        這種實現(xiàn)的優(yōu)勢在于三個階段可以并發(fā)執(zhí)行,從而提升效率。注意prepare階段沒有變,還是write/sync redo log.

        (另外:5.7中引入了MTS:多線程slave復(fù)制,也是通過binlog組提交實現(xiàn)的,在binlog組提交時,給每一個組提交打上一個seqno,然后在slave中就可以按照master中一樣按照seqno的大小順序,進行事務(wù)組提交了。)

        4.2 MySQL5.7中的binlog group commit:

        淘寶對binlog group commit進行了進一步的優(yōu)化,其原理如下:

        從XA恢復(fù)的邏輯我們可以知道,只要保證InnoDB Prepare的redo日志在寫B(tài)inlog前完成write/sync即可。因此我們對Group Commit的第一個stage的邏輯做了些許修改,大概描述如下:

        Step1. InnoDB Prepare,記錄當(dāng)前的LSN到thd中;

        Step2. 進入Group Commit的flush stage;Leader搜集隊列,同時算出隊列中最大的LSN。

        Step3. 將InnoDB的redo log write/fsync到指定的LSN (注:這一步就是redo log的組寫入。因為小于等于LSN的redo log被一次性寫入到ib_logfile[0|1])

        Step4. 寫B(tài)inlog并進行隨后的工作(sync Binlog, InnoDB commit , etc)

        也就是將 redo log的write/sync延遲到了 binlog group commit的 flush stage 之后,sync binlog之前。

        通過延遲寫redo log的方式,顯式的為redo log做了一次組寫入(redo log group write),并減少了(redo log) log_sys->mutex的競爭。

        也就是將 binlog group commit 對應(yīng)的redo log也進行了 group write. 這樣binlog 和 redo log都進行了優(yōu)化。

        官方MySQL在5.7.6的代碼中引入了淘寶的優(yōu)化,對應(yīng)的Release Note如下:

        When using InnoDB with binary logging enabled, concurrent transactions written in the InnoDB redo log are now grouped together before synchronizing to disk when innodb_flush_log_at_trx_commit is set to 1, which reduces the amount of synchronization operations. This can lead to improved performance.

        5. XA參數(shù) innodb_support_xa

        http://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_support_xa

        Command-Line Format --innodb_support_xa

        System Variable Name innodb_support_xa

        Variable Scope Global, Session

        Dynamic Variable Yes

        Permitted Values Type boolean

        Default TRUE

        Enables InnoDB support for two-phase commit(2PC) in XA transactions, causing an extra disk flush for transaction preparation. This setting is the default. The XA mechanism is used internally and is essential for any server that has its binary log turned on and is accepting changes to its data from more than one thread. If you turn it off, transactions can be written to the binary log in a different order from the one in which the live database is committing them. This can produce different data when the binary log is replayed in disaster recovery or on a replication slave. Do not turn it off on a replication master server unless you have an unusual setup where only one thread is able to change data.

        For a server that is accepting data changes from only one thread, it is safe and recommended to turn off this option to improve performance forInnoDB tables. For example, you can turn it off on replication slaves where only the replication SQL thread is changing data.

        You can also turn off this option if you do not need it for safe binary logging or replication, and you also do not use an external XA transaction manager.

        參數(shù)innodb_support_xa默認為true,表示啟用XA,雖然它會導(dǎo)致一次額外的磁盤flush(prepare階段flush redo log). 但是我們必須啟用,而不能關(guān)閉它。因為關(guān)閉會導(dǎo)致binlog寫入的順序和實際的事務(wù)提交順序不一致,會導(dǎo)致崩潰恢復(fù)和slave復(fù)制時發(fā)生數(shù)據(jù)錯誤。如果啟用了log-bin參數(shù),并且不止一個線程對數(shù)據(jù)庫進行修改,那么就必須啟用innodb_support_xa參數(shù)。

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        MySQLbinlog組提交與XA(兩階段提交)_MySQL

        MySQLbinlog組提交與XA(兩階段提交)_MySQL:1. XA-2PC (two phase commit, 兩階段提交 ) XA是由X/Open組織提出的分布式事務(wù)的規(guī)范(X代表transaction; A代表accordant?)。XA規(guī)范主要定義了(全局)事務(wù)管理器(TM: Transaction Manager)和(局部)資源管理器(RM: Resource
        推薦度:
        標簽: 提交 xa mysql
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 成人黄页网站免费观看大全| 亚洲一卡2卡4卡5卡6卡残暴在线| 亚洲欧洲免费无码| 三根一起会坏掉的好痛免费三级全黄的视频在线观看 | 亚洲国产品综合人成综合网站| 亚洲人成网站在线观看青青| 毛片免费在线观看网站| 久久免费观看国产精品88av| 特级aa**毛片免费观看| 亚洲国产精品无码久久久秋霞2| 色播在线永久免费视频| 成年大片免费视频播放一级| 日韩亚洲国产高清免费视频| 亚洲欧洲中文日韩av乱码| 日本免费在线观看| 国产免费久久久久久无码| 偷自拍亚洲视频在线观看99| 亚洲v高清理论电影| 成人在线免费观看| 99久久免费国产香蕉麻豆 | 国产免费变态视频网址网站| 成人免费一区二区无码视频| 插鸡网站在线播放免费观看| 污视频网站免费观看| 亚洲精品V天堂中文字幕| 亚洲AV中文无码字幕色三| 亚洲综合色成在线播放| 午夜国产羞羞视频免费网站| 免费观看男人免费桶女人视频| 成人免费一区二区无码视频| 最近中文字幕mv免费高清视频7 | 亚洲av永久无码制服河南实里| 国产专区一va亚洲v天堂| 亚洲国产精品13p| xvideos亚洲永久网址| 永久在线免费观看| 妻子5免费完整高清电视| 国产成在线观看免费视频| 妞干网手机免费视频| 日韩视频在线免费观看| 五月天婷亚洲天综合网精品偷|