概述
今天主要介紹下MySQL一個崩潰恢復(fù)很重要的特性--double write.
The doublewrite buffer is a storage area located in the system tablespace where InnoDB writes pages that are flushed from the InnoDB buffer pool, before the pages are written to their proper positions in the data file. Only after flushing and writing pages to the doublewrite buffer, does InnoDB write pages to their proper positions. If there is an operating system, storage subsystem, or mysqld process crash in the middle of a page write, InnoDB can later find a good copy of the page from the doublewrite buffer during crash recovery.
一、partial page write 問題
InnoDB 的Page Size一般是16KB,其數(shù)據(jù)校驗也是針對這16KB來計算的,將數(shù)據(jù)寫入到磁盤是以Page為單位進(jìn)行操作的。而計算機硬件和操作系統(tǒng),在極端情況下(比如斷電)往往并不能保證這一操作的原子性,16K的數(shù)據(jù),寫入4K 時,發(fā)生了系統(tǒng)斷電/os crash ,只有一部分寫是成功的,這種情況下就是 partial page write 問題。
很多DBA 會想到系統(tǒng)恢復(fù)后,MySQL 可以根據(jù)redolog 進(jìn)行恢復(fù),而mysql在恢復(fù)的過程中是檢查page的checksum,checksum就是pgae的最后事務(wù)號,發(fā)生partial page write 問題時,page已經(jīng)損壞,找不到該page中的事務(wù)號,就無法恢復(fù)。

二、 double write原理(double write buffer --> double write(ibdata1) --> ibd)
Double write 是InnoDB在 tablespace上的128個頁(2個區(qū))是2MB;
其原理:
為了解決 partial page write 問題 ,當(dāng)mysql將臟數(shù)據(jù)flush到data file的時候, 先使用memcopy 將臟數(shù)據(jù)復(fù)制到內(nèi)存中的double write buffer ,之后通過double write buffer再分2次,每次寫入1MB到共享表空間,然后馬上調(diào)用fsync函數(shù),同步到磁盤上,避免緩沖帶來的問題,在這個過程中,doublewrite是順序?qū)懀_銷并不大,在完成doublewrite寫入后,在將double write buffer寫入各表空間文件,這時是離散寫入。
如果發(fā)生了極端情況(斷電),InnoDB再次啟動后,發(fā)現(xiàn)了一個Page數(shù)據(jù)已經(jīng)損壞,那么此時就可以從doublewrite buffer中進(jìn)行數(shù)據(jù)恢復(fù)了。

三、double write對性能的影響
位于共享表空間上的double write buffer實際上也是一個文件,寫DWB會導(dǎo)致系統(tǒng)有更多的fsync操作, 而硬盤的fsync性能, 所以它會降低mysql的整體性能. 但是并不會降低到原來的50%. 這主要是因為:
1) double write 是一個連接的存儲空間, 所以硬盤在寫數(shù)據(jù)的時候是順序?qū)? 而不是隨機寫, 這樣性能更高.
2) 將數(shù)據(jù)從double write buffer寫到真正的segment中的時候, 系統(tǒng)會自動合并連接空間刷新的方式, 每次可以刷新多個pages;
如果頁大小是 16k ,那么就有 128 個頁 (1M) 需要寫,但是 128 個頁寫入到共享表空間是 1 次 IO 完成,也就是說doublewrite 寫開銷是 1+128 次。其中 128 次是寫數(shù)據(jù)文件表空間。
doublewrite 寫入是順序的,性能開銷取決于寫入量,通常 5%-25% 的性能影響。
四、double write在恢復(fù)的時候是如何工作的?
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本身失敗,那么這些數(shù)據(jù)不會被寫到磁盤,InnoDB此時會從磁盤載入原始的數(shù)據(jù),然后通過InnoDB的事務(wù)日志來計算出正確的數(shù)據(jù),重新 寫入到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就不用通過事務(wù)日志來計算了,而是直接用buffer的數(shù)據(jù)再寫一遍.
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.
在恢復(fù)的時候,InnoDB直接比較頁面的checksum,如果不對的話,就從硬盤載入原始數(shù)據(jù),再由事務(wù)日志 開始推演出正確的數(shù)據(jù).所以InnoDB的恢復(fù)通常需要較長的時間.
五、 double write相關(guān)參數(shù)
InnoDB_doublewrite=1表示啟動double write,show status like 'InnoDB_dblwr%'可以查詢double write的使用情況;
#是否開啟double write mysql> show variables like '%double%write%'; #Double write的使用情況 mysql> show status like "%InnoDB_dblwr%"; InnoDB_dblwr_pages_written #從bp flush 到 DBWB的個數(shù) InnoDB_dblwr_writes #寫文件的次數(shù)
每次寫操作合并page的個數(shù)= InnoDB_dblwr_pages_written/InnoDB_dblwr_writes

六、是否一定需要 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.
1、Fursion-io 原子寫,如果每次寫16k就是16k,每次寫都是16k不會出現(xiàn)部分寫partial write寫4k的情況。
2、特定的文件系統(tǒng)( b-tree file system),支持原子寫。