17
17
package execution
18
18
19
19
import (
20
- "runtime"
21
20
"sort"
22
21
"time"
23
22
@@ -94,6 +93,8 @@ func newOperator(expr parser.Expr, storage *engstore.SelectorPool, opts *query.O
94
93
return exchange .NewDuplicateLabelCheck (op , opts ), nil
95
94
case * parser.StepInvariantExpr :
96
95
return newStepInvariantExpression (e , storage , opts , hints )
96
+ case logicalplan.Coalesce :
97
+ return newCoalesce (e , storage , opts , hints )
97
98
case logicalplan.Deduplicate :
98
99
return newDeduplication (e , storage , opts , hints )
99
100
case logicalplan.RemoteExecution :
@@ -117,19 +118,10 @@ func newVectorSelector(e *logicalplan.VectorSelector, storage *engstore.Selector
117
118
selector := storage .GetFilteredSelector (start , end , opts .Step .Milliseconds (), e .LabelMatchers , e .Filters , hints )
118
119
selectTimestamp := e .SelectTimestamp
119
120
120
- numShards := runtime .GOMAXPROCS (0 ) / 2
121
- if numShards < 1 {
122
- numShards = 1
123
- }
124
-
125
- operators := make ([]model.VectorOperator , 0 , numShards )
126
- for i := 0 ; i < numShards ; i ++ {
127
- operator := scan .NewVectorSelector (
128
- model .NewVectorPool (opts .StepsBatch ), selector , opts , offset , hints , batchsize , selectTimestamp , i , numShards )
129
- operators = append (operators , exchange .NewConcurrent (operator , 2 , opts ))
130
- }
121
+ shard := e .Shard
122
+ numShards := e .NumShards
131
123
132
- return exchange . NewCoalesce (model .NewVectorPool (opts .StepsBatch ), opts , batchsize * int64 ( numShards ), operators ... )
124
+ return scan . NewVectorSelector (model .NewVectorPool (opts .StepsBatch ), selector , opts , offset , hints , batchsize , selectTimestamp , shard , numShards )
133
125
}
134
126
135
127
func newCall (e * parser.Call , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
@@ -148,6 +140,8 @@ func newCall(e *parser.Call, storage *engstore.SelectorPool, opts *query.Options
148
140
return newSubqueryFunction (e , t , storage , opts , hints )
149
141
case * logicalplan.MatrixSelector :
150
142
return newRangeVectorFunction (e , t , storage , opts , hints )
143
+ case logicalplan.Coalesce :
144
+ return newRangeVectorFunction (e , t , storage , opts , hints )
151
145
}
152
146
}
153
147
return newInstantVectorFunction (e , storage , opts , hints )
@@ -193,7 +187,7 @@ func newAbsentOverTimeOperator(call *parser.Call, storage *engstore.SelectorPool
193
187
}
194
188
}
195
189
196
- func newRangeVectorFunction (e * parser.Call , t * logicalplan. MatrixSelector , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
190
+ func newRangeVectorFunction (e * parser.Call , t parser. Expr , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
197
191
// TODO(saswatamcode): Range vector result might need new operator
198
192
// before it can be non-nested. https://github.com/thanos-io/promql-engine/issues/39
199
193
batchSize , vs , filters , err := unpackVectorSelector (t )
@@ -212,10 +206,6 @@ func newRangeVectorFunction(e *parser.Call, t *logicalplan.MatrixSelector, stora
212
206
hints .Range = milliSecondRange
213
207
filter := storage .GetFilteredSelector (start , end , opts .Step .Milliseconds (), vs .LabelMatchers , filters , hints )
214
208
215
- numShards := runtime .GOMAXPROCS (0 ) / 2
216
- if numShards < 1 {
217
- numShards = 1
218
- }
219
209
var arg float64
220
210
if e .Func .Name == "quantile_over_time" {
221
211
constVal , err := unwrapConstVal (e .Args [0 ])
@@ -224,28 +214,21 @@ func newRangeVectorFunction(e *parser.Call, t *logicalplan.MatrixSelector, stora
224
214
}
225
215
arg = constVal
226
216
}
227
-
228
- operators := make ([]model.VectorOperator , 0 , numShards )
229
- for i := 0 ; i < numShards ; i ++ {
230
- operator , err := scan .NewMatrixSelector (
231
- model .NewVectorPool (opts .StepsBatch ),
232
- filter ,
233
- e .Func .Name ,
234
- arg ,
235
- opts ,
236
- t .Range ,
237
- vs .Offset ,
238
- batchSize ,
239
- i ,
240
- numShards ,
241
- )
242
- if err != nil {
243
- return nil , err
244
- }
245
- operators = append (operators , exchange .NewConcurrent (operator , 2 , opts ))
246
- }
247
-
248
- return exchange .NewCoalesce (model .NewVectorPool (opts .StepsBatch ), opts , batchSize * int64 (numShards ), operators ... ), nil
217
+ shard := t .Shard
218
+ numShards := t .NumShards
219
+
220
+ return scan .NewMatrixSelector (
221
+ model .NewVectorPool (opts .StepsBatch ),
222
+ filter ,
223
+ e .Func .Name ,
224
+ arg ,
225
+ opts ,
226
+ t .Range ,
227
+ vs .Offset ,
228
+ batchSize ,
229
+ shard ,
230
+ numShards ,
231
+ )
249
232
}
250
233
251
234
func newSubqueryFunction (e * parser.Call , t * parser.SubqueryExpr , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
@@ -394,6 +377,18 @@ func newStepInvariantExpression(e *parser.StepInvariantExpr, storage *engstore.S
394
377
return step_invariant .NewStepInvariantOperator (model .NewVectorPoolWithSize (opts .StepsBatch , 1 ), next , e .Expr , opts )
395
378
}
396
379
380
+ func newCoalesce (e logicalplan.Coalesce , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
381
+ operators := make ([]model.VectorOperator , len (e .Exprs ))
382
+ for i , expr := range e .Exprs {
383
+ operator , err := newOperator (expr , storage , opts , hints )
384
+ if err != nil {
385
+ return nil , err
386
+ }
387
+ operators [i ] = exchange .NewConcurrent (operator , 2 , opts )
388
+ }
389
+ return exchange .NewCoalesce (model .NewVectorPool (opts .StepsBatch ), opts , 0 , operators ... ), nil
390
+ }
391
+
397
392
func newDeduplication (e logicalplan.Deduplicate , storage * engstore.SelectorPool , opts * query.Options , hints storage.SelectHints ) (model.VectorOperator , error ) {
398
393
// The Deduplicate operator will deduplicate samples using a last-sample-wins strategy.
399
394
// Sorting engines by MaxT ensures that samples produced due to
@@ -411,6 +406,7 @@ func newDeduplication(e logicalplan.Deduplicate, storage *engstore.SelectorPool,
411
406
}
412
407
operators [i ] = operator
413
408
}
409
+ // We dont need to use logical coalesce here since it was already pushed back above remote evaluation here
414
410
coalesce := exchange .NewCoalesce (model .NewVectorPool (opts .StepsBatch ), opts , 0 , operators ... )
415
411
dedup := exchange .NewDedupOperator (model .NewVectorPool (opts .StepsBatch ), coalesce , opts )
416
412
return exchange .NewConcurrent (dedup , 2 , opts ), nil
0 commit comments