1. 程式人生 > >記一次 MySQL semaphore crash 的分析(有彩蛋)

記一次 MySQL semaphore crash 的分析(有彩蛋)

文章來源:愛可生雲資料庫作者:洪斌

DBA應該對InnoDB: Semaphore wait has lasted > 600 seconds. We intentionally crash the server because it appears to be hung. 一點都不陌生,MySQL後臺執行緒srv_error_monitor_thread發現存在阻塞超過600s的latch鎖時,如果連續10次檢測該鎖仍沒有釋放,就會觸發panic避免服務持續hang下去。

發生了什麼

版本號:MySQL 5.5.40

日誌中持續輸出執行緒等待資料字典鎖,位置是dict0dict.c line 305,等待時間超過了900s。持有鎖的執行緒是 139998697924352 ,其十六進位制是7f53fca8a700。

--Thread 139998393616128 has waited at dict0dict.c line 305 for 934.00 seconds the semaphore:
X-lock on RW-latch at 0x105a1b8 created in file dict0dict.c line 748
a writer (thread id 139998697924352) has reserved it in mode  exclusive
number of readers 0, waiters flag 1, lock_word: 0
Last time read locked in file dict0dict.c line 302
Last time write locked in file /pb2/build/sb_0-13157587-1410170252.03/rpm/BUILD/mysql-5.5.40/mysql-5.5.40/storage/innobase/dict/dict0dict.c line 305

檢查下持鎖執行緒 139998697924352 是否存在其他鎖等待。

發現執行緒 139998697924352 ,self-lock 在 btr0sea.c line 1134,該鎖結構和 AHI 相關。

--Thread 139998697924352 has waited at btr0sea.c line 1134 for 934.00 seconds the semaphore:
X-lock (wait_ex) on RW-latch at 0x1eb06448 created in file btr0sea.c line 178
a writer (thread id 139998697924352) has reserved it in mode  wait exclusive
number of readers 1, waiters flag 1, lock_word: ffffffffffffffff
Last time read locked in file btr0sea.c line 1057
Last time write locked in file /pb2/build/sb_0-13157587-1410170252.03/rpm/BUILD/mysql-5.5.40/mysql-5.5.40/storage/innobase/btr/btr0sea.c line 1134

接下來看下兩處鎖結構分別在哪個函式內:

1.dict0dict.c line 305在dict_table_stats_lock函式內2.btr0sea.c line 1134在btr_search_drop_page_hash_index函式內

什麼情況會呼叫這些函式?

啟用 innodb_table_monitor,輸出日誌時呼叫 dict_table_stats_lock 上 X 鎖,本案例未開啟。啟用 innodb_stats_on_metadata 時,查詢資料字典表會觸發統計資訊的更新操作,會呼叫 dict_table_stats_lock 上 X 鎖。這與持鎖執行緒的事務資訊匹配。Adaptive hash index(AHI) 是 InnoDB 用來加速索引頁查詢的 hash 表結構。當頁面訪問次數滿足一定條件後,這個頁面的地址將存入一個 hash 表中,減少 B 樹查詢的開銷。MySQL 5.5 版本 AHI 是由全域性鎖 btr_search_latch 維護 hash 表修改的一致性。InnoDB buffer pool 狀態顯示 free buffer 基本保持0空閒。InnoDB buffer pool 驅逐資料頁時,會呼叫 btr_search_drop_page_hash_index 函式,從 AHI 中清理該資料頁。

-----------------------------
BUFFER POOL AND MEMORY
-----------------------------
Total memory allocated 17582522368; in additional pool allocated 0
Dictionary memory allocated 4289681
Buffer pool size   1048576
Free buffers       0
Database pages     1040831
Old database pages 384193
Modified db pages  0

小結

AHI 的全域性鎖 btr_search_latch 經常會是競爭熱點影響效能,5.7版本後有所改善與 InnoDB buffer 一樣做了多例項拆分。本案例在開啟 Innodb_stats_on_metadata 引數,查詢元資料資訊時觸發統計資訊更新,上鎖資料字典,阻塞了了大量業務操作,又由於 buffer pool 空間不足,導致表驅逐舊頁觸發 AHI 的 btr_search_latch 鎖競爭,最終導致訊號量超時 crash。

!! 彩蛋 !!

在動輒幾兆的日誌中分析 Semaphore crash,尋找鎖、執行緒、事務之間的關係,相當令人抓狂的。藉助 sed、awk、grep 三大法寶,雖有效率提升,但仍不夠高效。

為了偷懶寫了一個小程式,幫助DBA快速梳理出這些關係。

它的用法是這樣的:

[email protected] ~> mysqldba doctor -f /Users/hongbin/workbench/mysqld_safe.log

