/
block.go
213 lines (172 loc) · 5.34 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package querier
import (
"math"
"sort"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/cortexproject/cortex/pkg/querier/series"
)
func convertMatchersToLabelMatcher(matchers []*labels.Matcher) []storepb.LabelMatcher {
var converted []storepb.LabelMatcher
for _, m := range matchers {
var t storepb.LabelMatcher_Type
switch m.Type {
case labels.MatchEqual:
t = storepb.LabelMatcher_EQ
case labels.MatchNotEqual:
t = storepb.LabelMatcher_NEQ
case labels.MatchRegexp:
t = storepb.LabelMatcher_RE
case labels.MatchNotRegexp:
t = storepb.LabelMatcher_NRE
}
converted = append(converted, storepb.LabelMatcher{
Type: t,
Name: m.Name,
Value: m.Value,
})
}
return converted
}
// Implementation of storage.SeriesSet, based on individual responses from store client.
type blockQuerierSeriesSet struct {
series []*storepb.Series
warnings storage.Warnings
// next response to process
next int
currSeries storage.Series
}
func (bqss *blockQuerierSeriesSet) Next() bool {
bqss.currSeries = nil
if bqss.next >= len(bqss.series) {
return false
}
currLabels := labelpb.ZLabelsToPromLabels(bqss.series[bqss.next].Labels)
currChunks := bqss.series[bqss.next].Chunks
bqss.next++
// Merge chunks for current series. Chunks may come in multiple responses, but as soon
// as the response has chunks for a new series, we can stop searching. Series are sorted.
// See documentation for StoreClient.Series call for details.
for bqss.next < len(bqss.series) && labels.Compare(currLabels, labelpb.ZLabelsToPromLabels(bqss.series[bqss.next].Labels)) == 0 {
currChunks = append(currChunks, bqss.series[bqss.next].Chunks...)
bqss.next++
}
bqss.currSeries = newBlockQuerierSeries(currLabels, currChunks)
return true
}
func (bqss *blockQuerierSeriesSet) At() storage.Series {
return bqss.currSeries
}
func (bqss *blockQuerierSeriesSet) Err() error {
return nil
}
func (bqss *blockQuerierSeriesSet) Warnings() storage.Warnings {
return bqss.warnings
}
// newBlockQuerierSeries makes a new blockQuerierSeries. Input labels must be already sorted by name.
func newBlockQuerierSeries(lbls []labels.Label, chunks []storepb.AggrChunk) *blockQuerierSeries {
sort.Slice(chunks, func(i, j int) bool {
return chunks[i].MinTime < chunks[j].MinTime
})
return &blockQuerierSeries{labels: lbls, chunks: chunks}
}
type blockQuerierSeries struct {
labels labels.Labels
chunks []storepb.AggrChunk
}
func (bqs *blockQuerierSeries) Labels() labels.Labels {
return bqs.labels
}
func (bqs *blockQuerierSeries) Iterator() chunkenc.Iterator {
if len(bqs.chunks) == 0 {
// should not happen in practice, but we have a unit test for it
return series.NewErrIterator(errors.New("no chunks"))
}
its := make([]chunkenc.Iterator, 0, len(bqs.chunks))
for _, c := range bqs.chunks {
ch, err := chunkenc.FromData(chunkenc.EncXOR, c.Raw.Data)
if err != nil {
return series.NewErrIterator(errors.Wrapf(err, "failed to initialize chunk from XOR encoded raw data (series: %v min time: %d max time: %d)", bqs.Labels(), c.MinTime, c.MaxTime))
}
it := ch.Iterator(nil)
its = append(its, it)
}
return newBlockQuerierSeriesIterator(bqs.Labels(), its)
}
func newBlockQuerierSeriesIterator(labels labels.Labels, its []chunkenc.Iterator) *blockQuerierSeriesIterator {
return &blockQuerierSeriesIterator{labels: labels, iterators: its, lastT: math.MinInt64}
}
// blockQuerierSeriesIterator implements a series iterator on top
// of a list of time-sorted, non-overlapping chunks.
type blockQuerierSeriesIterator struct {
// only used for error reporting
labels labels.Labels
iterators []chunkenc.Iterator
i int
lastT int64
}
func (it *blockQuerierSeriesIterator) Seek(t int64) bool {
// We generally expect the chunks already to be cut down
// to the range we are interested in. There's not much to be gained from
// hopping across chunks so we just call next until we reach t.
for {
ct, _ := it.At()
if ct >= t {
return true
}
if !it.Next() {
return false
}
}
}
func (it *blockQuerierSeriesIterator) At() (int64, float64) {
if it.i >= len(it.iterators) {
return 0, 0
}
t, v := it.iterators[it.i].At()
it.lastT = t
return t, v
}
func (it *blockQuerierSeriesIterator) Next() bool {
if it.i >= len(it.iterators) {
return false
}
if it.iterators[it.i].Next() {
return true
}
if it.iterators[it.i].Err() != nil {
return false
}
for {
it.i++
if it.i >= len(it.iterators) {
return false
}
// we must advance iterator first, to see if it has any samples.
// Seek will call At() as its first operation.
if !it.iterators[it.i].Next() {
if it.iterators[it.i].Err() != nil {
return false
}
// Found empty iterator without error, skip it.
continue
}
// Chunks are guaranteed to be ordered but not generally guaranteed to not overlap.
// We must ensure to skip any overlapping range between adjacent chunks.
return it.Seek(it.lastT + 1)
}
}
func (it *blockQuerierSeriesIterator) Err() error {
if it.i >= len(it.iterators) {
return nil
}
err := it.iterators[it.i].Err()
if err != nil {
return errors.Wrapf(err, "cannot iterate chunk for series: %v", it.labels)
}
return nil
}