Use fcntl(fd,F_FULLFSYNC) instead of fsync on OSX, improve power failure safety (#9545)

On MacOS calling fsync does not guarantee the cache on the disk itself is flushed.
This commit is contained in:
guoxiang1996 2021-10-15 13:44:25 +08:00 committed by GitHub
parent 24b67d5520
commit 3c9e5271c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,7 @@
#define __CONFIG_H
#ifdef __APPLE__
#include <fcntl.h> // for fcntl(fd, F_FULLFSYNC)
#include <AvailabilityMacros.h>
#endif
@ -97,10 +98,12 @@
#endif
/* Define redis_fsync to fdatasync() in Linux and fsync() for all the rest */
#ifdef __linux__
#define redis_fsync fdatasync
#if defined(__linux__)
#define redis_fsync(fd) fdatasync(fd)
#elif defined(__APPLE__)
#define redis_fsync(fd) fcntl(fd, F_FULLFSYNC)
#else
#define redis_fsync fsync
#define redis_fsync(fd) fsync(fd)
#endif
#if __GNUC__ >= 4
@ -122,6 +125,8 @@
#if (defined(__linux__) && defined(SYNC_FILE_RANGE_WAIT_BEFORE))
#define HAVE_SYNC_FILE_RANGE 1
#define rdb_fsync_range(fd,off,size) sync_file_range(fd,off,size,SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE)
#elif defined(__APPLE__)
#define rdb_fsync_range(fd,off,size) fcntl(fd, F_FULLFSYNC)
#else
#define rdb_fsync_range(fd,off,size) fsync(fd)
#endif