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

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"寫在前面"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前段時間搭建了一套MySQL分佈式數據庫集羣,數據庫節點有12個,用來測試各種分佈式事務方案的性能和優缺點。測試MySQL XA事務時,正當測試腳本向數據庫中批量插入數據時,強制服務器斷電!注意:是直接拔電源,使其瞬間斷電,再次重啓服務器後,MySQL數據庫報錯了。特此記錄MySQL XA事務的恢復。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"MySQL XA事務問題"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務器強制斷電後重啓,此時MySQL報錯,查看MySQL啓動日誌時,發現如下所示的錯誤信息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"InnoDB: The log sequence number in ibdata files does not match\nInnoDB: the log sequence number in the ib_logfiles!\n100224 23:24:20 InnoDB: Database was not shut down normally!\nInnoDB: Starting crash recovery.\nInnoDB: Reading tablespace information from the .ibd files...\nInnoDB: Restoring possible half-written data pages from the doublewrite\nInnoDB: buffer...\nInnoDB: Transaction 0 4497755 was in the XA prepared state.\nInnoDB: Transaction 0 4468551 was in the XA prepared state.\nInnoDB: Transaction 0 4468140 was in the XA prepared state.\nInnoDB: 3 transaction(s) which must be rolled back or cleaned up\nInnoDB: in total 0 row operations to undo\nInnoDB: Trx id counter is 0 5312768\nInnoDB: Starting in background the rollback of uncommitted transactions\n100224 23:24:20 InnoDB: Rollback of non-prepared transactions completed\n100224 23:24:20 InnoDB: Started; log sequence number 0 3805002509\n100224 23:24:20 InnoDB: Starting recovery for XA transactions...\n100224 23:24:20 InnoDB: Transaction 0 4497755 in prepared state after recovery\n100224 23:24:20 InnoDB: Transaction contains changes to 8 rows\n100224 23:24:20 InnoDB: Transaction 0 4468551 in prepared state after recovery\n100224 23:24:20 InnoDB: Transaction contains changes to 1 rows\n100224 23:24:20 InnoDB: Transaction 0 4468140 in prepared state after recovery\n100224 23:24:20 InnoDB: Transaction contains changes to 1 rows\n100224 23:24:20 InnoDB: 3 transactions in prepared state after recovery\n100224 23:24:20 [Note] Found 3 prepared transaction(s) in InnoDB\n100224 23:24:20 [Warning] Found 3 prepared XA transactions\n100224 23:24:20 [Note] Event Scheduler: Loaded 0 events\n100224 23:24:20 [Note] /opt/mysql/bin/mysqld: ready for connections.\nVersion: '8.0.18' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL) "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從上面的日誌信息中,可以看出有三個XA的事務沒有提交或回滾。那該如何恢復MySQL的XA事務呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"恢復MySQL XA事務"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,登錄到MySQL,執行如下命令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"mysql> xa recover;\n+----------+--------------+--------------+------------------------------------------------------------+\n| formatID | gtrid_length | bqual_length | data |\n+----------+--------------+--------------+------------------------------------------------------------+\n| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f06397f000001:bae5:4b6928eb:f0650 |\n| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:fb5c37f000001:bae5:4b6928eb:fb5cd |\n| 131075 | 30 | 28 | 1-7f000001:bae5:4b6928eb:f03ea7f000001:bae5:4b6928eb:f0400 |\n+----------+--------------+--------------+------------------------------------------------------------+ "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據表示信息如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"formatIDis the formatIDpart of the transaction xid\ngtrid_lengthis the length in bytes of the gtridpart of the xid\nbqual_lengthis the length in bytes of the bqualpart of the xid\ndatais the concatenation of the gtridand bqualparts of the xid "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是三個XA事務的信息,準備直接回滾。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"mysql> xa rollback '1-7f000001:bae5:4b6928eb:fb5c3','7f000001:bae5:4b6928eb:fb5cd',131075;\nQuery OK, 0 rows affected (0.41 sec) "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"MySQL XA事務補充"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"XA事務支持限於InnoDB存儲引擎。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL XA實施是針對外部XA的,其中,MySQL服務器作爲資源管理器,而客戶端程序作爲事務管理器。未實施“內部XA”。這樣,就允許MySQL服務器內的單獨存儲引擎作爲RM(資源管理器),而服務器本身作爲TM(事務管理器)。處理包含1個以上存儲引擎的XA事務時,需要內部XA。內部XA的實施是不完整的,這是因爲,它要求存儲引擎在表處理程序層面上支持兩階段提交,目前僅對InnoDB實現了該特性。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於XA START,不支持JOIN和RESUME子句。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於XA END,不支持SUSPEND [FOR MIGRATE]子句。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在全局事務內,對於每個XA事務,xid值的bqual部分應是不同的,該要求是對當前MySQL XA實施的限制。它不是XA規範的組成部分。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果XA事務達到PREPARED狀態而且MySQL服務器宕機,當服務器重啓後,能夠繼續處理事務。就像原本應當的那樣。但是,如果客戶端連接中止而服務器繼續運行,服務器將回滾任何未完成的XA事務,即使該事務已達到PREPARED狀態也同樣。它應能提交或回滾PREPARED XA事務,但在不更改二進制日誌機制的情況下不能這樣。 \t\t\t\t\t\t\t\t"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"重磅福利"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"微信搜一搜【冰河技術】微信公衆號,關注這個有深度的程序員,每天閱讀超硬核技術乾貨,公衆號內回覆【PDF】有我準備的一線大廠面試資料和我原創的超硬核PDF技術文檔,以及我爲大家精心準備的多套簡歷模板(不斷更新中),希望大家都能找到心儀的工作,學習是一條時而鬱鬱寡歡,時而開懷大笑的路,加油。如果你通過努力成功進入到了心儀的公司,一定不要懈怠放鬆,職場成長和新技術學習一樣,不進則退。如果有幸我們江湖再見! "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,我開源的各個PDF,後續我都會持續更新和維護,感謝大家長期以來對冰河的支持!!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"寫在最後"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你覺得冰河寫的還不錯,請微信搜索並關注「 "},{"type":"text","marks":[{"type":"strong"}],"text":"冰河技術"},{"type":"text","text":" 」微信公衆號,跟冰河學習高併發、分佈式、微服務、大數據、互聯網和雲原生技術,「 "},{"type":"text","marks":[{"type":"strong"}],"text":"冰河技術"},{"type":"text","text":" 」微信公衆號更新了大量技術專題,每一篇技術文章乾貨滿滿!不少讀者已經通過閱讀「 "},{"type":"text","marks":[{"type":"strong"}],"text":"冰河技術"},{"type":"text","text":" 」微信公衆號文章,吊打面試官,成功跳槽到大廠;也有不少讀者實現了技術上的飛躍,成爲公司的技術骨幹!如果你也想像他們一樣提升自己的能力,實現技術能力的飛躍,進大廠,升職加薪,那就關注「 "},{"type":"text","marks":[{"type":"strong"}],"text":"冰河技術"},{"type":"text","text":" 」微信公衆號吧,每天更新超硬核技術乾貨,讓你對如何提升技術能力不再迷茫!"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/86/86642f445ccf193620f77650ac1ae2bc.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章