diff options
| author | Rain Li <blacktear23@gmail.com> | 2022-08-10 23:21:57 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-10 08:21:57 -0700 |
| commit | 670cb759f82815461bc854569542582da51a6199 (patch) | |
| tree | 39e3e2cdcbf92b9fcf957f5507c0f9544375e634 | |
| parent | 662ec97602443a702c13c34283e72ab239291048 (diff) | |
| download | seaweedfs-670cb759f82815461bc854569542582da51a6199.tar.xz seaweedfs-670cb759f82815461bc854569542582da51a6199.zip | |
Refactor for Sync method (#3426)
| -rw-r--r-- | weed/storage/backend/disk_file.go | 10 | ||||
| -rw-r--r-- | weed/storage/backend/disk_file_darwin.go | 43 | ||||
| -rw-r--r-- | weed/storage/backend/disk_file_linux.go | 14 | ||||
| -rw-r--r-- | weed/storage/backend/disk_file_others.go | 9 |
4 files changed, 69 insertions, 7 deletions
diff --git a/weed/storage/backend/disk_file.go b/weed/storage/backend/disk_file.go index 0dd153d5d..93a2f8ccc 100644 --- a/weed/storage/backend/disk_file.go +++ b/weed/storage/backend/disk_file.go @@ -1,10 +1,11 @@ package backend import ( - "github.com/seaweedfs/seaweedfs/weed/glog" - . "github.com/seaweedfs/seaweedfs/weed/storage/types" "os" "time" + + "github.com/seaweedfs/seaweedfs/weed/glog" + . "github.com/seaweedfs/seaweedfs/weed/storage/types" ) var ( @@ -79,8 +80,3 @@ func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) { func (df *DiskFile) Name() string { return df.fullFilePath } - -func (df *DiskFile) Sync() error { - return nil - // return df.File.Sync() -} diff --git a/weed/storage/backend/disk_file_darwin.go b/weed/storage/backend/disk_file_darwin.go new file mode 100644 index 000000000..76be810db --- /dev/null +++ b/weed/storage/backend/disk_file_darwin.go @@ -0,0 +1,43 @@ +//go:build darwin +// +build darwin + +package backend + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +const ( + // Using default File.Sync function, same as fcntl(fd, F_FULLFSYNC) + DM_SYNC = 1 + + // Using syscall.Fsync function, for MacOS this is not safe but is very fast. + DM_FSYNC = 2 + + // Using fcntl with F_BARRIERFSYNC parameter, for more details please refer: + // https://developer.apple.com/documentation/xcode/reducing-disk-writes + DM_BFSYNC = 3 + + F_BARRIERFSYNC = 85 +) + +var ( + // By default using F_BARRIERFSYNC + DarwinSyncMode = DM_BFSYNC +) + +func (df *DiskFile) Sync() error { + switch DarwinSyncMode { + case DM_SYNC: + return df.File.Sync() + case DM_BFSYNC: + fd := df.File.Fd() + _, err := unix.FcntlInt(fd, F_BARRIERFSYNC, 0) + return err + default: + fd := df.File.Fd() + return syscall.Fsync(int(fd)) + } +} diff --git a/weed/storage/backend/disk_file_linux.go b/weed/storage/backend/disk_file_linux.go new file mode 100644 index 000000000..a38489dcf --- /dev/null +++ b/weed/storage/backend/disk_file_linux.go @@ -0,0 +1,14 @@ +//go:build linux +// +build linux + +package backend + +import ( + "syscall" +) + +// Using Fdatasync to optimize file sync operation +func (df *DiskFile) Sync() error { + fd := df.File.Fd() + return syscall.Fdatasync(int(fd)) +} diff --git a/weed/storage/backend/disk_file_others.go b/weed/storage/backend/disk_file_others.go new file mode 100644 index 000000000..08d29d217 --- /dev/null +++ b/weed/storage/backend/disk_file_others.go @@ -0,0 +1,9 @@ +//go:build !linux && !darwin +// +build !linux,!darwin + +package backend + +// Using default sync operation +func (df *DiskFile) Sync() error { + return df.File.Sync() +} |
