Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Goutham Veeramachaneni <[email protected]>
  • Loading branch information
gouthamve committed Sep 20, 2018
1 parent 9ea6fd5 commit e68f826
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 16 additions & 4 deletions wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,13 @@ func (w *SegmentWAL) run(interval time.Duration) {

// Close syncs all data and closes the underlying resources.
func (w *SegmentWAL) Close() error {
// Make sure you can call Close() multiple times.
select {
case <-w.stopc:
return nil // Already closed.
default:
}

close(w.stopc)
<-w.donec

Expand All @@ -736,7 +743,7 @@ func (w *SegmentWAL) Close() error {
// only the current segment will still be open.
if hf := w.head(); hf != nil {
if err := hf.Close(); err != nil {
return errors.Wrapf(hf.Close(), "closing WAL head %s", hf.Name())
return errors.Wrapf(err, "closing WAL head %s", hf.Name())
}
}

Expand Down Expand Up @@ -1262,6 +1269,7 @@ func MigrateWAL(logger log.Logger, dir string) (err error) {
if err != nil {
return errors.Wrap(err, "open new WAL")
}

// It should've already been closed as part of the previous finalization.
// Do it once again in case of prior errors.
defer func() {
Expand All @@ -1274,6 +1282,7 @@ func MigrateWAL(logger log.Logger, dir string) (err error) {
if err != nil {
return errors.Wrap(err, "open old WAL")
}
defer w.Close()

rdr := w.Reader()

Expand Down Expand Up @@ -1307,12 +1316,15 @@ func MigrateWAL(logger log.Logger, dir string) (err error) {
if err != nil {
return errors.Wrap(err, "write new entries")
}
if err := repl.Close(); err != nil {
return errors.Wrap(err, "close new WAL")
}
// We explicitly close even when there is a defer for Windows to be
// able to delete it. The defer is in place to close it in-case there
// are errors above.
if err := w.Close(); err != nil {
return errors.Wrap(err, "close old WAL")
}
if err := repl.Close(); err != nil {
return errors.Wrap(err, "close new WAL")
}
if err := fileutil.Replace(tmpdir, dir); err != nil {
return errors.Wrap(err, "replace old WAL")
}
Expand Down
5 changes: 5 additions & 0 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ func (w *WAL) Repair(origErr error) error {
if err != nil {
return errors.Wrap(err, "open segment")
}
defer f.Close()

r := NewReader(bufio.NewReader(f))

for r.Next() {
Expand All @@ -329,6 +331,9 @@ func (w *WAL) Repair(origErr error) error {
}
// We expect an error here from r.Err(), so nothing to handle.

// We explicitly close even when there is a defer for Windows to be
// able to delete it. The defer is in place to close it in-case there
// are errors above.
if err := f.Close(); err != nil {
return errors.Wrap(err, "close corrupted file")
}
Expand Down

0 comments on commit e68f826

Please sign in to comment.