Skip to content

Commit

Permalink
lib/storage: update nextRotationTimestamp relative to the timestamp o…
Browse files Browse the repository at this point in the history
…f the indexdb rotation

Updates #1401
Updates #4563
  • Loading branch information
valyala committed Jul 29, 2023
1 parent dfe4816 commit 89ccf19
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/storage/index_db_test.go
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/storage/storage.go
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lib/storage/storage_test.go
Expand Up @@ -1238,7 +1238,7 @@ func TestStorageRotateIndexDB(t *testing.T) {
return
default:
time.Sleep(time.Millisecond)
s.mustRotateIndexDB()
s.mustRotateIndexDB(time.Now())
}
}
}()
Expand Down

0 comments on commit 89ccf19

Please sign in to comment.