Skip to content

Commit

Permalink
Tsdb/wal rotate fix (#6423)
Browse files Browse the repository at this point in the history
* rotate tsdb wal under mutex
Signed-off-by: Owen Diehl <ow.diehl@gmail.com>

* ensure we rotate and doublecheck conditions under lock in tsdb headmanager
Signed-off-by: Owen Diehl <ow.diehl@gmail.com>
  • Loading branch information
owen-d authored Jun 17, 2022
1 parent aa31b46 commit a1c72ad
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/storage/stores/tsdb/head_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ func managerPerTenantDir(parent string) string {
}

func (m *HeadManager) Rotate(t time.Time) error {
m.mtx.Lock()
defer m.mtx.Unlock()

if m.activeHeads != nil && m.period.PeriodFor(t) == m.period.PeriodFor(m.activeHeads.start) {
// no-op, we've already rotated to the desired period
return nil
}

// create new wal
nextWALPath := walPath(m.dir, t)
nextWAL, err := newHeadWAL(m.log, nextWALPath, t)
Expand All @@ -255,12 +263,10 @@ func (m *HeadManager) Rotate(t time.Time) error {
}

stopPrev("previous cycle") // stop the previous wal if it hasn't been cleaned up yet
m.mtx.Lock()
m.prev = m.active
m.prevHeads = m.activeHeads
m.active = nextWAL
m.activeHeads = nextHeads
m.mtx.Unlock()
stopPrev("freshly rotated") // stop the newly rotated-out wal

// build tsdb from rotated-out period
Expand Down Expand Up @@ -292,10 +298,8 @@ func (m *HeadManager) Rotate(t time.Time) error {
}

// Now that the tsdbManager has the updated TSDBs, we can remove our references
m.mtx.Lock()
m.prevHeads = nil
m.prev = nil
m.mtx.Unlock()
return nil
}

Expand Down

0 comments on commit a1c72ad

Please sign in to comment.