From 4ed6b9ed72a1357101efb27921dac09d05713724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Knecht?= Date: Wed, 13 Jun 2018 11:24:28 +0200 Subject: [PATCH] db: add test for chunks that span beyond a block's boundaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: BenoƮt Knecht --- db_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/db_test.go b/db_test.go index 72817a109..e7f2f4ad6 100644 --- a/db_test.go +++ b/db_test.go @@ -26,6 +26,8 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" + "github.com/prometheus/tsdb/chunks" + "github.com/prometheus/tsdb/index" "github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/testutil" ) @@ -1096,3 +1098,58 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { {Min: 8, Max: 9}: {nc1[8], nc1[9]}, // 7-10, 8-9 }, OverlappingBlocks(nc1)) } + +// Regression test for https://github.com/prometheus/tsdb/issues/347 +func TestChunkAtBlockBoundary(t *testing.T) { + db, close := openTestDB(t, nil) + defer close() + defer db.Close() + + app := db.Appender() + + blockRange := DefaultOptions.BlockRanges[0] + label := labels.FromStrings("foo", "bar") + + for i := int64(0); i < 3; i++ { + _, err := app.Add(label, i*blockRange, 0) + testutil.Ok(t, err) + _, err = app.Add(label, i*blockRange+1000, 0) + testutil.Ok(t, err) + } + + err := app.Commit() + testutil.Ok(t, err) + + _, err = db.compact() + testutil.Ok(t, err) + + for _, block := range db.blocks { + r, err := block.Index() + testutil.Ok(t, err) + defer r.Close() + + meta := block.Meta() + + p, err := r.Postings(index.AllPostingsKey()) + testutil.Ok(t, err) + + var ( + lset labels.Labels + chks []chunks.Meta + ) + + chunkCount := 0 + + for p.Next() { + err = r.Series(p.At(), &lset, &chks) + testutil.Ok(t, err) + for _, c := range chks { + testutil.Assert(t, meta.MinTime <= c.MinTime && c.MaxTime <= meta.MaxTime, + "chunk spans beyond block boundaries: [block.MinTime=%d, block.MaxTime=%d]; [chunk.MinTime=%d, chunk.MaxTime=%d]", + meta.MinTime, meta.MaxTime, c.MinTime, c.MaxTime) + chunkCount++ + } + } + testutil.Assert(t, chunkCount == 1, "expected 1 chunk in block %s, got %d", meta.ULID, chunkCount) + } +}