Add test for block meta version and comments

This commit is contained in:
Fabian Reinartz 2018-02-12 11:40:12 +01:00
parent 04d5ac5aae
commit a345231a0e
5 changed files with 28 additions and 4 deletions

View file

@ -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)

1
db.go
View file

@ -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
}

View file

@ -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)
}

View file

@ -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.

View file

@ -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 {