@@ -1085,6 +1085,17 @@ func (c *Compactor) RemoveTmpFiles(files []string) error {
1085
1085
return errors .Join (errs ... )
1086
1086
}
1087
1087
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
+
1088
1099
// writeNewFiles writes from the iterator into new TSM files, rotating
1089
1100
// to a new file once it has reached the max TSM file size.
1090
1101
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
1113
1124
// If the file only contained tombstoned entries, then it would be a 0 length
1114
1125
// file that we can drop.
1115
1126
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 )
1117
1129
}
1118
1130
break
1119
1131
} else if errors .As (err , & eInProgress ) {
@@ -1124,27 +1136,14 @@ func (c *Compactor) writeNewFiles(generation, sequence int, src []string, iter K
1124
1136
// planner keeps track of which files are assigned to compaction plans now.
1125
1137
logger .Warn ("file exists, compaction in progress already" , zap .String ("output_file" , fileName ))
1126
1138
}
1127
- return nil , err
1128
- } else if err != nil {
1129
- var errs []error
1130
- errs = append (errs , err )
1131
1139
// We hit an error and didn't finish the compaction. Abort.
1132
1140
// 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 ))
1148
1147
}
1149
1148
1150
1149
files = append (files , fileName )
0 commit comments