fix: ensure files are closed before they are deleted or moved in `deletetsm` (#22458)

* Close files before deleting parent dir in tests
* Use platform-independent function to rename output
* Close source TSM file before overwriting it
pull/22301/head^2
Daniel Moran 2021-09-13 14:39:50 -04:00 committed by GitHub
parent 3053af7719
commit 6f39255fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/pkg/file"
"github.com/influxdata/influxdb/v2/tsdb/engine/tsm1"
"github.com/spf13/cobra"
)
@ -141,9 +142,12 @@ func (a *args) process(cmd *cobra.Command, path string) error {
if err := w.Close(); err != nil {
return fmt.Errorf("failed to close TSM Writer: %w", err)
}
if err := r.Close(); err != nil {
return fmt.Errorf("failed to close TSM Reader: %w", err)
}
// Replace original file with new file.
if err := os.Rename(outputPath, path); err != nil {
if err := file.RenameFile(outputPath, path); err != nil {
return fmt.Errorf("failed to update TSM file %q: %w", path, err)
}
if !hasData {

View File

@ -164,9 +164,12 @@ func createTSMFile(t *testing.T, params tsmParams) (string, string) {
file, err = os.CreateTemp(dir, "*.txt")
}
require.NoError(t, err)
defer file.Close()
w, err := tsm1.NewTSMWriter(file)
require.NoError(t, err)
defer w.Close()
for _, key := range params.keys {
values := []tsm1.Value{tsm1.NewValue(0, 1.0)}
require.NoError(t, w.Write([]byte(key), values))
@ -179,7 +182,6 @@ func createTSMFile(t *testing.T, params tsmParams) (string, string) {
if params.invalid {
require.NoError(t, binary.Write(file, binary.BigEndian, []byte("foobar\n")))
}
require.NoError(t, w.Close())
return dir, file.Name()
}