這篇文章主要為大家展示了“MySQL 5.7中對XA支持的改進有哪些”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“MySQL 5.7中對XA支持的改進有哪些”這篇文章吧。
XA解決了當跨分布式資源情況下能在單個事務中保留ACID屬性的問題。資源本身可以是其他MySQL服務器,甚至可以其他是不同的數據庫技術。XA標準描述了全局事務管理器和本地資源管理器之間的交互。
如引言中所述,MySQL 5.0引入了XA支持,從而增加了參與全局事務的能力。XA支持可以提供可訪問事務資源的資源管理器和能夠在全局事務中協調事務的事務管理器。MySQL的XA實現了讓MySQL服務器來充當資源管理器,而連接到MySQL服務器的客戶端執行事務管理器的任務。
XA使用兩階段提交協議,其中第一階段是發出commit請求,然后再進行實際的commit。全局事務的各個分支完成執行后,將啟動兩階段提交協議:
在第一階段,事務管理器向全局事務中涉及的所有分支發出準備commit的消息。在資源管理器確認已準備好提交之前,它會將操作的記錄結果記錄并保存,為第二階段執行實際的提交做準備。
在第二階段,事務管理器如果從所有涉及的分支接收到確定的響應,則通知它們提交。但是,如果任何一個分支的答復為否,則會通知所有分支執行回滾。
一個事務管理器與多個資源管理器進行交互,以處理全局事務中的單個事務/分支。以下圖描述了涉及一個資源管理器中的XA事務。XA事務的語句以XA關鍵字,要執行的操作和唯一標識符開頭。在下面的示例中,字符串“ xatest”表示全局事務標識符。除了全局事務標識符之外,還可以為XA事務指定分支標識符和格式ID。分支標識符用于標識本地事務,格式ID指定前兩個組件使用的格式。
XA START / BEGIN 啟動事務并定義其全局事務標識符。
XA END 指定活動事務的結束。
XA PREPARE 為事務的COMMIT做準備。
XA COMMIT [ONE PHASE] COMMIT并結束一個已PREPARE的事務。
如果使用“單階段”選項,則準備和提交將在結束事務的單個步驟中執行。
XA ROLLBACK 回滾并終止事務。
XA RECOVER顯示有關所有PREPARED事務的信息。
讓我們看一下上述XA事務的狀態之間轉換。
XA START將事務置于活動狀態。一旦所有語句由活動事務執行后,就會發出XA_END語句,使該事務處于IDLE狀態。對于空閑事務,可以發出XA PREPARE或XA COMMIT ONE PHASE。XA PREPARE將事務置于PREPARED狀態。但是,XA COMMIT ONE PHASE會準備并提交事務。對于PREPARED XA事務,將發出XA COMMIT提交以結束事務。
在5.7.7之前,如果客戶端連接終止或服務器正常退出,則回滾PREPARED事務。當客戶端被殺死時,所有交易都會回滾。因此,即使XA事務處于PREPARED狀態,它也無法在XA RECOVER期間恢復該事務。理想情況下,在準備事務時,應該可以提交或回滾該事務。對于這種情況,讓我們看一下 錯誤12161中報告的 示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | mysql> CREATETABLEt1(fld1INT); QueryOK, 0 rowsaffected (0.01 sec) mysql> COMMIT; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> INSERTINTOt1VALUES (1); QueryOK, 1 rowaffected (0.00 sec) mysql> XAEND 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> Killed Nowstartanotherclientsession. mysql> XA 'test'; 1397 (XAE04): XAER_NOTA: UnknownXID mysql> XARECOVER; Emptyset (0.00 sec) |
同樣在5.7.7之前,如果XA事務處于PREPARED狀態且服務器異常退出,則可以在重新啟動服務器后恢復該事務-但不會復制該事務。 服務器重新啟動后,XA事務仍將以PREPARED狀態存在,但其內容無法記錄在二進制日志中。因此,二進制日志不同步,導致數據漂移。因此,XA不能安全地用于復制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | mysql> CREATETABLEt1(fld1INT); QueryOK, 0 rowsaffected (0.01 sec) mysql> COMMIT; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> INSERTINTOt1VALUES (1); QueryOK, 1 rowaffected (0.00 sec) mysql> XAEND 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) Nowkilltheserver. mysql> XARECOVER; 2006 (HY000): MySQLserverhasgoneaway Noconnection. Tryingto reconnect... Connectionid: 1 Currentdatabase: test +----------+--------------+--------------+------+ | formatID | gtrid_length | bqual_length | data | +----------+--------------+--------------+------+ | 1 | 4 | 0 | test | +----------+--------------+--------------+------+ 1 rowin set (0.02 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.02 sec) mysql> SHOWBINLOGEVENS\G; *************************** 1. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 4 Event_type: Format_desc Server_id: 1 End_log_pos: 120 Info: Serverver: 5.6.29-debug-log, Binlogver: 4 1 rowin set (0.00 sec) mysql> SELECT * FROMt1; +------+ | fld1 | +------+ | 1 | +------+ 1 rowin set (0.00 sec) |
overcoming the above mentioned restrictions required changes in the XA transaction recovery mechanism and binary logging mechanism. This improvement was made in 5.7.7 through the implementation of work log number 7193 and 6860/ bug 12161.
The XA recovery mechansim has been extended such that when a connection is terminated, the PREPARED XA transactions are left in the transaction cache and marked specially in InnoDB. This allows the client to RECOVER the PREPARED XA transactions and then COMMIT/ ROLLBACK.
The XA transactions are now binlogged in two phases using two different GTIDs which allows the transactions to be interleaved. During the first phase, when XA PREPARE is issued, the transaction up until that point is logged in the binary log and can be identified by XA_prepare_log_event.
During the second phase, when XA COMMIT/ROLLBACK is issued, the second part of the transaction is written into the binary log. Since XA PREPARE is persistent, the XA transaction is not rolled back and survives the server restart or client disconnect. The client can perform XA COMMIT/ROLLBACK and the binary log remains up to date. XA transactions also works well when GTID is ON and binary log is turned OFF.
Let us look at the output of the above examples after 5.7.7:
為了克服上述限制,需要對XA事務恢復機制和二進制日志記錄機制進行更改。通過執行工作日志編號 7193和 6860 / bug 12161在5.7.7中進行了改進。
XA恢復機制已得到擴展,以便在終止連接時,將PREPARED XA事務保留在事務緩存中,并在InnoDB中進行特殊標記。這允許客戶端恢復PREPARED XA事務,然后執行COMMIT / ROLLBACK。
現在,使用兩個不同的GTID在兩個階段對XA事務進行二進制記錄,從而可以使事務交織。在第一階段中,當發出XA PREPARE時,直到該點的事務都會記錄在二進制日志中,并且可以通過以下方式進行標識:XA_prepare_log_event.
在第二階段中,當發出XA COMMIT / ROLLBACK時,將事務的第二部分寫入二進制日志。由于XA PREPARE是持久性的,因此XA事務不會回滾,并且可以在服務器重新啟動或客戶端斷開連接后繼續存在??蛻舳丝梢詧绦蠿A COMMIT / ROLLBACK,并且二進制日志保持最新。當GTID設置為ON并且二進制日志設置為OFF時,XA事務也可以很好地工作。
讓我們看看5.7.7之后的上述示例的輸出:
客戶端斷開連接后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | mysql> CREATETABLEt1(fld1INT); QueryOK, 0 rowsaffected (0.01 sec) mysql> COMMIT; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> INSERTINTOt1VALUES (1); QueryOK, 1 rowaffected (0.00 sec) mysql> XAEND 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> Killed Nowstartanotherclientsession. mysql> XARECOVER; +----------+--------------+--------------+------+ | formatID | gtrid_length | bqual_length | data | +----------+--------------+--------------+------+ | 1 | 4 | 0 | test | +----------+--------------+--------------+------+ 1 rowin set (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.02 sec) |
服務器重啟后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | mysql> CREATETABLEt1(fld1INT); QueryOK, 0 rowsaffected (0.01 sec) mysql> COMMIT; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> INSERTINTOt1VALUES (1); QueryOK, 1 rowaffected (0.00 sec) mysql> XAEND 'test'; QueryOK, 0 rowsaffected (0.00 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.00 sec) Nowkilltheserver. mysql> XARECOVER; 2006 (HY000): MySQLserverhasgoneaway Noconnection. Tryingto reconnect... Connectionid: 1 Currentdatabase: test +----------+--------------+--------------+------+ | formatID | gtrid_length | bqual_length | data | +----------+--------------+--------------+------+ | 1 | 4 | 0 | test | +----------+--------------+--------------+------+ 1 rowin set (0.02 sec) mysql> XA 'test'; QueryOK, 0 rowsaffected (0.02 sec) mysql> SHOWBINLOGevents\G; *************************** 3. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 154 Event_type: Anonymous_Gtid Server_id: 0 End_log_pos: 219 Info: @@SESSION.GTID_NEXT= 'ANONYMOUS' *************************** 4. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 219 Event_type: Query Server_id: 0 End_log_pos: 319 Info: XA '74657374','',1 *************************** 5. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 319 Event_type: Query Server_id: 0 End_log_pos: 418 Info: use `test`; INSERTINTOt1VALUES (1) *************************** 6. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 418 Event_type: Query Server_id: 0 End_log_pos: 509 Info: XAEND '74657374','',1 *************************** 7. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000001 Pos: 509 Event_type: XA_prepare Server_id: 0 End_log_pos: 549 Info: XA '74657374','',1 *************************** 8. row *************************** Log_name: nisha-PORTEGE-Z30-A-bin.000002 Pos: 219 Event_type: Server_id: 0 End_log_pos: 313 Info: XA '74657374','',1 8 rowsin set (0.00 sec) |
以上是“MySQL 5.7中對XA支持的改進有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。