-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathquerier.go
286 lines (249 loc) · 8.69 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
package query
import (
"context"
"sort"
"strings"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/tracing"
)
// WarningReporter allows to report warnings to frontend layer.
//
// Warning can include partial errors `partialResponse` is enabled. It occurs when only part of the results are ready and
// another is not available because of the failure.
// It is required to be thread-safe.
type WarningReporter func(error)
// 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 the replicaLabel by default.
// maxResolutionMillis controls downsampling resolution that is allowed (specified in milliseconds).
// partialResponse controls `partialResponseDisabled` option of StoreAPI and partial response behaviour of proxy.
type QueryableCreator func(deduplicate bool, maxResolutionMillis int64, partialResponse bool, r WarningReporter) storage.Queryable
// NewQueryableCreator creates QueryableCreator.
func NewQueryableCreator(logger log.Logger, proxy storepb.StoreServer, replicaLabel string) QueryableCreator {
return func(deduplicate bool, maxResolutionMillis int64, partialResponse bool, r WarningReporter) storage.Queryable {
return &queryable{
logger: logger,
replicaLabel: replicaLabel,
proxy: proxy,
deduplicate: deduplicate,
maxResolutionMillis: maxResolutionMillis,
partialResponse: partialResponse,
warningReporter: r,
}
}
}
type queryable struct {
logger log.Logger
replicaLabel string
proxy storepb.StoreServer
deduplicate bool
maxResolutionMillis int64
partialResponse bool
warningReporter WarningReporter
}
// Querier returns a new storage querier against the underlying proxy store API.
func (q *queryable) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
return newQuerier(ctx, q.logger, mint, maxt, q.replicaLabel, q.proxy, q.deduplicate, int64(q.maxResolutionMillis), q.partialResponse, q.warningReporter), nil
}
type querier struct {
ctx context.Context
logger log.Logger
cancel func()
mint, maxt int64
replicaLabel string
proxy storepb.StoreServer
deduplicate bool
maxResolutionMillis int64
partialResponse bool
warningReporter WarningReporter
}
// newQuerier creates implementation of storage.Querier that fetches data from the proxy
// store API endpoints.
func newQuerier(
ctx context.Context,
logger log.Logger,
mint, maxt int64,
replicaLabel string,
proxy storepb.StoreServer,
deduplicate bool,
maxResolutionMillis int64,
partialResponse bool,
warningReporter WarningReporter,
) *querier {
if logger == nil {
logger = log.NewNopLogger()
}
if warningReporter == nil {
warningReporter = func(error) {}
}
ctx, cancel := context.WithCancel(ctx)
return &querier{
ctx: ctx,
logger: logger,
cancel: cancel,
mint: mint,
maxt: maxt,
replicaLabel: replicaLabel,
proxy: proxy,
deduplicate: deduplicate,
maxResolutionMillis: maxResolutionMillis,
partialResponse: partialResponse,
warningReporter: warningReporter,
}
}
func (q *querier) isDedupEnabled() bool {
return q.deduplicate && q.replicaLabel != ""
}
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
warnings []string
}
func (s *seriesServer) Send(r *storepb.SeriesResponse) error {
if r.GetWarning() != "" {
s.warnings = append(s.warnings, r.GetWarning())
return nil
}
if r.GetSeries() == nil {
return errors.New("no seriesSet")
}
s.seriesSet = append(s.seriesSet, *r.GetSeries())
return nil
}
func (s *seriesServer) Context() context.Context {
return s.ctx
}
type resAggr int
const (
resAggrAvg resAggr = iota
resAggrCount
resAggrSum
resAggrMin
resAggrMax
resAggrCounter
)
// aggrsFromFunc infers aggregates of the underlying data based on the wrapping
// function of a series selection.
func aggrsFromFunc(f string) ([]storepb.Aggr, resAggr) {
if f == "min" || strings.HasPrefix(f, "min_") {
return []storepb.Aggr{storepb.Aggr_MIN}, resAggrMin
}
if f == "max" || strings.HasPrefix(f, "max_") {
return []storepb.Aggr{storepb.Aggr_MAX}, resAggrMax
}
if f == "count" || strings.HasPrefix(f, "count_") {
return []storepb.Aggr{storepb.Aggr_COUNT}, resAggrCount
}
// f == "sum" falls through here since we want the actual samples
if strings.HasPrefix(f, "sum_") {
return []storepb.Aggr{storepb.Aggr_SUM}, resAggrSum
}
if f == "increase" || f == "rate" {
return []storepb.Aggr{storepb.Aggr_COUNTER}, resAggrCounter
}
// In the default case, we retrieve count and sum to compute an average.
return []storepb.Aggr{storepb.Aggr_COUNT, storepb.Aggr_SUM}, resAggrAvg
}
func (q *querier) Select(params *storage.SelectParams, ms ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
span, ctx := tracing.StartSpan(q.ctx, "querier_select")
defer span.Finish()
sms, err := translateMatchers(ms...)
if err != nil {
return nil, nil, errors.Wrap(err, "convert matchers")
}
queryAggrs, resAggr := aggrsFromFunc(params.Func)
resp := &seriesServer{ctx: ctx}
if err := q.proxy.Series(&storepb.SeriesRequest{
MinTime: q.mint,
MaxTime: q.maxt,
Matchers: sms,
MaxResolutionWindow: q.maxResolutionMillis,
Aggregates: queryAggrs,
PartialResponseDisabled: !q.partialResponse,
}, resp); err != nil {
return nil, nil, errors.Wrap(err, "proxy Series()")
}
for _, w := range resp.warnings {
// NOTE(bwplotka): We could use warnings return arguments here, however need reporter anyway for LabelValues and LabelNames method,
// so we choose to be consistent and keep reporter.
q.warningReporter(errors.New(w))
}
if !q.isDedupEnabled() {
// Return data without any deduplication.
return promSeriesSet{
mint: q.mint,
maxt: q.maxt,
set: newStoreSeriesSet(resp.seriesSet),
aggr: resAggr,
}, nil, nil
}
// TODO(fabxc): this could potentially pushed further down into the store API
// to make true streaming possible.
sortDedupLabels(resp.seriesSet, q.replicaLabel)
set := promSeriesSet{
mint: q.mint,
maxt: q.maxt,
set: newStoreSeriesSet(resp.seriesSet),
aggr: resAggr,
}
// The merged series set assembles all potentially-overlapping time ranges
// of the same series into a single one. The series are ordered so that equal series
// from different replicas are sequential. We can now deduplicate those.
return newDedupSeriesSet(set, q.replicaLabel), nil, nil
}
// sortDedupLabels resorts the set so that the same series with different replica
// labels are coming right after each other.
func sortDedupLabels(set []storepb.Series, replicaLabel string) {
for _, s := range set {
// Move the replica label to the very end.
sort.Slice(s.Labels, func(i, j int) bool {
if s.Labels[i].Name == replicaLabel {
return false
}
if s.Labels[j].Name == replicaLabel {
return true
}
return s.Labels[i].Name < s.Labels[j].Name
})
}
// With the re-ordered label sets, re-sorting all series aligns the same series
// from different replicas sequentially.
sort.Slice(set, func(i, j int) bool {
return storepb.CompareLabels(set[i].Labels, set[j].Labels) < 0
})
}
// LabelValues returns all potential values for a label name.
func (q *querier) LabelValues(name string) ([]string, error) {
span, ctx := tracing.StartSpan(q.ctx, "querier_label_values")
defer span.Finish()
resp, err := q.proxy.LabelValues(ctx, &storepb.LabelValuesRequest{Label: name, PartialResponseDisabled: !q.partialResponse})
if err != nil {
return nil, errors.Wrap(err, "proxy LabelValues()")
}
for _, w := range resp.Warnings {
q.warningReporter(errors.New(w))
}
return resp.Values, nil
}
// LabelNames returns all the unique label names present in the block in sorted order.
func (q *querier) LabelNames() ([]string, error) {
span, ctx := tracing.StartSpan(q.ctx, "querier_label_names")
defer span.Finish()
resp, err := q.proxy.LabelNames(ctx, &storepb.LabelNamesRequest{PartialResponseDisabled: !q.partialResponse})
if err != nil {
return nil, errors.Wrap(err, "proxy LabelNames()")
}
for _, w := range resp.Warnings {
q.warningReporter(errors.New(w))
}
return resp.Names, nil
}
func (q *querier) Close() error {
q.cancel()
return nil
}