From 9082a84566c288df98bf265b4e7564a83ae261fb Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 28 Jul 2023 19:47:02 -0700 Subject: [PATCH] lib/storage: update nextRotationTimestamp relative to the timestamp of the indexdb rotation Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1401 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4563 --- lib/storage/index_db_test.go | 2 +- lib/storage/storage.go | 9 +++++---- lib/storage/storage_test.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/storage/index_db_test.go b/lib/storage/index_db_test.go index 446feb803..074613f33 100644 --- a/lib/storage/index_db_test.go +++ b/lib/storage/index_db_test.go @@ -1479,7 +1479,7 @@ func TestIndexDBRepopulateAfterRotation(t *testing.T) { prevGeneration := db.generation // force index rotation - s.mustRotateIndexDB() + s.mustRotateIndexDB(time.Now()) // check tsidCache wasn't reset after the rotation var cs2 fastcache.Stats diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 9986191ee..5ef0a8290 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -692,8 +692,8 @@ func (s *Storage) retentionWatcher() { select { case <-s.stop: return - case <-time.After(time.Second * time.Duration(d)): - s.mustRotateIndexDB() + case currentTime := <-time.After(time.Second * time.Duration(d)): + s.mustRotateIndexDB(currentTime) } } } @@ -750,14 +750,15 @@ func (s *Storage) nextDayMetricIDsUpdater() { } } -func (s *Storage) mustRotateIndexDB() { +func (s *Storage) mustRotateIndexDB(currentTime time.Time) { // Create new indexdb table, which will be used as idbNext newTableName := nextIndexDBTableName() idbNewPath := filepath.Join(s.path, indexdbDirname, newTableName) idbNew := mustOpenIndexDB(idbNewPath, s, &s.isReadOnly) // Update nextRotationTimestamp - atomic.AddInt64(&s.nextRotationTimestamp, s.retentionMsecs/1000) + nextRotationTimestamp := currentTime.UnixMilli() + s.retentionMsecs/1000 + atomic.StoreInt64(&s.nextRotationTimestamp, nextRotationTimestamp) // Set idbNext to idbNew idbNext := s.idbNext.Load() diff --git a/lib/storage/storage_test.go b/lib/storage/storage_test.go index 19d3713b3..a098fc72a 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -1094,7 +1094,7 @@ func TestStorageRotateIndexDB(t *testing.T) { return default: time.Sleep(time.Millisecond) - s.mustRotateIndexDB() + s.mustRotateIndexDB(time.Now()) } } }()