From f4dd45609a05e8f582cdcd8ef369004d1f9e3c02 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev <8903888+krasi-georgiev@users.noreply.github.com> Date: Fri, 22 May 2020 14:30:06 +0300 Subject: [PATCH] Use min and maxt of the range head when creating a block (#7282) Signed-off-by: Krasi Georgiev <8903888+krasi-georgiev@users.noreply.github.com> --- tsdb/db.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index e797c95f0..1997dca2d 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -750,7 +750,7 @@ func (db *DB) Compact() (err error) { // consistently, we explicitly remove the last value // from the block interval here. head := NewRangeHead(db.head, mint, maxt-1) - if err := db.compactHead(head, mint, maxt); err != nil { + if err := db.compactHead(head); err != nil { return err } } @@ -759,17 +759,20 @@ func (db *DB) Compact() (err error) { } // CompactHead compacts the given the RangeHead. -func (db *DB) CompactHead(head *RangeHead, mint, maxt int64) (err error) { +func (db *DB) CompactHead(head *RangeHead) (err error) { db.cmtx.Lock() defer db.cmtx.Unlock() - return db.compactHead(head, mint, maxt) + return db.compactHead(head) } // compactHead compacts the given the RangeHead. // The compaction mutex should be held before calling this method. -func (db *DB) compactHead(head *RangeHead, mint, maxt int64) (err error) { - uid, err := db.compactor.Write(db.dir, head, mint, maxt, nil) +func (db *DB) compactHead(head *RangeHead) (err error) { + // Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime). + // Because of this block intervals are always +1 than the total samples it includes. + maxt := head.MaxTime() + 1 + uid, err := db.compactor.Write(db.dir, head, head.MinTime(), maxt, nil) if err != nil { return errors.Wrap(err, "persist head block") }