forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshardmapper.go
657 lines (577 loc) · 19.8 KB
/
shardmapper.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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package logql
import (
"fmt"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/loki/v3/pkg/logql/syntax"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/index"
util_log "github.com/grafana/loki/v3/pkg/util/log"
)
const (
ShardLastOverTime = "last_over_time"
ShardFirstOverTime = "first_over_time"
ShardQuantileOverTime = "quantile_over_time"
SupportApproxTopk = "approx_topk"
)
type ShardMapper struct {
shards ShardingStrategy
metrics *MapperMetrics
quantileOverTimeSharding bool
lastOverTimeSharding bool
firstOverTimeSharding bool
approxTopkSupport bool
}
func NewShardMapper(strategy ShardingStrategy, metrics *MapperMetrics, shardAggregation []string) ShardMapper {
mapper := ShardMapper{
shards: strategy,
metrics: metrics,
quantileOverTimeSharding: false,
lastOverTimeSharding: false,
firstOverTimeSharding: false,
approxTopkSupport: false,
}
for _, a := range shardAggregation {
switch a {
case ShardQuantileOverTime:
mapper.quantileOverTimeSharding = true
case ShardLastOverTime:
mapper.lastOverTimeSharding = true
case ShardFirstOverTime:
mapper.firstOverTimeSharding = true
case SupportApproxTopk:
mapper.approxTopkSupport = true
}
}
return mapper
}
func NewShardMapperMetrics(registerer prometheus.Registerer) *MapperMetrics {
return newMapperMetrics(registerer, "shard")
}
func (m ShardMapper) Parse(parsed syntax.Expr) (noop bool, bytesPerShard uint64, expr syntax.Expr, err error) {
recorder := m.metrics.downstreamRecorder()
mapped, bytesPerShard, err := m.Map(parsed, recorder, true)
if err != nil {
m.metrics.ParsedQueries.WithLabelValues(FailureKey).Inc()
return false, 0, nil, err
}
noop = isNoOp(parsed, mapped)
if noop {
m.metrics.ParsedQueries.WithLabelValues(NoopKey).Inc()
} else {
m.metrics.ParsedQueries.WithLabelValues(SuccessKey).Inc()
}
recorder.Finish() // only record metrics for successful mappings
return noop, bytesPerShard, mapped, err
}
func (m ShardMapper) Map(expr syntax.Expr, r *downstreamRecorder, topLevel bool) (syntax.Expr, uint64, error) {
// immediately clone the passed expr to avoid mutating the original
expr, err := syntax.Clone(expr)
if err != nil {
return nil, 0, err
}
switch e := expr.(type) {
case *syntax.LiteralExpr:
return e, 0, nil
case *syntax.VectorExpr:
return e, 0, nil
case *syntax.MatchersExpr, *syntax.PipelineExpr:
return m.mapLogSelectorExpr(e.(syntax.LogSelectorExpr), r)
case *syntax.VectorAggregationExpr:
return m.mapVectorAggregationExpr(e, r, topLevel)
case *syntax.LabelReplaceExpr:
return m.mapLabelReplaceExpr(e, r, topLevel)
case *syntax.RangeAggregationExpr:
return m.mapRangeAggregationExpr(e, r, topLevel)
case *syntax.BinOpExpr:
return m.mapBinOpExpr(e, r, topLevel)
default:
return nil, 0, errors.Errorf("unexpected expr type (%T) for ASTMapper type (%T) ", expr, m)
}
}
func (m ShardMapper) mapBinOpExpr(e *syntax.BinOpExpr, r *downstreamRecorder, topLevel bool) (*syntax.BinOpExpr, uint64, error) {
// In a BinOp expression both sides need to be either executed locally or wrapped
// into a downstream expression to be executed on the querier, since the default
// evaluator on the query frontend cannot select logs or samples.
// However, it can evaluate literals and vectors.
// check if LHS is shardable by mapping the tree
// only wrap in downstream expression if the mapping is a no-op and the
// expression is a vector or literal
lhsMapped, lhsBytesPerShard, err := m.Map(e.SampleExpr, r, topLevel)
if err != nil {
return nil, 0, err
}
if isNoOp(e.SampleExpr, lhsMapped) && !isLiteralOrVector(lhsMapped) {
lhsMapped = DownstreamSampleExpr{
shard: nil,
SampleExpr: e.SampleExpr,
}
}
// check if RHS is shardable by mapping the tree
// only wrap in downstream expression if the mapping is a no-op and the
// expression is a vector or literal
rhsMapped, rhsBytesPerShard, err := m.Map(e.RHS, r, topLevel)
if err != nil {
return nil, 0, err
}
if isNoOp(e.RHS, rhsMapped) && !isLiteralOrVector(rhsMapped) {
// TODO: check if literal or vector
rhsMapped = DownstreamSampleExpr{
shard: nil,
SampleExpr: e.RHS,
}
}
lhsSampleExpr, ok := lhsMapped.(syntax.SampleExpr)
if !ok {
return nil, 0, badASTMapping(lhsMapped)
}
rhsSampleExpr, ok := rhsMapped.(syntax.SampleExpr)
if !ok {
return nil, 0, badASTMapping(rhsMapped)
}
e.SampleExpr = lhsSampleExpr
e.RHS = rhsSampleExpr
// We take the maximum bytes per shard of both sides of the operation
bytesPerShard := uint64(max(int(lhsBytesPerShard), int(rhsBytesPerShard)))
return e, bytesPerShard, nil
}
func (m ShardMapper) mapLogSelectorExpr(expr syntax.LogSelectorExpr, r *downstreamRecorder) (syntax.LogSelectorExpr, uint64, error) {
var head *ConcatLogSelectorExpr
shards, maxBytesPerShard, err := m.shards.Shards(expr)
if err != nil {
return nil, 0, err
}
if len(shards) == 0 {
return &ConcatLogSelectorExpr{
DownstreamLogSelectorExpr: DownstreamLogSelectorExpr{
shard: nil,
LogSelectorExpr: expr,
},
}, maxBytesPerShard, nil
}
for i := len(shards) - 1; i >= 0; i-- {
head = &ConcatLogSelectorExpr{
DownstreamLogSelectorExpr: DownstreamLogSelectorExpr{
shard: &shards[i],
LogSelectorExpr: expr,
},
next: head,
}
}
r.Add(len(shards), StreamsKey)
return head, maxBytesPerShard, nil
}
func (m ShardMapper) mapSampleExpr(expr syntax.SampleExpr, r *downstreamRecorder) (syntax.SampleExpr, uint64, error) {
var head *ConcatSampleExpr
shards, maxBytesPerShard, err := m.shards.Shards(expr)
if err != nil {
return nil, 0, err
}
if len(shards) == 0 {
return &ConcatSampleExpr{
DownstreamSampleExpr: DownstreamSampleExpr{
shard: nil,
SampleExpr: expr,
},
}, maxBytesPerShard, nil
}
for i := len(shards) - 1; i >= 0; i-- {
head = &ConcatSampleExpr{
DownstreamSampleExpr: DownstreamSampleExpr{
shard: &shards[i],
SampleExpr: expr,
},
next: head,
}
}
r.Add(len(shards), MetricsKey)
return head, maxBytesPerShard, nil
}
// turn a vector aggr into a wrapped+sharded variant,
// used as a subroutine in mapping
func (m ShardMapper) wrappedShardedVectorAggr(expr *syntax.VectorAggregationExpr, r *downstreamRecorder) (*syntax.VectorAggregationExpr, uint64, error) {
sharded, bytesPerShard, err := m.mapSampleExpr(expr, r)
if err != nil {
return nil, 0, err
}
return &syntax.VectorAggregationExpr{
Left: sharded,
Grouping: expr.Grouping,
Params: expr.Params,
Operation: expr.Operation,
}, bytesPerShard, nil
}
// technically, std{dev,var} are also parallelizable if there is no cross-shard merging
// in descendent nodes in the AST. This optimization is currently avoided for simplicity.
func (m ShardMapper) mapVectorAggregationExpr(expr *syntax.VectorAggregationExpr, r *downstreamRecorder, topLevel bool) (syntax.SampleExpr, uint64, error) {
if expr.Shardable(topLevel) {
switch expr.Operation {
case syntax.OpTypeSum:
// sum(x) -> sum(sum(x, shard=1) ++ sum(x, shard=2)...)
return m.wrappedShardedVectorAggr(expr, r)
case syntax.OpTypeMin, syntax.OpTypeMax:
if syntax.ReducesLabels(expr.Left) {
// skip sharding optimizations at this level. If labels are reduced,
// the same series may exist on multiple shards and must be aggregated
// together before a max|min is applied
break
}
// max(x) -> max(max(x, shard=1) ++ max(x, shard=2)...)
// min(x) -> min(min(x, shard=1) ++ min(x, shard=2)...)
return m.wrappedShardedVectorAggr(expr, r)
case syntax.OpTypeAvg:
// avg(x) -> sum(x)/count(x), which is parallelizable
binOp := &syntax.BinOpExpr{
SampleExpr: &syntax.VectorAggregationExpr{
Left: expr.Left,
Grouping: expr.Grouping,
Operation: syntax.OpTypeSum,
},
RHS: &syntax.VectorAggregationExpr{
Left: expr.Left,
Grouping: expr.Grouping,
Operation: syntax.OpTypeCount,
},
Op: syntax.OpTypeDiv,
}
return m.mapBinOpExpr(binOp, r, topLevel)
case syntax.OpTypeCount:
if syntax.ReducesLabels(expr.Left) {
// skip sharding optimizations at this level. If labels are reduced,
// the same series may exist on multiple shards and must be aggregated
// together before a count is applied
break
}
// count(x) -> sum(count(x, shard=1) ++ count(x, shard=2)...)
sharded, bytesPerShard, err := m.mapSampleExpr(expr, r)
if err != nil {
return nil, 0, err
}
return &syntax.VectorAggregationExpr{
Left: sharded,
Grouping: expr.Grouping,
Operation: syntax.OpTypeSum,
}, bytesPerShard, nil
case syntax.OpTypeApproxTopK:
if !m.approxTopkSupport {
return nil, 0, fmt.Errorf("approx_topk is not enabled. See -limits.shard_aggregations")
}
// TODO(owen-d): integrate bounded sharding with approx_topk
// I'm not doing this now because it uses a separate code path and may not handle
// bounded shards in the same way
shards, bytesPerShard, err := m.shards.Resolver().Shards(expr)
if err != nil {
return nil, 0, err
}
// approx_topk(k, inner) ->
// topk(
// k,
// eval_cms(
// __count_min_sketch__(inner, shard=1) ++ __count_min_sketch__(inner, shard=2)...
// )
// )
countMinSketchExpr := syntax.MustClone(expr)
countMinSketchExpr.Operation = syntax.OpTypeCountMinSketch
countMinSketchExpr.Params = 0
// Even if this query is not sharded the user wants an approximation. This is helpful if some
// inferred label has a very high cardinality. Note that the querier does not support CountMinSketchEvalExpr
// which is why it's evaluated on the front end.
if shards == 0 {
return &syntax.VectorAggregationExpr{
Left: &CountMinSketchEvalExpr{
downstreams: []DownstreamSampleExpr{{
SampleExpr: countMinSketchExpr,
}},
},
Grouping: expr.Grouping,
Operation: syntax.OpTypeTopK,
Params: expr.Params,
}, bytesPerShard, nil
}
downstreams := make([]DownstreamSampleExpr, 0, shards)
for shard := 0; shard < shards; shard++ {
s := NewPowerOfTwoShard(index.ShardAnnotation{
Shard: uint32(shard),
Of: uint32(shards),
})
downstreams = append(downstreams, DownstreamSampleExpr{
shard: &ShardWithChunkRefs{
Shard: s,
},
SampleExpr: countMinSketchExpr,
})
}
sharded := &CountMinSketchEvalExpr{
downstreams: downstreams,
}
return &syntax.VectorAggregationExpr{
Left: sharded,
Grouping: expr.Grouping,
Operation: syntax.OpTypeTopK,
Params: expr.Params,
}, bytesPerShard, nil
default:
// this should not be reachable. If an operation is shardable it should
// have an optimization listed. Nonetheless, we log this as a warning
// and return the original expression unsharded.
level.Warn(util_log.Logger).Log(
"msg", "unexpected operation which appears shardable, ignoring",
"operation", expr.Operation,
)
exprStats, err := m.shards.Resolver().GetStats(expr)
if err != nil {
return nil, 0, err
}
return expr, exprStats.Bytes, nil
}
}
// if this AST contains unshardable operations, don't shard this at this level,
// but attempt to shard a child node.
subMapped, bytesPerShard, err := m.Map(expr.Left, r, false)
if err != nil {
return nil, 0, err
}
sampleExpr, ok := subMapped.(syntax.SampleExpr)
if !ok {
return nil, 0, badASTMapping(subMapped)
}
return &syntax.VectorAggregationExpr{
Left: sampleExpr,
Grouping: expr.Grouping,
Params: expr.Params,
Operation: expr.Operation,
}, bytesPerShard, nil
}
func (m ShardMapper) mapLabelReplaceExpr(expr *syntax.LabelReplaceExpr, r *downstreamRecorder, topLevel bool) (syntax.SampleExpr, uint64, error) {
subMapped, bytesPerShard, err := m.Map(expr.Left, r, topLevel)
if err != nil {
return nil, 0, err
}
cpy := *expr
cpy.Left = subMapped.(syntax.SampleExpr)
return &cpy, bytesPerShard, nil
}
// These functions require a different merge strategy than the default
// concatenation.
// This is because the same label sets may exist on multiple shards when label-reducing parsing is applied or when
// grouping by some subset of the labels. In this case, the resulting vector may have multiple values for the same
// series and we need to combine them appropriately given a particular operation.
var rangeMergeMap = map[string]string{
// all these may be summed
syntax.OpRangeTypeCount: syntax.OpTypeSum,
syntax.OpRangeTypeRate: syntax.OpTypeSum,
syntax.OpRangeTypeBytes: syntax.OpTypeSum,
syntax.OpRangeTypeBytesRate: syntax.OpTypeSum,
syntax.OpRangeTypeSum: syntax.OpTypeSum,
// min & max require taking the min|max of the shards
syntax.OpRangeTypeMin: syntax.OpTypeMin,
syntax.OpRangeTypeMax: syntax.OpTypeMax,
}
func (m ShardMapper) mapRangeAggregationExpr(expr *syntax.RangeAggregationExpr, r *downstreamRecorder, topLevel bool) (syntax.SampleExpr, uint64, error) {
if !expr.Shardable(topLevel) {
return noOp(expr, m.shards.Resolver())
}
switch expr.Operation {
case syntax.OpRangeTypeCount, syntax.OpRangeTypeRate, syntax.OpRangeTypeBytes, syntax.OpRangeTypeBytesRate, syntax.OpRangeTypeSum, syntax.OpRangeTypeMax, syntax.OpRangeTypeMin:
// if the expr can reduce labels, it can cause the same labelset to
// exist on separate shards and we'll need to merge the results
// accordingly. If it does not reduce labels and has no special grouping
// aggregation, we can shard it as normal via concatenation.
potentialConflict := syntax.ReducesLabels(expr)
if !potentialConflict && (expr.Grouping == nil || expr.Grouping.Noop()) {
return m.mapSampleExpr(expr, r)
}
// range aggregation groupings default to `without ()` behavior
// so we explicitly set the wrapping vector aggregation to this
// for parity when it's not explicitly set
grouping := expr.Grouping
if grouping == nil {
grouping = &syntax.Grouping{Without: true}
}
mapped, bytes, err := m.mapSampleExpr(expr, r)
// max_over_time(_) -> max without() (max_over_time(_) ++ max_over_time(_)...)
// max_over_time(_) by (foo) -> max by (foo) (max_over_time(_) by (foo) ++ max_over_time(_) by (foo)...)
merger, ok := rangeMergeMap[expr.Operation]
if !ok {
return nil, 0, fmt.Errorf(
"error while finding merge operation for %s", expr.Operation,
)
}
return &syntax.VectorAggregationExpr{
Left: mapped,
Grouping: grouping,
Operation: merger,
}, bytes, err
case syntax.OpRangeTypeAvg:
potentialConflict := syntax.ReducesLabels(expr)
if !potentialConflict && (expr.Grouping == nil || expr.Grouping.Noop()) {
return m.mapSampleExpr(expr, r)
}
grouping := expr.Grouping
if grouping == nil {
grouping = &syntax.Grouping{Without: true}
}
// avg_over_time() by (foo) -> sum by (foo) (sum_over_time()) / sum by (foo) (count_over_time())
lhs, lhsBytesPerShard, err := m.mapVectorAggregationExpr(&syntax.VectorAggregationExpr{
Left: &syntax.RangeAggregationExpr{
Left: expr.Left,
Operation: syntax.OpRangeTypeSum,
},
Grouping: grouping,
Operation: syntax.OpTypeSum,
}, r, false)
if err != nil {
return nil, 0, err
}
// Strip unwrap from log range
countOverTimeSelector, err := expr.Left.WithoutUnwrap()
if err != nil {
return nil, 0, err
}
// labelSampleExtractor includes the unwrap identifier in without() list if no grouping is specified
// similar change is required for the RHS here to ensure the resulting label sets match
rhsGrouping := *grouping
if rhsGrouping.Without {
if expr.Left.Unwrap != nil {
rhsGrouping.Groups = append(rhsGrouping.Groups, expr.Left.Unwrap.Identifier)
}
}
rhs, rhsBytesPerShard, err := m.mapVectorAggregationExpr(&syntax.VectorAggregationExpr{
Left: &syntax.RangeAggregationExpr{
Left: countOverTimeSelector,
Operation: syntax.OpRangeTypeCount,
},
Grouping: &rhsGrouping,
Operation: syntax.OpTypeSum,
}, r, false)
if err != nil {
return nil, 0, err
}
// We take the maximum bytes per shard of both sides of the operation
bytesPerShard := uint64(max(int(lhsBytesPerShard), int(rhsBytesPerShard)))
return &syntax.BinOpExpr{
SampleExpr: lhs,
RHS: rhs,
Op: syntax.OpTypeDiv,
}, bytesPerShard, nil
case syntax.OpRangeTypeQuantile:
if !m.quantileOverTimeSharding {
return noOp(expr, m.shards.Resolver())
}
potentialConflict := syntax.ReducesLabels(expr)
if !potentialConflict && (expr.Grouping == nil || expr.Grouping.Noop()) {
return m.mapSampleExpr(expr, r)
}
// TODO(owen-d): integrate bounded sharding with quantile over time
// I'm not doing this now because it uses a separate code path and may not handle
// bounded shards in the same way
shards, bytesPerShard, err := m.shards.Resolver().Shards(expr)
if err != nil {
return nil, 0, err
}
if shards == 0 {
return noOp(expr, m.shards.Resolver())
}
// quantile_over_time() by (foo) ->
// quantile_sketch_eval(quantile_merge by (foo)
// (__quantile_sketch_over_time__() by (foo)))
downstreams := make([]DownstreamSampleExpr, 0, shards)
expr.Operation = syntax.OpRangeTypeQuantileSketch
for shard := shards - 1; shard >= 0; shard-- {
s := NewPowerOfTwoShard(index.ShardAnnotation{
Shard: uint32(shard),
Of: uint32(shards),
})
downstreams = append(downstreams, DownstreamSampleExpr{
shard: &ShardWithChunkRefs{
Shard: s,
},
SampleExpr: expr,
})
}
return &QuantileSketchEvalExpr{
quantileMergeExpr: &QuantileSketchMergeExpr{
downstreams: downstreams,
},
quantile: expr.Params,
}, bytesPerShard, nil
case syntax.OpRangeTypeFirst:
if !m.firstOverTimeSharding {
return noOp(expr, m.shards.Resolver())
}
potentialConflict := syntax.ReducesLabels(expr)
if !potentialConflict && (expr.Grouping == nil || expr.Grouping.Noop()) {
return m.mapSampleExpr(expr, r)
}
shards, bytesPerShard, err := m.shards.Shards(expr)
if err != nil {
return nil, 0, err
}
if len(shards) == 0 {
return noOp(expr, m.shards.Resolver())
}
downstreams := make([]DownstreamSampleExpr, 0, len(shards))
// This is the magic. We send a custom operation
expr.Operation = syntax.OpRangeTypeFirstWithTimestamp
for i := len(shards) - 1; i >= 0; i-- {
downstreams = append(downstreams, DownstreamSampleExpr{
shard: &shards[i],
SampleExpr: expr,
})
}
return &MergeFirstOverTimeExpr{
downstreams: downstreams,
}, bytesPerShard, nil
case syntax.OpRangeTypeLast:
if !m.lastOverTimeSharding {
return noOp(expr, m.shards.Resolver())
}
potentialConflict := syntax.ReducesLabels(expr)
if !potentialConflict && (expr.Grouping == nil || expr.Grouping.Noop()) {
return m.mapSampleExpr(expr, r)
}
shards, bytesPerShard, err := m.shards.Shards(expr)
if err != nil {
return nil, 0, err
}
if len(shards) == 0 {
return noOp(expr, m.shards.Resolver())
}
downstreams := make([]DownstreamSampleExpr, 0, len(shards))
expr.Operation = syntax.OpRangeTypeLastWithTimestamp
for i := len(shards) - 1; i >= 0; i-- {
downstreams = append(downstreams, DownstreamSampleExpr{
shard: &shards[i],
SampleExpr: expr,
})
}
return &MergeLastOverTimeExpr{
downstreams: downstreams,
}, bytesPerShard, nil
default:
// don't shard if there's not an appropriate optimization
return noOp(expr, m.shards.Resolver())
}
}
func noOp[E syntax.Expr](expr E, shards ShardResolver) (E, uint64, error) {
exprStats, err := shards.GetStats(expr)
if err != nil {
var empty E
return empty, 0, err
}
return expr, exprStats.Bytes, nil
}
func isNoOp(left syntax.Expr, right syntax.Expr) bool {
return left.String() == right.String()
}
func isLiteralOrVector(e syntax.Expr) bool {
switch e.(type) {
case *syntax.VectorExpr, *syntax.LiteralExpr:
return true
default:
return false
}
}
func badASTMapping(got syntax.Expr) error {
return fmt.Errorf("bad AST mapping: expected SampleExpr, but got (%T)", got)
}