/
querier.go
480 lines (423 loc) · 15.4 KB
/
querier.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package query
import (
"context"
"strings"
"sync"
"time"
"github.com/go-kit/log"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/annotations"
"github.com/thanos-io/thanos/pkg/dedup"
"github.com/thanos-io/thanos/pkg/extprom"
"github.com/thanos-io/thanos/pkg/gate"
"github.com/thanos-io/thanos/pkg/store"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/tenancy"
"github.com/thanos-io/thanos/pkg/tracing"
)
type seriesStatsReporter func(seriesStats storepb.SeriesStatsCounter)
var NoopSeriesStatsReporter seriesStatsReporter = func(_ storepb.SeriesStatsCounter) {}
func NewAggregateStatsReporter(stats *[]storepb.SeriesStatsCounter) seriesStatsReporter {
var mutex sync.Mutex
return func(s storepb.SeriesStatsCounter) {
mutex.Lock()
defer mutex.Unlock()
*stats = append(*stats, s)
}
}
// QueryableCreator returns implementation of promql.Queryable that fetches data from the proxy store API endpoints.
// If deduplication is enabled, all data retrieved from it will be deduplicated along all replicaLabels by default.
// When the replicaLabels argument is not empty it overwrites the global replicaLabels flag. This allows specifying
// replicaLabels at query time.
// maxResolutionMillis controls downsampling resolution that is allowed (specified in milliseconds).
// partialResponse controls `partialResponseDisabled` option of StoreAPI and partial response behavior of proxy.
type QueryableCreator func(
deduplicate bool,
replicaLabels []string,
storeDebugMatchers [][]*labels.Matcher,
maxResolutionMillis int64,
partialResponse,
enableQueryPushdown,
skipChunks bool,
shardInfo *storepb.ShardInfo,
seriesStatsReporter seriesStatsReporter,
) storage.Queryable
// NewQueryableCreator creates QueryableCreator.
// NOTE(bwplotka): Proxy assumes to be replica_aware, see thanos.store.info.StoreInfo.replica_aware field.
func NewQueryableCreator(
logger log.Logger,
reg prometheus.Registerer,
proxy storepb.StoreServer,
maxConcurrentSelects int,
selectTimeout time.Duration,
) QueryableCreator {
gf := gate.NewGateFactory(extprom.WrapRegistererWithPrefix("concurrent_selects_", reg), maxConcurrentSelects, gate.Selects)
return func(
deduplicate bool,
replicaLabels []string,
storeDebugMatchers [][]*labels.Matcher,
maxResolutionMillis int64,
partialResponse,
enableQueryPushdown,
skipChunks bool,
shardInfo *storepb.ShardInfo,
seriesStatsReporter seriesStatsReporter,
) storage.Queryable {
return &queryable{
logger: logger,
replicaLabels: replicaLabels,
storeDebugMatchers: storeDebugMatchers,
proxy: proxy,
deduplicate: deduplicate,
maxResolutionMillis: maxResolutionMillis,
partialResponse: partialResponse,
skipChunks: skipChunks,
gateProviderFn: func() gate.Gate {
return gf.New()
},
maxConcurrentSelects: maxConcurrentSelects,
selectTimeout: selectTimeout,
enableQueryPushdown: enableQueryPushdown,
shardInfo: shardInfo,
seriesStatsReporter: seriesStatsReporter,
}
}
}
type queryable struct {
logger log.Logger
replicaLabels []string
storeDebugMatchers [][]*labels.Matcher
proxy storepb.StoreServer
deduplicate bool
maxResolutionMillis int64
partialResponse bool
skipChunks bool
gateProviderFn func() gate.Gate
maxConcurrentSelects int
selectTimeout time.Duration
enableQueryPushdown bool
shardInfo *storepb.ShardInfo
seriesStatsReporter seriesStatsReporter
}
// Querier returns a new storage querier against the underlying proxy store API.
func (q *queryable) Querier(mint, maxt int64) (storage.Querier, error) {
return newQuerier(q.logger, mint, maxt, q.replicaLabels, q.storeDebugMatchers, q.proxy, q.deduplicate, q.maxResolutionMillis, q.partialResponse, q.enableQueryPushdown, q.skipChunks, q.gateProviderFn(), q.selectTimeout, q.shardInfo, q.seriesStatsReporter), nil
}
type querier struct {
logger log.Logger
mint, maxt int64
replicaLabels []string
storeDebugMatchers [][]*labels.Matcher
proxy storepb.StoreServer
deduplicate bool
maxResolutionMillis int64
partialResponseStrategy storepb.PartialResponseStrategy
enableQueryPushdown bool
skipChunks bool
selectGate gate.Gate
selectTimeout time.Duration
shardInfo *storepb.ShardInfo
seriesStatsReporter seriesStatsReporter
}
// newQuerier creates implementation of storage.Querier that fetches data from the proxy
// store API endpoints.
func newQuerier(
logger log.Logger,
mint,
maxt int64,
replicaLabels []string,
storeDebugMatchers [][]*labels.Matcher,
proxy storepb.StoreServer,
deduplicate bool,
maxResolutionMillis int64,
partialResponse,
enableQueryPushdown,
skipChunks bool,
selectGate gate.Gate,
selectTimeout time.Duration,
shardInfo *storepb.ShardInfo,
seriesStatsReporter seriesStatsReporter,
) *querier {
if logger == nil {
logger = log.NewNopLogger()
}
rl := make(map[string]struct{})
for _, replicaLabel := range replicaLabels {
rl[replicaLabel] = struct{}{}
}
partialResponseStrategy := storepb.PartialResponseStrategy_ABORT
if partialResponse {
partialResponseStrategy = storepb.PartialResponseStrategy_WARN
}
return &querier{
logger: logger,
selectGate: selectGate,
selectTimeout: selectTimeout,
mint: mint,
maxt: maxt,
replicaLabels: replicaLabels,
storeDebugMatchers: storeDebugMatchers,
proxy: proxy,
deduplicate: deduplicate,
maxResolutionMillis: maxResolutionMillis,
partialResponseStrategy: partialResponseStrategy,
skipChunks: skipChunks,
enableQueryPushdown: enableQueryPushdown,
shardInfo: shardInfo,
seriesStatsReporter: seriesStatsReporter,
}
}
func (q *querier) isDedupEnabled() bool {
return q.deduplicate && len(q.replicaLabels) > 0
}
type seriesServer struct {
// This field just exist to pseudo-implement the unused methods of the interface.
storepb.Store_SeriesServer
ctx context.Context
seriesSet []storepb.Series
seriesSetStats storepb.SeriesStatsCounter
warnings annotations.Annotations
}
func (s *seriesServer) Send(r *storepb.SeriesResponse) error {
if r.GetWarning() != "" {
s.warnings.Add(errors.New(r.GetWarning()))
return nil
}
if r.GetSeries() != nil {
s.seriesSet = append(s.seriesSet, *r.GetSeries())
s.seriesSetStats.Count(r.GetSeries())
return nil
}
// Unsupported field, skip.
return nil
}
func (s *seriesServer) Context() context.Context {
return s.ctx
}
// aggrsFromFunc infers aggregates of the underlying data based on the wrapping
// function of a series selection.
func aggrsFromFunc(f string) []storepb.Aggr {
if f == "min" || strings.HasPrefix(f, "min_") {
return []storepb.Aggr{storepb.Aggr_MIN}
}
if f == "max" || strings.HasPrefix(f, "max_") {
return []storepb.Aggr{storepb.Aggr_MAX}
}
if f == "count" || strings.HasPrefix(f, "count_") {
return []storepb.Aggr{storepb.Aggr_COUNT}
}
// f == "sum" falls through here since we want the actual samples.
if strings.HasPrefix(f, "sum_") {
return []storepb.Aggr{storepb.Aggr_SUM}
}
if f == "increase" || f == "rate" || f == "irate" || f == "resets" {
return []storepb.Aggr{storepb.Aggr_COUNTER}
}
// In the default case, we retrieve count and sum to compute an average.
return []storepb.Aggr{storepb.Aggr_COUNT, storepb.Aggr_SUM}
}
func storeHintsFromPromHints(hints *storage.SelectHints) *storepb.QueryHints {
return &storepb.QueryHints{
StepMillis: hints.Step,
Func: &storepb.Func{
Name: hints.Func,
},
Grouping: &storepb.Grouping{
By: hints.By,
Labels: hints.Grouping,
},
Range: &storepb.Range{Millis: hints.Range},
}
}
func (q *querier) Select(ctx context.Context, _ bool, hints *storage.SelectHints, ms ...*labels.Matcher) storage.SeriesSet {
if hints == nil {
hints = &storage.SelectHints{
Start: q.mint,
End: q.maxt,
}
} else {
// NOTE(GiedriusS): need to make a copy here
// because the PromQL engine sorts these and
// we later on call String() the whole request (including this slice).
grouping := make([]string, 0, len(hints.Grouping))
grouping = append(grouping, hints.Grouping...)
hints.Grouping = grouping
}
matchers := make([]string, len(ms))
for i, m := range ms {
matchers[i] = m.String()
}
tenant := ctx.Value(tenancy.TenantKey)
// The context gets canceled as soon as query evaluation is completed by the engine.
// We want to prevent this from happening for the async store API calls we make while preserving tracing context.
// TODO(bwplotka): Does the above still is true? It feels weird to leave unfinished calls behind query API.
ctx = tracing.CopyTraceContext(context.Background(), ctx)
ctx = context.WithValue(ctx, tenancy.TenantKey, tenant)
ctx, cancel := context.WithTimeout(ctx, q.selectTimeout)
span, ctx := tracing.StartSpan(ctx, "querier_select", opentracing.Tags{
"minTime": hints.Start,
"maxTime": hints.End,
"matchers": "{" + strings.Join(matchers, ",") + "}",
})
promise := make(chan storage.SeriesSet, 1)
go func() {
defer close(promise)
var err error
tracing.DoInSpan(ctx, "querier_select_gate_ismyturn", func(ctx context.Context) {
err = q.selectGate.Start(ctx)
})
if err != nil {
promise <- storage.ErrSeriesSet(errors.Wrap(err, "failed to wait for turn"))
return
}
defer q.selectGate.Done()
span, ctx := tracing.StartSpan(ctx, "querier_select_select_fn")
defer span.Finish()
set, stats, err := q.selectFn(ctx, hints, ms...)
if err != nil {
promise <- storage.ErrSeriesSet(err)
return
}
q.seriesStatsReporter(stats)
promise <- set
}()
return &lazySeriesSet{create: func() (storage.SeriesSet, bool) {
defer cancel()
defer span.Finish()
// Only gets called once, for the first Next() call of the series set.
set, ok := <-promise
if !ok {
return storage.ErrSeriesSet(errors.New("channel closed before a value received")), false
}
return set, set.Next()
}}
}
func (q *querier) selectFn(ctx context.Context, hints *storage.SelectHints, ms ...*labels.Matcher) (storage.SeriesSet, storepb.SeriesStatsCounter, error) {
sms, err := storepb.PromMatchersToMatchers(ms...)
if err != nil {
return nil, storepb.SeriesStatsCounter{}, errors.Wrap(err, "convert matchers")
}
aggrs := aggrsFromFunc(hints.Func)
// TODO(bwplotka): Pass it using the SeriesRequest instead of relying on context.
ctx = context.WithValue(ctx, store.StoreMatcherKey, q.storeDebugMatchers)
// TODO(bwplotka): Use inprocess gRPC when we want to stream responses.
// Currently streaming won't help due to nature of the both PromQL engine which
// pulls all series before computations anyway.
resp := &seriesServer{ctx: ctx}
req := storepb.SeriesRequest{
MinTime: hints.Start,
MaxTime: hints.End,
Matchers: sms,
MaxResolutionWindow: q.maxResolutionMillis,
Aggregates: aggrs,
ShardInfo: q.shardInfo,
PartialResponseStrategy: q.partialResponseStrategy,
SkipChunks: q.skipChunks,
}
if q.enableQueryPushdown {
req.QueryHints = storeHintsFromPromHints(hints)
}
if q.isDedupEnabled() {
// Soft ask to sort without replica labels and push them at the end of labelset.
req.WithoutReplicaLabels = q.replicaLabels
}
if err := q.proxy.Series(&req, resp); err != nil {
return nil, storepb.SeriesStatsCounter{}, errors.Wrap(err, "proxy Series()")
}
warns := annotations.New().Merge(resp.warnings)
if q.enableQueryPushdown && (hints.Func == "max_over_time" || hints.Func == "min_over_time") {
// On query pushdown, delete the metric's name from the result because that's what the
// PromQL does either way, and we want our iterator to work with data
// that was either pushed down or not.
for i := range resp.seriesSet {
lbls := resp.seriesSet[i].Labels
for j, lbl := range lbls {
if lbl.Name != model.MetricNameLabel {
continue
}
resp.seriesSet[i].Labels = append(resp.seriesSet[i].Labels[:j], resp.seriesSet[i].Labels[j+1:]...)
break
}
}
}
if !q.isDedupEnabled() {
return &promSeriesSet{
mint: q.mint,
maxt: q.maxt,
set: newStoreSeriesSet(resp.seriesSet),
aggrs: aggrs,
warns: warns,
}, resp.seriesSetStats, nil
}
// TODO(bwplotka): Move to deduplication on chunk level inside promSeriesSet, similar to what we have in dedup.NewDedupChunkMerger().
// This however require big refactor, caring about correct AggrChunk to iterator conversion, pushdown logic and counter reset apply.
// For now we apply simple logic that splits potential overlapping chunks into separate replica series, so we can split the work.
set := &promSeriesSet{
mint: q.mint,
maxt: q.maxt,
set: dedup.NewOverlapSplit(newStoreSeriesSet(resp.seriesSet)),
aggrs: aggrs,
warns: warns,
}
return dedup.NewSeriesSet(set, hints.Func, q.enableQueryPushdown), resp.seriesSetStats, nil
}
// LabelValues returns all potential values for a label name.
func (q *querier) LabelValues(ctx context.Context, name string, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
span, ctx := tracing.StartSpan(ctx, "querier_label_values")
defer span.Finish()
// TODO(bwplotka): Pass it using the SeriesRequest instead of relying on context.
ctx = context.WithValue(ctx, store.StoreMatcherKey, q.storeDebugMatchers)
pbMatchers, err := storepb.PromMatchersToMatchers(matchers...)
if err != nil {
return nil, nil, errors.Wrap(err, "converting prom matchers to storepb matchers")
}
resp, err := q.proxy.LabelValues(ctx, &storepb.LabelValuesRequest{
Label: name,
PartialResponseStrategy: q.partialResponseStrategy,
Start: q.mint,
End: q.maxt,
Matchers: pbMatchers,
})
if err != nil {
return nil, nil, errors.Wrap(err, "proxy LabelValues()")
}
var warns annotations.Annotations
for _, w := range resp.Warnings {
warns.Add(errors.New(w))
}
return resp.Values, warns, nil
}
// LabelNames returns all the unique label names present in the block in sorted order constrained
// by the given matchers.
func (q *querier) LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
span, ctx := tracing.StartSpan(ctx, "querier_label_names")
defer span.Finish()
// TODO(bwplotka): Pass it using the SeriesRequest instead of relying on context.
ctx = context.WithValue(ctx, store.StoreMatcherKey, q.storeDebugMatchers)
pbMatchers, err := storepb.PromMatchersToMatchers(matchers...)
if err != nil {
return nil, nil, errors.Wrap(err, "converting prom matchers to storepb matchers")
}
resp, err := q.proxy.LabelNames(ctx, &storepb.LabelNamesRequest{
PartialResponseStrategy: q.partialResponseStrategy,
Start: q.mint,
End: q.maxt,
Matchers: pbMatchers,
})
if err != nil {
return nil, nil, errors.Wrap(err, "proxy LabelNames()")
}
var warns annotations.Annotations
for _, w := range resp.Warnings {
warns.Add(errors.New(w))
}
return resp.Names, warns, nil
}
func (q *querier) Close() error { return nil }