From a345231a0e43d4074a7ae97767f9bfa98e6b899f Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Mon, 12 Feb 2018 11:40:12 +0100 Subject: [PATCH] Add test for block meta version and comments --- block_test.go | 15 +++++++++++++++ db.go | 1 + index/index.go | 5 +++-- repair.go | 2 ++ repair_test.go | 9 +++++++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/block_test.go b/block_test.go index 4ebb9685d..370047d5d 100644 --- a/block_test.go +++ b/block_test.go @@ -23,6 +23,21 @@ import ( "github.com/prometheus/tsdb/testutil" ) +// In Prometheus 2.1.0 we had a bug where the meta.json version was falsely bumped +// to 2. We had a migration in place resetting it to 1 but we should move immediately to +// version 3 next time to avoid confusion and issues. +func TestBlockMetaMustNeverBeVersion2(t *testing.T) { + dir, err := ioutil.TempDir("", "metaversion") + testutil.Ok(t, err) + defer os.RemoveAll(dir) + + testutil.Ok(t, writeMetaFile(dir, &BlockMeta{})) + + meta, err := readMetaFile(dir) + testutil.Ok(t, err) + testutil.Assert(t, meta.Version != 2, "meta.json version must never be 2") +} + func TestSetCompactionFailed(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test-tsdb") testutil.Ok(t, err) diff --git a/db.go b/db.go index 6490c01df..1ac2d425f 100644 --- a/db.go +++ b/db.go @@ -188,6 +188,7 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db if opts == nil { opts = DefaultOptions } + // Fixup bad format written by Prometheus 2.1. if err := repairBadIndexVersion(l, dir); err != nil { return nil, err } diff --git a/index/index.go b/index/index.go index 4fdc0398c..146332d10 100644 --- a/index/index.go +++ b/index/index.go @@ -136,7 +136,7 @@ type indexTOC struct { postingsTable uint64 } -// NewWriter returns a new Writer to the given filename. +// NewWriter returns a new Writer to the given filename. It serializes data in format version 2. func NewWriter(fn string) (*Writer, error) { dir := filepath.Dir(fn) @@ -577,7 +577,8 @@ func (b realByteSlice) Sub(start, end int) ByteSlice { return b[start:end] } -// NewReader returns a new IndexReader on the given byte slice. +// NewReader returns a new IndexReader on the given byte slice. It automatically +// handles different format versions. func NewReader(b ByteSlice) (*Reader, error) { return newReader(b, nil) } diff --git a/repair.go b/repair.go index 61ec4bb8c..e9f2a9643 100644 --- a/repair.go +++ b/repair.go @@ -15,6 +15,8 @@ import ( "github.com/prometheus/tsdb/fileutil" ) +// repairBadIndexVersion repairs an issue in index and meta.json persistence introduced in +// commit 129773b41a565fde5156301e37f9a87158030443. func repairBadIndexVersion(logger log.Logger, dir string) error { // All blocks written by Prometheus 2.1 with a meta.json version of 2 are affected. // We must actually set the index file version to 2 and revert the meta.json version back to 1. diff --git a/repair_test.go b/repair_test.go index e133bceab..f4c9d2087 100644 --- a/repair_test.go +++ b/repair_test.go @@ -1,12 +1,12 @@ package tsdb import ( + "os" "reflect" "testing" "github.com/prometheus/tsdb/chunks" - "github.com/go-kit/kit/log" "github.com/prometheus/tsdb/index" "github.com/prometheus/tsdb/labels" ) @@ -52,6 +52,8 @@ func TestRepairBadIndexVersion(t *testing.T) { if err == nil { t.Fatal("error expected but got none") } + // Touch chunks dir in block. + os.MkdirAll(dir+"chunks", 0777) r, err := index.NewFileReader(dir + "index") if err != nil { @@ -73,9 +75,12 @@ func TestRepairBadIndexVersion(t *testing.T) { t.Fatal(err) } - if err := repairBadIndexVersion(log.NewNopLogger(), "testdata/repair_index_version"); err != nil { + // On DB opening all blocks in the base dir should be repaired. + db, _ := Open("testdata/repair_index_version", nil, nil, nil) + if err != nil { t.Fatal(err) } + db.Close() r, err = index.NewFileReader(dir + "index") if err != nil {