2016-12-10 09:08:50 -08:00
|
|
|
package tsdb
|
|
|
|
|
2016-12-13 06:26:58 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-15 06:23:15 -08:00
|
|
|
"sort"
|
2016-12-14 06:39:23 -08:00
|
|
|
"strings"
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2017-04-04 02:27:26 -07:00
|
|
|
"github.com/prometheus/tsdb/chunks"
|
|
|
|
"github.com/prometheus/tsdb/labels"
|
2016-12-13 06:26:58 -08:00
|
|
|
)
|
2016-12-12 10:12:55 -08:00
|
|
|
|
2016-12-10 09:08:50 -08:00
|
|
|
// Querier provides querying access over time series data of a fixed
|
|
|
|
// time range.
|
|
|
|
type Querier interface {
|
2016-12-14 06:39:23 -08:00
|
|
|
// Select returns a set of series that matches the given label matchers.
|
2016-12-21 00:39:01 -08:00
|
|
|
Select(...labels.Matcher) SeriesSet
|
2016-12-10 09:08:50 -08:00
|
|
|
|
|
|
|
// LabelValues returns all potential values for a label name.
|
2016-12-13 06:26:58 -08:00
|
|
|
LabelValues(string) ([]string, error)
|
2016-12-10 09:08:50 -08:00
|
|
|
// LabelValuesFor returns all potential values for a label name.
|
|
|
|
// under the constraint of another label.
|
2016-12-21 00:39:01 -08:00
|
|
|
LabelValuesFor(string, labels.Label) ([]string, error)
|
2016-12-10 09:08:50 -08:00
|
|
|
|
|
|
|
// Close releases the resources of the Querier.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Series represents a single time series.
|
|
|
|
type Series interface {
|
2016-12-13 06:26:58 -08:00
|
|
|
// Labels returns the complete set of labels identifying the series.
|
2016-12-21 00:39:01 -08:00
|
|
|
Labels() labels.Labels
|
2016-12-16 03:13:17 -08:00
|
|
|
|
2016-12-10 09:08:50 -08:00
|
|
|
// Iterator returns a new iterator of the data of the series.
|
2016-12-13 06:26:58 -08:00
|
|
|
Iterator() SeriesIterator
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
// querier aggregates querying results from time blocks within
|
2017-01-05 23:08:02 -08:00
|
|
|
// a single partition.
|
2017-01-06 03:37:28 -08:00
|
|
|
type querier struct {
|
|
|
|
db *DB
|
|
|
|
blocks []Querier
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
|
2017-01-05 23:08:02 -08:00
|
|
|
// Querier returns a new querier over the data partition for the given
|
2016-12-13 06:26:58 -08:00
|
|
|
// time range.
|
2017-01-06 02:40:09 -08:00
|
|
|
func (s *DB) Querier(mint, maxt int64) Querier {
|
2016-12-28 02:41:44 -08:00
|
|
|
s.mtx.RLock()
|
|
|
|
|
2017-03-04 07:50:48 -08:00
|
|
|
s.headmtx.RLock()
|
2016-12-15 07:14:33 -08:00
|
|
|
blocks := s.blocksForInterval(mint, maxt)
|
2017-03-04 07:50:48 -08:00
|
|
|
s.headmtx.RUnlock()
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
sq := &querier{
|
|
|
|
blocks: make([]Querier, 0, len(blocks)),
|
|
|
|
db: s,
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
for _, b := range blocks {
|
2017-03-20 02:21:21 -07:00
|
|
|
sq.blocks = append(sq.blocks, b.Querier(mint, maxt))
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sq
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
func (q *querier) LabelValues(n string) ([]string, error) {
|
2017-03-07 02:29:20 -08:00
|
|
|
if len(q.blocks) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-12-19 03:26:25 -08:00
|
|
|
res, err := q.blocks[0].LabelValues(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, bq := range q.blocks[1:] {
|
|
|
|
pr, err := bq.LabelValues(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Merge new values into deduplicated result.
|
|
|
|
res = mergeStrings(res, pr)
|
|
|
|
}
|
|
|
|
return res, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
func (q *querier) LabelValuesFor(string, labels.Label) ([]string, error) {
|
2016-12-14 09:38:46 -08:00
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
func (q *querier) Select(ms ...labels.Matcher) SeriesSet {
|
2016-12-20 04:10:37 -08:00
|
|
|
// Sets from different blocks have no time overlap. The reference numbers
|
|
|
|
// they emit point to series sorted in lexicographic order.
|
|
|
|
// We can fully connect partial series by simply comparing with the previous
|
|
|
|
// label set.
|
|
|
|
if len(q.blocks) == 0 {
|
|
|
|
return nopSeriesSet{}
|
|
|
|
}
|
|
|
|
r := q.blocks[0].Select(ms...)
|
|
|
|
|
|
|
|
for _, s := range q.blocks[1:] {
|
2017-03-14 07:24:08 -07:00
|
|
|
r = newMergedSeriesSet(r, s.Select(ms...))
|
2016-12-20 04:10:37 -08:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
func (q *querier) Close() error {
|
2016-12-28 02:41:44 -08:00
|
|
|
var merr MultiError
|
|
|
|
|
|
|
|
for _, bq := range q.blocks {
|
|
|
|
merr.Add(bq.Close())
|
|
|
|
}
|
2017-01-06 03:37:28 -08:00
|
|
|
q.db.mtx.RUnlock()
|
2016-12-28 02:41:44 -08:00
|
|
|
|
|
|
|
return merr.Err()
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// blockQuerier provides querying access to a single block database.
|
|
|
|
type blockQuerier struct {
|
|
|
|
index IndexReader
|
2017-02-23 01:50:22 -08:00
|
|
|
chunks ChunkReader
|
2016-12-14 09:38:46 -08:00
|
|
|
|
2017-01-05 06:13:01 -08:00
|
|
|
postingsMapper func(Postings) Postings
|
|
|
|
|
2016-12-14 09:38:46 -08:00
|
|
|
mint, maxt int64
|
|
|
|
}
|
|
|
|
|
2016-12-21 00:39:01 -08:00
|
|
|
func (q *blockQuerier) Select(ms ...labels.Matcher) SeriesSet {
|
2016-12-30 10:36:28 -08:00
|
|
|
var (
|
|
|
|
its []Postings
|
|
|
|
absent []string
|
|
|
|
)
|
2016-12-14 09:38:46 -08:00
|
|
|
for _, m := range ms {
|
2016-12-30 10:36:28 -08:00
|
|
|
// If the matcher checks absence of a label, don't select them
|
|
|
|
// but propagate the check into the series set.
|
|
|
|
if _, ok := m.(*labels.EqualMatcher); ok && m.Matches("") {
|
|
|
|
absent = append(absent, m.Name())
|
|
|
|
continue
|
|
|
|
}
|
2016-12-14 09:38:46 -08:00
|
|
|
its = append(its, q.selectSingle(m))
|
|
|
|
}
|
|
|
|
|
2017-01-05 06:13:01 -08:00
|
|
|
p := Intersect(its...)
|
|
|
|
|
|
|
|
if q.postingsMapper != nil {
|
|
|
|
p = q.postingsMapper(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blockSeriesSet{
|
2017-03-07 02:29:20 -08:00
|
|
|
set: &populatedChunkSeries{
|
|
|
|
set: &baseChunkSeries{
|
|
|
|
p: p,
|
|
|
|
index: q.index,
|
|
|
|
absent: absent,
|
|
|
|
},
|
|
|
|
chunks: q.chunks,
|
|
|
|
mint: q.mint,
|
|
|
|
maxt: q.maxt,
|
|
|
|
},
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 00:39:01 -08:00
|
|
|
func (q *blockQuerier) selectSingle(m labels.Matcher) Postings {
|
2017-04-05 05:14:30 -07:00
|
|
|
// Fast-path for equal matching.
|
|
|
|
if em, ok := m.(*labels.EqualMatcher); ok {
|
|
|
|
it, err := q.index.Postings(em.Name(), em.Value())
|
|
|
|
if err != nil {
|
|
|
|
return errPostings{err: err}
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2016-12-14 09:38:46 -08:00
|
|
|
tpls, err := q.index.LabelValues(m.Name())
|
|
|
|
if err != nil {
|
2016-12-14 12:58:29 -08:00
|
|
|
return errPostings{err: err}
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
// TODO(fabxc): use interface upgrading to provide fast solution
|
|
|
|
// for equality and prefix matches. Tuples are lexicographically sorted.
|
|
|
|
var res []string
|
|
|
|
|
|
|
|
for i := 0; i < tpls.Len(); i++ {
|
|
|
|
vals, err := tpls.At(i)
|
|
|
|
if err != nil {
|
2016-12-14 12:58:29 -08:00
|
|
|
return errPostings{err: err}
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
2016-12-21 00:39:01 -08:00
|
|
|
if m.Matches(vals[0]) {
|
2016-12-14 09:38:46 -08:00
|
|
|
res = append(res, vals[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
2016-12-28 02:02:19 -08:00
|
|
|
return emptyPostings
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
var rit []Postings
|
2016-12-14 09:38:46 -08:00
|
|
|
|
|
|
|
for _, v := range res {
|
|
|
|
it, err := q.index.Postings(m.Name(), v)
|
|
|
|
if err != nil {
|
2016-12-14 12:58:29 -08:00
|
|
|
return errPostings{err: err}
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
rit = append(rit, it)
|
|
|
|
}
|
|
|
|
|
2016-12-28 02:02:19 -08:00
|
|
|
return Merge(rit...)
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *blockQuerier) LabelValues(name string) ([]string, error) {
|
|
|
|
tpls, err := q.index.LabelValues(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := make([]string, 0, tpls.Len())
|
|
|
|
|
|
|
|
for i := 0; i < tpls.Len(); i++ {
|
|
|
|
vals, err := tpls.At(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, vals[0])
|
|
|
|
}
|
2016-12-19 03:26:25 -08:00
|
|
|
return res, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 00:39:01 -08:00
|
|
|
func (q *blockQuerier) LabelValuesFor(string, labels.Label) ([]string, error) {
|
2016-12-14 09:38:46 -08:00
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *blockQuerier) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:37:28 -08:00
|
|
|
func mergeStrings(a, b []string) []string {
|
|
|
|
maxl := len(a)
|
|
|
|
if len(b) > len(a) {
|
|
|
|
maxl = len(b)
|
|
|
|
}
|
|
|
|
res := make([]string, 0, maxl*10/9)
|
|
|
|
|
|
|
|
for len(a) > 0 && len(b) > 0 {
|
|
|
|
d := strings.Compare(a[0], b[0])
|
|
|
|
|
|
|
|
if d == 0 {
|
|
|
|
res = append(res, a[0])
|
|
|
|
a, b = a[1:], b[1:]
|
|
|
|
} else if d < 0 {
|
|
|
|
res = append(res, a[0])
|
|
|
|
a = a[1:]
|
|
|
|
} else if d > 0 {
|
|
|
|
res = append(res, b[0])
|
|
|
|
b = b[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append all remaining elements.
|
|
|
|
res = append(res, a...)
|
|
|
|
res = append(res, b...)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-12-14 09:38:46 -08:00
|
|
|
// SeriesSet contains a set of series.
|
|
|
|
type SeriesSet interface {
|
|
|
|
Next() bool
|
2017-01-02 04:27:52 -08:00
|
|
|
At() Series
|
2016-12-14 09:38:46 -08:00
|
|
|
Err() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopSeriesSet struct{}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (nopSeriesSet) Next() bool { return false }
|
|
|
|
func (nopSeriesSet) At() Series { return nil }
|
|
|
|
func (nopSeriesSet) Err() error { return nil }
|
2016-12-14 09:38:46 -08:00
|
|
|
|
2017-04-04 02:21:19 -07:00
|
|
|
// mergedSeriesSet takes two series sets as a single series set. The input series sets
|
|
|
|
// must be sorted and sequential in time, i.e. if they have the same label set,
|
|
|
|
// the datapoints of a must be before the datapoints of b.
|
2016-12-14 06:39:23 -08:00
|
|
|
type mergedSeriesSet struct {
|
|
|
|
a, b SeriesSet
|
|
|
|
|
2017-01-02 03:05:52 -08:00
|
|
|
cur Series
|
|
|
|
adone, bdone bool
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func newMergedSeriesSet(a, b SeriesSet) *mergedSeriesSet {
|
|
|
|
s := &mergedSeriesSet{a: a, b: b}
|
2016-12-14 06:39:23 -08:00
|
|
|
// Initialize first elements of both sets as Next() needs
|
|
|
|
// one element look-ahead.
|
2017-01-02 03:05:52 -08:00
|
|
|
s.adone = !s.a.Next()
|
|
|
|
s.bdone = !s.b.Next()
|
2016-12-14 06:39:23 -08:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func (s *mergedSeriesSet) At() Series {
|
2016-12-14 06:39:23 -08:00
|
|
|
return s.cur
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func (s *mergedSeriesSet) Err() error {
|
2016-12-14 06:39:23 -08:00
|
|
|
if s.a.Err() != nil {
|
|
|
|
return s.a.Err()
|
|
|
|
}
|
|
|
|
return s.b.Err()
|
|
|
|
}
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func (s *mergedSeriesSet) compare() int {
|
2017-01-02 03:05:52 -08:00
|
|
|
if s.adone {
|
2016-12-14 06:39:23 -08:00
|
|
|
return 1
|
|
|
|
}
|
2017-01-02 03:05:52 -08:00
|
|
|
if s.bdone {
|
2016-12-14 06:39:23 -08:00
|
|
|
return -1
|
|
|
|
}
|
2017-01-03 10:02:42 -08:00
|
|
|
return labels.Compare(s.a.At().Labels(), s.b.At().Labels())
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:24:08 -07:00
|
|
|
func (s *mergedSeriesSet) Next() bool {
|
2017-01-02 03:05:52 -08:00
|
|
|
if s.adone && s.bdone || s.Err() != nil {
|
2016-12-14 06:39:23 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
d := s.compare()
|
2017-01-04 00:47:20 -08:00
|
|
|
|
2016-12-14 06:39:23 -08:00
|
|
|
// Both sets contain the current series. Chain them into a single one.
|
|
|
|
if d > 0 {
|
2017-01-02 04:27:52 -08:00
|
|
|
s.cur = s.b.At()
|
2017-01-02 03:05:52 -08:00
|
|
|
s.bdone = !s.b.Next()
|
2016-12-14 06:39:23 -08:00
|
|
|
} else if d < 0 {
|
2017-01-02 04:27:52 -08:00
|
|
|
s.cur = s.a.At()
|
2017-01-02 03:05:52 -08:00
|
|
|
s.adone = !s.a.Next()
|
2016-12-14 06:39:23 -08:00
|
|
|
} else {
|
2017-01-02 04:27:52 -08:00
|
|
|
s.cur = &chainedSeries{series: []Series{s.a.At(), s.b.At()}}
|
2017-01-02 03:05:52 -08:00
|
|
|
s.adone = !s.a.Next()
|
|
|
|
s.bdone = !s.b.Next()
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:29:20 -08:00
|
|
|
type chunkSeriesSet interface {
|
|
|
|
Next() bool
|
2017-03-14 07:40:16 -07:00
|
|
|
At() (labels.Labels, []*ChunkMeta)
|
2017-03-07 02:29:20 -08:00
|
|
|
Err() error
|
|
|
|
}
|
2016-12-14 06:39:23 -08:00
|
|
|
|
2017-03-07 02:29:20 -08:00
|
|
|
// baseChunkSeries loads the label set and chunk references for a postings
|
|
|
|
// list from an index. It filters out series that have labels set that should be unset.
|
|
|
|
type baseChunkSeries struct {
|
|
|
|
p Postings
|
|
|
|
index IndexReader
|
|
|
|
absent []string // labels that must be unset in results.
|
|
|
|
|
|
|
|
lset labels.Labels
|
2017-03-14 07:40:16 -07:00
|
|
|
chks []*ChunkMeta
|
2017-03-07 02:29:20 -08:00
|
|
|
err error
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:40:16 -07:00
|
|
|
func (s *baseChunkSeries) At() (labels.Labels, []*ChunkMeta) { return s.lset, s.chks }
|
|
|
|
func (s *baseChunkSeries) Err() error { return s.err }
|
2017-03-07 02:29:20 -08:00
|
|
|
|
|
|
|
func (s *baseChunkSeries) Next() bool {
|
|
|
|
Outer:
|
|
|
|
for s.p.Next() {
|
|
|
|
lset, chunks, err := s.index.Series(s.p.At())
|
2016-12-16 03:13:17 -08:00
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return false
|
|
|
|
}
|
2016-12-31 06:35:08 -08:00
|
|
|
|
2016-12-30 10:36:28 -08:00
|
|
|
// If a series contains a label that must be absent, it is skipped as well.
|
|
|
|
for _, abs := range s.absent {
|
2016-12-31 06:35:08 -08:00
|
|
|
if lset.Get(abs) != "" {
|
2017-03-07 02:29:20 -08:00
|
|
|
continue Outer
|
2016-12-30 10:36:28 -08:00
|
|
|
}
|
2016-12-16 03:13:17 -08:00
|
|
|
}
|
2016-12-30 10:36:28 -08:00
|
|
|
|
2017-03-07 02:29:20 -08:00
|
|
|
s.lset = lset
|
|
|
|
s.chks = chunks
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := s.p.Err(); err != nil {
|
|
|
|
s.err = err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// populatedChunkSeries loads chunk data from a store for a set of series
|
|
|
|
// with known chunk references. It filters out chunks that do not fit the
|
|
|
|
// given time range.
|
|
|
|
type populatedChunkSeries struct {
|
|
|
|
set chunkSeriesSet
|
|
|
|
chunks ChunkReader
|
|
|
|
mint, maxt int64
|
|
|
|
|
|
|
|
err error
|
2017-03-14 07:40:16 -07:00
|
|
|
chks []*ChunkMeta
|
2017-03-07 02:29:20 -08:00
|
|
|
lset labels.Labels
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:40:16 -07:00
|
|
|
func (s *populatedChunkSeries) At() (labels.Labels, []*ChunkMeta) { return s.lset, s.chks }
|
|
|
|
func (s *populatedChunkSeries) Err() error { return s.err }
|
2017-03-07 02:29:20 -08:00
|
|
|
|
|
|
|
func (s *populatedChunkSeries) Next() bool {
|
|
|
|
for s.set.Next() {
|
|
|
|
lset, chks := s.set.At()
|
|
|
|
|
2017-03-14 07:40:16 -07:00
|
|
|
for i, c := range chks {
|
2016-12-31 06:35:08 -08:00
|
|
|
if c.MaxTime < s.mint {
|
2017-03-07 02:29:20 -08:00
|
|
|
chks = chks[1:]
|
2016-12-31 06:35:08 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c.MinTime > s.maxt {
|
2017-03-07 02:29:20 -08:00
|
|
|
chks = chks[:i]
|
2016-12-31 06:35:08 -08:00
|
|
|
break
|
|
|
|
}
|
2017-03-07 02:29:20 -08:00
|
|
|
c.Chunk, s.err = s.chunks.Chunk(c.Ref)
|
|
|
|
if s.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-31 06:35:08 -08:00
|
|
|
}
|
2017-03-07 02:29:20 -08:00
|
|
|
if len(chks) == 0 {
|
2016-12-31 06:35:08 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:29:20 -08:00
|
|
|
s.lset = lset
|
|
|
|
s.chks = chks
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := s.set.Err(); err != nil {
|
|
|
|
s.err = err
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockSeriesSet is a set of series from an inverted index query.
|
|
|
|
type blockSeriesSet struct {
|
|
|
|
set chunkSeriesSet
|
|
|
|
err error
|
|
|
|
cur Series
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *blockSeriesSet) Next() bool {
|
|
|
|
for s.set.Next() {
|
|
|
|
lset, chunks := s.set.At()
|
|
|
|
s.cur = &chunkSeries{labels: lset, chunks: chunks}
|
2016-12-30 10:36:28 -08:00
|
|
|
return true
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
2017-03-07 02:29:20 -08:00
|
|
|
if s.set.Err() != nil {
|
|
|
|
s.err = s.set.Err()
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
2016-12-16 03:13:17 -08:00
|
|
|
return false
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (s *blockSeriesSet) At() Series { return s.cur }
|
|
|
|
func (s *blockSeriesSet) Err() error { return s.err }
|
2016-12-14 06:39:23 -08:00
|
|
|
|
2016-12-19 03:26:25 -08:00
|
|
|
// chunkSeries is a series that is backed by a sequence of chunks holding
|
|
|
|
// time series data.
|
|
|
|
type chunkSeries struct {
|
2016-12-21 00:39:01 -08:00
|
|
|
labels labels.Labels
|
2017-03-14 07:40:16 -07:00
|
|
|
chunks []*ChunkMeta // in-order chunk refs
|
2016-12-16 03:13:17 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 00:39:01 -08:00
|
|
|
func (s *chunkSeries) Labels() labels.Labels {
|
2016-12-16 03:13:17 -08:00
|
|
|
return s.labels
|
|
|
|
}
|
|
|
|
|
2016-12-19 03:26:25 -08:00
|
|
|
func (s *chunkSeries) Iterator() SeriesIterator {
|
2017-03-07 02:29:20 -08:00
|
|
|
return newChunkSeriesIterator(s.chunks)
|
2016-12-16 03:13:17 -08:00
|
|
|
}
|
|
|
|
|
2016-12-10 09:08:50 -08:00
|
|
|
// SeriesIterator iterates over the data of a time series.
|
|
|
|
type SeriesIterator interface {
|
|
|
|
// Seek advances the iterator forward to the given timestamp.
|
|
|
|
// If there's no value exactly at ts, it advances to the last value
|
|
|
|
// before tt.
|
|
|
|
Seek(t int64) bool
|
2017-03-19 09:05:01 -07:00
|
|
|
// At returns the current timestamp/value pair.
|
2017-01-02 04:27:52 -08:00
|
|
|
At() (t int64, v float64)
|
2016-12-10 09:08:50 -08:00
|
|
|
// Next advances the iterator by one.
|
|
|
|
Next() bool
|
|
|
|
// Err returns the current error.
|
|
|
|
Err() error
|
|
|
|
}
|
2016-12-12 10:12:55 -08:00
|
|
|
|
2016-12-16 03:13:17 -08:00
|
|
|
// chainedSeries implements a series for a list of time-sorted series.
|
2016-12-19 03:26:25 -08:00
|
|
|
// They all must have the same labels.
|
2016-12-14 06:39:23 -08:00
|
|
|
type chainedSeries struct {
|
|
|
|
series []Series
|
|
|
|
}
|
|
|
|
|
2016-12-21 00:39:01 -08:00
|
|
|
func (s *chainedSeries) Labels() labels.Labels {
|
2016-12-14 06:39:23 -08:00
|
|
|
return s.series[0].Labels()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *chainedSeries) Iterator() SeriesIterator {
|
2016-12-16 03:13:17 -08:00
|
|
|
return &chainedSeriesIterator{series: s.series}
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// chainedSeriesIterator implements a series iterater over a list
|
2016-12-15 06:23:15 -08:00
|
|
|
// of time-sorted, non-overlapping iterators.
|
2016-12-14 06:39:23 -08:00
|
|
|
type chainedSeriesIterator struct {
|
2016-12-16 03:13:17 -08:00
|
|
|
series []Series // series in time order
|
2016-12-15 06:23:15 -08:00
|
|
|
|
|
|
|
i int
|
|
|
|
cur SeriesIterator
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *chainedSeriesIterator) Seek(t int64) bool {
|
2016-12-16 03:13:17 -08:00
|
|
|
// We just scan the chained series sequentially as they are already
|
|
|
|
// pre-selected by relevant time and should be accessed sequentially anyway.
|
|
|
|
for i, s := range it.series[it.i:] {
|
|
|
|
cur := s.Iterator()
|
|
|
|
if !cur.Seek(t) {
|
|
|
|
continue
|
2016-12-15 06:23:15 -08:00
|
|
|
}
|
2016-12-16 03:13:17 -08:00
|
|
|
it.cur = cur
|
|
|
|
it.i += i
|
|
|
|
return true
|
2016-12-15 06:23:15 -08:00
|
|
|
}
|
2016-12-14 06:39:23 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-15 06:23:15 -08:00
|
|
|
func (it *chainedSeriesIterator) Next() bool {
|
2016-12-19 02:44:11 -08:00
|
|
|
if it.cur == nil {
|
|
|
|
it.cur = it.series[it.i].Iterator()
|
|
|
|
}
|
2016-12-15 06:23:15 -08:00
|
|
|
if it.cur.Next() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := it.cur.Err(); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if it.i == len(it.series)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
it.i++
|
2016-12-16 03:13:17 -08:00
|
|
|
it.cur = it.series[it.i].Iterator()
|
2016-12-15 06:23:15 -08:00
|
|
|
|
|
|
|
return it.Next()
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (it *chainedSeriesIterator) At() (t int64, v float64) {
|
|
|
|
return it.cur.At()
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *chainedSeriesIterator) Err() error {
|
2016-12-15 06:23:15 -08:00
|
|
|
return it.cur.Err()
|
2016-12-14 06:39:23 -08:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:12:55 -08:00
|
|
|
// chunkSeriesIterator implements a series iterator on top
|
|
|
|
// of a list of time-sorted, non-overlapping chunks.
|
|
|
|
type chunkSeriesIterator struct {
|
2017-03-14 07:40:16 -07:00
|
|
|
chunks []*ChunkMeta
|
2016-12-12 10:12:55 -08:00
|
|
|
|
|
|
|
i int
|
|
|
|
cur chunks.Iterator
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:40:16 -07:00
|
|
|
func newChunkSeriesIterator(cs []*ChunkMeta) *chunkSeriesIterator {
|
2016-12-12 10:12:55 -08:00
|
|
|
return &chunkSeriesIterator{
|
|
|
|
chunks: cs,
|
|
|
|
i: 0,
|
2017-03-07 02:29:20 -08:00
|
|
|
cur: cs[0].Chunk.Iterator(),
|
2016-12-12 10:12:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *chunkSeriesIterator) Seek(t int64) (ok bool) {
|
2016-12-26 07:55:44 -08:00
|
|
|
// Only do binary search forward to stay in line with other iterators
|
|
|
|
// that can only move forward.
|
2017-03-07 02:29:20 -08:00
|
|
|
x := sort.Search(len(it.chunks[it.i:]), func(i int) bool { return it.chunks[i].MinTime >= t })
|
2016-12-26 07:55:44 -08:00
|
|
|
x += it.i
|
2016-12-15 06:23:15 -08:00
|
|
|
|
2016-12-26 07:55:44 -08:00
|
|
|
// If the timestamp was not found, it might be in the last chunk.
|
2017-03-07 02:29:20 -08:00
|
|
|
if x == len(it.chunks) {
|
2016-12-26 07:55:44 -08:00
|
|
|
x--
|
2016-12-15 06:23:15 -08:00
|
|
|
}
|
2016-12-26 07:55:44 -08:00
|
|
|
// Go to previous chunk if the chunk doesn't exactly start with t.
|
|
|
|
// If we are already at the first chunk, we use it as it's the best we have.
|
2017-03-07 02:29:20 -08:00
|
|
|
if x > 0 && it.chunks[x].MinTime > t {
|
2016-12-15 06:23:15 -08:00
|
|
|
x--
|
|
|
|
}
|
|
|
|
|
|
|
|
it.i = x
|
2017-03-07 02:29:20 -08:00
|
|
|
it.cur = it.chunks[x].Chunk.Iterator()
|
2016-12-15 06:23:15 -08:00
|
|
|
|
|
|
|
for it.cur.Next() {
|
2017-01-02 04:27:52 -08:00
|
|
|
t0, _ := it.cur.At()
|
2016-12-15 06:23:15 -08:00
|
|
|
if t0 >= t {
|
2016-12-16 03:13:17 -08:00
|
|
|
return true
|
2016-12-12 10:12:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (it *chunkSeriesIterator) At() (t int64, v float64) {
|
|
|
|
return it.cur.At()
|
2016-12-12 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *chunkSeriesIterator) Next() bool {
|
|
|
|
if it.cur.Next() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := it.cur.Err(); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if it.i == len(it.chunks)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
it.i++
|
2017-03-07 02:29:20 -08:00
|
|
|
it.cur = it.chunks[it.i].Chunk.Iterator()
|
2016-12-12 10:12:55 -08:00
|
|
|
|
|
|
|
return it.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *chunkSeriesIterator) Err() error {
|
|
|
|
return it.cur.Err()
|
|
|
|
}
|
|
|
|
|
2017-01-04 00:47:20 -08: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 newListSeriesSet(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 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type errSeriesSet struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s errSeriesSet) Next() bool { return false }
|
|
|
|
func (s errSeriesSet) At() Series { return nil }
|
|
|
|
func (s errSeriesSet) Err() error { return s.err }
|