2018-02-09 04:11:03 -08:00
|
|
|
package tsdb
|
|
|
|
|
2018-02-09 04:37:10 -08:00
|
|
|
import (
|
2018-02-12 02:40:12 -08:00
|
|
|
"os"
|
2018-02-09 04:37:10 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/tsdb/chunks"
|
2018-09-20 22:31:22 -07:00
|
|
|
"github.com/prometheus/tsdb/testutil"
|
2018-02-09 04:37:10 -08:00
|
|
|
|
|
|
|
"github.com/prometheus/tsdb/index"
|
|
|
|
"github.com/prometheus/tsdb/labels"
|
|
|
|
)
|
2018-02-09 04:11:03 -08:00
|
|
|
|
|
|
|
func TestRepairBadIndexVersion(t *testing.T) {
|
2018-02-09 04:37:10 -08:00
|
|
|
// The broken index used in this test was written by the following script
|
|
|
|
// at a broken revision.
|
|
|
|
//
|
|
|
|
// func main() {
|
|
|
|
// w, err := index.NewWriter("index")
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSymbols(map[string]struct{}{
|
|
|
|
// "a": struct{}{},
|
|
|
|
// "b": struct{}{},
|
|
|
|
// "1": struct{}{},
|
|
|
|
// "2": struct{}{},
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSeries(1, labels.FromStrings("a", "1", "b", "1"))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSeries(2, labels.FromStrings("a", "2", "b", "1"))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.WritePostings("b", "1", index.NewListPostings([]uint64{1, 2}))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// if err := w.Close(); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// In its current state, lookups should fail with the fixed code.
|
|
|
|
const dir = "testdata/repair_index_version/01BZJ9WJQPWHGNC2W4J9TA62KC/"
|
|
|
|
meta, err := readMetaFile(dir)
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.NotOk(t, err)
|
2018-02-12 02:40:12 -08:00
|
|
|
// Touch chunks dir in block.
|
|
|
|
os.MkdirAll(dir+"chunks", 0777)
|
2018-02-09 04:37:10 -08:00
|
|
|
|
|
|
|
r, err := index.NewFileReader(dir + "index")
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-02-09 04:37:10 -08:00
|
|
|
p, err := r.Postings("b", "1")
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-02-09 04:37:10 -08:00
|
|
|
for p.Next() {
|
|
|
|
t.Logf("next ID %d", p.At())
|
|
|
|
|
|
|
|
var lset labels.Labels
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.NotOk(t, r.Series(p.At(), &lset, nil))
|
2018-02-09 04:37:10 -08:00
|
|
|
}
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, p.Err())
|
2018-09-20 22:31:22 -07:00
|
|
|
testutil.Ok(t, r.Close())
|
2018-02-09 04:37:10 -08:00
|
|
|
|
2018-02-12 02:40:12 -08:00
|
|
|
// On DB opening all blocks in the base dir should be repaired.
|
2018-06-05 01:50:20 -07:00
|
|
|
db, err := Open("testdata/repair_index_version", nil, nil, nil)
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-02-12 02:40:12 -08:00
|
|
|
db.Close()
|
2018-02-09 04:37:10 -08:00
|
|
|
|
|
|
|
r, err = index.NewFileReader(dir + "index")
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-02-09 04:37:10 -08:00
|
|
|
p, err = r.Postings("b", "1")
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-02-09 04:37:10 -08:00
|
|
|
res := []labels.Labels{}
|
|
|
|
|
|
|
|
for p.Next() {
|
|
|
|
t.Logf("next ID %d", p.At())
|
|
|
|
|
|
|
|
var lset labels.Labels
|
|
|
|
var chks []chunks.Meta
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, r.Series(p.At(), &lset, &chks))
|
2018-02-09 04:37:10 -08:00
|
|
|
res = append(res, lset)
|
|
|
|
}
|
2018-09-20 23:17:59 -07:00
|
|
|
|
|
|
|
testutil.Ok(t, p.Err())
|
|
|
|
testutil.Equals(t, []labels.Labels{
|
2018-02-09 04:37:10 -08:00
|
|
|
{{"a", "1"}, {"b", "1"}},
|
|
|
|
{{"a", "2"}, {"b", "1"}},
|
2018-09-20 23:17:59 -07:00
|
|
|
}, res)
|
2018-02-09 04:11:03 -08:00
|
|
|
|
2018-02-09 04:37:10 -08:00
|
|
|
meta, err = readMetaFile(dir)
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Assert(t, meta.Version == 1, "unexpected meta version %d", meta.Version)
|
2018-02-09 04:11:03 -08:00
|
|
|
}
|