make necessary changes

This commit is contained in:
Yicheng Qin 2014-04-11 17:00:14 -07:00
parent 79a89dcb82
commit 56ef6fbcae
4 changed files with 15 additions and 13 deletions

View File

@ -33,7 +33,6 @@ import (
ehttp "github.com/coreos/etcd/http" ehttp "github.com/coreos/etcd/http"
"github.com/coreos/etcd/log" "github.com/coreos/etcd/log"
"github.com/coreos/etcd/metrics" "github.com/coreos/etcd/metrics"
"github.com/coreos/etcd/pkg/btrfs"
"github.com/coreos/etcd/server" "github.com/coreos/etcd/server"
"github.com/coreos/etcd/store" "github.com/coreos/etcd/store"
) )

View File

@ -1,6 +1,7 @@
package btrfs package btrfs
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"syscall" "syscall"
@ -39,33 +40,31 @@ func IsBtrfs(path string) bool {
return true return true
} }
// SetNOCOWDir sets NOCOW flag for the directory // SetNOCOWFile sets NOCOW flag for file
func SetNOCOWDir(path string) error { func SetNOCOWFile(path string) error {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
log.Warnf("Failed to open %v: %v", path, err)
return err return err
} }
defer file.Close() defer file.Close()
fileinfo, err := file.Stat() fileinfo, err := file.Stat()
if err != nil { if err != nil {
log.Warnf("Failed to stat %v: %v", path, err)
return err return err
} }
if !fileinfo.IsDir() { if fileinfo.IsDir() {
log.Infof("Skip NOCOW setting for non directory") return fmt.Errorf("skip directory")
return nil }
if fileinfo.Size() != 0 {
return fmt.Errorf("skip nonempty file")
} }
var attr int var attr int
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_GETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_GETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
log.Warnf("Failed to get file flags: %v", errno.Error())
return errno return errno
} }
attr |= FS_NOCOW_FL attr |= FS_NOCOW_FL
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_SETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_SETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
log.Warnf("Failed to set file flags: %v", errno.Error())
return errno return errno
} }
log.Infof("Set NOCOW to path %v succeeded", path) log.Infof("Set NOCOW to path %v succeeded", path)

View File

@ -9,14 +9,16 @@ import (
) )
func TestSetNOCOW(t *testing.T) { func TestSetNOCOW(t *testing.T) {
name, err := ioutil.TempDir(".", "etcdtest") f, err := ioutil.TempFile(".", "etcdtest")
if err != nil { if err != nil {
t.Fatal("Failed creating temp dir") t.Fatal("Failed creating temp dir")
} }
name := f.Name()
f.Close()
defer os.Remove(name) defer os.Remove(name)
if IsBtrfs(name) { if IsBtrfs(name) {
SetNOCOWDir(name) SetNOCOWFile(name)
cmd := exec.Command("lsattr", name) cmd := exec.Command("lsattr", name)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {

View File

@ -294,7 +294,9 @@ func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) er
// Set NOCOW for data directory in btrfs // Set NOCOW for data directory in btrfs
if btrfs.IsBtrfs(s.raftServer.LogPath()) { if btrfs.IsBtrfs(s.raftServer.LogPath()) {
btrfs.SetNOCOW(s.raftServer.LogPath()) if err := btrfs.SetNOCOWFile(s.raftServer.LogPath()); err != nil {
log.Warnf("Failed setting NOCOW: %v", err)
}
} }
s.findCluster(discoverURL, peers) s.findCluster(discoverURL, peers)