@@ -274,6 +274,9 @@ func pql(id uint32, c *ctx, optimizable bool,
274
274
}
275
275
}
276
276
277
+ maxDurationMs := getMaxDurationMs (rq .Statement ())
278
+ fromMs -= maxDurationMs
279
+
277
280
subsels := strings.Builder {}
278
281
subsels .WriteString ("{" )
279
282
if optimizable {
@@ -291,15 +294,18 @@ func pql(id uint32, c *ctx, optimizable bool,
291
294
if i != 0 {
292
295
subsels .WriteString ("," )
293
296
}
294
- subsels .WriteString (fmt .Sprintf (`"%s":"%s" ` , strconv .Quote (k ), strconv .Quote (v )))
297
+ subsels .WriteString (fmt .Sprintf (`%s:%s ` , strconv .Quote (k ), strconv .Quote (v )))
295
298
i ++
296
299
}
297
300
}
298
301
subsels .WriteString ("}" )
299
302
300
303
matchersJSON := getmatchersJSON (rq )
301
304
302
- c .response = []byte (fmt .Sprintf (`{"subqueries": %s, "matchers": %s}` , subsels .String (), matchersJSON ))
305
+ c .response = []byte (fmt .Sprintf (`{"subqueries": %s, "matchers": %s, "fromMs": %d}` ,
306
+ subsels .String (),
307
+ matchersJSON ,
308
+ fromMs ))
303
309
c .onDataLoad = func (c * ctx ) {
304
310
ctxId := gcContext .GetContextID ()
305
311
gcContext .SetContext (id )
@@ -312,6 +318,21 @@ func pql(id uint32, c *ctx, optimizable bool,
312
318
return 0
313
319
}
314
320
321
+ func getMaxDurationMs (q parser.Node ) int64 {
322
+ maxDurationMs := int64 (0 )
323
+ for _ , c := range parser .Children (q ) {
324
+ _m := getMaxDurationMs (c )
325
+ if _m > maxDurationMs {
326
+ maxDurationMs = _m
327
+ }
328
+ }
329
+ ms , _ := q .(* parser.MatrixSelector )
330
+ if ms != nil && maxDurationMs < ms .Range .Milliseconds () {
331
+ return ms .Range .Milliseconds ()
332
+ }
333
+ return maxDurationMs
334
+ }
335
+
315
336
func optimizeQuery (q promql.Query , fromMs int64 , toMs int64 , stepMs int64 ) (map [string ]string , promql.Query , error ) {
316
337
appliableNodes := findAppliableNodes (q .Statement (), nil )
317
338
var err error
@@ -352,7 +373,7 @@ func optimizeQuery(q promql.Query, fromMs int64, toMs int64, stepMs int64) (map[
352
373
IsCluster : false ,
353
374
From : time .Unix (0 , fromMs * 1000000 ),
354
375
To : time .Unix (0 , toMs * 1000000 ),
355
- Step : time .Millisecond * time .Duration (stepMs ),
376
+ Step : time .Millisecond * 15000 , /* time.Duration(stepMs)*/
356
377
TimeSeriesTable : "time_series" ,
357
378
TimeSeriesDistTable : "time_series_dist" ,
358
379
TimeSeriesGinTable : "time_series_gin" ,
@@ -448,76 +469,6 @@ func writeVector(v promql.Vector) string {
448
469
}
449
470
450
471
func main () {
451
- queriable := & TestQueryable {id : 0 , stepMs : 15000 }
452
- rq , err := getEng ().NewRangeQuery (
453
- queriable ,
454
- nil ,
455
- "histogram_quantile(0.5, sum by (container) (rate(network_usage{container=~\" awesome\" }[5m])))" ,
456
- time .Now ().Add (time .Hour * - 24 ),
457
- time .Now (),
458
- time .Millisecond * time .Duration (15000 ))
459
- if err != nil {
460
- panic (err )
461
- }
462
- matchers := findAppliableNodes (rq .Statement (), nil )
463
- for _ , m := range matchers {
464
- fmt .Println (m )
465
- opt := m .optimizer
466
- opt = & promql2.FinalizerOptimizer {
467
- SubOptimizer : opt ,
468
- }
469
- opt , err = promql2 .PlanOptimize (m .node , opt )
470
- if err != nil {
471
- panic (err )
472
- }
473
- planner , err := opt .Optimize (m .node )
474
- if err != nil {
475
- panic (err )
476
- }
477
- fakeMetric := fmt .Sprintf ("fake_metric_%d" , time .Now ().UnixNano ())
478
- fmt .Println (rq .Statement ())
479
- swapChild (m .parent , m .node , & parser.VectorSelector {
480
- Name : fakeMetric ,
481
- OriginalOffset : 0 ,
482
- Offset : 0 ,
483
- Timestamp : nil ,
484
- StartOrEnd : 0 ,
485
- LabelMatchers : []* labels.Matcher {
486
- {
487
- Type : labels .MatchEqual ,
488
- Name : "__name__" ,
489
- Value : fakeMetric ,
490
- },
491
- },
492
- UnexpandedSeriesSet : nil ,
493
- Series : nil ,
494
- PosRange : parser.PositionRange {},
495
- })
496
- fmt .Println (rq .Statement ())
497
- sel , err := planner .Process (& shared2.PlannerContext {
498
- IsCluster : false ,
499
- From : time .Now ().Add (time .Hour * - 24 ),
500
- To : time .Now (),
501
- Step : time .Millisecond * time .Duration (15000 ),
502
- TimeSeriesTable : "time_series" ,
503
- TimeSeriesDistTable : "time_series_dist" ,
504
- TimeSeriesGinTable : "time_series_gin" ,
505
- MetricsTable : "metrics_15s" ,
506
- MetricsDistTable : "metrics_15s_dist" ,
507
- })
508
- if err != nil {
509
- panic (err )
510
- }
511
- strSel , err := sel .String (& sql.Ctx {
512
- Params : map [string ]sql.SQLObject {},
513
- Result : map [string ]sql.SQLObject {},
514
- })
515
- if err != nil {
516
- panic (err )
517
- }
518
- println (strSel )
519
- }
520
-
521
472
}
522
473
523
474
func getOptimizer (n parser.Node ) promql2.IOptimizer {
@@ -696,10 +647,11 @@ type TestSeries struct {
696
647
data []byte
697
648
stepMs int64
698
649
699
- labels labels.Labels
700
- tsMs int64
701
- val float64
702
- i int
650
+ labels labels.Labels
651
+ tsMs int64
652
+ val float64
653
+ lastValTs int64
654
+ i int
703
655
704
656
state int
705
657
}
@@ -721,11 +673,14 @@ func (t *TestSeries) Next() bool {
721
673
t .tsMs += t .stepMs
722
674
if t .tsMs >= ts {
723
675
t .state = 0
676
+ } else if t .lastValTs + 300000 < t .tsMs {
677
+ t .state = 0
724
678
}
725
679
}
726
680
if t .state == 0 {
727
681
t .tsMs = ts
728
682
t .val = * (* float64 )(unsafe .Pointer (& t .data [t .i * 16 + 8 ]))
683
+ t .lastValTs = t .tsMs
729
684
t .i ++
730
685
t .state = 1
731
686
}
0 commit comments