Skip to content

Commit

Permalink
Merge pull request #9690 from gyuho/wal-error
Browse files Browse the repository at this point in the history
wal,fileutil: add more logs, clarify error messages
  • Loading branch information
gyuho authored May 3, 2018
2 parents 62dfb89 + f3d9a85 commit cac6ed6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/fileutil/lock_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fileutil

import (
"fmt"
"io"
"os"
"syscall"
Expand Down Expand Up @@ -62,7 +63,7 @@ func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
return nil, fmt.Errorf("ofdTryLockFile failed to open %q (%v)", path, err)
}

flock := wrlck
Expand All @@ -83,15 +84,14 @@ func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
func ofdLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
return nil, fmt.Errorf("ofdLockFile failed to open %q (%v)", path, err)
}

flock := wrlck
err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLKW, &flock)

if err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, err
return &LockedFile{f}, nil
}
62 changes: 62 additions & 0 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,48 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
}
}
if err := fileutil.CreateDirAll(tmpdirpath); err != nil {
if lg != nil {
lg.Warn(
"failed to create a temporary WAL directory",
zap.String("tmp-dir-path", tmpdirpath),
zap.String("dir-path", dirpath),
zap.Error(err),
)
}
return nil, err
}

p := filepath.Join(tmpdirpath, walName(0, 0))
f, err := fileutil.LockFile(p, os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode)
if err != nil {
if lg != nil {
lg.Warn(
"failed to flock an initial WAL file",
zap.String("path", p),
zap.Error(err),
)
}
return nil, err
}
if _, err = f.Seek(0, io.SeekEnd); err != nil {
if lg != nil {
lg.Warn(
"failed to seek an initial WAL file",
zap.String("path", p),
zap.Error(err),
)
}
return nil, err
}
if err = fileutil.Preallocate(f.File, SegmentSizeBytes, true); err != nil {
if lg != nil {
lg.Warn(
"failed to preallocate an initial WAL file",
zap.String("path", p),
zap.Int64("segment-bytes", SegmentSizeBytes),
zap.Error(err),
)
}
return nil, err
}

Expand All @@ -143,18 +173,50 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
}

if w, err = w.renameWal(tmpdirpath); err != nil {
if lg != nil {
lg.Warn(
"failed to rename the temporary WAL directory",
zap.String("tmp-dir-path", tmpdirpath),
zap.String("dir-path", w.dir),
zap.Error(err),
)
}
return nil, err
}

// directory was renamed; sync parent dir to persist rename
pdir, perr := fileutil.OpenDir(filepath.Dir(w.dir))
if perr != nil {
if lg != nil {
lg.Warn(
"failed to open the parent data directory",
zap.String("parent-dir-path", filepath.Dir(w.dir)),
zap.String("dir-path", w.dir),
zap.Error(perr),
)
}
return nil, perr
}
if perr = fileutil.Fsync(pdir); perr != nil {
if lg != nil {
lg.Warn(
"failed to fsync the parent data directory file",
zap.String("parent-dir-path", filepath.Dir(w.dir)),
zap.String("dir-path", w.dir),
zap.Error(perr),
)
}
return nil, perr
}
if perr = pdir.Close(); err != nil {
if lg != nil {
lg.Warn(
"failed to close the parent data directory file",
zap.String("parent-dir-path", filepath.Dir(w.dir)),
zap.String("dir-path", w.dir),
zap.Error(perr),
)
}
return nil, perr
}

Expand Down

0 comments on commit cac6ed6

Please sign in to comment.