/
directed.go
192 lines (165 loc) · 5.24 KB
/
directed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package pipeline
import (
"fmt"
"strings"
"github.com/observiq/stanza/errors"
"github.com/observiq/stanza/operator"
"gonum.org/v1/gonum/graph/encoding/dot"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo"
)
var _ Pipeline = (*DirectedPipeline)(nil)
// DirectedPipeline is a pipeline backed by a directed graph
type DirectedPipeline struct {
Graph *simple.DirectedGraph
}
// Start will start the operators in a pipeline in reverse topological order
func (p *DirectedPipeline) Start() error {
sortedNodes, err := topo.Sort(p.Graph)
if err != nil {
return err
}
for i := len(sortedNodes) - 1; i >= 0; i-- {
operator := sortedNodes[i].(OperatorNode).Operator()
operator.Logger().Debug("Starting operator")
if err := operator.Start(); err != nil {
return err
}
operator.Logger().Debug("Started operator")
}
return nil
}
// Stop will stop the operators in a pipeline in topological order
func (p *DirectedPipeline) Stop() error {
sortedNodes, err := topo.Sort(p.Graph)
if err != nil {
return err
}
for _, node := range sortedNodes {
operator := node.(OperatorNode).Operator()
operator.Logger().Debug("Stopping operator")
_ = operator.Stop()
operator.Logger().Debug("Stopped operator")
}
return nil
}
// Render will render the pipeline as a dot graph
func (p *DirectedPipeline) Render() ([]byte, error) {
return dot.Marshal(p.Graph, "G", "", " ")
}
// Operators returns a slice of operators that make up the pipeline graph
func (p *DirectedPipeline) Operators() []operator.Operator {
operators := make([]operator.Operator, 0)
nodes := p.Graph.Nodes()
for nodes.Next() {
operators = append(operators, nodes.Node().(OperatorNode).Operator())
}
return operators
}
// addNodes will add operators as nodes to the supplied graph.
func addNodes(graph *simple.DirectedGraph, operators []operator.Operator) error {
for _, operator := range operators {
operatorNode := createOperatorNode(operator)
if graph.Node(operatorNode.ID()) != nil {
return errors.NewError(
fmt.Sprintf("operator with id '%s' already exists in pipeline", operatorNode.Operator().ID()),
"ensure that each operator has a unique `type` or `id`",
)
}
graph.AddNode(operatorNode)
}
return nil
}
// connectNodes will connect the nodes in the supplied graph.
func connectNodes(graph *simple.DirectedGraph) error {
nodes := graph.Nodes()
for nodes.Next() {
node := nodes.Node().(OperatorNode)
if err := connectNode(graph, node); err != nil {
return err
}
}
if _, err := topo.Sort(graph); err != nil {
return errors.NewError(
"pipeline has a circular dependency",
"ensure that all operators are connected in a straight, acyclic line",
"cycles", unorderableToCycles(err.(topo.Unorderable)),
)
}
return nil
}
// connectNode will connect a node to its outputs in the supplied graph.
func connectNode(graph *simple.DirectedGraph, inputNode OperatorNode) error {
for outputOperatorID, outputNodeID := range inputNode.OutputIDs() {
if graph.Node(outputNodeID) == nil {
return errors.NewError(
"operators cannot be connected, because the output does not exist in the pipeline",
"ensure that the output operator is defined",
"input_operator", inputNode.Operator().ID(),
"output_operator", outputOperatorID,
)
}
outputNode := graph.Node(outputNodeID).(OperatorNode)
if !outputNode.Operator().CanProcess() {
return errors.NewError(
"operators cannot be connected, because the output operator can not process logs",
"ensure that the output operator can process logs (like a parser or destination)",
"input_operator", inputNode.Operator().ID(),
"output_operator", outputOperatorID,
)
}
if graph.HasEdgeFromTo(inputNode.ID(), outputNodeID) {
return errors.NewError(
"operators cannot be connected, because a connection already exists",
"ensure that only a single connection exists between the two operators",
"input_operator", inputNode.Operator().ID(),
"output_operator", outputOperatorID,
)
}
edge := graph.NewEdge(inputNode, outputNode)
graph.SetEdge(edge)
}
return nil
}
// setOperatorOutputs will set the outputs on operators that can output.
func setOperatorOutputs(operators []operator.Operator) error {
for _, operator := range operators {
if !operator.CanOutput() {
continue
}
if err := operator.SetOutputs(operators); err != nil {
return errors.WithDetails(err, "operator_id", operator.ID())
}
}
return nil
}
// NewDirectedPipeline creates a new directed pipeline
func NewDirectedPipeline(operators []operator.Operator) (*DirectedPipeline, error) {
if err := setOperatorOutputs(operators); err != nil {
return nil, err
}
graph := simple.NewDirectedGraph()
if err := addNodes(graph, operators); err != nil {
return nil, err
}
if err := connectNodes(graph); err != nil {
return nil, err
}
return &DirectedPipeline{Graph: graph}, nil
}
func unorderableToCycles(err topo.Unorderable) string {
var cycles strings.Builder
for i, cycle := range err {
if i != 0 {
cycles.WriteByte(',')
}
cycles.WriteByte('(')
for _, node := range cycle {
cycles.WriteString(node.(OperatorNode).operator.ID())
cycles.Write([]byte(` -> `))
}
cycles.WriteString(cycle[0].(OperatorNode).operator.ID())
cycles.WriteByte(')')
}
return cycles.String()
}