Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pkg/querier/tripperware/distributed_optimizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tripperware

import (
"github.com/prometheus/prometheus/util/annotations"
"github.com/thanos-io/promql-engine/logicalplan"
"github.com/thanos-io/promql-engine/query"
)

// This is a simplified implementation.
// Future versions of the distributed optimizer are expected to:
// - Support more complex query patterns.
// - Incorporate diverse optimization strategies.
// - Extend support to node types beyond binary operations.

type DistributedOptimizer struct{}

func (d *DistributedOptimizer) Optimize(root logicalplan.Node, opts *query.Options) (logicalplan.Node, annotations.Annotations) {
warns := annotations.New()

logicalplan.TraverseBottomUp(nil, &root, func(parent, current *logicalplan.Node) bool {

if (*current).Type() == logicalplan.BinaryNode {
ch := (*current).Children()

for i, child := range ch {
remoteNode := d.wrapWithRemoteExecution(*child, opts)
*ch[i] = remoteNode
}
}

return false
})
return root, *warns
}

func (d *DistributedOptimizer) wrapWithRemoteExecution(node logicalplan.Node, opts *query.Options) logicalplan.Node {
// the current version only creates one remote execution for one node, no extra sharding based on time ranges

remoteNodes := make([]logicalplan.RemoteExecution, 1)

remoteNodes[0] = logicalplan.RemoteExecution{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to reuse RemoteExecution from thanos engine's distributed optimizer. We should define our own.
The exising remote node is for thanos engine to work as it has query and start end information. For us to work we need to define our own logicalplan node type and pass down the information we need like fragment ID and query ID, etc

Query: node.Clone(),
QueryRangeStart: opts.Start,
QueryRangeEnd: opts.End,
}

return logicalplan.Deduplicate{
Expressions: remoteNodes,
}
}
85 changes: 85 additions & 0 deletions pkg/querier/tripperware/distributed_optimizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package tripperware

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)

// Before:
// binary node: sum(a) + sum(b)
// / \
// aggr: sum(a) aggr: sum(b)
// | |
// vector selector vector selector

// After dummy distributed optimizer:
// binary node: sum(a) + sum(b)
// / \
// remote exec remote exec
// | |
// aggr: sum(a) aggr: sum(b)
// | |
// vector selector vector selector

func TestDistributedOptimizer(t *testing.T) {
testCases := []struct {
name string
query string
start int64
end int64
step time.Duration
expected struct {
childrenCount int
remoteExecCount int
deduplicateType bool
}
}{
{
name: "binary operation with aggregations",
query: "sum(rate(node_cpu_seconds_total{mode!=\"idle\"}[5m])) + sum(rate(node_memory_Active_bytes[5m]))",
start: 100000,
end: 100000,
step: time.Minute,
expected: struct {
childrenCount int
remoteExecCount int
deduplicateType bool
}{
childrenCount: 2, // binary node should have 2 children
deduplicateType: true, // each RemoteExecution should be wrapped in Deduplicate
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

req := &PrometheusRequest{
Start: tc.start,
End: tc.end,
Query: tc.query,
}

middleware := DistributedQueryMiddleware(tc.step, 5*time.Minute)
handler := middleware.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) {
return nil, nil
}))
_, err := handler.Do(context.Background(), req)
require.NoError(t, err)
require.NotNil(t, req.LogicalPlan, "logical plan should be populated")

startNode := req.LogicalPlan.Root().Children()[0]
children := (*startNode).Children()
require.Len(t, children, tc.expected.childrenCount)

LHS := *children[0]
RHS := *children[1]

require.Equal(t, LHS.String(), "dedup(remote(sum(rate(node_cpu_seconds_total{mode!=\"idle\"}[5m]))) [1970-01-01 00:01:40 +0000 UTC, 1970-01-01 00:01:40 +0000 UTC])")
require.Equal(t, RHS.String(), "dedup(remote(sum(rate(node_memory_Active_bytes[5m]))) [1970-01-01 00:01:40 +0000 UTC, 1970-01-01 00:01:40 +0000 UTC])")
})
}
}
6 changes: 5 additions & 1 deletion pkg/querier/tripperware/distributed_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (d distributedQueryMiddleware) newLogicalPlan(qs string, start time.Time, e
logicalPlan := logicalplan.NewFromAST(expr, &qOpts, planOpts)
optimizedPlan, _ := logicalPlan.Optimize(logicalplan.DefaultOptimizers)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can append distributed optimizer here and optimize the plan in one pass


return &optimizedPlan, nil
dOptimizer := DistributedOptimizer{}
dOptimizedPlanNode, _ := dOptimizer.Optimize(optimizedPlan.Root(), &qOpts)
lp := logicalplan.New(dOptimizedPlanNode, &qOpts, planOpts)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This step is unnecessary. We should just return dOptimizedPlanNode


return &lp, nil
}

func (d distributedQueryMiddleware) Do(ctx context.Context, r Request) (Response, error) {
Expand Down
Loading