Skip to content

Commit

Permalink
wal: fsync directory after wal file rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Romano committed Sep 7, 2016
1 parent 0b63502 commit c108b9d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 @@ -373,6 +377,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 @@ -467,6 +475,7 @@ func (w *WAL) Close() error {
return err
}
}

for _, l := range w.locks {
if l == nil {
continue
Expand All @@ -475,7 +484,8 @@ func (w *WAL) Close() error {
plog.Errorf("failed to unlock during closing wal: %s", err)
}
}
return nil

return w.dirFile.Close()
}

func (w *WAL) saveEntry(e *raftpb.Entry) error {
Expand Down
7 changes: 7 additions & 0 deletions wal/wal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
}

w.fp = newFilePipeline(w.dir, SegmentSizeBytes)

df, derr := os.Open(w.dir)
if derr != nil {
return nil, derr
}
w.dirFile = df

return w, nil
}
8 changes: 8 additions & 0 deletions wal/wal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
newWAL.Close()
return nil, err
}

// windows expects a writeable file for fsync
df, derr := os.OpenFile(w.dir, os.O_WRONLY, fileutil.PrivateFileMode)
if derr != nil {
return nil, derr
}
w.dirFile = df

return newWAL, nil
}

0 comments on commit c108b9d

Please sign in to comment.