-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
163 lines (133 loc) · 4.22 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
package gomeetupbris
import (
"github.com/sirupsen/logrus"
"gitlab.com/priceshield/agent-gateway/api"
"strconv"
"time"
)
// pipeline models the data gathering pipeline
type pipeline struct {
in chan<- pipelineItem
sink <-chan pipelineItem
cache *cache
liveEventsFetcher *liveEventsFetcher
merger *merger
publisher *publisher
steps []pipelineStep
}
// pipelineStep models a step in the pipeline
type pipelineStep interface {
start()
stop()
}
type pipelineResult struct {
numEvents int
numSuccess int
numErrs int
duration time.Duration
}
func newPipeline(cache *cache, liveSportsEndpoint string, agentGatewayClient api.AgentGatewayClient, deltasOnly bool) *pipeline {
// liveEventsFetcher fetches provider specific live events, converts them to the agent model and add these to the provided channel
liveEventsFetcher := newLiveEventsFetcher(liveSportsEndpoint)
// the sink chan is where the pipeline finishes. Both success and error results are sent here
sink := make(chan pipelineItem, 100)
toConvert := make(chan pipelineItem, 100)
converted := make(chan pipelineItem, 100)
// converter converts provider state to the common format
converter := newConverter(toConvert, converted)
merged := make(chan pipelineItem, 100)
// merger merges new state with previous state, and outputs changes to the provided output channel
merger := newMerger(converted, merged, sink)
// publisher receives outgoing deltas (from deltaOutputChan), publishes them to the agent-gateway service, and publishes the result to the sink
publisher := newPublisher(merged, sink, agentGatewayClient, deltasOnly)
steps := []pipelineStep{
converter,
merger,
publisher,
}
return &pipeline{
in: toConvert,
sink: sink,
cache: cache,
liveEventsFetcher: liveEventsFetcher,
merger: merger,
publisher: publisher,
steps: steps,
}
}
type pipelineItem struct {
providerEvent ProviderEvent // a provider event
prevState *api.AgentEvent // The state from any previous run
converted api.AgentEvent // the converted event
merged *api.AgentEvent // the merged event
delta *api.AgentEvent // the delta from the previous state
err error // any error that occurs in the pipeline
}
func (p *pipeline) run() pipelineResult {
logrus.Info("running_pipeline")
startTime := time.Now()
// start all pipeline steps. chans at the ready
for _, ps := range p.steps {
go ps.start()
}
// ensure we stop them all when we finish
defer func() {
for _, ps := range p.steps {
go ps.stop()
}
}()
pr := pipelineResult{}
// fetch live events. This has to happen first, in order to know how many pipeline items to later wait for
providerEvents, err := p.liveEventsFetcher.run()
if err != nil {
pr.numErrs++
logrus.WithError(err).Error("fetch_live_events")
return pr
}
for _, pe := range providerEvents {
// find the related cache item (if any)
// do this here whilst in a single goroutine before we fanout
var prevState *api.AgentEvent
provEventID := strconv.FormatInt(pe.MasterEventID, 10)
ae, ok := p.cache.get(provEventID)
if ok {
prevState = &ae
}
// each element can enter the pipeline asynchronously
go func(pe ProviderEvent) {
pi := pipelineItem{
providerEvent: pe,
prevState: prevState,
}
// pass to the first step in the pipeline
p.in <- pi
}(pe)
}
pr.numEvents = len(providerEvents)
// wait for the pipeline items to finish. note that both successes and failures are written to the sink chan
// read the sink items in a single goroutine. This allows safe writing to the cache
rcvd := 0
for pi := range p.sink {
rcvd++
if pi.err != nil {
logrus.WithError(pi.err).Error()
pr.numErrs++
if rcvd == pr.numEvents {
break
}
continue
}
pr.numSuccess++
p.cache.set(*pi.merged)
if rcvd == pr.numEvents {
break
}
}
pr.duration = time.Since(startTime)
logrus.WithField("numEvents", pr.numEvents).
WithField("numSuccess", pr.numSuccess).
WithField("numErrs", pr.numErrs).
WithField("duration (ms)", pr.duration.Nanoseconds()/int64(time.Millisecond)).
Info("pipeline_completed")
return pr
}