Skip to content

Commit a1c72ad

Browse files
authored
Tsdb/wal rotate fix (grafana#6423)
* rotate tsdb wal under mutex Signed-off-by: Owen Diehl <[email protected]> * ensure we rotate and doublecheck conditions under lock in tsdb headmanager Signed-off-by: Owen Diehl <[email protected]>
1 parent aa31b46 commit a1c72ad

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pkg/storage/stores/tsdb/head_manager.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ func managerPerTenantDir(parent string) string {
231231
}
232232

233233
func (m *HeadManager) Rotate(t time.Time) error {
234+
m.mtx.Lock()
235+
defer m.mtx.Unlock()
236+
237+
if m.activeHeads != nil && m.period.PeriodFor(t) == m.period.PeriodFor(m.activeHeads.start) {
238+
// no-op, we've already rotated to the desired period
239+
return nil
240+
}
241+
234242
// create new wal
235243
nextWALPath := walPath(m.dir, t)
236244
nextWAL, err := newHeadWAL(m.log, nextWALPath, t)
@@ -255,12 +263,10 @@ func (m *HeadManager) Rotate(t time.Time) error {
255263
}
256264

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

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

294300
// Now that the tsdbManager has the updated TSDBs, we can remove our references
295-
m.mtx.Lock()
296301
m.prevHeads = nil
297302
m.prev = nil
298-
m.mtx.Unlock()
299303
return nil
300304
}
301305

0 commit comments

Comments
 (0)