Browse Source

Refactor for Sync method (#3426)

pull/3431/head
Rain Li 2 years ago
committed by GitHub
parent
commit
670cb759f8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      weed/storage/backend/disk_file.go
  2. 43
      weed/storage/backend/disk_file_darwin.go
  3. 14
      weed/storage/backend/disk_file_linux.go
  4. 9
      weed/storage/backend/disk_file_others.go

10
weed/storage/backend/disk_file.go

@ -1,10 +1,11 @@
package backend package backend
import ( import (
"github.com/seaweedfs/seaweedfs/weed/glog"
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
"os" "os"
"time" "time"
"github.com/seaweedfs/seaweedfs/weed/glog"
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
) )
var ( var (
@ -79,8 +80,3 @@ func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) {
func (df *DiskFile) Name() string { func (df *DiskFile) Name() string {
return df.fullFilePath return df.fullFilePath
} }
func (df *DiskFile) Sync() error {
return nil
// return df.File.Sync()
}

43
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))
}
}

14
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))
}

9
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()
}
Loading…
Cancel
Save