From 08fe7949d1aedf6b9ed7b5ba0aac77980affc181 Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
Date: Fri, 6 Sep 2024 16:17:04 +0200
Subject: [PATCH] lib/logstorage: consistently use nsecsPerDay constant and
 remove nsecPerDay constant

---
 lib/logstorage/parser.go         |  4 ++--
 lib/logstorage/storage.go        | 20 +++++++++-----------
 lib/logstorage/storage_search.go |  4 ++--
 lib/logstorage/storage_test.go   |  4 ++--
 4 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/lib/logstorage/parser.go b/lib/logstorage/parser.go
index 45f59768f7..131e5eef62 100644
--- a/lib/logstorage/parser.go
+++ b/lib/logstorage/parser.go
@@ -1644,8 +1644,8 @@ func getDayRangeArg(lex *lexer) (int64, string, error) {
 	if offset < 0 {
 		offset = 0
 	}
-	if offset >= nsecPerDay {
-		offset = nsecPerDay - 1
+	if offset >= nsecsPerDay {
+		offset = nsecsPerDay - 1
 	}
 	return offset, argStr, nil
 }
diff --git a/lib/logstorage/storage.go b/lib/logstorage/storage.go
index efa29c5f2b..b0d94babf1 100644
--- a/lib/logstorage/storage.go
+++ b/lib/logstorage/storage.go
@@ -200,8 +200,8 @@ func (ptw *partitionWrapper) decRef() {
 }
 
 func (ptw *partitionWrapper) canAddAllRows(lr *LogRows) bool {
-	minTimestamp := ptw.day * nsecPerDay
-	maxTimestamp := minTimestamp + nsecPerDay - 1
+	minTimestamp := ptw.day * nsecsPerDay
+	maxTimestamp := minTimestamp + nsecsPerDay - 1
 	for _, ts := range lr.timestamps {
 		if ts < minTimestamp || ts > maxTimestamp {
 			return false
@@ -286,7 +286,7 @@ func MustOpenStorage(path string, cfg *StorageConfig) *Storage {
 		if err != nil {
 			logger.Panicf("FATAL: cannot parse partition filename %q at %q; it must be in the form YYYYMMDD: %s", fname, partitionsPath, err)
 		}
-		day := t.UTC().UnixNano() / nsecPerDay
+		day := t.UTC().UnixNano() / nsecsPerDay
 
 		partitionPath := filepath.Join(partitionsPath, fname)
 		pt := mustOpenPartition(s, partitionPath)
@@ -441,11 +441,11 @@ func (s *Storage) watchMaxDiskSpaceUsage() {
 }
 
 func (s *Storage) getMinAllowedDay() int64 {
-	return time.Now().UTC().Add(-s.retention).UnixNano() / nsecPerDay
+	return time.Now().UTC().Add(-s.retention).UnixNano() / nsecsPerDay
 }
 
 func (s *Storage) getMaxAllowedDay() int64 {
-	return time.Now().UTC().Add(s.futureRetention).UnixNano() / nsecPerDay
+	return time.Now().UTC().Add(s.futureRetention).UnixNano() / nsecsPerDay
 }
 
 // MustClose closes s.
@@ -514,11 +514,11 @@ func (s *Storage) MustAddRows(lr *LogRows) {
 	maxAllowedDay := s.getMaxAllowedDay()
 	m := make(map[int64]*LogRows)
 	for i, ts := range lr.timestamps {
-		day := ts / nsecPerDay
+		day := ts / nsecsPerDay
 		if day < minAllowedDay {
 			rf := RowFormatter(lr.rows[i])
 			tsf := TimeFormatter(ts)
-			minAllowedTsf := TimeFormatter(minAllowedDay * nsecPerDay)
+			minAllowedTsf := TimeFormatter(minAllowedDay * nsecsPerDay)
 			tooSmallTimestampLogger.Warnf("skipping log entry with too small timestamp=%s; it must be bigger than %s according "+
 				"to the configured -retentionPeriod=%dd. See https://docs.victoriametrics.com/victorialogs/#retention ; "+
 				"log entry: %s", &tsf, &minAllowedTsf, durationToDays(s.retention), &rf)
@@ -528,7 +528,7 @@ func (s *Storage) MustAddRows(lr *LogRows) {
 		if day > maxAllowedDay {
 			rf := RowFormatter(lr.rows[i])
 			tsf := TimeFormatter(ts)
-			maxAllowedTsf := TimeFormatter(maxAllowedDay * nsecPerDay)
+			maxAllowedTsf := TimeFormatter(maxAllowedDay * nsecsPerDay)
 			tooBigTimestampLogger.Warnf("skipping log entry with too big timestamp=%s; it must be smaller than %s according "+
 				"to the configured -futureRetention=%dd; see https://docs.victoriametrics.com/victorialogs/#retention ; "+
 				"log entry: %s", &tsf, &maxAllowedTsf, durationToDays(s.futureRetention), &rf)
@@ -553,8 +553,6 @@ func (s *Storage) MustAddRows(lr *LogRows) {
 var tooSmallTimestampLogger = logger.WithThrottler("too_small_timestamp", 5*time.Second)
 var tooBigTimestampLogger = logger.WithThrottler("too_big_timestamp", 5*time.Second)
 
-const nsecPerDay = 24 * 3600 * 1e9
-
 // TimeFormatter implements fmt.Stringer for timestamp in nanoseconds
 type TimeFormatter int64
 
@@ -582,7 +580,7 @@ func (s *Storage) getPartitionForDay(day int64) *partitionWrapper {
 	}
 	if ptw == nil {
 		// Missing partition for the given day. Create it.
-		fname := time.Unix(0, day*nsecPerDay).UTC().Format(partitionNameFormat)
+		fname := time.Unix(0, day*nsecsPerDay).UTC().Format(partitionNameFormat)
 		partitionPath := filepath.Join(s.path, partitionsDirname, fname)
 		mustCreatePartition(partitionPath)
 
diff --git a/lib/logstorage/storage_search.go b/lib/logstorage/storage_search.go
index ba82ccd0dc..3f9185f54d 100644
--- a/lib/logstorage/storage_search.go
+++ b/lib/logstorage/storage_search.go
@@ -673,12 +673,12 @@ func (s *Storage) search(workersCount int, so *genericSearchOptions, stopCh <-ch
 	// Select partitions according to the selected time range
 	s.partitionsLock.Lock()
 	ptws := s.partitions
-	minDay := so.minTimestamp / nsecPerDay
+	minDay := so.minTimestamp / nsecsPerDay
 	n := sort.Search(len(ptws), func(i int) bool {
 		return ptws[i].day >= minDay
 	})
 	ptws = ptws[n:]
-	maxDay := so.maxTimestamp / nsecPerDay
+	maxDay := so.maxTimestamp / nsecsPerDay
 	n = sort.Search(len(ptws), func(i int) bool {
 		return ptws[i].day > maxDay
 	})
diff --git a/lib/logstorage/storage_test.go b/lib/logstorage/storage_test.go
index b67e90a5f0..cad816a4ea 100644
--- a/lib/logstorage/storage_test.go
+++ b/lib/logstorage/storage_test.go
@@ -78,10 +78,10 @@ func TestStorageMustAddRows(t *testing.T) {
 	s = MustOpenStorage(path, cfg)
 
 	lr = newTestLogRows(3, 10, 0)
-	now := time.Now().UTC().UnixNano() - int64(len(lr.timestamps)/2)*nsecPerDay
+	now := time.Now().UTC().UnixNano() - int64(len(lr.timestamps)/2)*nsecsPerDay
 	for i := range lr.timestamps {
 		lr.timestamps[i] = now
-		now += nsecPerDay
+		now += nsecsPerDay
 	}
 	totalRowsCount += uint64(len(lr.timestamps))
 	s.MustAddRows(lr)