MySQL double write



doublewrite


默认存放在ibdata1中共享表空间里,默认大小2M,写之前将脏页写入到innodb buffer中的doublewrite buffer(2M)中,将2M的buffer数据直接写入到共享表空间的doublewrite段中,当写共享表空间的doublewrite失败了,没有关系,因为此时的数据文件ibd中的数据是干净的,处于一致的状态,可以通过redo进行恢复,【先写共享表空间的double 再写数据文件ibd】,doublewrite是覆盖写,共享表空间只存2M给doublewrite用。Redo能恢复的页一定是干净的完整的一致的,因为mysql会有partial write所以引入doublewrite机制。



double write buffer







double write(ibdata1)







ibd


如果页大小是

16k

,那么就有

128

个页

(1M)

需要写,但是

128

个页写入到共享表空间是

1



IO

完成,也就是说

doublewrite

写开销是

1+128

次。其中

128

次是写数据文件表空间。

doublewrite

写入是顺序的,性能开销取决于写入量,通常

5%-25%

的性能影响。


当系统负载非常高的情况下,性能开销减少

25%

Double write

技术对用户来说是透明的,我们只关注下面参数开启或关闭即可。


1、Fursion-io


原子写,如果每次写16k就是16k,每次写都是16k不会出现部分写partial write写4k的情况。设备带来的益处。


2、


特定的文件系统,支持原子写。


b-tree file system


介绍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

相关推荐: xtrabackup备份恢复测试

测试准备 1、安装xtrabackup工具 以下测试安装xtrabackup备份恢复工具的二进制包,进行测试。安装的路径为mysql安装路径,本次测试mysql的安装路径为:/home/q/percona-server/。 tar -xzf percona-x…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/05 11:57
下一篇 06/05 11:57

相关推荐