Skip to content

Commit

Permalink
Return error when TSM writer close returns one
Browse files Browse the repository at this point in the history
The TSM writer uses a bufio.Writer that needs to be flushed before
it's closed.  If the flush fails for some reason, the error is not
handled by the defer and the compactor continues on as if all is good.
This can create files with truncated indexes or zero-length TSM files.

Fixes #5889
  • Loading branch information
jwilder committed Mar 21, 2016
1 parent 8afff49 commit a4e5446
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tsdb/engine/tsm1/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func (c *Compactor) writeNewFiles(generation, sequence int, iter KeyIterator) ([
return files, nil
}

func (c *Compactor) write(path string, iter KeyIterator) error {
func (c *Compactor) write(path string, iter KeyIterator) (err error) {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return fmt.Errorf("%v already file exists. aborting", path)
}
Expand All @@ -553,7 +553,12 @@ func (c *Compactor) write(path string, iter KeyIterator) error {
if err != nil {
return err
}
defer w.Close()
defer func() {
closeErr := w.Close()
if err == nil {
err = closeErr
}
}()

for iter.Next() {
select {
Expand Down Expand Up @@ -590,7 +595,6 @@ func (c *Compactor) write(path string, iter KeyIterator) error {
if err := w.WriteIndex(); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit a4e5446

Please sign in to comment.