2017-04-10 11:59:45 -07:00
|
|
|
// 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.
|
|
|
|
|
2016-12-14 06:47:05 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-07-05 07:19:28 -07:00
|
|
|
"fmt"
|
2018-10-23 14:35:52 -07:00
|
|
|
"io/ioutil"
|
2017-04-13 12:06:14 -07:00
|
|
|
"math"
|
2017-04-13 07:27:31 -07:00
|
|
|
"math/rand"
|
2018-10-23 14:35:52 -07:00
|
|
|
"os"
|
2018-10-25 02:32:57 -07:00
|
|
|
"path/filepath"
|
2016-12-19 02:44:11 -08:00
|
|
|
"sort"
|
2016-12-14 06:47:05 -08:00
|
|
|
"testing"
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/tsdb/chunkenc"
|
2017-04-09 07:00:25 -07:00
|
|
|
"github.com/prometheus/tsdb/chunks"
|
2017-11-30 06:34:49 -08:00
|
|
|
"github.com/prometheus/tsdb/index"
|
2017-04-04 02:27:26 -07:00
|
|
|
"github.com/prometheus/tsdb/labels"
|
2017-12-06 17:06:14 -08:00
|
|
|
"github.com/prometheus/tsdb/testutil"
|
2018-10-25 13:06:19 -07:00
|
|
|
"github.com/prometheus/tsdb/tsdbutil"
|
2016-12-14 06:47:05 -08:00
|
|
|
)
|
|
|
|
|
2018-09-21 01:07:35 -07:00
|
|
|
type mockSeriesSet struct {
|
|
|
|
next func() bool
|
|
|
|
series func() Series
|
|
|
|
err func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockSeriesSet) Next() bool { return m.next() }
|
|
|
|
func (m *mockSeriesSet) At() Series { return m.series() }
|
|
|
|
func (m *mockSeriesSet) Err() error { return m.err() }
|
|
|
|
|
|
|
|
func newMockSeriesSet(list []Series) *mockSeriesSet {
|
|
|
|
i := -1
|
|
|
|
return &mockSeriesSet{
|
|
|
|
next: func() bool {
|
|
|
|
i++
|
|
|
|
return i < len(list)
|
|
|
|
},
|
|
|
|
series: func() Series {
|
|
|
|
return list[i]
|
|
|
|
},
|
|
|
|
err: func() error { return nil },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
type mockSeriesIterator struct {
|
2017-01-02 04:27:52 -08:00
|
|
|
seek func(int64) bool
|
|
|
|
at func() (int64, float64)
|
|
|
|
next func() bool
|
|
|
|
err func() error
|
2016-12-19 02:44:11 -08:00
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (m *mockSeriesIterator) Seek(t int64) bool { return m.seek(t) }
|
|
|
|
func (m *mockSeriesIterator) At() (int64, float64) { return m.at() }
|
|
|
|
func (m *mockSeriesIterator) Next() bool { return m.next() }
|
|
|
|
func (m *mockSeriesIterator) Err() error { return m.err() }
|
2016-12-19 02:44:11 -08:00
|
|
|
|
|
|
|
type mockSeries struct {
|
2016-12-21 06:12:26 -08:00
|
|
|
labels func() labels.Labels
|
2016-12-19 02:44:11 -08:00
|
|
|
iterator func() SeriesIterator
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
type Sample = tsdbutil.Sample
|
|
|
|
|
|
|
|
func newSeries(l map[string]string, s []Sample) Series {
|
2017-05-03 10:15:28 -07:00
|
|
|
return &mockSeries{
|
|
|
|
labels: func() labels.Labels { return labels.FromMap(l) },
|
|
|
|
iterator: func() SeriesIterator { return newListSeriesIterator(s) },
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 06:12:26 -08:00
|
|
|
func (m *mockSeries) Labels() labels.Labels { return m.labels() }
|
2016-12-19 02:44:11 -08:00
|
|
|
func (m *mockSeries) Iterator() SeriesIterator { return m.iterator() }
|
|
|
|
|
|
|
|
type listSeriesIterator struct {
|
2018-10-25 13:06:19 -07:00
|
|
|
list []Sample
|
2016-12-19 02:44:11 -08:00
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
func newListSeriesIterator(list []Sample) *listSeriesIterator {
|
2016-12-19 02:44:11 -08:00
|
|
|
return &listSeriesIterator{list: list, idx: -1}
|
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (it *listSeriesIterator) At() (int64, float64) {
|
2016-12-19 02:44:11 -08:00
|
|
|
s := it.list[it.idx]
|
2018-10-25 13:06:19 -07:00
|
|
|
return s.T(), s.V()
|
2016-12-19 02:44:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *listSeriesIterator) Next() bool {
|
|
|
|
it.idx++
|
|
|
|
return it.idx < len(it.list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *listSeriesIterator) Seek(t int64) bool {
|
2016-12-21 07:06:33 -08:00
|
|
|
if it.idx == -1 {
|
|
|
|
it.idx = 0
|
|
|
|
}
|
2016-12-19 02:44:11 -08:00
|
|
|
// Do binary search between current position and end.
|
|
|
|
it.idx = sort.Search(len(it.list)-it.idx, func(i int) bool {
|
|
|
|
s := it.list[i+it.idx]
|
2018-10-25 13:06:19 -07:00
|
|
|
return s.T() >= t
|
2016-12-19 02:44:11 -08:00
|
|
|
})
|
2016-12-21 07:06:33 -08:00
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
return it.idx < len(it.list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *listSeriesIterator) Err() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func TestMergedSeriesSet(t *testing.T) {
|
2016-12-19 02:44:11 -08:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
// The input sets in order (samples in series in b are strictly
|
|
|
|
// after those in a).
|
|
|
|
a, b SeriesSet
|
2017-01-05 23:08:02 -08:00
|
|
|
// The composition of a and b in the partition series set must yield
|
2016-12-19 02:44:11 -08:00
|
|
|
// results equivalent to the result series set.
|
|
|
|
exp SeriesSet
|
|
|
|
}{
|
|
|
|
{
|
2018-09-21 01:07:35 -07:00
|
|
|
a: newMockSeriesSet([]Series{
|
2016-12-19 02:44:11 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
2016-12-19 02:44:11 -08:00
|
|
|
}),
|
|
|
|
}),
|
2018-09-21 01:07:35 -07:00
|
|
|
b: newMockSeriesSet([]Series{
|
2016-12-19 02:44:11 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 2, v: 2},
|
2016-12-19 02:44:11 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"b": "b",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
2016-12-19 02:44:11 -08:00
|
|
|
}),
|
|
|
|
}),
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2016-12-19 02:44:11 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
|
|
|
sample{t: 2, v: 2},
|
2016-12-19 02:44:11 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"b": "b",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
2016-12-19 02:44:11 -08:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
2017-01-03 10:02:42 -08:00
|
|
|
{
|
2018-09-21 01:07:35 -07:00
|
|
|
a: newMockSeriesSet([]Series{
|
2017-01-03 10:02:42 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "prometheus",
|
|
|
|
"instance": "127.0.0.1:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "prometheus",
|
|
|
|
"instance": "localhost:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 2},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
}),
|
2018-09-21 01:07:35 -07:00
|
|
|
b: newMockSeriesSet([]Series{
|
2017-01-03 10:02:42 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "prometheus",
|
|
|
|
"instance": "127.0.0.1:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 2, v: 1},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "query",
|
|
|
|
"instance": "localhost:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 2, v: 2},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
}),
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2017-01-03 10:02:42 -08:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "prometheus",
|
|
|
|
"instance": "127.0.0.1:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 1},
|
|
|
|
sample{t: 2, v: 1},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "prometheus",
|
|
|
|
"instance": "localhost:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 1, v: 2},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"handler": "query",
|
|
|
|
"instance": "localhost:9090",
|
2018-10-25 13:06:19 -07:00
|
|
|
}, []Sample{
|
|
|
|
sample{t: 2, v: 2},
|
2017-01-03 10:02:42 -08:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
2016-12-19 02:44:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for _, c := range cases {
|
2017-03-14 07:24:08 -07:00
|
|
|
res := newMergedSeriesSet(c.a, c.b)
|
2016-12-19 02:44:11 -08:00
|
|
|
|
|
|
|
for {
|
|
|
|
eok, rok := c.exp.Next(), res.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, eok, rok)
|
2016-12-19 02:44:11 -08:00
|
|
|
|
|
|
|
if !eok {
|
|
|
|
continue Outer
|
|
|
|
}
|
2017-01-02 04:27:52 -08:00
|
|
|
sexp := c.exp.At()
|
|
|
|
sres := res.At()
|
2016-12-19 02:44:11 -08:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, sexp.Labels(), sres.Labels())
|
2016-12-19 02:44:11 -08:00
|
|
|
|
|
|
|
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
|
|
|
|
smplRes, errRes := expandSeriesIterator(sres.Iterator())
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
2016-12-19 02:44:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandSeriesIterator(it SeriesIterator) (r []sample, err error) {
|
|
|
|
for it.Next() {
|
2017-01-02 04:27:52 -08:00
|
|
|
t, v := it.At()
|
2016-12-19 02:44:11 -08:00
|
|
|
r = append(r, sample{t: t, v: v})
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, it.Err()
|
|
|
|
}
|
2017-04-09 07:00:25 -07:00
|
|
|
|
2018-10-12 02:45:19 -07:00
|
|
|
type seriesSamples struct {
|
2017-04-13 07:27:31 -07:00
|
|
|
lset map[string]string
|
|
|
|
chunks [][]sample
|
2018-10-12 02:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index: labels -> postings -> chunkMetas -> chunkRef
|
|
|
|
// ChunkReader: ref -> vals
|
|
|
|
func createIdxChkReaders(tc []seriesSamples) (IndexReader, ChunkReader) {
|
2017-04-13 07:27:31 -07:00
|
|
|
sort.Slice(tc, func(i, j int) bool {
|
|
|
|
return labels.Compare(labels.FromMap(tc[i].lset), labels.FromMap(tc[i].lset)) < 0
|
|
|
|
})
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
postings := index.NewMemPostings()
|
|
|
|
chkReader := mockChunkReader(make(map[uint64]chunkenc.Chunk))
|
2017-07-21 01:37:52 -07:00
|
|
|
lblIdx := make(map[string]stringset)
|
2017-04-13 07:27:31 -07:00
|
|
|
mi := newMockIndex()
|
|
|
|
|
|
|
|
for i, s := range tc {
|
2017-05-22 01:01:57 -07:00
|
|
|
i = i + 1 // 0 is not a valid posting.
|
2017-11-30 06:34:49 -08:00
|
|
|
metas := make([]chunks.Meta, 0, len(s.chunks))
|
2017-04-13 07:27:31 -07:00
|
|
|
for _, chk := range s.chunks {
|
|
|
|
// Collisions can be there, but for tests, its fine.
|
|
|
|
ref := rand.Uint64()
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
metas = append(metas, chunks.Meta{
|
2017-04-13 07:27:31 -07:00
|
|
|
MinTime: chk[0].t,
|
|
|
|
MaxTime: chk[len(chk)-1].t,
|
|
|
|
Ref: ref,
|
|
|
|
})
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
chunk := chunkenc.NewXORChunk()
|
2017-04-13 07:27:31 -07:00
|
|
|
app, _ := chunk.Appender()
|
|
|
|
for _, smpl := range chk {
|
|
|
|
app.Append(smpl.t, smpl.v)
|
|
|
|
}
|
|
|
|
chkReader[ref] = chunk
|
|
|
|
}
|
|
|
|
|
2017-07-21 01:37:52 -07:00
|
|
|
ls := labels.FromMap(s.lset)
|
2017-09-04 07:08:38 -07:00
|
|
|
mi.AddSeries(uint64(i), ls, metas...)
|
2017-04-13 07:27:31 -07:00
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
postings.Add(uint64(i), ls)
|
2017-07-21 01:37:52 -07:00
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
for _, l := range ls {
|
2017-07-21 01:37:52 -07:00
|
|
|
vs, present := lblIdx[l.Name]
|
|
|
|
if !present {
|
|
|
|
vs = stringset{}
|
|
|
|
lblIdx[l.Name] = vs
|
|
|
|
}
|
|
|
|
vs.set(l.Value)
|
2017-04-13 07:27:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 01:37:52 -07:00
|
|
|
for l, vs := range lblIdx {
|
|
|
|
mi.WriteLabelIndex([]string{l}, vs.slice())
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
postings.Iter(func(l labels.Label, p index.Postings) error {
|
|
|
|
return mi.WritePostings(l.Name, l.Value, p)
|
|
|
|
})
|
2017-04-13 07:27:31 -07:00
|
|
|
|
|
|
|
return mi, chkReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockQuerier(t *testing.T) {
|
2018-10-25 13:06:19 -07:00
|
|
|
newSeries := func(l map[string]string, s []Sample) Series {
|
2017-04-13 12:06:14 -07:00
|
|
|
return &mockSeries{
|
|
|
|
labels: func() labels.Labels { return labels.FromMap(l) },
|
|
|
|
iterator: func() SeriesIterator { return newListSeriesIterator(s) },
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 07:27:31 -07:00
|
|
|
|
2017-04-21 13:08:26 -07:00
|
|
|
type query struct {
|
|
|
|
mint, maxt int64
|
|
|
|
ms []labels.Matcher
|
|
|
|
exp SeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := struct {
|
2018-10-12 02:45:19 -07:00
|
|
|
data []seriesSamples
|
2017-04-21 13:08:26 -07:00
|
|
|
|
|
|
|
queries []query
|
2017-04-13 07:27:31 -07:00
|
|
|
}{
|
2018-10-12 02:45:19 -07:00
|
|
|
data: []seriesSamples{
|
2017-04-13 07:27:31 -07:00
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 2}, {2, 3}, {3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{5, 2}, {6, 3}, {7, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 1}, {2, 2}, {3, 3},
|
|
|
|
},
|
|
|
|
{
|
2017-04-13 12:06:14 -07:00
|
|
|
{5, 3}, {6, 6},
|
2017-04-13 07:27:31 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"b": "b",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 3}, {2, 2}, {3, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{5, 1}, {6, 7}, {7, 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-04-21 13:08:26 -07:00
|
|
|
queries: []query{
|
|
|
|
{
|
|
|
|
mint: 0,
|
|
|
|
maxt: 0,
|
|
|
|
ms: []labels.Matcher{},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{}),
|
2017-04-21 13:08:26 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 0,
|
|
|
|
maxt: 0,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{}),
|
2017-04-21 13:08:26 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 1,
|
|
|
|
maxt: 0,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{}),
|
2017-04-21 13:08:26 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 2,
|
|
|
|
maxt: 6,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2017-04-21 13:08:26 -07:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{2, 3}, sample{3, 4}, sample{5, 2}, sample{6, 3}},
|
2017-04-21 13:08:26 -07:00
|
|
|
),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{2, 2}, sample{3, 3}, sample{5, 3}, sample{6, 6}},
|
2017-04-21 13:08:26 -07:00
|
|
|
),
|
|
|
|
}),
|
|
|
|
},
|
2017-04-13 12:06:14 -07:00
|
|
|
},
|
2017-04-13 07:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Outer:
|
2017-12-06 16:27:09 -08:00
|
|
|
for _, c := range cases.queries {
|
2017-04-21 13:08:26 -07:00
|
|
|
ir, cr := createIdxChkReaders(cases.data)
|
2017-04-13 07:27:31 -07:00
|
|
|
querier := &blockQuerier{
|
2017-05-17 02:19:42 -07:00
|
|
|
index: ir,
|
|
|
|
chunks: cr,
|
2018-11-14 08:40:01 -08:00
|
|
|
tombstones: newMemTombstones(),
|
2017-04-13 07:27:31 -07:00
|
|
|
|
|
|
|
mint: c.mint,
|
|
|
|
maxt: c.maxt,
|
|
|
|
}
|
|
|
|
|
2017-11-13 03:16:58 -08:00
|
|
|
res, err := querier.Select(c.ms...)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(t, err)
|
2017-04-13 07:27:31 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
eok, rok := c.exp.Next(), res.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, eok, rok)
|
2017-04-13 07:27:31 -07:00
|
|
|
|
|
|
|
if !eok {
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
sexp := c.exp.At()
|
|
|
|
sres := res.At()
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, sexp.Labels(), sres.Labels())
|
2017-04-13 07:27:31 -07:00
|
|
|
|
|
|
|
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
|
|
|
|
smplRes, errRes := expandSeriesIterator(sres.Iterator())
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
2017-04-13 07:27:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-04-09 07:00:25 -07:00
|
|
|
|
2017-05-22 01:01:57 -07:00
|
|
|
func TestBlockQuerierDelete(t *testing.T) {
|
2018-10-25 13:06:19 -07:00
|
|
|
newSeries := func(l map[string]string, s []Sample) Series {
|
2017-05-22 01:01:57 -07:00
|
|
|
return &mockSeries{
|
|
|
|
labels: func() labels.Labels { return labels.FromMap(l) },
|
|
|
|
iterator: func() SeriesIterator { return newListSeriesIterator(s) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type query struct {
|
|
|
|
mint, maxt int64
|
|
|
|
ms []labels.Matcher
|
|
|
|
exp SeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := struct {
|
2018-10-12 02:45:19 -07:00
|
|
|
data []seriesSamples
|
2017-05-22 01:01:57 -07:00
|
|
|
|
2017-11-13 04:32:24 -08:00
|
|
|
tombstones TombstoneReader
|
2017-05-22 01:01:57 -07:00
|
|
|
queries []query
|
|
|
|
}{
|
2018-10-12 02:45:19 -07:00
|
|
|
data: []seriesSamples{
|
2017-05-22 01:01:57 -07:00
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 2}, {2, 3}, {3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{5, 2}, {6, 3}, {7, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 1}, {2, 2}, {3, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{4, 15}, {5, 3}, {6, 6},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: map[string]string{
|
|
|
|
"b": "b",
|
|
|
|
},
|
|
|
|
chunks: [][]sample{
|
|
|
|
{
|
|
|
|
{1, 3}, {2, 2}, {3, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{5, 1}, {6, 7}, {7, 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-10 06:24:13 -07:00
|
|
|
tombstones: &memTombstones{intvlGroups: map[uint64]Intervals{
|
2018-07-06 05:30:27 -07:00
|
|
|
1: Intervals{{1, 3}},
|
|
|
|
2: Intervals{{1, 3}, {6, 10}},
|
|
|
|
3: Intervals{{6, 10}},
|
|
|
|
}},
|
2017-05-22 01:01:57 -07:00
|
|
|
queries: []query{
|
|
|
|
{
|
|
|
|
mint: 2,
|
|
|
|
maxt: 7,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2017-05-22 01:01:57 -07:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{5, 2}, sample{6, 3}, sample{7, 4}},
|
2017-05-22 01:01:57 -07:00
|
|
|
),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{4, 15}, sample{5, 3}},
|
2017-05-22 01:01:57 -07:00
|
|
|
),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 2,
|
|
|
|
maxt: 7,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("b", "b")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2017-05-22 01:01:57 -07:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{4, 15}, sample{5, 3}},
|
2017-05-22 01:01:57 -07:00
|
|
|
),
|
|
|
|
newSeries(map[string]string{
|
|
|
|
"b": "b",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{2, 2}, sample{3, 6}, sample{5, 1}},
|
2017-05-22 01:01:57 -07:00
|
|
|
),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 1,
|
|
|
|
maxt: 4,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{
|
2017-05-22 01:01:57 -07:00
|
|
|
newSeries(map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
[]Sample{sample{4, 15}},
|
2017-05-22 01:01:57 -07:00
|
|
|
),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mint: 1,
|
|
|
|
maxt: 3,
|
|
|
|
ms: []labels.Matcher{labels.NewEqualMatcher("a", "a")},
|
2018-09-21 01:07:35 -07:00
|
|
|
exp: newMockSeriesSet([]Series{}),
|
2017-05-22 01:01:57 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for _, c := range cases.queries {
|
|
|
|
ir, cr := createIdxChkReaders(cases.data)
|
|
|
|
querier := &blockQuerier{
|
|
|
|
index: ir,
|
|
|
|
chunks: cr,
|
2017-05-23 22:54:24 -07:00
|
|
|
tombstones: cases.tombstones,
|
2017-05-22 01:01:57 -07:00
|
|
|
|
|
|
|
mint: c.mint,
|
|
|
|
maxt: c.maxt,
|
|
|
|
}
|
|
|
|
|
2017-11-13 03:16:58 -08:00
|
|
|
res, err := querier.Select(c.ms...)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(t, err)
|
2017-05-22 01:01:57 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
eok, rok := c.exp.Next(), res.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, eok, rok)
|
2017-05-22 01:01:57 -07:00
|
|
|
|
|
|
|
if !eok {
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
sexp := c.exp.At()
|
|
|
|
sres := res.At()
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, sexp.Labels(), sres.Labels())
|
2017-05-22 01:01:57 -07:00
|
|
|
|
|
|
|
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
|
|
|
|
smplRes, errRes := expandSeriesIterator(sres.Iterator())
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
2017-05-22 01:01:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-09 07:00:25 -07:00
|
|
|
func TestBaseChunkSeries(t *testing.T) {
|
|
|
|
type refdSeries struct {
|
|
|
|
lset labels.Labels
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks []chunks.Meta
|
2017-04-09 07:00:25 -07:00
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
ref uint64
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
series []refdSeries
|
|
|
|
// Postings should be in the sorted order of the the series
|
2017-09-04 07:08:38 -07:00
|
|
|
postings []uint64
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
expIdxs []int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
series: []refdSeries{
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"a", "a"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{
|
2017-04-09 07:00:25 -07:00
|
|
|
{Ref: 29}, {Ref: 45}, {Ref: 245}, {Ref: 123}, {Ref: 4232}, {Ref: 5344},
|
|
|
|
{Ref: 121},
|
|
|
|
},
|
|
|
|
ref: 12,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"a", "a"}, {"b", "b"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{
|
2017-04-09 07:00:25 -07:00
|
|
|
{Ref: 82}, {Ref: 23}, {Ref: 234}, {Ref: 65}, {Ref: 26},
|
|
|
|
},
|
|
|
|
ref: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"b", "c"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{{Ref: 8282}},
|
2017-04-09 07:00:25 -07:00
|
|
|
ref: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"b", "b"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{
|
2017-04-09 07:00:25 -07:00
|
|
|
{Ref: 829}, {Ref: 239}, {Ref: 2349}, {Ref: 659}, {Ref: 269},
|
|
|
|
},
|
|
|
|
ref: 108,
|
|
|
|
},
|
|
|
|
},
|
2017-10-11 00:33:35 -07:00
|
|
|
postings: []uint64{12, 13, 10, 108}, // 13 doesn't exist and should just be skipped over.
|
|
|
|
expIdxs: []int{0, 1, 3},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
series: []refdSeries{
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"a", "a"}, {"b", "b"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{
|
2017-04-09 07:00:25 -07:00
|
|
|
{Ref: 82}, {Ref: 23}, {Ref: 234}, {Ref: 65}, {Ref: 26},
|
|
|
|
},
|
|
|
|
ref: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lset: labels.New([]labels.Label{{"b", "c"}}...),
|
2017-11-30 06:34:49 -08:00
|
|
|
chunks: []chunks.Meta{{Ref: 8282}},
|
2017-10-11 00:33:35 -07:00
|
|
|
ref: 3,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
},
|
2017-09-04 07:08:38 -07:00
|
|
|
postings: []uint64{},
|
2017-10-11 00:33:35 -07:00
|
|
|
expIdxs: []int{},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
mi := newMockIndex()
|
|
|
|
for _, s := range tc.series {
|
|
|
|
mi.AddSeries(s.ref, s.lset, s.chunks...)
|
|
|
|
}
|
|
|
|
|
|
|
|
bcs := &baseChunkSeries{
|
2017-11-30 06:34:49 -08:00
|
|
|
p: index.NewListPostings(tc.postings),
|
2017-05-17 02:19:42 -07:00
|
|
|
index: mi,
|
2018-11-14 08:40:01 -08:00
|
|
|
tombstones: newMemTombstones(),
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for bcs.Next() {
|
2017-05-17 02:19:42 -07:00
|
|
|
lset, chks, _ := bcs.At()
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
idx := tc.expIdxs[i]
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, tc.series[idx].lset, lset)
|
|
|
|
testutil.Equals(t, tc.series[idx].chunks, chks)
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, len(tc.expIdxs), i)
|
|
|
|
testutil.Ok(t, bcs.Err())
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove after simpleSeries is merged
|
|
|
|
type itSeries struct {
|
|
|
|
si SeriesIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s itSeries) Iterator() SeriesIterator { return s.si }
|
|
|
|
func (s itSeries) Labels() labels.Labels { return labels.Labels{} }
|
|
|
|
|
|
|
|
func TestSeriesIterator(t *testing.T) {
|
|
|
|
itcases := []struct {
|
2018-10-25 13:06:19 -07:00
|
|
|
a, b, c []Sample
|
|
|
|
exp []Sample
|
2017-04-13 12:07:21 -07:00
|
|
|
|
|
|
|
mint, maxt int64
|
2017-04-09 07:00:25 -07:00
|
|
|
}{
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{},
|
|
|
|
b: []Sample{},
|
|
|
|
c: []Sample{},
|
2017-04-09 07:00:25 -07:00
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{},
|
2017-04-13 12:07:21 -07:00
|
|
|
|
|
|
|
mint: math.MinInt64,
|
|
|
|
maxt: math.MaxInt64,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{1, 2},
|
|
|
|
sample{2, 3},
|
|
|
|
sample{3, 5},
|
|
|
|
sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{},
|
|
|
|
c: []Sample{
|
|
|
|
sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{1, 2}, sample{2, 3}, sample{3, 5}, sample{6, 1}, sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: math.MinInt64,
|
|
|
|
maxt: math.MaxInt64,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{},
|
|
|
|
b: []Sample{
|
|
|
|
sample{1, 2}, sample{2, 3}, sample{3, 5}, sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{1, 2}, sample{2, 3}, sample{3, 5}, sample{6, 1}, sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 2,
|
|
|
|
maxt: 8,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{1, 2}, sample{2, 3}, sample{3, 5}, sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{
|
|
|
|
sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{1, 2}, sample{2, 3}, sample{3, 5}, sample{6, 1}, sample{7, 89}, sample{9, 8}, sample{10, 22}, sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 6,
|
|
|
|
maxt: 10,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
seekcases := []struct {
|
2018-10-25 13:06:19 -07:00
|
|
|
a, b, c []Sample
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
seek int64
|
|
|
|
success bool
|
2018-10-25 13:06:19 -07:00
|
|
|
exp []Sample
|
2017-04-13 12:07:21 -07:00
|
|
|
|
|
|
|
mint, maxt int64
|
2017-04-09 07:00:25 -07:00
|
|
|
}{
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{},
|
|
|
|
b: []Sample{},
|
|
|
|
c: []Sample{},
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
seek: 0,
|
|
|
|
success: false,
|
|
|
|
exp: nil,
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{2, 3},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{},
|
|
|
|
c: []Sample{
|
|
|
|
sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 10,
|
|
|
|
success: false,
|
|
|
|
exp: nil,
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: math.MinInt64,
|
|
|
|
maxt: math.MaxInt64,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{},
|
|
|
|
b: []Sample{
|
|
|
|
sample{1, 2}, sample{3, 5}, sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 2,
|
|
|
|
success: true,
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{3, 5}, sample{6, 1}, sample{7, 89}, sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 5,
|
|
|
|
maxt: 8,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{
|
|
|
|
sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 10,
|
|
|
|
success: true,
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 10,
|
|
|
|
maxt: 203,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{6, 1},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{
|
|
|
|
sample{9, 8},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 203,
|
|
|
|
success: true,
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{
|
|
|
|
sample{203, 3493},
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 7,
|
|
|
|
maxt: 203,
|
2017-04-09 07:00:25 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Chunk", func(t *testing.T) {
|
|
|
|
for _, tc := range itcases {
|
2017-11-30 06:34:49 -08:00
|
|
|
chkMetas := []chunks.Meta{
|
2018-10-25 13:06:19 -07:00
|
|
|
tsdbutil.ChunkFromSamples(tc.a),
|
|
|
|
tsdbutil.ChunkFromSamples(tc.b),
|
|
|
|
tsdbutil.ChunkFromSamples(tc.c),
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
res := newChunkSeriesIterator(chkMetas, nil, tc.mint, tc.maxt)
|
2017-04-13 12:07:21 -07:00
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
smplValid := make([]Sample, 0)
|
2017-04-13 12:07:21 -07:00
|
|
|
for _, s := range tc.exp {
|
2018-10-25 13:06:19 -07:00
|
|
|
if s.T() >= tc.mint && s.T() <= tc.maxt {
|
|
|
|
smplValid = append(smplValid, Sample(s))
|
2017-04-13 12:07:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exp := newListSeriesIterator(smplValid)
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
smplExp, errExp := expandSeriesIterator(exp)
|
|
|
|
smplRes, errRes := expandSeriesIterator(res)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Seek", func(t *testing.T) {
|
2017-04-13 12:07:21 -07:00
|
|
|
extra := []struct {
|
2018-10-25 13:06:19 -07:00
|
|
|
a, b, c []Sample
|
2017-04-13 12:07:21 -07:00
|
|
|
|
|
|
|
seek int64
|
|
|
|
success bool
|
2018-10-25 13:06:19 -07:00
|
|
|
exp []Sample
|
2017-04-13 12:07:21 -07:00
|
|
|
|
|
|
|
mint, maxt int64
|
|
|
|
}{
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{6, 1},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{
|
|
|
|
sample{9, 8},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 203,
|
|
|
|
success: false,
|
|
|
|
exp: nil,
|
|
|
|
mint: 2,
|
|
|
|
maxt: 202,
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 13:06:19 -07:00
|
|
|
a: []Sample{
|
|
|
|
sample{6, 1},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
b: []Sample{
|
|
|
|
sample{9, 8},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
2018-10-25 13:06:19 -07:00
|
|
|
c: []Sample{
|
|
|
|
sample{10, 22}, sample{203, 3493},
|
2017-04-13 12:07:21 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
seek: 5,
|
|
|
|
success: true,
|
2018-10-25 13:06:19 -07:00
|
|
|
exp: []Sample{sample{10, 22}},
|
2017-04-13 12:07:21 -07:00
|
|
|
mint: 10,
|
|
|
|
maxt: 202,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
seekcases2 := append(seekcases, extra...)
|
|
|
|
|
|
|
|
for _, tc := range seekcases2 {
|
2017-11-30 06:34:49 -08:00
|
|
|
chkMetas := []chunks.Meta{
|
2018-10-25 13:06:19 -07:00
|
|
|
tsdbutil.ChunkFromSamples(tc.a),
|
|
|
|
tsdbutil.ChunkFromSamples(tc.b),
|
|
|
|
tsdbutil.ChunkFromSamples(tc.c),
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
res := newChunkSeriesIterator(chkMetas, nil, tc.mint, tc.maxt)
|
2017-04-13 12:07:21 -07:00
|
|
|
|
2018-10-25 13:06:19 -07:00
|
|
|
smplValid := make([]Sample, 0)
|
2017-04-13 12:07:21 -07:00
|
|
|
for _, s := range tc.exp {
|
2018-10-25 13:06:19 -07:00
|
|
|
if s.T() >= tc.mint && s.T() <= tc.maxt {
|
|
|
|
smplValid = append(smplValid, Sample(s))
|
2017-04-13 12:07:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exp := newListSeriesIterator(smplValid)
|
2017-04-09 07:00:25 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, tc.success, res.Seek(tc.seek))
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
if tc.success {
|
|
|
|
// Init the list and then proceed to check.
|
|
|
|
remaining := exp.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, remaining == true, "")
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
for remaining {
|
|
|
|
sExp, eExp := exp.At()
|
|
|
|
sRes, eRes := res.At()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, eExp, eRes)
|
|
|
|
testutil.Equals(t, sExp, sRes)
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
remaining = exp.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, remaining, res.Next())
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Chain", func(t *testing.T) {
|
|
|
|
for _, tc := range itcases {
|
|
|
|
a, b, c := itSeries{newListSeriesIterator(tc.a)},
|
|
|
|
itSeries{newListSeriesIterator(tc.b)},
|
|
|
|
itSeries{newListSeriesIterator(tc.c)}
|
|
|
|
|
|
|
|
res := newChainedSeriesIterator(a, b, c)
|
2018-10-25 13:06:19 -07:00
|
|
|
exp := newListSeriesIterator([]Sample(tc.exp))
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
smplExp, errExp := expandSeriesIterator(exp)
|
|
|
|
smplRes, errRes := expandSeriesIterator(res)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Seek", func(t *testing.T) {
|
|
|
|
for _, tc := range seekcases {
|
|
|
|
a, b, c := itSeries{newListSeriesIterator(tc.a)},
|
|
|
|
itSeries{newListSeriesIterator(tc.b)},
|
|
|
|
itSeries{newListSeriesIterator(tc.c)}
|
|
|
|
|
|
|
|
res := newChainedSeriesIterator(a, b, c)
|
|
|
|
exp := newListSeriesIterator(tc.exp)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, tc.success, res.Seek(tc.seek))
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
if tc.success {
|
|
|
|
// Init the list and then proceed to check.
|
|
|
|
remaining := exp.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, remaining == true, "")
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
for remaining {
|
|
|
|
sExp, eExp := exp.At()
|
|
|
|
sRes, eRes := res.At()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, eExp, eRes)
|
|
|
|
testutil.Equals(t, sExp, sRes)
|
2017-04-09 07:00:25 -07:00
|
|
|
|
|
|
|
remaining = exp.Next()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, remaining, res.Next())
|
2017-04-09 07:00:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 02:03:56 -07:00
|
|
|
|
2017-06-13 00:51:22 -07:00
|
|
|
// Regression for: https://github.com/prometheus/tsdb/pull/97
|
2017-06-30 06:06:27 -07:00
|
|
|
func TestChunkSeriesIterator_DoubleSeek(t *testing.T) {
|
2017-11-30 06:34:49 -08:00
|
|
|
chkMetas := []chunks.Meta{
|
2018-10-25 13:06:19 -07:00
|
|
|
tsdbutil.ChunkFromSamples([]Sample{}),
|
|
|
|
tsdbutil.ChunkFromSamples([]Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
tsdbutil.ChunkFromSamples([]Sample{sample{4, 4}, sample{5, 5}}),
|
2017-06-13 00:51:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
res := newChunkSeriesIterator(chkMetas, nil, 2, 8)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, res.Seek(1) == true, "")
|
|
|
|
testutil.Assert(t, res.Seek(2) == true, "")
|
2017-06-13 00:51:22 -07:00
|
|
|
ts, v := res.At()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, int64(2), ts)
|
|
|
|
testutil.Equals(t, float64(2), v)
|
2017-06-13 00:51:22 -07:00
|
|
|
}
|
|
|
|
|
2017-06-30 06:06:27 -07:00
|
|
|
// Regression when seeked chunks were still found via binary search and we always
|
|
|
|
// skipped to the end when seeking a value in the current chunk.
|
|
|
|
func TestChunkSeriesIterator_SeekInCurrentChunk(t *testing.T) {
|
2017-11-30 06:34:49 -08:00
|
|
|
metas := []chunks.Meta{
|
2018-10-25 13:06:19 -07:00
|
|
|
tsdbutil.ChunkFromSamples([]Sample{}),
|
|
|
|
tsdbutil.ChunkFromSamples([]Sample{sample{1, 2}, sample{3, 4}, sample{5, 6}, sample{7, 8}}),
|
|
|
|
tsdbutil.ChunkFromSamples([]Sample{}),
|
2017-06-30 06:06:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
it := newChunkSeriesIterator(metas, nil, 1, 7)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, it.Next() == true, "")
|
2017-06-30 06:06:27 -07:00
|
|
|
ts, v := it.At()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, int64(1), ts)
|
|
|
|
testutil.Equals(t, float64(2), v)
|
2017-06-30 06:06:27 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, it.Seek(4) == true, "")
|
2017-06-30 06:06:27 -07:00
|
|
|
ts, v = it.At()
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, int64(5), ts)
|
|
|
|
testutil.Equals(t, float64(6), v)
|
2017-06-30 06:06:27 -07:00
|
|
|
}
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
// Regression when calling Next() with a time bounded to fit within two samples.
|
|
|
|
// Seek gets called and advances beyond the max time, which was just accepted as a valid sample.
|
|
|
|
func TestChunkSeriesIterator_NextWithMinTime(t *testing.T) {
|
2017-11-30 06:34:49 -08:00
|
|
|
metas := []chunks.Meta{
|
2018-10-25 13:06:19 -07:00
|
|
|
tsdbutil.ChunkFromSamples([]Sample{sample{1, 6}, sample{5, 6}, sample{7, 8}}),
|
2017-08-28 15:39:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
it := newChunkSeriesIterator(metas, nil, 2, 4)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, it.Next() == false, "")
|
2017-08-28 15:39:17 -07:00
|
|
|
}
|
|
|
|
|
2017-05-01 02:03:56 -07:00
|
|
|
func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
|
|
|
|
lbls := []labels.Labels{labels.New(labels.Label{"a", "b"})}
|
2017-11-30 06:34:49 -08:00
|
|
|
chunkMetas := [][]chunks.Meta{
|
2017-05-01 02:03:56 -07:00
|
|
|
{
|
|
|
|
{MinTime: 1, MaxTime: 2, Ref: 1},
|
|
|
|
{MinTime: 3, MaxTime: 4, Ref: 2},
|
|
|
|
{MinTime: 10, MaxTime: 12, Ref: 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cr := mockChunkReader(
|
2017-11-30 06:34:49 -08:00
|
|
|
map[uint64]chunkenc.Chunk{
|
|
|
|
1: chunkenc.NewXORChunk(),
|
|
|
|
2: chunkenc.NewXORChunk(),
|
|
|
|
3: chunkenc.NewXORChunk(),
|
2017-05-01 02:03:56 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
m := &mockChunkSeriesSet{l: lbls, cm: chunkMetas, i: -1}
|
|
|
|
p := &populatedChunkSeries{
|
|
|
|
set: m,
|
|
|
|
chunks: cr,
|
|
|
|
|
2017-05-01 02:31:17 -07:00
|
|
|
mint: 0,
|
|
|
|
maxt: 0,
|
2017-05-01 02:03:56 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, p.Next() == false, "")
|
2017-05-01 02:31:17 -07:00
|
|
|
|
|
|
|
p.mint = 6
|
|
|
|
p.maxt = 9
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, p.Next() == false, "")
|
2017-05-01 02:31:17 -07:00
|
|
|
|
2017-05-03 10:15:28 -07:00
|
|
|
// Test the case where 1 chunk could cause an unpopulated chunk to be returned.
|
2017-11-30 06:34:49 -08:00
|
|
|
chunkMetas = [][]chunks.Meta{
|
2017-05-03 10:15:28 -07:00
|
|
|
{
|
|
|
|
{MinTime: 1, MaxTime: 2, Ref: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m = &mockChunkSeriesSet{l: lbls, cm: chunkMetas, i: -1}
|
|
|
|
p = &populatedChunkSeries{
|
|
|
|
set: m,
|
|
|
|
chunks: cr,
|
|
|
|
|
|
|
|
mint: 10,
|
|
|
|
maxt: 15,
|
|
|
|
}
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, p.Next() == false, "")
|
2017-05-01 02:03:56 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockChunkSeriesSet struct {
|
|
|
|
l []labels.Labels
|
2017-11-30 06:34:49 -08:00
|
|
|
cm [][]chunks.Meta
|
2017-05-01 02:03:56 -07:00
|
|
|
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChunkSeriesSet) Next() bool {
|
|
|
|
if len(m.l) != len(m.cm) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.i++
|
|
|
|
return m.i < len(m.l)
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (m *mockChunkSeriesSet) At() (labels.Labels, []chunks.Meta, Intervals) {
|
2017-05-22 04:12:36 -07:00
|
|
|
return m.l[m.i], m.cm[m.i], nil
|
2017-05-01 02:03:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChunkSeriesSet) Err() error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-05 07:19:28 -07:00
|
|
|
|
|
|
|
// Test the cost of merging series sets for different number of merged sets and their size.
|
|
|
|
// The subset are all equivalent so this does not capture merging of partial or non-overlapping sets well.
|
|
|
|
func BenchmarkMergedSeriesSet(b *testing.B) {
|
|
|
|
var sel func(sets []SeriesSet) SeriesSet
|
|
|
|
|
|
|
|
sel = func(sets []SeriesSet) SeriesSet {
|
|
|
|
if len(sets) == 0 {
|
2017-11-13 03:16:58 -08:00
|
|
|
return EmptySeriesSet()
|
2017-07-05 07:19:28 -07:00
|
|
|
}
|
|
|
|
if len(sets) == 1 {
|
|
|
|
return sets[0]
|
|
|
|
}
|
|
|
|
l := len(sets) / 2
|
|
|
|
return newMergedSeriesSet(sel(sets[:l]), sel(sets[l:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range []int{
|
|
|
|
100,
|
|
|
|
1000,
|
|
|
|
10000,
|
2018-09-13 09:34:26 -07:00
|
|
|
20000,
|
2017-07-05 07:19:28 -07:00
|
|
|
} {
|
|
|
|
for _, j := range []int{1, 2, 4, 8, 16, 32} {
|
|
|
|
b.Run(fmt.Sprintf("series=%d,blocks=%d", k, j), func(b *testing.B) {
|
2018-10-25 02:32:57 -07:00
|
|
|
lbls, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), k)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(b, err)
|
2017-07-05 07:19:28 -07:00
|
|
|
|
|
|
|
sort.Sort(labels.Slice(lbls))
|
|
|
|
|
|
|
|
in := make([][]Series, j)
|
|
|
|
|
|
|
|
for _, l := range lbls {
|
|
|
|
l2 := l
|
|
|
|
for j := range in {
|
|
|
|
in[j] = append(in[j], &mockSeries{labels: func() labels.Labels { return l2 }})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
var sets []SeriesSet
|
|
|
|
for _, s := range in {
|
2018-09-21 01:07:35 -07:00
|
|
|
sets = append(sets, newMockSeriesSet(s))
|
2017-07-05 07:19:28 -07:00
|
|
|
}
|
|
|
|
ms := sel(sets)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for ms.Next() {
|
|
|
|
i++
|
|
|
|
}
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(b, ms.Err())
|
|
|
|
testutil.Equals(b, len(lbls), i)
|
2017-07-05 07:19:28 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 06:34:49 -08:00
|
|
|
|
2018-10-23 14:35:52 -07:00
|
|
|
func BenchmarkPersistedQueries(b *testing.B) {
|
|
|
|
for _, nSeries := range []int{10, 100} {
|
|
|
|
for _, nSamples := range []int{1000, 10000, 100000} {
|
|
|
|
b.Run(fmt.Sprintf("series=%d,samplesPerSeries=%d", nSeries, nSamples), func(b *testing.B) {
|
|
|
|
dir, err := ioutil.TempDir("", "bench_persisted")
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
defer os.RemoveAll(dir)
|
2018-12-28 23:23:56 -08:00
|
|
|
block, err := OpenBlock(createPopulatedBlock(b, dir, nSeries, 1, int64(nSamples)), nil)
|
|
|
|
testutil.Ok(b, err)
|
2018-10-23 14:35:52 -07:00
|
|
|
defer block.Close()
|
|
|
|
|
|
|
|
q, err := NewBlockQuerier(block, block.Meta().MinTime, block.Meta().MaxTime)
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
defer q.Close()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ss, err := q.Select(labels.NewMustRegexpMatcher("__name__", ".+"))
|
|
|
|
for ss.Next() {
|
|
|
|
s := ss.At()
|
|
|
|
s.Labels()
|
|
|
|
it := s.Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
}
|
|
|
|
testutil.Ok(b, it.Err())
|
|
|
|
}
|
|
|
|
testutil.Ok(b, ss.Err())
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
type mockChunkReader map[uint64]chunkenc.Chunk
|
|
|
|
|
|
|
|
func (cr mockChunkReader) Chunk(id uint64) (chunkenc.Chunk, error) {
|
|
|
|
chk, ok := cr[id]
|
|
|
|
if ok {
|
|
|
|
return chk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Chunk with ref not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr mockChunkReader) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletedIterator(t *testing.T) {
|
2017-12-21 02:55:58 -08:00
|
|
|
chk := chunkenc.NewXORChunk()
|
2017-11-30 06:34:49 -08:00
|
|
|
app, err := chk.Appender()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
// Insert random stuff from (0, 1000).
|
|
|
|
act := make([]sample, 1000)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
act[i].t = int64(i)
|
|
|
|
act[i].v = rand.Float64()
|
|
|
|
app.Append(act[i].t, act[i].v)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
r Intervals
|
|
|
|
}{
|
|
|
|
{r: Intervals{{1, 20}}},
|
|
|
|
{r: Intervals{{1, 10}, {12, 20}, {21, 23}, {25, 30}}},
|
|
|
|
{r: Intervals{{1, 10}, {12, 20}, {20, 30}}},
|
|
|
|
{r: Intervals{{1, 10}, {12, 23}, {25, 30}}},
|
|
|
|
{r: Intervals{{1, 23}, {12, 20}, {25, 30}}},
|
|
|
|
{r: Intervals{{1, 23}, {12, 20}, {25, 3000}}},
|
|
|
|
{r: Intervals{{0, 2000}}},
|
|
|
|
{r: Intervals{{500, 2000}}},
|
|
|
|
{r: Intervals{{0, 200}}},
|
|
|
|
{r: Intervals{{1000, 20000}}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
i := int64(-1)
|
|
|
|
it := &deletedIterator{it: chk.Iterator(), intervals: c.r[:]}
|
|
|
|
ranges := c.r[:]
|
|
|
|
for it.Next() {
|
|
|
|
i++
|
|
|
|
for _, tr := range ranges {
|
|
|
|
if tr.inBounds(i) {
|
|
|
|
i = tr.Maxt + 1
|
|
|
|
ranges = ranges[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
testutil.Assert(t, i < 1000, "")
|
2017-11-30 06:34:49 -08:00
|
|
|
|
|
|
|
ts, v := it.At()
|
|
|
|
testutil.Equals(t, act[i].t, ts)
|
|
|
|
testutil.Equals(t, act[i].v, v)
|
|
|
|
}
|
|
|
|
// There has been an extra call to Next().
|
|
|
|
i++
|
|
|
|
for _, tr := range ranges {
|
|
|
|
if tr.inBounds(i) {
|
|
|
|
i = tr.Maxt + 1
|
|
|
|
ranges = ranges[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
testutil.Assert(t, i >= 1000, "")
|
2017-11-30 06:34:49 -08:00
|
|
|
testutil.Ok(t, it.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type series struct {
|
|
|
|
l labels.Labels
|
|
|
|
chunks []chunks.Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockIndex struct {
|
|
|
|
series map[uint64]series
|
|
|
|
labelIndex map[string][]string
|
|
|
|
postings map[labels.Label][]uint64
|
|
|
|
symbols map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockIndex() mockIndex {
|
|
|
|
ix := mockIndex{
|
|
|
|
series: make(map[uint64]series),
|
|
|
|
labelIndex: make(map[string][]string),
|
|
|
|
postings: make(map[labels.Label][]uint64),
|
|
|
|
symbols: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
return ix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) Symbols() (map[string]struct{}, error) {
|
|
|
|
return m.symbols, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) AddSeries(ref uint64, l labels.Labels, chunks ...chunks.Meta) error {
|
|
|
|
if _, ok := m.series[ref]; ok {
|
|
|
|
return errors.Errorf("series with reference %d already added", ref)
|
|
|
|
}
|
|
|
|
for _, lbl := range l {
|
|
|
|
m.symbols[lbl.Name] = struct{}{}
|
|
|
|
m.symbols[lbl.Value] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
s := series{l: l}
|
|
|
|
// Actual chunk data is not stored in the index.
|
|
|
|
for _, c := range chunks {
|
|
|
|
c.Chunk = nil
|
|
|
|
s.chunks = append(s.chunks, c)
|
|
|
|
}
|
|
|
|
m.series[ref] = s
|
|
|
|
|
|
|
|
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 index.Postings) error {
|
|
|
|
l := labels.Label{Name: name, Value: value}
|
|
|
|
if _, ok := m.postings[l]; ok {
|
|
|
|
return errors.Errorf("postings for %s already added", l)
|
|
|
|
}
|
|
|
|
ep, err := index.ExpandPostings(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.postings[l] = ep
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) LabelValues(names ...string) (index.StringTuples, error) {
|
|
|
|
// TODO support composite indexes
|
|
|
|
if len(names) != 1 {
|
|
|
|
return nil, errors.New("composite indexes not supported yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
return index.NewStringTuples(m.labelIndex[names[0]], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) Postings(name, value string) (index.Postings, error) {
|
|
|
|
l := labels.Label{Name: name, Value: value}
|
|
|
|
return index.NewListPostings(m.postings[l]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) SortedPostings(p index.Postings) index.Postings {
|
|
|
|
ep, err := index.ExpandPostings(p)
|
|
|
|
if err != nil {
|
|
|
|
return index.ErrPostings(errors.Wrap(err, "expand postings"))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(ep, func(i, j int) bool {
|
|
|
|
return labels.Compare(m.series[ep[i]].l, m.series[ep[j]].l) < 0
|
|
|
|
})
|
|
|
|
return index.NewListPostings(ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta) error {
|
|
|
|
s, ok := m.series[ref]
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
*lset = append((*lset)[:0], s.l...)
|
|
|
|
*chks = append((*chks)[:0], s.chunks...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-11-07 07:52:41 -08:00
|
|
|
|
|
|
|
func (m mockIndex) LabelNames() ([]string, error) {
|
|
|
|
labelNames := make([]string, 0, len(m.labelIndex))
|
|
|
|
for name := range m.labelIndex {
|
|
|
|
labelNames = append(labelNames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(labelNames)
|
|
|
|
return labelNames, nil
|
|
|
|
}
|