tsdb: drop deleted series from the WAL sooner (#12297)

`head.deleted` holds the WAL segment in use at the time each series was
removed from the head. At the end of `truncateWAL()` we will delete
all segments up to `last`, so we can drop any series that were last seen
in a segment at or before that point.

(same change in Prometheus Agent too)

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
pull/12307/head
Bryan Boreham 2 years ago committed by GitHub
parent 0d049feac7
commit 0ab9553611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -665,7 +665,7 @@ func (db *DB) truncate(mint int64) error {
} }
seg, ok := db.deleted[id] seg, ok := db.deleted[id]
return ok && seg >= first return ok && seg > last
} }
db.metrics.checkpointCreationTotal.Inc() db.metrics.checkpointCreationTotal.Inc()
@ -687,7 +687,7 @@ func (db *DB) truncate(mint int64) error {
// The checkpoint is written and segments before it are truncated, so we // The checkpoint is written and segments before it are truncated, so we
// no longer need to track deleted series that were being kept around. // no longer need to track deleted series that were being kept around.
for ref, segment := range db.deleted { for ref, segment := range db.deleted {
if segment < first { if segment <= last {
delete(db.deleted, ref) delete(db.deleted, ref)
} }
} }

@ -1212,9 +1212,9 @@ func (h *Head) truncateWAL(mint int64) error {
return true return true
} }
h.deletedMtx.Lock() h.deletedMtx.Lock()
_, ok := h.deleted[id] keepUntil, ok := h.deleted[id]
h.deletedMtx.Unlock() h.deletedMtx.Unlock()
return ok return ok && keepUntil > last
} }
h.metrics.checkpointCreationTotal.Inc() h.metrics.checkpointCreationTotal.Inc()
if _, err = wlog.Checkpoint(h.logger, h.wal, first, last, keep, mint); err != nil { if _, err = wlog.Checkpoint(h.logger, h.wal, first, last, keep, mint); err != nil {
@ -1235,7 +1235,7 @@ func (h *Head) truncateWAL(mint int64) error {
// longer need to track deleted series that are before it. // longer need to track deleted series that are before it.
h.deletedMtx.Lock() h.deletedMtx.Lock()
for ref, segment := range h.deleted { for ref, segment := range h.deleted {
if segment < first { if segment <= last {
delete(h.deleted, ref) delete(h.deleted, ref)
} }
} }

Loading…
Cancel
Save