From 6fd6d4c2de60e257cd848c7ccd91b1a3ce429e76 Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
Date: Sat, 24 Feb 2024 00:47:39 +0200
Subject: [PATCH] lib/storage: replace the remaining atomic.* functions with
 atomic.* types for the sake of consistency

See ea9e2b19a5fa8aecc25c9da10fad8f4c1c58df38
---
 lib/storage/index_db_timing_test.go     |  4 ++--
 lib/storage/storage.go                  |  8 ++++++--
 lib/storage/storage_timing_test.go      |  4 ++--
 lib/storage/table_search_timing_test.go | 11 +++++++----
 4 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/lib/storage/index_db_timing_test.go b/lib/storage/index_db_timing_test.go
index dad44d3090..484f863296 100644
--- a/lib/storage/index_db_timing_test.go
+++ b/lib/storage/index_db_timing_test.go
@@ -46,7 +46,7 @@ func BenchmarkIndexDBAddTSIDs(b *testing.B) {
 
 	const recordsPerLoop = 1e3
 
-	var goroutineID uint32
+	var goroutineID atomic.Uint32
 
 	b.ReportAllocs()
 	b.SetBytes(recordsPerLoop)
@@ -54,7 +54,7 @@ func BenchmarkIndexDBAddTSIDs(b *testing.B) {
 	b.RunParallel(func(pb *testing.PB) {
 		var mn MetricName
 		var genTSID generationTSID
-		mn.AccountID = atomic.AddUint32(&goroutineID, 1)
+		mn.AccountID = goroutineID.Add(1)
 
 		// The most common tags.
 		mn.Tags = []Tag{
diff --git a/lib/storage/storage.go b/lib/storage/storage.go
index ad2f2187bd..ec593dee0d 100644
--- a/lib/storage/storage.go
+++ b/lib/storage/storage.go
@@ -2762,8 +2762,12 @@ func (s *Storage) mustOpenIndexDBTables(path string) (next, curr, prev *indexDB)
 var indexDBTableNameRegexp = regexp.MustCompile("^[0-9A-F]{16}$")
 
 func nextIndexDBTableName() string {
-	n := atomic.AddUint64(&indexDBTableIdx, 1)
+	n := indexDBTableIdx.Add(1)
 	return fmt.Sprintf("%016X", n)
 }
 
-var indexDBTableIdx = uint64(time.Now().UnixNano())
+var indexDBTableIdx = func() *atomic.Uint64 {
+	var x atomic.Uint64
+	x.Store(uint64(time.Now().UnixNano()))
+	return &x
+}()
diff --git a/lib/storage/storage_timing_test.go b/lib/storage/storage_timing_test.go
index 9f9fffe9d1..2c080a5ea9 100644
--- a/lib/storage/storage_timing_test.go
+++ b/lib/storage/storage_timing_test.go
@@ -25,7 +25,7 @@ func benchmarkStorageAddRows(b *testing.B, rowsPerBatch int) {
 		}
 	}()
 
-	var globalOffset uint64
+	var globalOffset atomic.Uint64
 
 	b.SetBytes(int64(rowsPerBatch))
 	b.ReportAllocs()
@@ -39,7 +39,7 @@ func benchmarkStorageAddRows(b *testing.B, rowsPerBatch int) {
 			{[]byte("instance"), []byte("1.2.3.4")},
 		}
 		for pb.Next() {
-			offset := int(atomic.AddUint64(&globalOffset, uint64(rowsPerBatch)))
+			offset := int(globalOffset.Add(uint64(rowsPerBatch)))
 			for i := 0; i < rowsPerBatch; i++ {
 				mn.AccountID = uint32(i)
 				mn.ProjectID = uint32(i % 3)
diff --git a/lib/storage/table_search_timing_test.go b/lib/storage/table_search_timing_test.go
index 246cf9c60e..ac4a5025ec 100644
--- a/lib/storage/table_search_timing_test.go
+++ b/lib/storage/table_search_timing_test.go
@@ -69,8 +69,11 @@ func createBenchTable(b *testing.B, path string, startTimestamp int64, rowsPerIn
 	strg := newTestStorage()
 	tb := mustOpenTable(path, strg)
 
-	insertsCount := uint64((rowsCount + rowsPerInsert - 1) / rowsPerInsert)
-	timestamp := uint64(startTimestamp)
+	var insertsCount atomic.Int64
+	insertsCount.Store(int64((rowsCount + rowsPerInsert - 1) / rowsPerInsert))
+
+	var timestamp atomic.Uint64
+	timestamp.Store(uint64(startTimestamp))
 
 	var wg sync.WaitGroup
 	for k := 0; k < cgroup.AvailableCPUs(); k++ {
@@ -79,9 +82,9 @@ func createBenchTable(b *testing.B, path string, startTimestamp int64, rowsPerIn
 			rng := rand.New(rand.NewSource(int64(n)))
 			rows := make([]rawRow, rowsPerInsert)
 			value := float64(100)
-			for int(atomic.AddUint64(&insertsCount, ^uint64(0))) >= 0 {
+			for insertsCount.Add(-1) >= 0 {
 				for j := 0; j < rowsPerInsert; j++ {
-					ts := atomic.AddUint64(&timestamp, uint64(10+rng.Int63n(2)))
+					ts := timestamp.Add(uint64(10 + rng.Int63n(2)))
 					value += float64(int(rng.NormFloat64() * 5))
 
 					r := &rows[j]