Browse Source

feat(topo): extract and modify topo artifacts

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang 3 years atrás
parent
commit
f846bd6fbc

+ 18 - 12
internal/processor/rule.go

@@ -129,7 +129,7 @@ func (p *RuleProcessor) getDefaultRule(name, sql string) *api.Rule {
 	}
 }
 
-func (p *RuleProcessor) getRuleByJson(name, ruleJson string) (*api.Rule, error) {
+func (p *RuleProcessor) getRuleByJson(id, ruleJson string) (*api.Rule, error) {
 	opt := conf.Config.Rule
 	//set default rule options
 	rule := &api.Rule{
@@ -140,23 +140,29 @@ func (p *RuleProcessor) getRuleByJson(name, ruleJson string) (*api.Rule, error)
 	}
 
 	//validation
-	if rule.Id == "" && name == "" {
+	if rule.Id == "" && id == "" {
 		return nil, fmt.Errorf("Missing rule id.")
 	}
-	if name != "" && rule.Id != "" && name != rule.Id {
+	if id != "" && rule.Id != "" && id != rule.Id {
 		return nil, fmt.Errorf("Name is not consistent with rule id.")
 	}
 	if rule.Id == "" {
-		rule.Id = name
+		rule.Id = id
 	}
-	if rule.Sql == "" {
-		return nil, fmt.Errorf("Missing rule SQL.")
-	}
-	if _, err := xsql.GetStatementFromSql(rule.Sql); err != nil {
-		return nil, err
-	}
-	if rule.Actions == nil || len(rule.Actions) == 0 {
-		return nil, fmt.Errorf("Missing rule actions.")
+	if rule.Sql != "" {
+		if rule.Graph != nil {
+			return nil, fmt.Errorf("Rule %s has both sql and graph.", rule.Id)
+		}
+		if _, err := xsql.GetStatementFromSql(rule.Sql); err != nil {
+			return nil, err
+		}
+		if rule.Actions == nil || len(rule.Actions) == 0 {
+			return nil, fmt.Errorf("Missing rule actions.")
+		}
+	} else {
+		if rule.Graph == nil {
+			return nil, fmt.Errorf("Rule %s has neither sql nor graph.", rule.Id)
+		}
 	}
 	if rule.Options == nil {
 		rule.Options = &opt

+ 1 - 1
internal/processor/rule_query.go

@@ -25,7 +25,7 @@ import (
 )
 
 func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*topo.Topo, error) {
-	if tp, err := planner.PlanWithSourcesAndSinks(p.getDefaultRule(ruleid, sql), nil, []*node.SinkNode{node.NewSinkNode("sink_memory_log", "logToMemory", nil)}); err != nil {
+	if tp, err := planner.PlanSQLWithSourcesAndSinks(p.getDefaultRule(ruleid, sql), nil, []*node.SinkNode{node.NewSinkNode("sink_memory_log", "logToMemory", nil)}); err != nil {
 		return nil, err
 	} else {
 		go func() {

+ 3 - 3
internal/server/rule_manager.go

@@ -36,10 +36,10 @@ type RuleState struct {
 	Topology  *topo.Topo
 	Triggered bool
 	// temporary storage for topo graph to make sure even rule close, the graph is still available
-	topoGraph *topo.PrintableTopo
+	topoGraph *api.PrintableTopo
 }
 
-func (rs *RuleState) GetTopoGraph() *topo.PrintableTopo {
+func (rs *RuleState) GetTopoGraph() *api.PrintableTopo {
 	if rs.topoGraph != nil {
 		return rs.topoGraph
 	} else if rs.Topology != nil {
@@ -293,7 +293,7 @@ func recoverRule(name string) string {
 			Name: name,
 		}
 		registry.Store(name, rs)
-		return fmt.Sprintf("Rule %s was stoped.", name)
+		return fmt.Sprintf("Rule %s was stopped.", name)
 	}
 
 	err = startRule(name)

+ 8 - 3
internal/topo/node/node.go

@@ -19,6 +19,7 @@ import (
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/topo/checkpoint"
 	"github.com/lf-edge/ekuiper/internal/topo/node/metric"
+	"github.com/lf-edge/ekuiper/internal/xsql"
 	"github.com/lf-edge/ekuiper/pkg/api"
 	"github.com/lf-edge/ekuiper/pkg/ast"
 	"strings"
@@ -87,12 +88,16 @@ func (o *defaultNode) GetMetrics() (result [][]interface{}) {
 }
 
 func (o *defaultNode) Broadcast(val interface{}) error {
-	if !o.sendError {
-		if _, ok := val.(error); ok {
+	switch d := val.(type) {
+	case error:
+		if !o.sendError {
 			return nil
 		}
+	case xsql.TupleRow:
+		val = d.Clone()
+	case xsql.Collection:
+		val = d.Clone()
 	}
-
 	if o.qos >= api.AtLeastOnce {
 		boe := &checkpoint.BufferOrEvent{
 			Data:    val,

+ 50 - 0
internal/topo/operator/func_operator.go

@@ -0,0 +1,50 @@
+// Copyright 2022 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package operator
+
+import (
+	"fmt"
+	"github.com/lf-edge/ekuiper/internal/xsql"
+	"github.com/lf-edge/ekuiper/pkg/api"
+	"github.com/lf-edge/ekuiper/pkg/ast"
+)
+
+type FuncOp struct {
+	CallExpr *ast.Call
+	Name     string
+}
+
+func (p *FuncOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
+	ctx.GetLogger().Debugf("FuncOp receive: %s", data)
+	switch input := data.(type) {
+	case error:
+		return input
+	case xsql.Valuer:
+		ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(input, fv)}
+		result := ve.Eval(p.CallExpr)
+		if e, ok := result.(error); ok {
+			return e
+		}
+		switch val := input.(type) {
+		case xsql.Row:
+			val.Set(p.Name, result)
+			return val
+		default:
+			return fmt.Errorf("unknow type")
+		}
+	default:
+		return fmt.Errorf("run func error: invalid input %[1]T(%[1]v)", input)
+	}
+}

+ 7 - 3
internal/topo/planner/planner.go

@@ -29,11 +29,15 @@ import (
 )
 
 func Plan(rule *api.Rule) (*topo.Topo, error) {
-	return PlanWithSourcesAndSinks(rule, nil, nil)
+	if rule.Sql != "" {
+		return PlanSQLWithSourcesAndSinks(rule, nil, nil)
+	} else {
+		return PlanByGraph(rule)
+	}
 }
 
-// For test only
-func PlanWithSourcesAndSinks(rule *api.Rule, sources []*node.SourceNode, sinks []*node.SinkNode) (*topo.Topo, error) {
+// PlanSQLWithSourcesAndSinks For test only
+func PlanSQLWithSourcesAndSinks(rule *api.Rule, sources []*node.SourceNode, sinks []*node.SinkNode) (*topo.Topo, error) {
 	sql := rule.Sql
 
 	conf.Log.Infof("Init rule with options %+v", rule.Options)

+ 185 - 0
internal/topo/planner/planner_graph.go

@@ -0,0 +1,185 @@
+// Copyright 2022 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package planner
+
+import (
+	"errors"
+	"fmt"
+	"github.com/lf-edge/ekuiper/internal/topo"
+	"github.com/lf-edge/ekuiper/internal/topo/node"
+	"github.com/lf-edge/ekuiper/internal/topo/operator"
+	"github.com/lf-edge/ekuiper/internal/xsql"
+	"github.com/lf-edge/ekuiper/pkg/api"
+	"github.com/lf-edge/ekuiper/pkg/ast"
+	"github.com/lf-edge/ekuiper/pkg/cast"
+	"github.com/lf-edge/ekuiper/pkg/message"
+	"strings"
+)
+
+// PlanByGraph returns a topo.Topo object by a graph
+// TODO in the future, graph may also be converted to a plan and get optimized
+func PlanByGraph(rule *api.Rule) (*topo.Topo, error) {
+	graph := rule.Graph
+	if graph == nil {
+		return nil, errors.New("no graph")
+	}
+	tp, err := topo.NewWithNameAndQos(rule.Id, rule.Options.Qos, rule.Options.CheckpointInterval)
+	if err != nil {
+		return nil, err
+	}
+	var (
+		nodeMap = make(map[string]api.TopNode)
+		sinks   = make(map[string]bool)
+		sources = make(map[string]bool)
+	)
+	for nodeName, gn := range graph.Nodes {
+		switch gn.Type {
+		case "source":
+			sourceType, ok := gn.Props["source_type"]
+			if !ok {
+				sourceType = "stream"
+			}
+			st, ok := sourceType.(string)
+			if !ok {
+				return nil, fmt.Errorf("source_type %v is not string", sourceType)
+			}
+			st = strings.ToLower(st)
+			sourceOption := &ast.Options{}
+			err := cast.MapToStruct(gn.Props, sourceOption)
+			if err != nil {
+				return nil, err
+			}
+			sourceOption.TYPE = gn.NodeType
+			switch st {
+			case "stream":
+				// TODO deal with conf key
+				pp, err := operator.NewPreprocessor(true, nil, true, nil, rule.Options.IsEventTime, sourceOption.TIMESTAMP, sourceOption.TIMESTAMP_FORMAT, strings.EqualFold(sourceOption.FORMAT, message.FormatBinary), sourceOption.STRICT_VALIDATION)
+				if err != nil {
+					return nil, err
+				}
+				srcNode := node.NewSourceNode(nodeName, ast.TypeStream, pp, sourceOption, rule.Options.SendError)
+				nodeMap[nodeName] = srcNode
+				tp.AddSrc(srcNode)
+			case "table":
+				// TODO add table
+			default:
+				return nil, fmt.Errorf("unknown source type %s", st)
+			}
+			sources[nodeName] = true
+		case "sink":
+			if _, ok := graph.Topo.Edges[nodeName]; ok {
+				return nil, fmt.Errorf("sink %s has edge", nodeName)
+			}
+			nodeMap[nodeName] = node.NewSinkNode(nodeName, gn.NodeType, gn.Props)
+			sinks[nodeName] = true
+		case "operator":
+			switch gn.NodeType {
+			case "function":
+				fop, err := parseFunc(gn.Props)
+				if err != nil {
+					return nil, err
+				}
+				op := Transform(fop, nodeName, rule.Options)
+				nodeMap[nodeName] = op
+			case "filter":
+				fop, err := parseFilter(gn.Props)
+				if err != nil {
+					return nil, err
+				}
+				op := Transform(fop, nodeName, rule.Options)
+				nodeMap[nodeName] = op
+			default: // TODO other node type
+				return nil, fmt.Errorf("unknown operator type %s", gn.NodeType)
+			}
+		default:
+			return nil, fmt.Errorf("unknown node type %s", gn.Type)
+		}
+	}
+	// validate source node
+	for _, nodeName := range graph.Topo.Sources {
+		if _, ok := sources[nodeName]; !ok {
+			return nil, fmt.Errorf("source %s is not a source type node", nodeName)
+		}
+	}
+	// reverse edges
+	reversedEdges := make(map[string][]string)
+	for fromNode, toNodes := range graph.Topo.Edges {
+		for _, toNode := range toNodes {
+			reversedEdges[toNode] = append(reversedEdges[toNode], fromNode)
+		}
+	}
+	// add the linkages
+	for nodeName, fromNodes := range reversedEdges {
+		inputs := make([]api.Emitter, len(fromNodes))
+		for i, fromNode := range fromNodes {
+			inputs[i] = nodeMap[fromNode].(api.Emitter)
+		}
+		n := nodeMap[nodeName]
+		if _, ok := sinks[nodeName]; ok {
+			tp.AddSink(inputs, n.(*node.SinkNode))
+		} else {
+			tp.AddOperator(inputs, n.(node.OperatorNode))
+		}
+	}
+	return tp, nil
+}
+
+func parseFunc(props map[string]interface{}) (*operator.FuncOp, error) {
+	m, ok := props["expr"]
+	if !ok {
+		return nil, errors.New("no expr")
+	}
+	funcExpr, ok := m.(string)
+	if !ok {
+		return nil, fmt.Errorf("expr %v is not string", m)
+	}
+	stmt, err := xsql.NewParser(strings.NewReader("select " + funcExpr + " from nonexist")).Parse()
+	if err != nil {
+		return nil, err
+	}
+	f := stmt.Fields[0]
+	c, ok := f.Expr.(*ast.Call)
+	if !ok {
+		// never happen
+		return nil, fmt.Errorf("expr %v is not ast.Call", stmt.Fields[0].Expr)
+	}
+	var name string
+	if f.AName != "" {
+		name = f.AName
+	} else {
+		name = f.Name
+	}
+	return &operator.FuncOp{CallExpr: c, Name: name}, nil
+}
+
+func parseFilter(props map[string]interface{}) (*operator.FilterOp, error) {
+	m, ok := props["expr"]
+	if !ok {
+		return nil, errors.New("no expr")
+	}
+	conditionExpr, ok := m.(string)
+	if !ok {
+		return nil, fmt.Errorf("expr %v is not string", m)
+	}
+	p := xsql.NewParser(strings.NewReader("where " + conditionExpr))
+	if exp, err := p.ParseCondition(); err != nil {
+		return nil, err
+	} else {
+		if exp != nil {
+			return &operator.FilterOp{Condition: exp}, nil
+		}
+	}
+	return nil, fmt.Errorf("expr %v is not a condition", m)
+}

+ 3 - 8
internal/topo/topo.go

@@ -29,11 +29,6 @@ import (
 	"sync"
 )
 
-type PrintableTopo struct {
-	Sources []string            `json:"sources"`
-	Edges   map[string][]string `json:"edges"`
-}
-
 type Topo struct {
 	sources            []node.DataSourceNode
 	sinks              []*node.SinkNode
@@ -46,7 +41,7 @@ type Topo struct {
 	checkpointInterval int
 	store              api.Store
 	coordinator        *checkpoint.Coordinator
-	topo               *PrintableTopo
+	topo               *api.PrintableTopo
 	mu                 sync.Mutex
 }
 
@@ -55,7 +50,7 @@ func NewWithNameAndQos(name string, qos api.Qos, checkpointInterval int) (*Topo,
 		name:               name,
 		qos:                qos,
 		checkpointInterval: checkpointInterval,
-		topo: &PrintableTopo{
+		topo: &api.PrintableTopo{
 			Sources: make([]string, 0),
 			Edges:   make(map[string][]string),
 		},
@@ -229,6 +224,6 @@ func (s *Topo) GetMetrics() (keys []string, values []interface{}) {
 	return
 }
 
-func (s *Topo) GetTopo() *PrintableTopo {
+func (s *Topo) GetTopo() *api.PrintableTopo {
 	return s.topo
 }

+ 3 - 3
internal/topo/topotest/mock_topo.go

@@ -1,4 +1,4 @@
-// Copyright 2021 EMQ Technologies Co., Ltd.
+// Copyright 2021-2022 EMQ Technologies Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ type RuleTest struct {
 	Sql  string
 	R    interface{}            // The result
 	M    map[string]interface{} // final metrics
-	T    *topo.PrintableTopo    // printable topo, an optional field
+	T    *api.PrintableTopo     // printable topo, an optional field
 	W    int                    // wait time for each data sending, in milli
 }
 
@@ -251,7 +251,7 @@ func createStream(t *testing.T, tt RuleTest, j int, opt *api.RuleOption, sinkPro
 	}
 	mockSink := mocknode.NewMockSink()
 	sink := node.NewSinkNodeWithSink("mockSink", mockSink, sinkProps)
-	tp, err := planner.PlanWithSourcesAndSinks(&api.Rule{Id: fmt.Sprintf("%s_%d", tt.Name, j), Sql: tt.Sql, Options: opt}, sources, []*node.SinkNode{sink})
+	tp, err := planner.PlanSQLWithSourcesAndSinks(&api.Rule{Id: fmt.Sprintf("%s_%d", tt.Name, j), Sql: tt.Sql, Options: opt}, sources, []*node.SinkNode{sink})
 	if err != nil {
 		t.Error(err)
 		return nil, 0, nil, nil, nil

+ 2 - 3
internal/topo/topotest/rule_test.go

@@ -16,7 +16,6 @@ package topotest
 
 import (
 	"encoding/json"
-	"github.com/lf-edge/ekuiper/internal/topo"
 	"github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
 	"github.com/lf-edge/ekuiper/pkg/api"
 	"testing"
@@ -72,7 +71,7 @@ func TestSingleSQL(t *testing.T) {
 				"source_demo_0_records_in_total":  int64(5),
 				"source_demo_0_records_out_total": int64(5),
 			},
-			T: &topo.PrintableTopo{
+			T: &api.PrintableTopo{
 				Sources: []string{"source_demo"},
 				Edges: map[string][]string{
 					"source_demo":  {"op_2_project"},
@@ -366,7 +365,7 @@ func TestSingleSQL(t *testing.T) {
 				"source_demo_0_records_in_total":  int64(5),
 				"source_demo_0_records_out_total": int64(5),
 			},
-			T: &topo.PrintableTopo{
+			T: &api.PrintableTopo{
 				Sources: []string{"source_demo"},
 				Edges: map[string][]string{
 					"source_demo":  {"op_2_project"},

+ 1 - 2
internal/topo/topotest/window_rule_test.go

@@ -15,7 +15,6 @@
 package topotest
 
 import (
-	"github.com/lf-edge/ekuiper/internal/topo"
 	"github.com/lf-edge/ekuiper/pkg/api"
 	"testing"
 )
@@ -223,7 +222,7 @@ func TestWindow(t *testing.T) {
 				"op_4_join_0_records_in_total":   int64(10),
 				"op_4_join_0_records_out_total":  int64(8),
 			},
-			T: &topo.PrintableTopo{
+			T: &api.PrintableTopo{
 				Sources: []string{"source_demo", "source_demo1"},
 				Edges: map[string][]string{
 					"source_demo":  {"op_3_window"},

+ 2 - 2
internal/xsql/parser.go

@@ -42,7 +42,7 @@ type Parser struct {
 	clause string
 }
 
-func (p *Parser) parseCondition() (ast.Expr, error) {
+func (p *Parser) ParseCondition() (ast.Expr, error) {
 	if tok, _ := p.scanIgnoreWhitespace(); tok != ast.WHERE {
 		p.unscan()
 		return nil, nil
@@ -149,7 +149,7 @@ func (p *Parser) Parse() (*ast.SelectStatement, error) {
 		selects.Joins = joins
 	}
 	p.clause = "where"
-	if exp, err := p.parseCondition(); err != nil {
+	if exp, err := p.ParseCondition(); err != nil {
 		return nil, err
 	} else {
 		if exp != nil {

+ 25 - 5
pkg/api/stream.go

@@ -124,12 +124,32 @@ type RuleOption struct {
 	CheckpointInterval int   `json:"checkpointInterval" yaml:"checkpointInterval"`
 }
 
+type PrintableTopo struct {
+	Sources []string            `json:"sources"`
+	Edges   map[string][]string `json:"edges"`
+}
+
+type GraphNode struct {
+	Type     string                 `json:"type"`
+	NodeType string                 `json:"nodeType"`
+	Props    map[string]interface{} `json:"props"`
+}
+
+type RuleGraph struct {
+	Nodes map[string]*GraphNode `json:"nodes"`
+	Topo  *PrintableTopo        `json:"topo"`
+}
+
+// Rule the definition of the business logic
+// Sql and Graph are mutually exclusive, at least one of them should be set
 type Rule struct {
-	Triggered bool                     `json:"triggered"`
-	Id        string                   `json:"id"`
-	Sql       string                   `json:"sql"`
-	Actions   []map[string]interface{} `json:"actions"`
-	Options   *RuleOption              `json:"options"`
+	Triggered bool                     `json:"triggered,omitempty"`
+	Id        string                   `json:"id,omitempty"`
+	Name      string                   `json:"name,omitempty"` // The display name of a rule
+	Sql       string                   `json:"sql,omitempty"`
+	Graph     *RuleGraph               `json:"graph,omitempty"`
+	Actions   []map[string]interface{} `json:"actions,omitempty"`
+	Options   *RuleOption              `json:"options,omitempty"`
 }
 
 type StreamContext interface {

+ 12 - 12
pkg/ast/sourceStmt.go

@@ -76,19 +76,19 @@ type RecType struct {
 	FieldType
 }
 
-// The stream AST tree
+// Options The stream AST tree
 type Options struct {
-	DATASOURCE        string
-	KEY               string
-	FORMAT            string
-	CONF_KEY          string
-	TYPE              string
-	STRICT_VALIDATION bool
-	TIMESTAMP         string
-	TIMESTAMP_FORMAT  string
-	RETAIN_SIZE       int
-	SHARED            bool
-	SCHEMAID          string
+	DATASOURCE        string `json:"datasource,omitempty"`
+	KEY               string `json:"key,omitempty"`
+	FORMAT            string `json:"format,omitempty"`
+	CONF_KEY          string `json:"confKey,omitempty"`
+	TYPE              string `json:"type,omitempty"`
+	STRICT_VALIDATION bool   `json:"strictValidation,omitempty"`
+	TIMESTAMP         string `json:"timestamp,omitempty"`
+	TIMESTAMP_FORMAT  string `json:"timestampFormat,omitempty"`
+	RETAIN_SIZE       int    `json:"retainSize,omitempty"`
+	SHARED            bool   `json:"shared,omitempty"`
+	SCHEMAID          string `json:"schemaid,omitempty"`
 }
 
 func (o Options) node() {}