forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_query_range_sharder.go
More file actions
527 lines (429 loc) · 16.1 KB
/
metrics_query_range_sharder.go
File metadata and controls
527 lines (429 loc) · 16.1 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package frontend
import (
"context"
"errors"
"fmt"
"math"
"net/http"
"time"
"github.com/go-kit/log" //nolint:all deprecated
"github.com/go-kit/log/level"
"github.com/gogo/protobuf/jsonpb"
"github.com/grafana/dskit/user"
"github.com/opentracing/opentracing-go"
"github.com/segmentio/fasthash/fnv1a"
"github.com/grafana/tempo/modules/frontend/combiner"
"github.com/grafana/tempo/modules/frontend/pipeline"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/modules/querier"
"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/pkg/traceql"
"github.com/grafana/tempo/tempodb"
"github.com/grafana/tempo/tempodb/backend"
)
type queryRangeSharder struct {
next pipeline.AsyncRoundTripper[combiner.PipelineResponse]
reader tempodb.Reader
overrides overrides.Interface
cfg QueryRangeSharderConfig
logger log.Logger
replicationFactor uint32
}
type QueryRangeSharderConfig struct {
ConcurrentRequests int `yaml:"concurrent_jobs,omitempty"`
TargetBytesPerRequest int `yaml:"target_bytes_per_job,omitempty"`
MaxDuration time.Duration `yaml:"max_duration"`
QueryBackendAfter time.Duration `yaml:"query_backend_after,omitempty"`
Interval time.Duration `yaml:"interval,omitempty"`
RF1ReadPath bool `yaml:"rf1_read_path,omitempty"`
}
// newAsyncQueryRangeSharder creates a sharding middleware for search
func newAsyncQueryRangeSharder(reader tempodb.Reader, o overrides.Interface, cfg QueryRangeSharderConfig, logger log.Logger) pipeline.AsyncMiddleware[combiner.PipelineResponse] {
var replicationFactor uint32
if cfg.RF1ReadPath {
replicationFactor = 1
}
return pipeline.AsyncMiddlewareFunc[combiner.PipelineResponse](func(next pipeline.AsyncRoundTripper[combiner.PipelineResponse]) pipeline.AsyncRoundTripper[combiner.PipelineResponse] {
return queryRangeSharder{
next: next,
reader: reader,
overrides: o,
cfg: cfg,
logger: logger,
replicationFactor: replicationFactor,
}
})
}
func (s queryRangeSharder) RoundTrip(pipelineRequest pipeline.Request) (pipeline.Responses[combiner.PipelineResponse], error) {
r := pipelineRequest.HTTPRequest()
span, ctx := opentracing.StartSpanFromContext(r.Context(), "frontend.QueryRangeSharder")
defer span.Finish()
req, err := api.ParseQueryRangeRequest(r)
if err != nil {
return pipeline.NewBadRequest(err), nil
}
expr, _, _, _, err := traceql.NewEngine().Compile(req.Query)
if err != nil {
return pipeline.NewBadRequest(err), nil
}
tenantID, err := user.ExtractOrgID(ctx)
if err != nil {
return pipeline.NewBadRequest(err), nil
}
if req.Step == 0 {
return pipeline.NewBadRequest(errors.New("step must be greater than 0")), nil
}
traceql.AlignRequest(req)
// calculate and enforce max search duration
// Note: this is checked after alignment for consistency.
maxDuration := s.maxDuration(tenantID)
if maxDuration != 0 && time.Duration(req.End-req.Start)*time.Nanosecond > maxDuration {
err = fmt.Errorf(fmt.Sprintf("range specified by start and end (%s) exceeds %s. received start=%d end=%d", time.Duration(req.End-req.Start), maxDuration, req.Start, req.End))
return pipeline.NewBadRequest(err), nil
}
var (
allowUnsafe = s.overrides.UnsafeQueryHints(tenantID)
samplingRate = s.samplingRate(expr, allowUnsafe)
targetBytesPerRequest = s.jobSize(expr, samplingRate, allowUnsafe)
interval = s.jobInterval(expr, allowUnsafe)
cutoff = time.Now().Add(-s.cfg.QueryBackendAfter)
)
// if interval is 0 then the backend requests code will loop forever. technically if we are here with a 0 interval it should mean a bad request
// b/c it was specified using a query hint. we're going to assume that and return 400
if interval == 0 {
return pipeline.NewBadRequest(errors.New("invalid interval specified: 0")), nil
}
generatorReq := s.generatorRequest(*req, r, tenantID, cutoff)
reqCh := make(chan pipeline.Request, 2) // buffer of 2 allows us to insert generatorReq and metrics
if generatorReq != nil {
reqCh <- pipeline.NewHTTPRequest(generatorReq)
}
var (
totalJobs, totalBlocks uint32
totalBlockBytes uint64
)
if s.cfg.RF1ReadPath {
totalJobs, totalBlocks, totalBlockBytes = s.backendRequests(ctx, tenantID, r, *req, cutoff, samplingRate, targetBytesPerRequest, interval, reqCh)
} else {
totalJobs, totalBlocks, totalBlockBytes = s.shardedBackendRequests(ctx, tenantID, r, *req, cutoff, samplingRate, targetBytesPerRequest, interval, reqCh, nil)
}
span.SetTag("totalJobs", totalJobs)
span.SetTag("totalBlocks", totalBlocks)
span.SetTag("totalBlockBytes", totalBlockBytes)
// send a job to communicate the search metrics. this is consumed by the combiner to calculate totalblocks/bytes/jobs
var jobMetricsResponse pipeline.Responses[combiner.PipelineResponse]
if totalBlocks > 0 {
resp := &tempopb.QueryRangeResponse{
Metrics: &tempopb.SearchMetrics{
TotalJobs: totalJobs,
TotalBlocks: totalBlocks,
TotalBlockBytes: totalBlockBytes,
},
}
m := jsonpb.Marshaler{}
body, err := m.MarshalToString(resp)
if err != nil {
return nil, fmt.Errorf("failed to marshal search metrics: %w", err)
}
jobMetricsResponse = pipeline.NewSuccessfulResponse(body)
}
return pipeline.NewAsyncSharderChan(ctx, s.cfg.ConcurrentRequests, reqCh, jobMetricsResponse, s.next), nil
}
// blockMetas returns all relevant blockMetas given a start/end
func (s *queryRangeSharder) blockMetas(start, end int64, tenantID string) []*backend.BlockMeta {
// reduce metas to those in the requested range
allMetas := s.reader.BlockMetas(tenantID)
metas := make([]*backend.BlockMeta, 0, len(allMetas)/50) // divide by 50 for luck
for _, m := range allMetas {
if m.StartTime.UnixNano() <= end &&
m.EndTime.UnixNano() >= start &&
m.ReplicationFactor == s.replicationFactor {
metas = append(metas, m)
}
}
return metas
}
func (s *queryRangeSharder) shardedBackendRequests(ctx context.Context, tenantID string, parent *http.Request, searchReq tempopb.QueryRangeRequest, cutoff time.Time, samplingRate float64, targetBytesPerRequest int, interval time.Duration, reqCh chan pipeline.Request, _ func(error)) (totalJobs, totalBlocks uint32, totalBlockBytes uint64) {
// request without start or end, search only in generator
if searchReq.Start == 0 || searchReq.End == 0 {
close(reqCh)
return
}
// Make a copy and limit to backend time range.
backendReq := searchReq
traceql.TrimToBefore(&backendReq, cutoff)
// If empty window then no need to search backend
if backendReq.Start == backendReq.End {
close(reqCh)
return
}
// Blocks within overall time range. This is just for instrumentation, more precise time
// range is checked for each window.
blocks := s.blockMetas(int64(backendReq.Start), int64(backendReq.End), tenantID)
if len(blocks) == 0 {
// no need to search backend
close(reqCh)
return
}
// count blocks
totalBlocks = uint32(len(blocks))
for _, b := range blocks {
totalBlockBytes += b.Size
}
// count jobs. same loops as below
var (
start = backendReq.Start
end = backendReq.End
timeWindowSize = uint64(interval.Nanoseconds())
)
for start < end {
thisStart := start
thisEnd := start + timeWindowSize
if thisEnd > end {
thisEnd = end
}
blocks := s.blockMetas(int64(thisStart), int64(thisEnd), tenantID)
if len(blocks) == 0 {
start = thisEnd
continue
}
totalBlockSize := uint64(0)
for _, b := range blocks {
totalBlockSize += b.Size
}
shards := uint32(math.Ceil(float64(totalBlockSize) / float64(targetBytesPerRequest)))
for i := uint32(1); i <= shards; i++ {
totalJobs++
}
start = thisEnd
}
go func() {
s.buildShardedBackendRequests(ctx, tenantID, parent, backendReq, samplingRate, targetBytesPerRequest, interval, reqCh)
}()
return
}
func (s *queryRangeSharder) buildShardedBackendRequests(ctx context.Context, tenantID string, parent *http.Request, searchReq tempopb.QueryRangeRequest, samplingRate float64, targetBytesPerRequest int, interval time.Duration, reqCh chan pipeline.Request) {
defer close(reqCh)
var (
start = searchReq.Start
end = searchReq.End
timeWindowSize = uint64(interval.Nanoseconds())
)
for start < end {
thisStart := start
thisEnd := start + timeWindowSize
if thisEnd > end {
thisEnd = end
}
blocks := s.blockMetas(int64(thisStart), int64(thisEnd), tenantID)
if len(blocks) == 0 {
start = thisEnd
continue
}
totalBlockSize := uint64(0)
for _, b := range blocks {
totalBlockSize += b.Size
}
shards := uint32(math.Ceil(float64(totalBlockSize) / float64(targetBytesPerRequest)))
for i := uint32(1); i <= shards; i++ {
shardR := searchReq
shardR.Start = thisStart
shardR.End = thisEnd
shardR.ShardID = i
shardR.ShardCount = shards
httpReq := s.toUpstreamRequest(ctx, shardR, parent, tenantID)
pipelineR := pipeline.NewHTTPRequest(httpReq)
if samplingRate != 1.0 {
shardR.ShardID *= uint32(1.0 / samplingRate)
shardR.ShardCount *= uint32(1.0 / samplingRate)
// Set final sampling rate after integer rounding
samplingRate = float64(shards) / float64(shardR.ShardCount)
pipelineR.SetResponseData(samplingRate)
}
select {
case reqCh <- pipelineR:
case <-ctx.Done():
return
}
}
start = thisEnd
}
}
func (s *queryRangeSharder) backendRequests(ctx context.Context, tenantID string, parent *http.Request, searchReq tempopb.QueryRangeRequest, cutoff time.Time, _ float64, targetBytesPerRequest int, _ time.Duration, reqCh chan pipeline.Request) (totalJobs, totalBlocks uint32, totalBlockBytes uint64) {
// request without start or end, search only in generator
if searchReq.Start == 0 || searchReq.End == 0 {
close(reqCh)
return
}
// Make a copy and limit to backend time range.
// Preserve instant nature of request if needed
backendReq := searchReq
traceql.TrimToBefore(&backendReq, cutoff)
// If empty window then no need to search backend
if backendReq.Start == backendReq.End {
close(reqCh)
return
}
// Blocks within overall time range. This is just for instrumentation, more precise time
// range is checked for each window.
blocks := s.blockMetas(int64(backendReq.Start), int64(backendReq.End), tenantID)
if len(blocks) == 0 {
// no need to search backend
close(reqCh)
return
}
// calculate metrics to return to the caller
totalBlocks = uint32(len(blocks))
for _, b := range blocks {
p := pagesPerRequest(b, targetBytesPerRequest)
totalJobs += b.TotalRecords / uint32(p)
if int(b.TotalRecords)%p != 0 {
totalJobs++
}
totalBlockBytes += b.Size
}
go func() {
s.buildBackendRequests(ctx, tenantID, parent, backendReq, blocks, targetBytesPerRequest, reqCh)
}()
return
}
func (s *queryRangeSharder) buildBackendRequests(ctx context.Context, tenantID string, parent *http.Request, searchReq tempopb.QueryRangeRequest, metas []*backend.BlockMeta, targetBytesPerRequest int, reqCh chan<- pipeline.Request) {
defer close(reqCh)
queryHash := hashForQueryRangeRequest(&searchReq)
for _, m := range metas {
if m.EndTime.Before(m.StartTime) {
// Ignore blocks with bad timings from debugging
continue
}
pages := pagesPerRequest(m, targetBytesPerRequest)
if pages == 0 {
continue
}
for startPage := 0; startPage < int(m.TotalRecords); startPage += pages {
subR := parent.Clone(ctx)
dc, err := m.DedicatedColumns.ToTempopb()
if err != nil {
// errFn(fmt.Errorf("failed to convert dedicated columns. block: %s tempopb: %w", blockID, err))
continue
}
// Trim and align the request for this block. I.e. if the request is "Last Hour" we don't want to
// cache the response for that, we want only the few minutes time range for this block. This has
// size savings but the main thing is that the response is reuseable for any overlapping query.
start, end, step := traceql.TrimToOverlap(searchReq.Start, searchReq.End, searchReq.Step, uint64(m.StartTime.UnixNano()), uint64(m.EndTime.UnixNano()))
if start == end || step == 0 {
level.Warn(s.logger).Log("msg", "invalid start/step end. skipping", "start", start, "end", end, "step", step, "blockStart", m.StartTime.UnixNano(), "blockEnd", m.EndTime.UnixNano())
continue
}
queryRangeReq := &tempopb.QueryRangeRequest{
Query: searchReq.Query,
Start: start,
End: end,
Step: step,
// ShardID: uint32, // No sharding with RF=1
// ShardCount: uint32, // No sharding with RF=1
QueryMode: searchReq.QueryMode,
// New RF1 fields
BlockID: m.BlockID.String(),
StartPage: uint32(startPage),
PagesToSearch: uint32(pages),
Version: m.Version,
Encoding: m.Encoding.String(),
Size_: m.Size,
FooterSize: m.FooterSize,
DedicatedColumns: dc,
}
subR = api.BuildQueryRangeRequest(subR, queryRangeReq)
subR.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
prepareRequestForQueriers(subR, tenantID)
pipelineR := pipeline.NewHTTPRequest(subR)
// TODO: Handle sampling rate
key := queryRangeCacheKey(tenantID, queryHash, int64(queryRangeReq.Start), int64(queryRangeReq.End), m, int(queryRangeReq.StartPage), int(queryRangeReq.PagesToSearch))
if len(key) > 0 {
pipelineR.SetCacheKey(key)
}
select {
case reqCh <- pipelineR:
case <-ctx.Done():
return
}
}
}
}
func (s *queryRangeSharder) generatorRequest(searchReq tempopb.QueryRangeRequest, parent *http.Request, tenantID string, cutoff time.Time) *http.Request {
traceql.TrimToAfter(&searchReq, cutoff)
// if start == end then we don't need to query it
if searchReq.Start == searchReq.End {
return nil
}
searchReq.QueryMode = querier.QueryModeRecent
req := s.toUpstreamRequest(parent.Context(), searchReq, parent, tenantID)
req.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
return req
}
func (s *queryRangeSharder) toUpstreamRequest(ctx context.Context, req tempopb.QueryRangeRequest, parent *http.Request, tenantID string) *http.Request {
subR := parent.Clone(ctx)
subR = api.BuildQueryRangeRequest(subR, &req)
prepareRequestForQueriers(subR, tenantID)
return subR
}
// maxDuration returns the max search duration allowed for this tenant.
func (s *queryRangeSharder) maxDuration(tenantID string) time.Duration {
// check overrides first, if no overrides then grab from our config
maxDuration := s.overrides.MaxMetricsDuration(tenantID)
if maxDuration != 0 {
return maxDuration
}
return s.cfg.MaxDuration
}
func (s *queryRangeSharder) samplingRate(expr *traceql.RootExpr, allowUnsafe bool) float64 {
samplingRate := 1.0
if v, ok := expr.Hints.GetFloat(traceql.HintSample, allowUnsafe); ok {
if v > 0 && v < 1.0 {
samplingRate = v
}
}
return samplingRate
}
func (s *queryRangeSharder) jobSize(expr *traceql.RootExpr, samplingRate float64, allowUnsafe bool) int {
// If we have a query hint then use it
if v, ok := expr.Hints.GetInt(traceql.HintJobSize, allowUnsafe); ok && v > 0 {
return v
}
// Else use configured value.
size := s.cfg.TargetBytesPerRequest
// Automatically scale job size when sampling less than 100%
// This improves performance.
if samplingRate < 1.0 {
factor := 1.0 / samplingRate
// Keep it within reason
if factor > 10.0 {
factor = 10.0
}
size = int(float64(size) * factor)
}
return size
}
func (s *queryRangeSharder) jobInterval(expr *traceql.RootExpr, allowUnsafe bool) time.Duration {
// If we have a query hint then use it
if v, ok := expr.Hints.GetDuration(traceql.HintJobInterval, allowUnsafe); ok && v > 0 {
return v
}
// Else use configured value
return s.cfg.Interval
}
func hashForQueryRangeRequest(req *tempopb.QueryRangeRequest) uint64 {
if req.Query == "" {
return 0
}
ast, err := traceql.Parse(req.Query)
if err != nil { // this should never occur. if we've made this far we've already validated the query can parse. however, for sanity, just fail to cache if we can't parse
return 0
}
// forces the query into a canonical form
query := ast.String()
// add the query, limit and spss to the hash
hash := fnv1a.HashString64(query)
hash = fnv1a.AddUint64(hash, req.Step)
return hash
}