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
This commit is contained in:
Aliaksandr Valialkin 2023-07-28 19:47:02 -07:00
parent c25f053945
commit 9082a84566
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 7 additions and 6 deletions

View file

@ -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

View file

@ -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()

View file

@ -1094,7 +1094,7 @@ func TestStorageRotateIndexDB(t *testing.T) {
return
default:
time.Sleep(time.Millisecond)
s.mustRotateIndexDB()
s.mustRotateIndexDB(time.Now())
}
}
}()