From 140148edfb391ee1a16a645977981358eea1527f Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Tue, 14 Sep 2021 17:34:55 -0400 Subject: [PATCH] test: close temp sfile before running buildtsi compaction test (#22481) --- cmd/influxd/inspect/build_tsi/build_tsi.go | 4 ++-- .../inspect/build_tsi/build_tsi_test.go | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/influxd/inspect/build_tsi/build_tsi.go b/cmd/influxd/inspect/build_tsi/build_tsi.go index 766f2ebd98..5bb26df0a9 100644 --- a/cmd/influxd/inspect/build_tsi/build_tsi.go +++ b/cmd/influxd/inspect/build_tsi/build_tsi.go @@ -259,7 +259,7 @@ func (buildTSICmd *buildTSI) compactSeriesFilePartition(path string) error { src := dst + tmpExt buildTSICmd.Logger.Debug("Renaming new segment", zap.String("prev", src), zap.String("new", dst)) - if err = file.RenameFile(src, dst); err != nil && !os.IsNotExist(err) { + if err := file.RenameFile(src, dst); err != nil && !os.IsNotExist(err) { return fmt.Errorf("serious failure. Please rebuild index and series file: %w", err) } } @@ -267,7 +267,7 @@ func (buildTSICmd *buildTSI) compactSeriesFilePartition(path string) error { // Remove index file so it will be rebuilt when reopened. buildTSICmd.Logger.Debug("Removing index file", zap.String("path", indexPath)) - if err = os.Remove(indexPath); err != nil && !os.IsNotExist(err) { // index won't exist for low cardinality + if err := os.Remove(indexPath); err != nil && !os.IsNotExist(err) { // index won't exist for low cardinality return err } diff --git a/cmd/influxd/inspect/build_tsi/build_tsi_test.go b/cmd/influxd/inspect/build_tsi/build_tsi_test.go index 08503f3e0b..68f1c7d0b6 100644 --- a/cmd/influxd/inspect/build_tsi/build_tsi_test.go +++ b/cmd/influxd/inspect/build_tsi/build_tsi_test.go @@ -37,7 +37,7 @@ type cmdOuts struct { expectErr bool expectBuiltIndex bool expectCompactSeries bool - sfile *tsdb.SeriesFile + sfilePath string } func Test_BuildTSI_ShardID_Without_BucketID(t *testing.T) { @@ -234,8 +234,7 @@ func Test_BuildTSI_Valid_Compact_Series(t *testing.T) { // Create new series file sfile := tsdb.NewSeriesFile(filepath.Join(tempDir, "data", "12345", "_series")) - err := sfile.Open() - require.NoError(t, err) + require.NoError(t, sfile.Open()) defer sfile.Close() // Generate a bunch of keys. @@ -247,7 +246,7 @@ func Test_BuildTSI_Valid_Compact_Series(t *testing.T) { } // Add all to the series file. - _, err = sfile.CreateSeriesListIfNotExists(mms, tagSets) + _, err := sfile.CreateSeriesListIfNotExists(mms, tagSets) require.NoError(t, err) params := cmdParams{ @@ -262,9 +261,10 @@ func Test_BuildTSI_Valid_Compact_Series(t *testing.T) { outs := cmdOuts{ expectCompactSeries: true, - sfile: sfile, + sfilePath: sfile.Path(), } + require.NoError(t, sfile.Close()) runCommand(t, params, outs) } @@ -277,10 +277,10 @@ func initCommand(t *testing.T, params cmdParams) *cobra.Command { // Set args allArgs := make([]string, 0) - if params.dataPath != os.Getenv("HOME")+"/.influxdbv2/engine/data" { + if params.dataPath != filepath.Join(os.Getenv("HOME"), ".influxdbv2", "engine", "data") { allArgs = append(allArgs, "--data-path", params.dataPath) } - if params.walPath != os.Getenv("HOME")+"/.influxdbv2/engine/wal" { + if params.walPath != filepath.Join(os.Getenv("HOME"), ".influxdbv2", "engine", "wal") { allArgs = append(allArgs, "--wal-path", params.walPath) } if params.bucketID != "" { @@ -356,9 +356,14 @@ func runCommand(t *testing.T, params cmdParams, outs cmdOuts) { } if outs.expectCompactSeries { + sfile := tsdb.NewSeriesFile(outs.sfilePath) + require.NoError(t, sfile.Open()) + defer sfile.Close() + // Get size of all partitions before series compaction - beforeSize, err := outs.sfile.FileSize() + beforeSize, err := sfile.FileSize() require.NoError(t, err) + require.NoError(t, sfile.Close()) // Run command with series compaction option chosen require.NoError(t, cmd.Execute()) @@ -367,7 +372,8 @@ func runCommand(t *testing.T, params cmdParams, outs cmdOuts) { require.DirExists(t, filepath.Join(params.dataPath, "12345", "_series")) // Get size of all partitions after series compaction - afterSize, err := outs.sfile.FileSize() + require.NoError(t, sfile.Open()) + afterSize, err := sfile.FileSize() require.NoError(t, err) // Check that collective size of all series partitions has decreased after compaction