## 【分布式事務】面試官問我:MySQL中的XA事務崩潰了如何恢復??


寫在前面

前段時間搭建了一套MySQL分布式數據庫集群,數據庫節點有12個,用來測試各種分布式事務方案的性能和優缺點。測試MySQL XA事務時,正當測試腳本向數據庫中批量插入數據時,強制服務器斷電!注意:是直接拔電源,使其瞬間斷電,再次重啟服務器后,MySQL數據庫報錯了。特此記錄MySQL XA事務的恢復。

MySQL XA事務問題

服務器強制斷電后重啟,此時MySQL報錯,查看MySQL啟動日志時,發現如下所示的錯誤信息。

InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
100224 23:24:20 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
InnoDB: Transaction 0 4497755 was in the XA prepared state.
InnoDB: Transaction 0 4468551 was in the XA prepared state.
InnoDB: Transaction 0 4468140 was in the XA prepared state.
InnoDB: 3 transaction(s) which must be rolled back or cleaned up
InnoDB: in total 0 row operations to undo
InnoDB: Trx id counter is 0 5312768
InnoDB: Starting in background the rollback of uncommitted transactions
100224 23:24:20 InnoDB: Rollback of non-prepared transactions completed
100224 23:24:20 InnoDB: Started; log sequence number 0 3805002509
100224 23:24:20 InnoDB: Starting recovery for XA transactions...
100224 23:24:20 InnoDB: Transaction 0 4497755 in prepared state after recovery
100224 23:24:20 InnoDB: Transaction contains changes to 8 rows
100224 23:24:20 InnoDB: Transaction 0 4468551 in prepared state after recovery
100224 23:24:20 InnoDB: Transaction contains changes to 1 rows
100224 23:24:20 InnoDB: Transaction 0 4468140 in prepared state after recovery
100224 23:24:20 InnoDB: Transaction contains changes to 1 rows
100224 23:24:20 InnoDB: 3 transactions in prepared state after recovery
100224 23:24:20 [Note] Found 3 prepared transaction(s) in InnoDB
100224 23:24:20 [Warning] Found 3 prepared XA transactions
100224 23:24:20 [Note] Event Scheduler: Loaded 0 events
100224 23:24:20 [Note] /opt/mysql/bin/mysqld: ready for connections.
Version: '8.0.18' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL) 

從上面的日志信息中,可以看出有三個XA的事務沒有提交或回滾。那該如何恢復MySQL的XA事務呢?

恢復MySQL XA事務

首先,登錄到MySQL,執行如下命令。

mysql> xa recover;
+----------+--------------+--------------+------------------------------------------------------------+
| formatID | gtrid_length | bqual_length | data |
+----------+--------------+--------------+------------------------------------------------------------+
| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f06397f000001:bae5:4b6928eb:f0650 |
| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:fb5c37f000001:bae5:4b6928eb:fb5cd |
| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f03ea7f000001:bae5:4b6928eb:f0400 |
+----------+--------------+--------------+------------------------------------------------------------+ 

數據表示信息如下:

formatIDis the formatIDpart of the transaction xid
gtrid_lengthis the length in bytes of the gtridpart of the xid
bqual_lengthis the length in bytes of the bqualpart of the xid
datais the concatenation of the gtridand bqualparts of the xid 

這是三個XA事務的信息,准備直接回滾。

mysql> xa rollback '1-7f000001:bae5:4b6928eb:fb5c3','7f000001:bae5:4b6928eb:fb5cd',131075;
Query OK, 0 rows affected (0.41 sec) 

MySQL XA事務補充

XA事務支持限於InnoDB存儲引擎。

MySQL XA實施是針對外部XA的,其中,MySQL服務器作為資源管理器,而客戶端程序作為事務管理器。未實施“內部XA”。這樣,就允許MySQL服務器內的單獨存儲引擎作為RM(資源管理器),而服務器本身作為TM(事務管理器)。處理包含1個以上存儲引擎的XA事務時,需要內部XA。內部XA的實施是不完整的,這是因為,它要求存儲引擎在表處理程序層面上支持兩階段提交,目前僅對InnoDB實現了該特性。

對於XA START,不支持JOIN和RESUME子句。

對於XA END,不支持SUSPEND [FOR MIGRATE]子句。

在全局事務內,對於每個XA事務,xid值的bqual部分應是不同的,該要求是對當前MySQL XA實施的限制。它不是XA規范的組成部分。

如果XA事務達到PREPARED狀態而且MySQL服務器宕機,當服務器重啟后,能夠繼續處理事務。就像原本應當的那樣。但是,如果客戶端連接中止而服務器繼續運行,服務器將回滾任何未完成的XA事務,即使該事務已達到PREPARED狀態也同樣。它應能提交或回滾PREPARED XA事務,但在不更改二進制日志機制的情況下不能這樣。

重磅福利

微信搜一搜【冰河技術】微信公眾號,關注這個有深度的程序員,每天閱讀超硬核技術干貨,公眾號內回復【PDF】有我准備的一線大廠面試資料和我原創的超硬核PDF技術文檔,以及我為大家精心准備的多套簡歷模板(不斷更新中),希望大家都能找到心儀的工作,學習是一條時而郁郁寡歡,時而開懷大笑的路,加油。如果你通過努力成功進入到了心儀的公司,一定不要懈怠放松,職場成長和新技術學習一樣,不進則退。如果有幸我們江湖再見!

另外,我開源的各個PDF,后續我都會持續更新和維護,感謝大家長期以來對冰河的支持!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM