Skip to content

Commit

Permalink
wal: fsync directory after wal file rename
Browse files Browse the repository at this point in the history
Fixes #6368
  • Loading branch information
Anthony Romano authored and gyuho committed Sep 9, 2016
1 parent 6e83ec0 commit 202da92
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
50 changes: 43 additions & 7 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ var (
// A just opened WAL is in read mode, and ready for reading records.
// The WAL will be ready for appending after reading out all the previous records.
type WAL struct {
dir string // the living directory of the underlay files
dir string // the living directory of the underlay files

// dirFile is a fd for the wal directory for syncing on Rename
dirFile *os.File

metadata []byte // metadata recorded at the head of each WAL
state raftpb.HardState // hardstate recorded at the head of WAL

Expand Down Expand Up @@ -106,7 +110,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
if err != nil {
return nil, err
}
if _, err := f.Seek(0, os.SEEK_END); err != nil {
if _, err = f.Seek(0, os.SEEK_END); err != nil {
return nil, err
}
if err := fileutil.Preallocate(f.File, segmentSizeBytes, true); err != nil {
Expand All @@ -119,17 +123,33 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
encoder: newEncoder(f, 0),
}
w.locks = append(w.locks, f)
if err := w.saveCrc(0); err != nil {
if err = w.saveCrc(0); err != nil {
return nil, err
}
if err = w.encoder.encode(&walpb.Record{Type: metadataType, Data: metadata}); err != nil {
return nil, err
}
if err := w.encoder.encode(&walpb.Record{Type: metadataType, Data: metadata}); err != nil {
if err = w.SaveSnapshot(walpb.Snapshot{}); err != nil {
return nil, err
}
if err := w.SaveSnapshot(walpb.Snapshot{}); err != nil {

if w, err = w.renameWal(tmpdirpath); err != nil {
return nil, err
}

return w.renameWal(tmpdirpath)
// directory was renamed; sync parent dir to persist rename
pdir, perr := fileutil.OpenDir(path.Dir(w.dir))
if perr != nil {
return nil, perr
}
if perr = fileutil.Fsync(pdir); perr != nil {
return nil, perr
}
if perr = pdir.Close(); err != nil {
return nil, perr
}

return w, nil
}

// Open opens the WAL at the given snap.
Expand All @@ -139,7 +159,14 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
// the given snap. The WAL cannot be appended to before reading out all of its
// previous records.
func Open(dirpath string, snap walpb.Snapshot) (*WAL, error) {
return openAtIndex(dirpath, snap, true)
w, err := openAtIndex(dirpath, snap, true)
if err != nil {
return nil, err
}
if w.dirFile, err = fileutil.OpenDir(w.dir); err != nil {
return nil, err
}
return w, nil
}

// OpenForRead only opens the wal files for read.
Expand Down Expand Up @@ -371,6 +398,10 @@ func (w *WAL) cut() error {
if err = os.Rename(newTail.Name(), fpath); err != nil {
return err
}
if err = fileutil.Fsync(w.dirFile); err != nil {
return err
}

newTail.Close()

if newTail, err = fileutil.LockFile(fpath, os.O_WRONLY, fileutil.PrivateFileMode); err != nil {
Expand Down Expand Up @@ -473,6 +504,11 @@ func (w *WAL) Close() error {
plog.Errorf("failed to unlock during closing wal: %s", err)
}
}

if err := w.dirFile.Close(); err != nil {
return err
}

return nil
}

Expand Down
10 changes: 8 additions & 2 deletions wal/wal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package wal

import "os"
import (
"os"

"github.com/coreos/etcd/pkg/fileutil"
)

func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
// On non-Windows platforms, hold the lock while renaming. Releasing
Expand All @@ -34,5 +38,7 @@ func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
}

w.fp = newFilePipeline(w.dir, segmentSizeBytes)
return w, nil
df, err := fileutil.OpenDir(w.dir)
w.dirFile = df
return w, err
}

0 comments on commit 202da92

Please sign in to comment.