-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathiter.go
321 lines (275 loc) · 8.44 KB
/
iter.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package query
import (
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/util/annotations"
"github.com/thanos-io/thanos/pkg/compact/downsample"
"github.com/thanos-io/thanos/pkg/dedup"
"github.com/thanos-io/thanos/pkg/store/storepb"
)
// promSeriesSet implements the SeriesSet interface of the Prometheus storage
// package on top of our storepb SeriesSet. Overlapping chunks will be naively deduplicated (random selection).
type promSeriesSet struct {
set storepb.SeriesSet
mint, maxt int64
aggrs []storepb.Aggr
warns annotations.Annotations
}
// NewPromSeriesSet constructs a promSeriesSet.
func NewPromSeriesSet(seriesSet storepb.SeriesSet, mint, maxt int64, aggrs []storepb.Aggr, warns annotations.Annotations) storage.SeriesSet {
return &promSeriesSet{
set: seriesSet,
mint: mint,
maxt: maxt,
aggrs: aggrs,
warns: warns,
}
}
func (s *promSeriesSet) Next() bool {
return s.set.Next()
}
func (s *promSeriesSet) At() storage.Series {
if s.set.Err() != nil {
return nil
}
currLset, currChunks := s.set.At()
return newChunkSeries(currLset, currChunks, s.mint, s.maxt, s.aggrs)
}
func (s *promSeriesSet) Err() error {
return s.set.Err()
}
func (s *promSeriesSet) Warnings() annotations.Annotations {
return s.warns
}
// storeSeriesSet implements a storepb SeriesSet against a list of storepb.Series.
type storeSeriesSet struct {
// TODO(bwplotka): Don't buffer all, we have to buffer single series (to sort and dedup chunks), but nothing more.
series []storepb.Series
i int
}
func newStoreSeriesSet(s []storepb.Series) *storeSeriesSet {
return &storeSeriesSet{series: s, i: -1}
}
func (s *storeSeriesSet) Next() bool {
if s.i >= len(s.series)-1 {
return false
}
s.i++
return true
}
func (*storeSeriesSet) Err() error {
return nil
}
func (s *storeSeriesSet) At() (labels.Labels, []storepb.AggrChunk) {
return s.series[s.i].PromLabels(), s.series[s.i].Chunks
}
// chunkSeries implements storage.Series for a series on storepb types.
type chunkSeries struct {
lset labels.Labels
chunks []storepb.AggrChunk
mint, maxt int64
aggrs []storepb.Aggr
}
// newChunkSeries allows to iterate over samples for each sorted and non-overlapped chunks.
func newChunkSeries(lset labels.Labels, chunks []storepb.AggrChunk, mint, maxt int64, aggrs []storepb.Aggr) *chunkSeries {
return &chunkSeries{
lset: lset,
chunks: chunks,
mint: mint,
maxt: maxt,
aggrs: aggrs,
}
}
func (s *chunkSeries) Labels() labels.Labels {
return s.lset
}
func (s *chunkSeries) Iterator(_ chunkenc.Iterator) chunkenc.Iterator {
var sit chunkenc.Iterator
its := make([]chunkenc.Iterator, 0, len(s.chunks))
if len(s.aggrs) == 1 {
switch s.aggrs[0] {
case storepb.Aggr_COUNT:
for _, c := range s.chunks {
its = append(its, getFirstIterator(c.Count, c.Raw))
}
sit = newChunkSeriesIterator(its)
case storepb.Aggr_SUM:
for _, c := range s.chunks {
its = append(its, getFirstIterator(c.Sum, c.Raw))
}
sit = newChunkSeriesIterator(its)
case storepb.Aggr_MIN:
for _, c := range s.chunks {
its = append(its, getFirstIterator(c.Min, c.Raw))
}
sit = newChunkSeriesIterator(its)
case storepb.Aggr_MAX:
for _, c := range s.chunks {
its = append(its, getFirstIterator(c.Max, c.Raw))
}
sit = newChunkSeriesIterator(its)
case storepb.Aggr_COUNTER:
for _, c := range s.chunks {
its = append(its, getFirstIterator(c.Counter, c.Raw))
}
// TODO(bwplotka): This breaks resets function. See https://github.com/thanos-io/thanos/issues/3644
sit = downsample.NewApplyCounterResetsIterator(its...)
default:
return errSeriesIterator{err: errors.Errorf("unexpected result aggregate type %v", s.aggrs)}
}
return dedup.NewBoundedSeriesIterator(sit, s.mint, s.maxt)
}
if len(s.aggrs) != 2 {
return errSeriesIterator{err: errors.Errorf("unexpected result aggregate type %v", s.aggrs)}
}
switch {
case s.aggrs[0] == storepb.Aggr_SUM && s.aggrs[1] == storepb.Aggr_COUNT,
s.aggrs[0] == storepb.Aggr_COUNT && s.aggrs[1] == storepb.Aggr_SUM:
for _, c := range s.chunks {
if c.Raw != nil {
its = append(its, getFirstIterator(c.Raw))
} else {
sum, cnt := getFirstIterator(c.Sum), getFirstIterator(c.Count)
its = append(its, downsample.NewAverageChunkIterator(cnt, sum))
}
}
sit = newChunkSeriesIterator(its)
default:
return errSeriesIterator{err: errors.Errorf("unexpected result aggregate type %v", s.aggrs)}
}
return dedup.NewBoundedSeriesIterator(sit, s.mint, s.maxt)
}
func getFirstIterator(cs ...*storepb.Chunk) chunkenc.Iterator {
for _, c := range cs {
if c == nil {
continue
}
chk, err := chunkenc.FromData(chunkEncoding(c.Type), c.Data)
if err != nil {
return errSeriesIterator{err}
}
return chk.Iterator(nil)
}
return errSeriesIterator{errors.New("no valid chunk found")}
}
func chunkEncoding(e storepb.Chunk_Encoding) chunkenc.Encoding {
switch e {
case storepb.Chunk_XOR:
return chunkenc.EncXOR
case storepb.Chunk_HISTOGRAM:
return chunkenc.EncHistogram
case storepb.Chunk_FLOAT_HISTOGRAM:
return chunkenc.EncFloatHistogram
}
return 255 // Invalid.
}
type errSeriesIterator struct {
err error
}
func (errSeriesIterator) Seek(int64) chunkenc.ValueType { return chunkenc.ValNone }
func (errSeriesIterator) Next() chunkenc.ValueType { return chunkenc.ValNone }
func (errSeriesIterator) At() (int64, float64) { return 0, 0 }
func (errSeriesIterator) AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram) {
return 0, nil
}
func (errSeriesIterator) AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
return 0, nil
}
func (errSeriesIterator) AtT() int64 { return 0 }
func (it errSeriesIterator) Err() error { return it.err }
// chunkSeriesIterator implements a series iterator on top
// of a list of time-sorted, non-overlapping chunks.
type chunkSeriesIterator struct {
chunks []chunkenc.Iterator
i int
lastVal chunkenc.ValueType
}
func newChunkSeriesIterator(cs []chunkenc.Iterator) chunkenc.Iterator {
if len(cs) == 0 {
// This should not happen. StoreAPI implementations should not send empty results.
return errSeriesIterator{err: errors.Errorf("store returned an empty result")}
}
return &chunkSeriesIterator{chunks: cs}
}
func (it *chunkSeriesIterator) Seek(t int64) chunkenc.ValueType {
// 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.AtT()
if ct >= t {
return it.lastVal
}
it.lastVal = it.Next()
if it.lastVal == chunkenc.ValNone {
return chunkenc.ValNone
}
}
}
func (it *chunkSeriesIterator) At() (t int64, v float64) {
return it.chunks[it.i].At()
}
func (it *chunkSeriesIterator) AtHistogram(h *histogram.Histogram) (int64, *histogram.Histogram) {
return it.chunks[it.i].AtHistogram(h)
}
func (it *chunkSeriesIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
return it.chunks[it.i].AtFloatHistogram(fh)
}
func (it *chunkSeriesIterator) AtT() int64 {
return it.chunks[it.i].AtT()
}
func (it *chunkSeriesIterator) Next() chunkenc.ValueType {
lastT := it.AtT()
if valueType := it.chunks[it.i].Next(); valueType != chunkenc.ValNone {
it.lastVal = valueType
return valueType
}
if it.Err() != nil {
return chunkenc.ValNone
}
if it.i >= len(it.chunks)-1 {
return chunkenc.ValNone
}
// Chunks are guaranteed to be ordered but not generally guaranteed to not overlap.
// We must ensure to skip any overlapping range between adjacent chunks.
it.i++
return it.Seek(lastT + 1)
}
func (it *chunkSeriesIterator) Err() error {
return it.chunks[it.i].Err()
}
type lazySeriesSet struct {
create func() (s storage.SeriesSet, ok bool)
set storage.SeriesSet
}
func (c *lazySeriesSet) Next() bool {
if c.set != nil {
return c.set.Next()
}
var ok bool
c.set, ok = c.create()
return ok
}
func (c *lazySeriesSet) Err() error {
if c.set != nil {
return c.set.Err()
}
return nil
}
func (c *lazySeriesSet) At() storage.Series {
if c.set != nil {
return c.set.At()
}
return nil
}
func (c *lazySeriesSet) Warnings() annotations.Annotations {
if c.set != nil {
return c.set.Warnings()
}
return nil
}