-
Notifications
You must be signed in to change notification settings - Fork 828
add logical plan distributed optimizer to query frontend #6974
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
cortexproject:master
Choose a base branch
from
rubywtl: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.
+618
−4
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
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,192 @@ | ||
package distributed_execution | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"math" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/thanos-io/promql-engine/logicalplan" | ||
) | ||
|
||
type jsonNode struct { | ||
Type logicalplan.NodeType `json:"type"` | ||
Data json.RawMessage `json:"data"` | ||
Children []json.RawMessage `json:"children,omitempty"` | ||
} | ||
|
||
const ( | ||
nanVal = `"NaN"` | ||
infVal = `"+Inf"` | ||
negInfVal = `"-Inf"` | ||
) | ||
|
||
func Unmarshal(data []byte) (logicalplan.Node, error) { | ||
return unmarshalNode(data) | ||
} | ||
|
||
func unmarshalNode(data []byte) (logicalplan.Node, error) { | ||
t := jsonNode{} | ||
if err := json.Unmarshal(data, &t); err != nil { | ||
return nil, err | ||
} | ||
|
||
switch t.Type { | ||
case logicalplan.VectorSelectorNode: | ||
v := &logicalplan.VectorSelector{} | ||
if err := json.Unmarshal(t.Data, v); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
for i, m := range v.LabelMatchers { | ||
v.LabelMatchers[i], err = labels.NewMatcher(m.Type, m.Name, m.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return v, nil | ||
case logicalplan.MatrixSelectorNode: | ||
m := &logicalplan.MatrixSelector{} | ||
if err := json.Unmarshal(t.Data, m); err != nil { | ||
return nil, err | ||
} | ||
vs, err := unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.VectorSelector = vs.(*logicalplan.VectorSelector) | ||
return m, nil | ||
case logicalplan.AggregationNode: | ||
a := &logicalplan.Aggregation{} | ||
if err := json.Unmarshal(t.Data, a); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
a.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(t.Children) > 1 { | ||
a.Param, err = unmarshalNode(t.Children[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return a, nil | ||
case logicalplan.BinaryNode: | ||
b := &logicalplan.Binary{} | ||
if err := json.Unmarshal(t.Data, b); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
b.LHS, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.RHS, err = unmarshalNode(t.Children[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
case logicalplan.FunctionNode: | ||
f := &logicalplan.FunctionCall{} | ||
if err := json.Unmarshal(t.Data, f); err != nil { | ||
return nil, err | ||
} | ||
for _, c := range t.Children { | ||
child, err := unmarshalNode(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.Args = append(f.Args, child) | ||
} | ||
return f, nil | ||
case logicalplan.NumberLiteralNode: | ||
n := &logicalplan.NumberLiteral{} | ||
if bytes.Equal(t.Data, []byte(infVal)) { | ||
n.Val = math.Inf(1) | ||
} else if bytes.Equal(t.Data, []byte(negInfVal)) { | ||
n.Val = math.Inf(-1) | ||
} else if bytes.Equal(t.Data, []byte(nanVal)) { | ||
n.Val = math.NaN() | ||
} else { | ||
if err := json.Unmarshal(t.Data, n); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return n, nil | ||
case logicalplan.StringLiteralNode: | ||
s := &logicalplan.StringLiteral{} | ||
if err := json.Unmarshal(t.Data, s); err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
case logicalplan.SubqueryNode: | ||
s := &logicalplan.Subquery{} | ||
if err := json.Unmarshal(t.Data, s); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
s.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
case logicalplan.CheckDuplicateNode: | ||
c := &logicalplan.CheckDuplicateLabels{} | ||
if err := json.Unmarshal(t.Data, c); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
c.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
case logicalplan.StepInvariantNode: | ||
s := &logicalplan.StepInvariantExpr{} | ||
if err := json.Unmarshal(t.Data, s); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
s.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
case logicalplan.ParensNode: | ||
p := &logicalplan.Parens{} | ||
if err := json.Unmarshal(t.Data, p); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
p.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p, nil | ||
case logicalplan.UnaryNode: | ||
u := &logicalplan.Unary{} | ||
if err := json.Unmarshal(t.Data, u); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
u.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return u, nil | ||
case RemoteNode: | ||
r := &Remote{} | ||
if err := json.Unmarshal(t.Data, r); err != nil { | ||
return nil, err | ||
} | ||
var err error | ||
r.Expr, err = unmarshalNode(t.Children[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
return nil, nil | ||
} |
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,70 @@ | ||
package distributed_execution | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/promql-engine/logicalplan" | ||
) | ||
|
||
func TestUnmarshalWithLogicalPlan(t *testing.T) { | ||
t.Run("unmarshal complex query plan", func(t *testing.T) { | ||
start := time.Now() | ||
end := start.Add(1 * time.Hour) | ||
step := 15 * time.Second | ||
|
||
testCases := []struct { | ||
name string | ||
query string | ||
}{ | ||
{ | ||
name: "binary operation", | ||
query: "http_requests_total + rate(node_cpu_seconds_total[5m])", | ||
}, | ||
{ | ||
name: "aggregation", | ||
query: "sum(rate(http_requests_total[5m])) by (job)", | ||
}, | ||
{ | ||
name: "complex query", | ||
query: "sum(rate(http_requests_total{job='prometheus'}[5m])) by (job) / sum(rate(node_cpu_seconds_total[5m])) by (job)", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
plan, _, err := CreateTestLogicalPlan(tc.query, start, end, step) | ||
require.NoError(t, err) | ||
require.NotNil(t, plan) | ||
|
||
data, err := logicalplan.Marshal((*plan).Root()) | ||
require.NoError(t, err) | ||
|
||
node, err := Unmarshal(data) | ||
require.NoError(t, err) | ||
require.NotNil(t, node) | ||
|
||
// the logical plan node before and after marshal/unmarshal should be the same | ||
verifyNodeStructure(t, (*plan).Root(), node) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
func verifyNodeStructure(t *testing.T, expected logicalplan.Node, actual logicalplan.Node) { | ||
require.Equal(t, expected.Type(), actual.Type()) | ||
require.Equal(t, expected.String(), actual.String()) | ||
require.Equal(t, expected.ReturnType(), actual.ReturnType()) | ||
|
||
expectedChildren := expected.Children() | ||
actualChildren := actual.Children() | ||
|
||
require.Equal(t, len(expectedChildren), len(actualChildren)) | ||
|
||
for i := 0; i < len(expectedChildren); i++ { | ||
if expectedChildren[i] != nil && actualChildren[i] != nil { | ||
verifyNodeStructure(t, *expectedChildren[i], *actualChildren[i]) | ||
} | ||
} | ||
} |
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,47 @@ | ||
package distributed_execution | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prometheus/prometheus/util/annotations" | ||
"github.com/thanos-io/promql-engine/logicalplan" | ||
) | ||
|
||
// This is a simplified implementation that only handles binary aggregation cases | ||
// 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) (logicalplan.Node, annotations.Annotations, error) { | ||
warns := annotations.New() | ||
|
||
if root == nil { | ||
return nil, *warns, fmt.Errorf("nil root node") | ||
} | ||
|
||
var hasAggregation bool | ||
logicalplan.TraverseBottomUp(nil, &root, func(parent, current *logicalplan.Node) bool { | ||
|
||
if (*current).Type() == logicalplan.AggregationNode { | ||
hasAggregation = true | ||
} | ||
|
||
if (*current).Type() == logicalplan.BinaryNode && hasAggregation { | ||
ch := (*current).Children() | ||
|
||
for _, child := range ch { | ||
temp := (*child).Clone() | ||
*child = &Remote{} | ||
*(*child).Children()[0] = temp | ||
} | ||
|
||
hasAggregation = false | ||
} | ||
|
||
return false | ||
}) | ||
return root, *warns, nil | ||
} |
Oops, something went wrong.
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.
Even though it is just a dummy optimizer, we should probably add constraints to only mark as remote node if the child has aggregation. We don't want to optimize queries like
up + up
as each child returns raw data instead of aggregated data