Skip to content

Commit 4c647ab

Browse files
Merge branch 'main' into mhoffm-add-sample-limits
Signed-off-by: Michael Hoffmann <[email protected]>
2 parents 318b752 + 5816f4f commit 4c647ab

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

engine/distributed_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/efficientgo/core/errors"
1314
"github.com/efficientgo/core/testutil"
1415
"github.com/prometheus/prometheus/model/labels"
1516
"github.com/prometheus/prometheus/promql"
17+
"github.com/prometheus/prometheus/storage"
1618

1719
"github.com/thanos-io/promql-engine/api"
1820
"github.com/thanos-io/promql-engine/engine"
@@ -329,3 +331,32 @@ func TestDistributedAggregations(t *testing.T) {
329331
}
330332
}
331333
}
334+
335+
func TestDistributedEngineWarnings(t *testing.T) {
336+
querier := &storage.MockQueryable{
337+
MockQuerier: &storage.MockQuerier{
338+
SelectMockFunction: func(sortSeries bool, hints *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
339+
return newWarningsSeriesSet(storage.Warnings{errors.New("test warning")})
340+
},
341+
},
342+
}
343+
344+
opts := engine.Opts{
345+
EngineOpts: promql.EngineOpts{
346+
MaxSamples: math.MaxInt64,
347+
Timeout: 1 * time.Minute,
348+
},
349+
}
350+
remote := engine.NewRemoteEngine(opts, querier, math.MinInt64, math.MaxInt64, nil)
351+
ng := engine.NewDistributedEngine(opts, api.NewStaticEndpoints([]api.RemoteEngine{remote}))
352+
var (
353+
start = time.UnixMilli(0)
354+
end = time.UnixMilli(600)
355+
step = 30 * time.Second
356+
)
357+
q, err := ng.NewRangeQuery(context.Background(), nil, nil, "test", start, end, step)
358+
testutil.Ok(t, err)
359+
360+
res := q.Exec(context.Background())
361+
testutil.Equals(t, 1, len(res.Warnings))
362+
}

engine/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type distributedEngine struct {
136136

137137
func NewDistributedEngine(opts Opts, endpoints api.RemoteEndpoints) v1.QueryEngine {
138138
opts.LogicalOptimizers = []logicalplan.Optimizer{
139+
logicalplan.PassthroughOptimizer{Endpoints: endpoints},
139140
logicalplan.DistributedExecutionOptimizer{Endpoints: endpoints},
140141
}
141142

execution/remote/operator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/thanos-io/promql-engine/execution/scan"
1717
engstore "github.com/thanos-io/promql-engine/execution/storage"
1818
"github.com/thanos-io/promql-engine/execution/tracking"
19+
"github.com/thanos-io/promql-engine/execution/warnings"
1920
"github.com/thanos-io/promql-engine/query"
2021
)
2122

@@ -103,6 +104,7 @@ func (s *storageAdapter) GetSeries(ctx context.Context, _, _ int) ([]engstore.Si
103104

104105
func (s *storageAdapter) executeQuery(ctx context.Context) {
105106
result := s.query.Exec(ctx)
107+
warnings.AddToContext(result.Warnings, ctx)
106108
if result.Err != nil {
107109
s.err = result.Err
108110
return

logicalplan/passthrough.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) The Thanos Community Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package logicalplan
5+
6+
import (
7+
"github.com/thanos-io/promql-engine/api"
8+
"github.com/thanos-io/promql-engine/parser"
9+
)
10+
11+
// PassthroughOptimizer optimizes queries which can be simply passed
12+
// through to a RemoteEngine.
13+
type PassthroughOptimizer struct {
14+
Endpoints api.RemoteEndpoints
15+
}
16+
17+
func (m PassthroughOptimizer) Optimize(plan parser.Expr, opts *Opts) parser.Expr {
18+
engines := m.Endpoints.Engines()
19+
if len(engines) == 1 {
20+
return RemoteExecution{
21+
Engine: engines[0],
22+
Query: plan.String(),
23+
QueryRangeStart: opts.Start,
24+
}
25+
}
26+
27+
return plan
28+
}

logicalplan/passthrough_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) The Thanos Community Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package logicalplan
5+
6+
import (
7+
"math"
8+
"testing"
9+
"time"
10+
11+
"github.com/efficientgo/core/testutil"
12+
"github.com/prometheus/prometheus/model/labels"
13+
14+
"github.com/thanos-io/promql-engine/api"
15+
"github.com/thanos-io/promql-engine/parser"
16+
)
17+
18+
func TestPassthrough(t *testing.T) {
19+
expr, err := parser.ParseExpr(`time()`)
20+
testutil.Ok(t, err)
21+
22+
t.Run("optimized with one engine", func(t *testing.T) {
23+
engines := []api.RemoteEngine{
24+
newEngineMock(math.MinInt64, math.MinInt64, []labels.Labels{labels.FromStrings("region", "east"), labels.FromStrings("region", "south")}),
25+
}
26+
optimizers := []Optimizer{PassthroughOptimizer{Endpoints: api.NewStaticEndpoints(engines)}}
27+
28+
plan := New(expr, &Opts{Start: time.Unix(0, 0), End: time.Unix(0, 0)})
29+
optimizedPlan := plan.Optimize(optimizers)
30+
31+
testutil.Equals(t, "remote(time())", optimizedPlan.Expr().String())
32+
})
33+
34+
t.Run("not optimized with one engine", func(t *testing.T) {
35+
engines := []api.RemoteEngine{
36+
newEngineMock(math.MinInt64, math.MinInt64, []labels.Labels{labels.FromStrings("region", "east"), labels.FromStrings("region", "south")}),
37+
newEngineMock(math.MinInt64, math.MinInt64, []labels.Labels{labels.FromStrings("region", "west")}),
38+
}
39+
optimizers := []Optimizer{PassthroughOptimizer{Endpoints: api.NewStaticEndpoints(engines)}}
40+
41+
plan := New(expr, &Opts{Start: time.Unix(0, 0), End: time.Unix(0, 0)})
42+
optimizedPlan := plan.Optimize(optimizers)
43+
44+
testutil.Equals(t, "time()", optimizedPlan.Expr().String())
45+
})
46+
47+
}

0 commit comments

Comments
 (0)