Skip to content

Commit 1efb8da

Browse files
fix: remove temp files on error in Compactor.writeNewFiles (#26074)
Compactor.writeNewFiles should delete temporary files created on iterations before an error halts the compaction. closes #26073
1 parent ba95c9b commit 1efb8da

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

tsdb/engine/tsm1/compact.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,17 @@ func (c *Compactor) RemoveTmpFiles(files []string) error {
10851085
return errors.Join(errs...)
10861086
}
10871087

1088+
func (c *Compactor) RemoveTmpFilesOnErr(files []string, originalErrs ...error) error {
1089+
removeErr := c.RemoveTmpFiles(files)
1090+
if removeErr == nil {
1091+
return errors.Join(originalErrs...)
1092+
} else if errJoin, ok := removeErr.(interface{ Unwrap() []error }); ok {
1093+
return errors.Join(append(originalErrs, errJoin.Unwrap()...)...)
1094+
} else {
1095+
return errors.Join(append(originalErrs, removeErr)...)
1096+
}
1097+
}
1098+
10881099
// writeNewFiles writes from the iterator into new TSM files, rotating
10891100
// to a new file once it has reached the max TSM file size.
10901101
func (c *Compactor) writeNewFiles(generation, sequence int, src []string, iter KeyIterator, throttle bool, logger *zap.Logger) ([]string, error) {
@@ -1113,7 +1124,8 @@ func (c *Compactor) writeNewFiles(generation, sequence int, src []string, iter K
11131124
// If the file only contained tombstoned entries, then it would be a 0 length
11141125
// file that we can drop.
11151126
if err := os.RemoveAll(fileName); err != nil {
1116-
return nil, err
1127+
// Only return an error if we couldn't remove the temp files
1128+
return nil, c.RemoveTmpFilesOnErr(files, err)
11171129
}
11181130
break
11191131
} else if errors.As(err, &eInProgress) {
@@ -1124,27 +1136,14 @@ func (c *Compactor) writeNewFiles(generation, sequence int, src []string, iter K
11241136
// planner keeps track of which files are assigned to compaction plans now.
11251137
logger.Warn("file exists, compaction in progress already", zap.String("output_file", fileName))
11261138
}
1127-
return nil, err
1128-
} else if err != nil {
1129-
var errs []error
1130-
errs = append(errs, err)
11311139
// We hit an error and didn't finish the compaction. Abort.
11321140
// Remove any tmp files we already completed
1133-
// discard later errors to return the first one from the write() call
1134-
for _, f := range files {
1135-
err = os.RemoveAll(f)
1136-
if err != nil {
1137-
errs = append(errs, err)
1138-
}
1139-
}
1140-
// Remove the temp file
1141-
// discard later errors to return the first one from the write() call
1142-
err = os.RemoveAll(fileName)
1143-
if err != nil {
1144-
errs = append(errs, err)
1145-
}
1146-
1147-
return nil, errors.Join(errs...)
1141+
return nil, c.RemoveTmpFilesOnErr(files, err)
1142+
} else if err != nil {
1143+
// We hit an error and didn't finish the compaction. Abort.
1144+
// Remove any tmp files we already completed, as well as the current
1145+
// file we were writing to.
1146+
return nil, c.RemoveTmpFilesOnErr(files, err, os.RemoveAll(fileName))
11481147
}
11491148

11501149
files = append(files, fileName)

0 commit comments

Comments
 (0)