Ensure temp index files are cleaned up on error

pull/8886/head
Jason Wilder 2017-09-27 07:59:07 -06:00
parent a5afaf7499
commit 70817350b7
2 changed files with 41 additions and 0 deletions

View File

@ -973,6 +973,18 @@ func (c *Compactor) write(path string, iter KeyIterator) (err error) {
if err == nil {
err = closeErr
}
// Check for errors where we should not remove the file
_, inProgress := err.(errCompactionInProgress)
maxBlocks := err == ErrMaxBlocksExceeded
maxFileSize := err == errMaxFileExceeded
if inProgress || maxBlocks || maxFileSize {
return
}
if err != nil {
w.Remove()
}
}()
for iter.Next() {

View File

@ -142,6 +142,8 @@ type TSMWriter interface {
// Size returns the current size in bytes of the file.
Size() uint32
Remove() error
}
// IndexWriter writes a TSMIndex.
@ -165,6 +167,8 @@ type IndexWriter interface {
WriteTo(w io.Writer) (int64, error)
Close() error
Remove() error
}
// IndexEntry is the index information for a given block in a TSM file.
@ -463,6 +467,18 @@ func (d *directIndex) Close() error {
return os.Remove(d.fd.Name())
}
// Remove removes the index from any tempory storage
func (d *directIndex) Remove() error {
if d.fd == nil {
return nil
}
if err := d.fd.Close(); err != nil {
return nil
}
return os.Remove(d.fd.Name())
}
// tsmWriter writes keys and values in the TSM format
type tsmWriter struct {
wrapped io.Writer
@ -652,6 +668,19 @@ func (t *tsmWriter) Close() error {
return nil
}
// Remove removes any temporary storage used by the writer.
func (t *tsmWriter) Remove() error {
if err := t.index.Remove(); err != nil {
return err
}
if f, ok := t.wrapped.(*os.File); ok {
f.Close()
return os.Remove(f.Name())
}
return nil
}
func (t *tsmWriter) Size() uint32 {
return uint32(t.n) + t.index.Size()
}