diff --git a/tsdb/engine/tsm1/compact.go b/tsdb/engine/tsm1/compact.go index 9ab37139ee..948bd7fce6 100644 --- a/tsdb/engine/tsm1/compact.go +++ b/tsdb/engine/tsm1/compact.go @@ -748,6 +748,11 @@ func (c *Compactor) CompactFull(tsmFiles []string) ([]string, error) { c.mu.RUnlock() if !enabled { + c.mu.Lock() + defer c.mu.Unlock() + if err := c.cleanupDisabledCompaction(tsmFiles); err != nil { + return nil, err + } return nil, errCompactionsDisabled } @@ -777,6 +782,11 @@ func (c *Compactor) CompactFast(tsmFiles []string) ([]string, error) { c.mu.RUnlock() if !enabled { + c.mu.Lock() + defer c.mu.Unlock() + if err := c.cleanupDisabledCompaction(tsmFiles); err != nil { + return nil, err + } return nil, errCompactionsDisabled } @@ -784,6 +794,24 @@ func (c *Compactor) CompactFast(tsmFiles []string) ([]string, error) { } +// cleanupDisabledCompaction is responsible for cleaning up a compaction that +// was started, but then abandoned before the temporary files were dealt with. +func (c *Compactor) cleanupDisabledCompaction(tsmFiles []string) error { + for _, f := range tsmFiles { + files, err := filepath.Glob(filepath.Join(filepath.Dir(f), fmt.Sprintf("*.%s", CompactionTempExtension))) + if err != nil { + return fmt.Errorf("error cleaning up compaction temp files: %s", err.Error()) + } + + for _, f := range files { + if err := os.Remove(f); err != nil { + return fmt.Errorf("error removing temp compaction file: %v", err) + } + } + } + return nil +} + // writeNewFiles writes from the iterator into new TSM files, rotating // to a new file once it has reached the max TSM file size. func (c *Compactor) writeNewFiles(generation, sequence int, iter KeyIterator) ([]string, error) {