目標版本,查程式碼時找對應版本:

MySQL Server Version: '5.7.16-log’

日誌中出現的 semaphore crash 次數和 mysql 啟動次數,如果啟動次數大於 crash 次數說明可能是正常啟動或其他 crash 造成:

********** MySQL service start count **********

MySQL Semaphore crash -> 3 times ["2018-08-13 23:12:18" "2018-08-14 12:13:43" "2018-08-16 13:42:36"]
  MySQL Service start -> 3 times ["2018-08-13 23:12:59" "2018-08-14 12:15:20" "2018-08-16 13:46:37"]

執行緒主要在等待哪些 RW-latch,內容包括:鎖位置、出現次數、執行緒 id (出現次數),重點關注出現次數較多的:

********** Which thread waited lock **********

    row0purge.cc:861 ->  58  140477266503424:(57) 140617703745280:(1)
         gi.cc:14791 ->   1  140477035656960:(1)
     trx0undo.ic:171 ->   1  140617682765568:(1)
  ha_innodb.cc:14791 -> 620  140617389913856:(58) 140202719565568:(58) 140202716903168:(57) 140477029533440:(56) 140617407219456:(55) 140477035656960:(52) 140477035124480:(29) 140477108467456:(29) 140477025539840:(26) 140477031130880:(25) 140477027669760:(22) 140617634944768:(21) 140617634146048:(21) 140477019948800:(21) 140477026604800:(20) 140477022078720:(18) 140477018883840:(16) 140477038585600:(15) 140477028734720:(10) 140477022877440:(9) 140477034325760:(1) 140477031663360:(1)
     srv0srv.cc:1968 -> 208  140477276993280:(185) 140617714235136:(23)
   ha_innodb.cc:5510 -> 601  140617398167296:(57) 140617409615616:(55) 140617392043776:(53) 140477110597376:(52) 140617395771136:(50) 140617636275968:(45) 140617632548608:(40) 140617634146048:(33) 140617639675648:(32) 140617397102336:(28) 140617639409408:(23) 140617635743488:(21) 140617637811968:(18) 140617638610688:(16) 140617399232256:(12) 140617638344448:(10) 140617638078208:(10) 140477033793280:(10) 140477029267200:(10) 140617397368576:(9) 140617635211008:(6) 140617393641216:(5) 140617637545728:(3) 140617402693376:(2) 140477037254400:(1)
   dict0dict.cc:1239 -> 136  140477122623232:(50) 140617392842496:(35) 140202726487808:(26) 140477123688192:(12) 140477038851840:(5) 140477030065920:(4) 140617634412288:(4)
   row0trunc.cc:1835 ->   1  140477109798656:(1)

上述鎖被哪些寫執行緒持有 X 鎖,重點關注出現次數較多的:

********** Which writer threads block at **********
     140616681907968 ->   1  trx0undo.ic:171:(1)
     140477173069568 -> 243  srv0srv.cc:1968:(185) row0purge.cc:861:(57) row0trunc.cc:1835:(1)
     140617682765568 ->  29  srv0srv.cc:1968:(23) ha_innodb.cc:5510:(5) row0purge.cc:861:(1)

寫執行緒對應的事務資訊,也可能存在日誌記錄沒有輸出事務資訊:

********** These writer threads trx state **********
MySQL thread id 83874, OS thread handle 140477173069568, query id 13139674 10.0.1.146 aml deleting from reference tables

統計寫執行緒持有 S 鎖情況:

****These writer threads at last time reads locked ****
     140477173069568 -> 243  row0purge.cc:861:(243)
     140617682765568 ->  24  row0purge.cc:861:(24)
     140616681907968 ->   1  trx0undo.ic:190:(1)

統計寫執行緒持有 X 鎖情況:

****These writer threads at last time write locked ****
     140477173069568 -> 243  dict0stats.cc:2366:(243)
     140617682765568 ->  24  dict0stats.cc:2366:(24)
     140616681907968 ->   1  buf0flu.cc:1198:(1)

通過事後日誌分析,有可能出現執行緒的事務資訊沒有輸出到日誌中的情況,無法獲知事務具體執行了什麼操作。應對這種情況,小程式加入了事中採集事務資訊。

用法是這樣的:

[email protected] ~> mysqldba -uxxx -pxxx doctor -w

它會監視目標 mysql 的錯誤日誌,一旦出現“a writer (thread id 140616681907968) has reserved it in mode” 關鍵字就查詢 ps 中的事務資訊,並儲存下來。

以上只是小程式一個用法,作為一個為DBA服務的小程式,還有其他功能等你去發現。歡迎與我交流你的想法。