精彩文章免费看

Linux:o_direct 后调用fsync的原因


MySQL 可以使用o_direct打开数据文件,但是还是调用了fsync,原因如下:

  • MySQL官方解释
    https://stackoverflow.com/questions/41440492/why-mysql-still-use-fsync-to-flush-the-data-when-the-option-is-o-direct
    MySQL bug [#45892](https://bugs.mysql.com/bug.php?id=45892) contains additional information:
    > Some testing by Domas has shown that some filesystems (XFS) do not sync metadata without the fsync. If the metadata would change, then you need to still use fsync (or O_SYNC for file open).
    > For example, if a file grows while O_DIRECT is enabled it will still write to the new part of the file, however since the metadata doesn't reflect the new size of the file the tail portion can be lost in the event of a crash.
    > Solution:
    > Continue to use fsync when important metadata changes or use O_SYNC in addition to O_DIRECT.
    
  • Linux api解释
  •        O_DIRECT (Since Linux 2.4.10)
                  Try  to  minimize cache effects of the I/O to and from this file.  In general this will degrade performance, but it is useful in special situations, such as when applica‐
                  tions do their own caching.  File I/O is done directly to/from user-space buffers.  The O_DIRECT flag on its own makes an effort to transfer data synchronously, but  does
                  not  give  the guarantees of the O_SYNC flag that data and necessary metadata are transferred.  To guarantee synchronous I/O, O_SYNC must be used in addition to O_DIRECT.
                  See NOTES below for further discussion.
                  A semantically similar (but deprecated) interface for block devices is described in raw(8).
    
  •