mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
430 lines
10 KiB
430 lines
10 KiB
8 years ago
|
// Copyright 2017 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
7 years ago
|
package index
|
||
8 years ago
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
8 years ago
|
"math/rand"
|
||
8 years ago
|
"os"
|
||
8 years ago
|
"path/filepath"
|
||
8 years ago
|
"sort"
|
||
8 years ago
|
"testing"
|
||
8 years ago
|
|
||
8 years ago
|
"github.com/pkg/errors"
|
||
7 years ago
|
"github.com/prometheus/tsdb/chunkenc"
|
||
8 years ago
|
"github.com/prometheus/tsdb/chunks"
|
||
6 years ago
|
"github.com/prometheus/tsdb/encoding"
|
||
8 years ago
|
"github.com/prometheus/tsdb/labels"
|
||
7 years ago
|
"github.com/prometheus/tsdb/testutil"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
type series struct {
|
||
|
l labels.Labels
|
||
7 years ago
|
chunks []chunks.Meta
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
type mockIndex struct {
|
||
7 years ago
|
series map[uint64]series
|
||
8 years ago
|
labelIndex map[string][]string
|
||
7 years ago
|
postings map[labels.Label][]uint64
|
||
7 years ago
|
symbols map[string]struct{}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func newMockIndex() mockIndex {
|
||
7 years ago
|
ix := mockIndex{
|
||
7 years ago
|
series: make(map[uint64]series),
|
||
8 years ago
|
labelIndex: make(map[string][]string),
|
||
7 years ago
|
postings: make(map[labels.Label][]uint64),
|
||
7 years ago
|
symbols: make(map[string]struct{}),
|
||
8 years ago
|
}
|
||
7 years ago
|
return ix
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
func (m mockIndex) Symbols() (map[string]struct{}, error) {
|
||
|
return m.symbols, nil
|
||
|
}
|
||
|
|
||
7 years ago
|
func (m mockIndex) AddSeries(ref uint64, l labels.Labels, chunks ...chunks.Meta) error {
|
||
8 years ago
|
if _, ok := m.series[ref]; ok {
|
||
|
return errors.Errorf("series with reference %d already added", ref)
|
||
|
}
|
||
7 years ago
|
for _, lbl := range l {
|
||
|
m.symbols[lbl.Name] = struct{}{}
|
||
|
m.symbols[lbl.Value] = struct{}{}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
s := series{l: l}
|
||
|
// Actual chunk data is not stored in the index.
|
||
|
for _, c := range chunks {
|
||
7 years ago
|
c.Chunk = nil
|
||
|
s.chunks = append(s.chunks, c)
|
||
8 years ago
|
}
|
||
8 years ago
|
m.series[ref] = s
|
||
8 years ago
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m mockIndex) WriteLabelIndex(names []string, values []string) error {
|
||
|
// TODO support composite indexes
|
||
|
if len(names) != 1 {
|
||
|
return errors.New("composite indexes not supported yet")
|
||
|
}
|
||
|
sort.Strings(values)
|
||
|
m.labelIndex[names[0]] = values
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m mockIndex) WritePostings(name, value string, it Postings) error {
|
||
7 years ago
|
l := labels.Label{Name: name, Value: value}
|
||
|
if _, ok := m.postings[l]; ok {
|
||
|
return errors.Errorf("postings for %s already added", l)
|
||
8 years ago
|
}
|
||
7 years ago
|
ep, err := ExpandPostings(it)
|
||
7 years ago
|
if err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
7 years ago
|
m.postings[l] = ep
|
||
|
return nil
|
||
8 years ago
|
}
|
||
|
|
||
|
func (m mockIndex) Close() error {
|
||
|
return nil
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func (m mockIndex) LabelValues(names ...string) (StringTuples, error) {
|
||
|
// TODO support composite indexes
|
||
|
if len(names) != 1 {
|
||
|
return nil, errors.New("composite indexes not supported yet")
|
||
|
}
|
||
|
|
||
7 years ago
|
return NewStringTuples(m.labelIndex[names[0]], 1)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func (m mockIndex) Postings(name, value string) (Postings, error) {
|
||
7 years ago
|
l := labels.Label{Name: name, Value: value}
|
||
|
return NewListPostings(m.postings[l]), nil
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
func (m mockIndex) SortedPostings(p Postings) Postings {
|
||
7 years ago
|
ep, err := ExpandPostings(p)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
return ErrPostings(errors.Wrap(err, "expand postings"))
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
sort.Slice(ep, func(i, j int) bool {
|
||
|
return labels.Compare(m.series[ep[i]].l, m.series[ep[j]].l) < 0
|
||
|
})
|
||
7 years ago
|
return NewListPostings(ep)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta) error {
|
||
8 years ago
|
s, ok := m.series[ref]
|
||
|
if !ok {
|
||
7 years ago
|
return errors.New("not found")
|
||
8 years ago
|
}
|
||
7 years ago
|
*lset = append((*lset)[:0], s.l...)
|
||
|
*chks = append((*chks)[:0], s.chunks...)
|
||
8 years ago
|
|
||
7 years ago
|
return nil
|
||
8 years ago
|
}
|
||
|
|
||
|
func (m mockIndex) LabelIndices() ([][]string, error) {
|
||
|
res := make([][]string, 0, len(m.labelIndex))
|
||
|
for k := range m.labelIndex {
|
||
|
res = append(res, []string{k})
|
||
|
}
|
||
|
return res, nil
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func TestIndexRW_Create_Open(t *testing.T) {
|
||
|
dir, err := ioutil.TempDir("", "test_index_create")
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
defer func() {
|
||
|
testutil.Ok(t, os.RemoveAll(dir))
|
||
|
}()
|
||
8 years ago
|
|
||
6 years ago
|
fn := filepath.Join(dir, indexFilename)
|
||
7 years ago
|
|
||
8 years ago
|
// An empty index must still result in a readable file.
|
||
7 years ago
|
iw, err := NewWriter(fn)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
|
testutil.Ok(t, iw.Close())
|
||
8 years ago
|
|
||
7 years ago
|
ir, err := NewFileReader(fn)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
|
testutil.Ok(t, ir.Close())
|
||
8 years ago
|
|
||
8 years ago
|
// Modify magic header must cause open to fail.
|
||
7 years ago
|
f, err := os.OpenFile(fn, os.O_WRONLY, 0666)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
_, err = f.WriteAt([]byte{0, 0}, 0)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
f.Close()
|
||
8 years ago
|
|
||
7 years ago
|
_, err = NewFileReader(dir)
|
||
7 years ago
|
testutil.NotOk(t, err)
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
func TestIndexRW_Postings(t *testing.T) {
|
||
|
dir, err := ioutil.TempDir("", "test_index_postings")
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
defer func() {
|
||
|
testutil.Ok(t, os.RemoveAll(dir))
|
||
|
}()
|
||
8 years ago
|
|
||
6 years ago
|
fn := filepath.Join(dir, indexFilename)
|
||
7 years ago
|
|
||
|
iw, err := NewWriter(fn)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
|
series := []labels.Labels{
|
||
|
labels.FromStrings("a", "1", "b", "1"),
|
||
|
labels.FromStrings("a", "1", "b", "2"),
|
||
|
labels.FromStrings("a", "1", "b", "3"),
|
||
|
labels.FromStrings("a", "1", "b", "4"),
|
||
|
}
|
||
|
|
||
7 years ago
|
err = iw.AddSymbols(map[string]struct{}{
|
||
7 years ago
|
"a": {},
|
||
|
"b": {},
|
||
|
"1": {},
|
||
|
"2": {},
|
||
|
"3": {},
|
||
|
"4": {},
|
||
7 years ago
|
})
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
7 years ago
|
|
||
8 years ago
|
// Postings lists are only written if a series with the respective
|
||
|
// reference was added before.
|
||
7 years ago
|
testutil.Ok(t, iw.AddSeries(1, series[0]))
|
||
|
testutil.Ok(t, iw.AddSeries(2, series[1]))
|
||
|
testutil.Ok(t, iw.AddSeries(3, series[2]))
|
||
|
testutil.Ok(t, iw.AddSeries(4, series[3]))
|
||
8 years ago
|
|
||
6 years ago
|
err = iw.WritePostings("a", "1", newListPostings(1, 2, 3, 4))
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Ok(t, iw.Close())
|
||
8 years ago
|
|
||
7 years ago
|
ir, err := NewFileReader(fn)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
|
p, err := ir.Postings("a", "1")
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
var l labels.Labels
|
||
7 years ago
|
var c []chunks.Meta
|
||
7 years ago
|
|
||
8 years ago
|
for i := 0; p.Next(); i++ {
|
||
7 years ago
|
err := ir.Series(p.At(), &l, &c)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
|
testutil.Equals(t, 0, len(c))
|
||
|
testutil.Equals(t, series[i], l)
|
||
8 years ago
|
}
|
||
7 years ago
|
testutil.Ok(t, p.Err())
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Ok(t, ir.Close())
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func TestPersistence_index_e2e(t *testing.T) {
|
||
|
dir, err := ioutil.TempDir("", "test_persistence_e2e")
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
defer func() {
|
||
|
testutil.Ok(t, os.RemoveAll(dir))
|
||
|
}()
|
||
8 years ago
|
|
||
6 years ago
|
lbls, err := labels.ReadLabels(filepath.Join("..", "testdata", "20kseries.json"), 20000)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
// Sort labels as the index writer expects series in sorted order.
|
||
|
sort.Sort(labels.Slice(lbls))
|
||
|
|
||
|
symbols := map[string]struct{}{}
|
||
|
for _, lset := range lbls {
|
||
|
for _, l := range lset {
|
||
|
symbols[l.Name] = struct{}{}
|
||
|
symbols[l.Value] = struct{}{}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
var input indexWriterSeriesSlice
|
||
|
|
||
|
// Generate ChunkMetas for every label set.
|
||
|
for i, lset := range lbls {
|
||
7 years ago
|
var metas []chunks.Meta
|
||
8 years ago
|
|
||
|
for j := 0; j <= (i % 20); j++ {
|
||
7 years ago
|
metas = append(metas, chunks.Meta{
|
||
8 years ago
|
MinTime: int64(j * 10000),
|
||
|
MaxTime: int64((j + 1) * 10000),
|
||
|
Ref: rand.Uint64(),
|
||
7 years ago
|
Chunk: chunkenc.NewXORChunk(),
|
||
8 years ago
|
})
|
||
|
}
|
||
|
input = append(input, &indexWriterSeries{
|
||
|
labels: lset,
|
||
|
chunks: metas,
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
iw, err := NewWriter(filepath.Join(dir, indexFilename))
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Ok(t, iw.AddSymbols(symbols))
|
||
7 years ago
|
|
||
8 years ago
|
// Population procedure as done by compaction.
|
||
|
var (
|
||
7 years ago
|
postings = NewMemPostings()
|
||
|
values = map[string]map[string]struct{}{}
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
mi := newMockIndex()
|
||
|
|
||
8 years ago
|
for i, s := range input {
|
||
7 years ago
|
err = iw.AddSeries(uint64(i), s.labels, s.chunks...)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
testutil.Ok(t, mi.AddSeries(uint64(i), s.labels, s.chunks...))
|
||
8 years ago
|
|
||
|
for _, l := range s.labels {
|
||
|
valset, ok := values[l.Name]
|
||
|
if !ok {
|
||
7 years ago
|
valset = map[string]struct{}{}
|
||
8 years ago
|
values[l.Name] = valset
|
||
|
}
|
||
7 years ago
|
valset[l.Value] = struct{}{}
|
||
8 years ago
|
}
|
||
7 years ago
|
postings.Add(uint64(i), s.labels)
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
for k, v := range values {
|
||
7 years ago
|
var vals []string
|
||
|
for e := range v {
|
||
|
vals = append(vals, e)
|
||
|
}
|
||
|
sort.Strings(vals)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Ok(t, iw.WriteLabelIndex([]string{k}, vals))
|
||
|
testutil.Ok(t, mi.WriteLabelIndex([]string{k}, vals))
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
all := make([]uint64, len(lbls))
|
||
8 years ago
|
for i := range all {
|
||
7 years ago
|
all[i] = uint64(i)
|
||
8 years ago
|
}
|
||
6 years ago
|
err = iw.WritePostings("", "", newListPostings(all...))
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
6 years ago
|
testutil.Ok(t, mi.WritePostings("", "", newListPostings(all...)))
|
||
8 years ago
|
|
||
6 years ago
|
for n, e := range postings.m {
|
||
|
for v := range e {
|
||
|
err = iw.WritePostings(n, v, postings.Get(n, v))
|
||
|
testutil.Ok(t, err)
|
||
|
mi.WritePostings(n, v, postings.Get(n, v))
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
err = iw.Close()
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
6 years ago
|
ir, err := NewFileReader(filepath.Join(dir, indexFilename))
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
for p := range mi.postings {
|
||
7 years ago
|
gotp, err := ir.Postings(p.Name, p.Value)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
expp, err := mi.Postings(p.Name, p.Value)
|
||
6 years ago
|
testutil.Ok(t, err)
|
||
7 years ago
|
|
||
|
var lset, explset labels.Labels
|
||
7 years ago
|
var chks, expchks []chunks.Meta
|
||
8 years ago
|
|
||
8 years ago
|
for gotp.Next() {
|
||
7 years ago
|
testutil.Assert(t, expp.Next() == true, "")
|
||
8 years ago
|
|
||
8 years ago
|
ref := gotp.At()
|
||
8 years ago
|
|
||
7 years ago
|
err := ir.Series(ref, &lset, &chks)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
err = mi.Series(expp.At(), &explset, &expchks)
|
||
6 years ago
|
testutil.Ok(t, err)
|
||
7 years ago
|
testutil.Equals(t, explset, lset)
|
||
|
testutil.Equals(t, expchks, chks)
|
||
8 years ago
|
}
|
||
7 years ago
|
testutil.Assert(t, expp.Next() == false, "")
|
||
|
testutil.Ok(t, gotp.Err())
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
for k, v := range mi.labelIndex {
|
||
7 years ago
|
tplsExp, err := NewStringTuples(v, 1)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
|
tplsRes, err := ir.LabelValues(k)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Equals(t, tplsExp.Len(), tplsRes.Len())
|
||
8 years ago
|
for i := 0; i < tplsExp.Len(); i++ {
|
||
|
strsExp, err := tplsExp.At(i)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
|
strsRes, err := tplsRes.At(i)
|
||
7 years ago
|
testutil.Ok(t, err)
|
||
8 years ago
|
|
||
7 years ago
|
testutil.Equals(t, strsExp, strsRes)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
gotSymbols, err := ir.Symbols()
|
||
|
testutil.Ok(t, err)
|
||
|
|
||
|
testutil.Equals(t, len(mi.symbols), len(gotSymbols))
|
||
|
for s := range mi.symbols {
|
||
|
_, ok := gotSymbols[s]
|
||
|
testutil.Assert(t, ok, "")
|
||
|
}
|
||
|
|
||
7 years ago
|
testutil.Ok(t, ir.Close())
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
func TestDecbufUvariantWithInvalidBuffer(t *testing.T) {
|
||
7 years ago
|
b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81})
|
||
|
|
||
6 years ago
|
db := encoding.NewDecbufUvarintAt(b, 0, castagnoliTable)
|
||
|
testutil.NotOk(t, db.Err())
|
||
7 years ago
|
}
|
||
6 years ago
|
|
||
|
func TestReaderWithInvalidBuffer(t *testing.T) {
|
||
|
b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81})
|
||
|
|
||
|
_, err := NewReader(b)
|
||
|
testutil.NotOk(t, err)
|
||
|
}
|
||
6 years ago
|
|
||
|
// TestNewFileReaderErrorNoOpenFiles ensures that in case of an error no file remains open.
|
||
|
func TestNewFileReaderErrorNoOpenFiles(t *testing.T) {
|
||
|
dir := testutil.NewTemporaryDirectory("block", t)
|
||
|
|
||
|
idxName := filepath.Join(dir.Path(), "index")
|
||
|
err := ioutil.WriteFile(idxName, []byte("corrupted contents"), 0644)
|
||
|
testutil.Ok(t, err)
|
||
|
|
||
|
_, err = NewFileReader(idxName)
|
||
|
testutil.NotOk(t, err)
|
||
|
|
||
|
// dir.Close will fail on Win if idxName fd is not closed on error path.
|
||
|
dir.Close()
|
||
|
}
|