共計 2652 個字符,預計需要花費 7 分鐘才能閱讀完成。
這篇文章給大家介紹如何淺析 mysql 中的 double write,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
介紹 double write 之前我們有必要了解 partial page write 問題 :
InnoDB 的 Page Size 一般是 16KB,其數據校驗也是針對這 16KB 來計算的,將數據寫入到磁盤是以 Page 為單位進行操作的。而計算機硬件和操作系統,在極端情況下(比如斷電)往往并不能保證這一操作的原子性,16K 的數據,寫入 4K 時,發生了系統斷電 /os crash,只有一部分寫是成功的,這種情況下就是 partial page write 問題。
很多 DBA 會想到系統恢復后,MySQL 可以根據 redolog 進行恢復,而 mysql 在恢復的過程中是檢查 page 的 checksum,checksum 就是 pgae 的最后事務號,發生 partial page write 問題時,page 已經損壞,找不到該 page 中的事務號,就無法恢復。
一 double write 是什么?
Double write 是 InnoDB 在 tablespace 上的 128 個頁(2 個區)是 2MB;
其原理:
為了解決 partial page write 問題,當 mysql 將臟數據 flush 到 data file 的時候, 先使用 memcopy 將臟數據復制到內存中的 double write buffer,之后通過 double write buffer 再分 2 次,每次寫入 1MB 到共享表空間,然后馬上調用 fsync 函數,同步到磁盤上,避免緩沖帶來的問題,在這個過程中,doublewrite 是順序寫,開銷并不大,在完成 doublewrite 寫入后,在將 double write buffer 寫入各表空間文件,這時是離散寫入。
如果發生了極端情況(斷電),InnoDB 再次啟動后,發現了一個 Page 數據已經損壞,那么此時就可以從 doublewrite buffer 中進行數據恢復了。
二 double write 的缺點是什么?
位于共享表空間上的 double write buffer 實際上也是一個文件,寫 DWB 會導致系統有更多的 fsync 操作, 而硬盤的 fsync 性能, 所以它會降低 mysql 的整體性能. 但是并不會降低到原來的 50%. 這主要是因為:
1) double write 是一個連接的存儲空間, 所以硬盤在寫數據的時候是順序寫, 而不是隨機寫, 這樣性能更高.
2) 將數據從 double write buffer 寫到真正的 segment 中的時候, 系統會自動合并連接空間刷新的方式, 每次可以刷新多個 pages;
三 double write 在恢復的時候是如何工作的?
If there’s a partial page write to the doublewrite buffer itself, the original page will still be on disk in its real location.-
– 如果是寫 doublewrite buffer 本身失敗, 那么這些數據不會被寫到磁盤,InnoDB 此時會從磁盤載入原始的數據, 然后通過 InnoDB 的事務日志來計算出正確的數據, 重新 寫入到 doublewrite buffer.
When InnoDB recovers, it will use the original page instead of the corrupted copy in the doublewrite buffer. However, if the doublewrite buffer succeeds and the write to the page’s real location fails, InnoDB will use the copy in the doublewrite buffer during recovery.
– 如果 doublewrite buffer 寫成功的話, 但是寫磁盤失敗,InnoDB 就不用通過事務日志來計算了, 而是直接用 buffer 的數據再寫一遍.
InnoDB knows when a page is corrupt because each page has a checksum at the end; the checksum is the last thing to be written, so if the page’s contents don’t match the checksum, the page is corrupt. Upon recovery, therefore, InnoDB just reads each page in the doublewrite buffer and verifies the checksums. If a page’s checksum is incorrect, it reads the page from its original location.
– 在恢復的時候,InnoDB 直接比較頁面的 checksum, 如果不對的話, 就從硬盤載入原始數據, 再由事務日志 開始推演出正確的數據. 所以 InnoDB 的恢復通常需要較長的時間.
四 我們是否一定需要 double write?
In some cases, the doublewrite buffer really isn’t necessary—for example, you might want to disable it on slaves. Also, some filesystems (such as ZFS) do the same thing themselves, so it is redundant for InnoDB to do it. You can disable the doublewrite buffer by setting InnoDB_doublewrite to 0.
五 如何使用 double write
InnoDB_doublewrite= 1 表示啟動 double write
show status like InnoDB_dblwr% 可以查詢 double write 的使用情況;
相關參數與狀態
Double write 的使用情況:
show status like %InnoDB_dblwr%
InnoDB_dblwr_pages_written 從 bp flush 到 DBWB 的個數
InnoDB_dblwr_writes 寫文件的次數
每次寫操作合并 page 的個數 = InnoDB_dblwr_pages_written/InnoDB_dblwr_writes
關于如何淺析 mysql 中的 double write 就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。