Skip to content

Commit

Permalink
raft: cleanup wal directory if creation fails
Browse files Browse the repository at this point in the history
  delete <data-dir>/member/wal if any operation after the rename in
  wal.Create fails to avoid reading an inconsistent WAL on restart.

  Fixes #10688
  • Loading branch information
Joshua Coutinho authored and joshcc3 committed Apr 28, 2019
1 parent efcc108 commit 38ec8d2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
zap.Error(perr),
)
}
w.cleanupWAL(lg)
return nil, perr
}
if perr = fileutil.Fsync(pdir); perr != nil {
Expand All @@ -206,9 +207,10 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
zap.Error(perr),
)
}
w.cleanupWAL(lg)
return nil, perr
}
if perr = pdir.Close(); err != nil {
if perr = pdir.Close(); perr != nil {
if lg != nil {
lg.Warn(
"failed to close the parent data directory file",
Expand All @@ -217,12 +219,31 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
zap.Error(perr),
)
}
w.cleanupWAL(lg)
return nil, perr
}

return w, nil
}

func (w *WAL) cleanupWAL(lg *zap.Logger) {
var err error
if err = w.Close(); err != nil {
if lg != nil {
lg.Panic("failed to cleanup WAL", zap.Error(err))
} else {
plog.Panicf("failed to cleanup WAL: %v", err)
}
}
if err = os.RemoveAll(w.dir); err != nil {
if lg != nil {
lg.Panic("failed to cleanup WAL", zap.Error(err))
} else {
plog.Panicf("failed to cleanup WAL: %v", err)
}
}
}

func (w *WAL) renameWAL(tmpdirpath string) (*WAL, error) {
if err := os.RemoveAll(w.dir); err != nil {
return nil, err
Expand Down

0 comments on commit 38ec8d2

Please sign in to comment.