From 89ccf19b700238d0f6227bb1a589150f17ad9a25 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 53c38c88e..00da15919 100644 --- a/lib/storage/index_db_test.go +++ b/lib/storage/index_db_test.go @@ -1585,7 +1585,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 50c85a94e..cf481be53 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -707,8 +707,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) } } } @@ -765,14 +765,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 7b8afe21e..2537a5d2e 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -1238,7 +1238,7 @@ func TestStorageRotateIndexDB(t *testing.T) { return default: time.Sleep(time.Millisecond) - s.mustRotateIndexDB() + s.mustRotateIndexDB(time.Now()) } } }()