-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy pathquery.go
333 lines (282 loc) · 11.4 KB
/
query.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
package distributor
import (
"context"
"io"
"sort"
"time"
"github.com/go-kit/log/level"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/weaveworks/common/instrument"
"github.com/cortexproject/cortex/pkg/cortexpb"
ingester_client "github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/querier/stats"
"github.com/cortexproject/cortex/pkg/ring"
"github.com/cortexproject/cortex/pkg/tenant"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/extract"
"github.com/cortexproject/cortex/pkg/util/grpcutil"
"github.com/cortexproject/cortex/pkg/util/limiter"
"github.com/cortexproject/cortex/pkg/util/validation"
)
func (d *Distributor) QueryExemplars(ctx context.Context, from, to model.Time, matchers ...[]*labels.Matcher) (*ingester_client.ExemplarQueryResponse, error) {
var result *ingester_client.ExemplarQueryResponse
err := instrument.CollectedRequest(ctx, "Distributor.QueryExemplars", d.queryDuration, instrument.ErrorCode, func(ctx context.Context) error {
req, err := ingester_client.ToExemplarQueryRequest(from, to, matchers...)
if err != nil {
return err
}
// We ask for all ingesters without passing matchers because exemplar queries take in an array of array of label matchers.
replicationSet, err := d.GetIngestersForQuery(ctx)
if err != nil {
return err
}
result, err = d.queryIngestersExemplars(ctx, replicationSet, req)
if err != nil {
return err
}
if s := opentracing.SpanFromContext(ctx); s != nil {
s.LogKV("series", len(result.Timeseries))
}
return nil
})
return result, err
}
// QueryStream multiple ingesters via the streaming interface and returns big ol' set of chunks.
func (d *Distributor) QueryStream(ctx context.Context, from, to model.Time, matchers ...*labels.Matcher) (*ingester_client.QueryStreamResponse, error) {
var result *ingester_client.QueryStreamResponse
err := instrument.CollectedRequest(ctx, "Distributor.QueryStream", d.queryDuration, instrument.ErrorCode, func(ctx context.Context) error {
req, err := ingester_client.ToQueryRequest(from, to, matchers)
if err != nil {
return err
}
replicationSet, err := d.GetIngestersForQuery(ctx, matchers...)
if err != nil {
return err
}
result, err = d.queryIngesterStream(ctx, replicationSet, req)
if err != nil {
return err
}
if s := opentracing.SpanFromContext(ctx); s != nil {
s.LogKV("chunk-series", len(result.GetChunkseries()))
}
return nil
})
return result, err
}
// GetIngestersForQuery returns a replication set including all ingesters that should be queried
// to fetch series matching input label matchers.
func (d *Distributor) GetIngestersForQuery(ctx context.Context, matchers ...*labels.Matcher) (ring.ReplicationSet, error) {
userID, err := tenant.TenantID(ctx)
if err != nil {
return ring.ReplicationSet{}, err
}
// If shuffle sharding is enabled we should only query ingesters which are
// part of the tenant's subring.
if d.cfg.ShardingStrategy == util.ShardingStrategyShuffle {
shardSize := d.limits.IngestionTenantShardSize(userID)
lookbackPeriod := d.cfg.ShuffleShardingLookbackPeriod
if shardSize > 0 && lookbackPeriod > 0 {
return d.ingestersRing.ShuffleShardWithLookback(userID, shardSize, lookbackPeriod, time.Now()).GetReplicationSetForOperation(ring.Read)
}
}
// If "shard by all labels" is disabled, we can get ingesters by metricName if exists.
if !d.cfg.ShardByAllLabels && len(matchers) > 0 {
metricNameMatcher, _, ok := extract.MetricNameMatcherFromMatchers(matchers)
if ok && metricNameMatcher.Type == labels.MatchEqual {
return d.ingestersRing.Get(shardByMetricName(userID, metricNameMatcher.Value), ring.Read, nil, nil, nil)
}
}
return d.ingestersRing.GetReplicationSetForOperation(ring.Read)
}
// GetIngestersForMetadata returns a replication set including all ingesters that should be queried
// to fetch metadata (eg. label names/values or series).
func (d *Distributor) GetIngestersForMetadata(ctx context.Context) (ring.ReplicationSet, error) {
userID, err := tenant.TenantID(ctx)
if err != nil {
return ring.ReplicationSet{}, err
}
// If shuffle sharding is enabled we should only query ingesters which are
// part of the tenant's subring.
if d.cfg.ShardingStrategy == util.ShardingStrategyShuffle {
shardSize := d.limits.IngestionTenantShardSize(userID)
lookbackPeriod := d.cfg.ShuffleShardingLookbackPeriod
if shardSize > 0 && lookbackPeriod > 0 {
return d.ingestersRing.ShuffleShardWithLookback(userID, shardSize, lookbackPeriod, time.Now()).GetReplicationSetForOperation(ring.Read)
}
}
return d.ingestersRing.GetReplicationSetForOperation(ring.Read)
}
// mergeExemplarSets merges and dedupes two sets of already sorted exemplar pairs.
// Both a and b should be lists of exemplars from the same series.
// Defined here instead of pkg/util to avoid a import cycle.
func mergeExemplarSets(a, b []cortexpb.Exemplar) []cortexpb.Exemplar {
result := make([]cortexpb.Exemplar, 0, len(a)+len(b))
i, j := 0, 0
for i < len(a) && j < len(b) {
if a[i].TimestampMs < b[j].TimestampMs {
result = append(result, a[i])
i++
} else if a[i].TimestampMs > b[j].TimestampMs {
result = append(result, b[j])
j++
} else {
result = append(result, a[i])
i++
j++
}
}
// Add the rest of a or b. One of them is empty now.
result = append(result, a[i:]...)
result = append(result, b[j:]...)
return result
}
// queryIngestersExemplars queries the ingesters for exemplars.
func (d *Distributor) queryIngestersExemplars(ctx context.Context, replicationSet ring.ReplicationSet, req *ingester_client.ExemplarQueryRequest) (*ingester_client.ExemplarQueryResponse, error) {
// Fetch exemplars from multiple ingesters in parallel, using the replicationSet
// to deal with consistency.
results, err := replicationSet.Do(ctx, d.cfg.ExtraQueryDelay, false, func(ctx context.Context, ing *ring.InstanceDesc) (interface{}, error) {
client, err := d.ingesterPool.GetClientFor(ing.Addr)
if err != nil {
return nil, err
}
ingesterId, err := d.ingestersRing.GetInstanceIdByAddr(ing.Addr)
if err != nil {
level.Warn(d.log).Log("msg", "instance not found in the ring", "addr", ing.Addr, "err", err)
}
resp, err := client.(ingester_client.IngesterClient).QueryExemplars(ctx, req)
d.ingesterQueries.WithLabelValues(ingesterId).Inc()
if err != nil {
d.ingesterQueryFailures.WithLabelValues(ingesterId).Inc()
return nil, err
}
return resp, nil
})
if err != nil {
return nil, err
}
return mergeExemplarQueryResponses(results), nil
}
func mergeExemplarQueryResponses(results []interface{}) *ingester_client.ExemplarQueryResponse {
var keys []string
exemplarResults := make(map[string]cortexpb.TimeSeries)
buf := make([]byte, 0, 1024)
for _, result := range results {
r := result.(*ingester_client.ExemplarQueryResponse)
for _, ts := range r.Timeseries {
lbls := string(cortexpb.FromLabelAdaptersToLabels(ts.Labels).Bytes(buf))
e, ok := exemplarResults[lbls]
if !ok {
exemplarResults[lbls] = ts
keys = append(keys, lbls)
} else {
// Merge in any missing values from another ingesters exemplars for this series.
e.Exemplars = mergeExemplarSets(e.Exemplars, ts.Exemplars)
exemplarResults[lbls] = e
}
}
}
// Query results from each ingester were sorted, but are not necessarily still sorted after merging.
sort.Strings(keys)
result := make([]cortexpb.TimeSeries, len(exemplarResults))
for i, k := range keys {
result[i] = exemplarResults[k]
}
return &ingester_client.ExemplarQueryResponse{Timeseries: result}
}
// queryIngesterStream queries the ingesters using the new streaming API.
func (d *Distributor) queryIngesterStream(ctx context.Context, replicationSet ring.ReplicationSet, req *ingester_client.QueryRequest) (*ingester_client.QueryStreamResponse, error) {
var (
queryLimiter = limiter.QueryLimiterFromContextWithFallback(ctx)
reqStats = stats.FromContext(ctx)
)
// Fetch samples from multiple ingesters
results, err := replicationSet.Do(ctx, d.cfg.ExtraQueryDelay, false, func(ctx context.Context, ing *ring.InstanceDesc) (interface{}, error) {
client, err := d.ingesterPool.GetClientFor(ing.Addr)
if err != nil {
return nil, err
}
ingesterId, err := d.ingestersRing.GetInstanceIdByAddr(ing.Addr)
if err != nil {
level.Warn(d.log).Log("msg", "instance not found in the ring", "addr", ing.Addr, "err", err)
}
d.ingesterQueries.WithLabelValues(ingesterId).Inc()
stream, err := client.(ingester_client.IngesterClient).QueryStream(ctx, req)
if err != nil {
d.ingesterQueryFailures.WithLabelValues(ingesterId).Inc()
return nil, err
}
defer stream.CloseSend() //nolint:errcheck
result := &ingester_client.QueryStreamResponse{}
for {
resp, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
// Do not track a failure if the context was canceled.
if !grpcutil.IsGRPCContextCanceled(err) {
d.ingesterQueryFailures.WithLabelValues(ingesterId).Inc()
}
return nil, err
}
// Enforce the max chunks limits.
if chunkLimitErr := queryLimiter.AddChunks(resp.ChunksCount()); chunkLimitErr != nil {
return nil, validation.LimitError(chunkLimitErr.Error())
}
s := make([][]cortexpb.LabelAdapter, 0, len(resp.Chunkseries))
for _, series := range resp.Chunkseries {
s = append(s, series.Labels)
}
if limitErr := queryLimiter.AddSeries(s...); limitErr != nil {
return nil, validation.LimitError(limitErr.Error())
}
if chunkBytesLimitErr := queryLimiter.AddChunkBytes(resp.ChunksSize()); chunkBytesLimitErr != nil {
return nil, validation.LimitError(chunkBytesLimitErr.Error())
}
if dataBytesLimitErr := queryLimiter.AddDataBytes(resp.Size()); dataBytesLimitErr != nil {
return nil, validation.LimitError(dataBytesLimitErr.Error())
}
result.DesymbolizeLabels()
result.Chunkseries = append(result.Chunkseries, resp.Chunkseries...)
}
return result, nil
})
if err != nil {
return nil, err
}
span, _ := opentracing.StartSpanFromContext(ctx, "Distributor.MergeIngesterStreams")
defer span.Finish()
hashToChunkseries := map[string]ingester_client.TimeSeriesChunk{}
for _, result := range results {
response := result.(*ingester_client.QueryStreamResponse)
// Parse any chunk series
for _, series := range response.Chunkseries {
key := ingester_client.LabelsToKeyString(cortexpb.FromLabelAdaptersToLabels(series.Labels))
existing := hashToChunkseries[key]
existing.Labels = series.Labels
existing.Chunks = append(existing.Chunks, series.Chunks...)
hashToChunkseries[key] = existing
}
}
resp := &ingester_client.QueryStreamResponse{
Chunkseries: make([]ingester_client.TimeSeriesChunk, 0, len(hashToChunkseries)),
}
for _, series := range hashToChunkseries {
resp.Chunkseries = append(resp.Chunkseries, series)
}
respSize := resp.Size()
chksSize := resp.ChunksSize()
chksCount := resp.ChunksCount()
span.SetTag("fetched_series", len(resp.Chunkseries))
span.SetTag("fetched_chunks", chksCount)
span.SetTag("fetched_data_bytes", respSize)
span.SetTag("fetched_chunks_bytes", chksSize)
reqStats.AddFetchedSeries(uint64(len(resp.Chunkseries)))
reqStats.AddFetchedChunkBytes(uint64(chksSize))
reqStats.AddFetchedDataBytes(uint64(respSize))
reqStats.AddFetchedChunks(uint64(chksCount))
reqStats.AddFetchedSamples(uint64(resp.SamplesCount()))
return resp, nil
}