From 7d359d17d167ff9b4bcb4c51dd3df596c4064b5b Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
Date: Thu, 13 Jul 2023 17:13:24 -0700
Subject: [PATCH] lib/storage: properly free up resources from newTestStorage()
 by calling stopTestStorage()

---
 lib/storage/index_db_timing_test.go     | 2 --
 lib/storage/merge_test.go               | 2 ++
 lib/storage/merge_timing_test.go        | 2 ++
 lib/storage/partition_search_test.go    | 1 +
 lib/storage/storage.go                  | 4 +---
 lib/storage/table_search_test.go        | 1 +
 lib/storage/table_search_timing_test.go | 8 +++++---
 lib/storage/table_test.go               | 2 ++
 lib/storage/table_timing_test.go        | 1 +
 9 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/storage/index_db_timing_test.go b/lib/storage/index_db_timing_test.go
index a05bd3b3d6..eb02e9bd24 100644
--- a/lib/storage/index_db_timing_test.go
+++ b/lib/storage/index_db_timing_test.go
@@ -315,7 +315,6 @@ func BenchmarkIndexDBGetTSIDs(b *testing.B) {
 	b.RunParallel(func(pb *testing.PB) {
 		var genTSIDLocal generationTSID
 		var metricNameLocal []byte
-		var metricNameLocalRaw []byte
 		mnLocal := mn
 		is := db.getIndexSearch(0, 0, noDeadline)
 		for pb.Next() {
@@ -324,7 +323,6 @@ func BenchmarkIndexDBGetTSIDs(b *testing.B) {
 				mnLocal.ProjectID = uint32(i % projectsCount)
 				mnLocal.sortTags()
 				metricNameLocal = mnLocal.Marshal(metricNameLocal[:0])
-				metricNameLocalRaw = mnLocal.marshalRaw(metricNameLocalRaw[:0])
 				if !is.getTSIDByMetricName(&genTSIDLocal, metricNameLocal, date) {
 					panic(fmt.Errorf("cannot obtain tsid for row %d", i))
 				}
diff --git a/lib/storage/merge_test.go b/lib/storage/merge_test.go
index 35a6d7d94e..4017e65329 100644
--- a/lib/storage/merge_test.go
+++ b/lib/storage/merge_test.go
@@ -384,6 +384,7 @@ func TestMergeForciblyStop(t *testing.T) {
 	if rowsDeleted != 0 {
 		t.Fatalf("unexpected rowsDeleted; got %d; want %d", rowsDeleted, 0)
 	}
+	stopTestStorage(strg)
 }
 
 func testMergeBlockStreams(t *testing.T, bsrs []*blockStreamReader, expectedBlocksCount, expectedRowsCount int, expectedMinTimestamp, expectedMaxTimestamp int64) {
@@ -399,6 +400,7 @@ func testMergeBlockStreams(t *testing.T, bsrs []*blockStreamReader, expectedBloc
 	if err := mergeBlockStreams(&mp.ph, &bsw, bsrs, nil, strg, 0, &rowsMerged, &rowsDeleted); err != nil {
 		t.Fatalf("unexpected error in mergeBlockStreams: %s", err)
 	}
+	stopTestStorage(strg)
 
 	// Verify written data.
 	if mp.ph.RowsCount != uint64(expectedRowsCount) {
diff --git a/lib/storage/merge_timing_test.go b/lib/storage/merge_timing_test.go
index 34ddde98f0..324812d9f8 100644
--- a/lib/storage/merge_timing_test.go
+++ b/lib/storage/merge_timing_test.go
@@ -47,6 +47,8 @@ func benchmarkMergeBlockStreams(b *testing.B, mps []*inmemoryPart, rowsPerLoop i
 			}
 		}
 	})
+
+	stopTestStorage(strg)
 }
 
 var benchTwoSourcesWorstCaseMPS = func() []*inmemoryPart {
diff --git a/lib/storage/partition_search_test.go b/lib/storage/partition_search_test.go
index 2f2d285277..4e37ea6aea 100644
--- a/lib/storage/partition_search_test.go
+++ b/lib/storage/partition_search_test.go
@@ -186,6 +186,7 @@ func testPartitionSearchEx(t *testing.T, ptt int64, tr TimeRange, partsCount, ma
 	pt = mustOpenPartition(smallPartsPath, bigPartsPath, strg)
 	testPartitionSearch(t, pt, tsids, tr, rbsExpected, rowsCountExpected)
 	pt.MustClose()
+	stopTestStorage(strg)
 
 	if err := os.RemoveAll("small-table"); err != nil {
 		t.Fatalf("cannot remove small parts directory: %s", err)
diff --git a/lib/storage/storage.go b/lib/storage/storage.go
index 7af78760de..747f7d8727 100644
--- a/lib/storage/storage.go
+++ b/lib/storage/storage.go
@@ -1953,7 +1953,7 @@ func SetLogNewSeries(ok bool) {
 
 var logNewSeries = false
 
-func (s *Storage) createAllIndexesForMetricName(is *indexSearch, mn *MetricName, metricNameRaw []byte, genTSID *generationTSID, date uint64) error {
+func (s *Storage) createAllIndexesForMetricName(is *indexSearch, mn *MetricName, metricNameRaw []byte, genTSID *generationTSID, date uint64) {
 	is.createGlobalIndexes(&genTSID.TSID, mn)
 	is.createPerDayIndexes(date, &genTSID.TSID, mn)
 
@@ -1964,8 +1964,6 @@ func (s *Storage) createAllIndexesForMetricName(is *indexSearch, mn *MetricName,
 	// Register the (date, metricID) entry in the cache,
 	// so next time the entry is found there instead of searching for it in the indexdb.
 	s.dateMetricIDCache.Set(date, genTSID.TSID.MetricID)
-
-	return nil
 }
 
 func (s *Storage) registerSeriesCardinality(metricID uint64, metricNameRaw []byte) bool {
diff --git a/lib/storage/table_search_test.go b/lib/storage/table_search_test.go
index 20a3870617..d92e8f7868 100644
--- a/lib/storage/table_search_test.go
+++ b/lib/storage/table_search_test.go
@@ -203,6 +203,7 @@ func testTableSearchEx(t *testing.T, rng *rand.Rand, trData, trSearch TimeRange,
 	tb = mustOpenTable("test-table", strg)
 	testTableSearch(t, tb, tsids, trSearch, rbsExpected, rowsCountExpected)
 	tb.MustClose()
+	stopTestStorage(strg)
 }
 
 func testTableSearch(t *testing.T, tb *table, tsids []TSID, tr TimeRange, rbsExpected []rawBlock, rowsCountExpected int64) {
diff --git a/lib/storage/table_search_timing_test.go b/lib/storage/table_search_timing_test.go
index 1e9a62b487..a0c8fd376d 100644
--- a/lib/storage/table_search_timing_test.go
+++ b/lib/storage/table_search_timing_test.go
@@ -38,7 +38,7 @@ func BenchmarkTableSearch(b *testing.B) {
 	}
 }
 
-func openBenchTable(b *testing.B, startTimestamp int64, rowsPerInsert, rowsCount, tsidsCount int) *table {
+func openBenchTable(b *testing.B, startTimestamp int64, rowsPerInsert, rowsCount, tsidsCount int) (*table, *Storage) {
 	b.Helper()
 
 	path := filepath.Join("benchmarkTableSearch", fmt.Sprintf("rows%d_tsids%d", rowsCount, tsidsCount))
@@ -58,7 +58,7 @@ func openBenchTable(b *testing.B, startTimestamp int64, rowsPerInsert, rowsCount
 		b.Fatalf("unexpected rows count in the table %q; got %d; want %d", path, rowsCount, rowsCountExpected)
 	}
 
-	return tb
+	return tb, strg
 }
 
 var createdBenchTables = make(map[string]bool)
@@ -98,13 +98,14 @@ func createBenchTable(b *testing.B, path string, startTimestamp int64, rowsPerIn
 	wg.Wait()
 
 	tb.MustClose()
+	stopTestStorage(strg)
 }
 
 func benchmarkTableSearch(b *testing.B, rowsCount, tsidsCount, tsidsSearch int) {
 	startTimestamp := timestampFromTime(time.Now()) - 365*24*3600*1000
 	rowsPerInsert := getMaxRawRowsPerShard()
 
-	tb := openBenchTable(b, startTimestamp, rowsPerInsert, rowsCount, tsidsCount)
+	tb, strg := openBenchTable(b, startTimestamp, rowsPerInsert, rowsCount, tsidsCount)
 	tr := TimeRange{
 		MinTimestamp: startTimestamp,
 		MaxTimestamp: (1 << 63) - 1,
@@ -135,4 +136,5 @@ func benchmarkTableSearch(b *testing.B, rowsCount, tsidsCount, tsidsSearch int)
 	b.StopTimer()
 
 	tb.MustClose()
+	stopTestStorage(strg)
 }
diff --git a/lib/storage/table_test.go b/lib/storage/table_test.go
index 2e8da8aadb..c664a55fe1 100644
--- a/lib/storage/table_test.go
+++ b/lib/storage/table_test.go
@@ -29,4 +29,6 @@ func TestTableOpenClose(t *testing.T) {
 		tb := mustOpenTable(path, strg)
 		tb.MustClose()
 	}
+
+	stopTestStorage(strg)
 }
diff --git a/lib/storage/table_timing_test.go b/lib/storage/table_timing_test.go
index 56865900f2..d7363105f9 100644
--- a/lib/storage/table_timing_test.go
+++ b/lib/storage/table_timing_test.go
@@ -104,4 +104,5 @@ func benchmarkTableAddRows(b *testing.B, rowsPerInsert, tsidsCount int) {
 			b.Fatalf("cannot remove table %q: %s", tablePath, err)
 		}
 	}
+	stopTestStorage(strg)
 }