forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.go
246 lines (207 loc) · 6.62 KB
/
pipeline.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package transporter
import (
"log"
"time"
"github.com/compose/transporter/pkg/adaptor"
"github.com/compose/transporter/pkg/events"
"github.com/compose/transporter/pkg/state"
)
// VERSION the library
const (
VERSION = "0.1.1"
)
// A Pipeline is a the end to end description of a transporter data flow.
// including the source, sink, and all the transformers along the way
type Pipeline struct {
source *Node
emitter events.Emitter
sessionStore state.SessionStore
metricsTicker *time.Ticker
// Err is the fatal error that was sent from the adaptor
// that caused us to stop this process. If this is nil, then
// the transporter is running
Err error
sessionTicker *time.Ticker
}
// NewDefaultPipeline returns a new Transporter Pipeline with the given node tree, and
// uses the events.HttpPostEmitter to deliver metrics.
// eg.
// source :=
// transporter.NewNode("source", "mongo", adaptor.Config{"uri": "mongodb://localhost/", "namespace": "boom.foo", "debug": false, "tail": true}).
// Add(transporter.NewNode("out", "file", adaptor.Config{"uri": "stdout://"}))
// pipeline, err := transporter.NewDefaultPipeline(source, events.Api{URI: "http://localhost/endpoint"}, 1*time.Second)
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// pipeline.Run()
func NewDefaultPipeline(source *Node, uri, key, pid string, interval time.Duration) (*Pipeline, error) {
emitter := events.NewHTTPPostEmitter(uri, key, pid)
return NewPipeline(source, emitter, interval, nil, 10*time.Second)
}
// NewPipeline creates a new Transporter Pipeline using the given tree of nodes, and Event Emitter
// eg.
// source :=
// transporter.NewNode("source", "mongo", adaptor.Config{"uri": "mongodb://localhost/", "namespace": "boom.foo", "debug": false, "tail": true}).
// Add(transporter.NewNode("out", "file", adaptor.Config{"uri": "stdout://"}))
// pipeline, err := transporter.NewPipeline(source, events.NewNoopEmitter(), 1*time.Second, state.NewFilestore(pid, "/tmp/transporter.state"), 10*time.Second)
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// pipeline.Run()
func NewPipeline(source *Node, emitter events.Emitter, interval time.Duration, sessionStore state.SessionStore, sessionInterval time.Duration) (*Pipeline, error) {
pipeline := &Pipeline{
source: source,
emitter: emitter,
metricsTicker: time.NewTicker(interval),
}
if sessionStore != nil {
pipeline.sessionStore = sessionStore
pipeline.sessionTicker = time.NewTicker(sessionInterval)
}
// init the pipeline
err := pipeline.source.Init(interval)
if err != nil {
return pipeline, err
}
// init the emitter with the right chan
pipeline.emitter.Init(source.pipe.Event)
// start the emitters
go pipeline.startErrorListener(source.pipe.Err)
go pipeline.startMetricsGatherer()
if sessionStore != nil {
pipeline.initState()
go pipeline.startStateSaver()
}
pipeline.emitter.Start()
return pipeline, nil
}
func (pipeline *Pipeline) String() string {
out := pipeline.source.String()
return out
}
// Stop sends a stop signal to the emitter and all the nodes, whether they are running or not.
// the node's database adaptors are expected to clean up after themselves, and stop will block until
// all nodes have stopped successfully
func (pipeline *Pipeline) Stop() {
pipeline.source.Stop()
pipeline.emitter.Stop()
if pipeline.sessionStore != nil {
pipeline.sessionTicker.Stop()
}
pipeline.metricsTicker.Stop()
}
// Run the pipeline
func (pipeline *Pipeline) Run() error {
endpoints := pipeline.source.Endpoints()
// send a boot event
pipeline.source.pipe.Event <- events.NewBootEvent(time.Now().Unix(), VERSION, endpoints)
// start the source
err := pipeline.source.Start()
if err != nil && pipeline.Err == nil {
pipeline.Err = err // only set it if it hasn't been set already.
}
// pipeline has stopped, emit one last round of metrics and send the exit event
pipeline.emitMetrics()
if pipeline.sessionStore != nil {
pipeline.setState()
}
pipeline.source.pipe.Event <- events.NewExitEvent(time.Now().Unix(), VERSION, endpoints)
// the source has exited, stop all the other nodes
pipeline.Stop()
return pipeline.Err
}
// start error listener consumes all the events on the pipe's Err channel, and stops the pipeline
// when it receives one
func (pipeline *Pipeline) startErrorListener(cherr chan error) {
for err := range cherr {
if aerr, ok := err.(adaptor.Error); ok {
pipeline.source.pipe.Event <- events.NewErrorEvent(time.Now().Unix(), aerr.Path, aerr.Record, aerr.Error())
if aerr.Lvl == adaptor.ERROR || aerr.Lvl == adaptor.CRITICAL {
log.Println(aerr)
}
} else {
if pipeline.Err == nil {
pipeline.Err = err
}
pipeline.Stop()
}
}
}
func (pipeline *Pipeline) startMetricsGatherer() {
for _ = range pipeline.metricsTicker.C {
pipeline.emitMetrics()
}
}
// emit the metrics
func (pipeline *Pipeline) emitMetrics() {
frontier := make([]*Node, 1)
frontier[0] = pipeline.source
for {
// pop the first item
node := frontier[0]
frontier = frontier[1:]
// do something with the node
pipeline.source.pipe.Event <- events.NewMetricsEvent(time.Now().Unix(), node.Path(), node.pipe.MessageCount)
// add this nodes children to the frontier
for _, child := range node.Children {
frontier = append(frontier, child)
}
// if we're empty
if len(frontier) == 0 {
break
}
}
}
func (pipeline *Pipeline) startStateSaver() {
for _ = range pipeline.sessionTicker.C {
pipeline.setState()
}
}
func (pipeline *Pipeline) setState() {
frontier := make([]*Node, 1)
frontier[0] = pipeline.source
for {
// pop the first item
node := frontier[0]
frontier = frontier[1:]
// do something with the node
if node.Type != "transformer" && node.pipe.LastMsg != nil {
pipeline.sessionStore.Set(node.Path(), &state.MsgState{Msg: node.pipe.LastMsg, Extra: node.pipe.ExtraState})
}
// add this nodes children to the frontier
for _, child := range node.Children {
frontier = append(frontier, child)
}
// if we're empty
if len(frontier) == 0 {
break
}
}
}
func (pipeline *Pipeline) initState() {
frontier := make([]*Node, 1)
frontier[0] = pipeline.source
for {
// pop the first item
node := frontier[0]
frontier = frontier[1:]
// do something with the node
if node.Type != "transformer" {
nodeState, _ := pipeline.sessionStore.Get(node.Path())
if nodeState != nil {
node.pipe.LastMsg = nodeState.Msg
node.pipe.ExtraState = nodeState.Extra
}
}
// add this nodes children to the frontier
for _, child := range node.Children {
frontier = append(frontier, child)
}
// if we're empty
if len(frontier) == 0 {
break
}
}
}