forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
logical plan distributed optimizer #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rubywtl
wants to merge
1
commit into
master
Choose a base branch
from
logicalplan-distributed-optimizer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
Query: node.Clone(), | ||
QueryRangeStart: opts.Start, | ||
QueryRangeEnd: opts.End, | ||
} | ||
|
||
return logicalplan.Deduplicate{ | ||
Expressions: remoteNodes, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])") | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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