-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
pipe.go
329 lines (286 loc) · 8.57 KB
/
pipe.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package machine
import (
"context"
"fmt"
"net/http"
"os"
"sync"
"time"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/sirupsen/logrus"
)
var defaultLogger = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.WarnLevel,
}
// Subscription is an interface for creating a pull based stream.
// It requires 2 methods Read and Close.
//
// Read is called when the interval passes and the resulting
// payload is sent down the Stream.
//
// Close is called during a graceful termination and any errors
// are logged.
type Subscription interface {
Read(ctx context.Context) []Data
Close() error
}
// LogStore is an interface for allowing a distributed cluster of workers
// It requires 3 methods Join, Write, and Leave.
//
// Join is called during the run method of the Pipe and it is used for
// accouncing membership to the cluster with an identifier, a callback
// for injection, and the list of Streams that will be running in the pipe.
// Injection is how work can be restarted in the system and is the responsibility
// of the implementation to decide when and how it is reinitiated.
//
// Write is called at the beginning of every vertex and provides the current
// payload about to be run. The implementation is considered the owner of that
// payload and may modify the data in special known circumstances if need be.
//
// Leave is called during a graceful termination and any errors
// are logged.
type LogStore interface {
Join(id string, callback InjectionCallback, streamIDs ...string) error
Write(logs ...*Log)
Leave(id string) error
}
// InjectionCallback is a function provided to the LogStore Join method so that
// the cluster may restart work that has been dropped by one of the workers.
// Injections will only be processed for vertices that have the Injectable option
// set to true, which is the default.
type InjectionCallback func(logs ...*Log)
// Log type for holding the data that is recorded from the streams and sent to
// the LogStore instance
type Log struct {
OwnerID string `json:"owner_id"`
StreamID string `json:"stream_id"`
VertexID string `json:"vertex_id"`
VertexType string `json:"vertex_type"`
State string `json:"state"`
Packet *Packet `json:"packet"`
When time.Time `json:"when"`
}
// Pipe is the representation of the system. It can run multiple Streams and
// controls the start and stop functionality of the system.
type Pipe struct {
id string
app *fiber.App
streams map[string]Stream
healthInfo map[string]*HealthInfo
logStore LogStore
logger *logrus.Logger
}
// HealthInfo is the type used for providing basic healthcheck information
// about last start time of payloads
type HealthInfo struct {
StreamID string `json:"stream_id"`
LastPayload time.Time `json:"last_payload"`
mtx sync.Mutex
}
// Run starts the Pipe and subsequent Streams. It requires a context, a port to run
// an instance of fiber.App which hosts the /health endpoint and any HTTP based streams
// at /strea/:id, and a gracePeriod for which graceful shutdown can take place.
func (pipe *Pipe) Run(ctx context.Context, port string, gracePeriod time.Duration) error {
if len(pipe.streams) < 1 {
return fmt.Errorf("no streams found")
}
streamIDs := []string{}
for key := range pipe.streams {
streamIDs = append(streamIDs, key)
}
if err := pipe.logStore.Join(pipe.id, pipe.injectionCallback(ctx), streamIDs...); err != nil {
return err
}
for key, stream := range pipe.streams {
if err := stream.Run(ctx, pipe.recorder(key)); err != nil {
return err
}
}
go func() {
Loop:
for {
select {
case <-ctx.Done():
if err := pipe.logStore.Leave(pipe.id); err != nil {
pipe.logger.Error(err)
}
if err := pipe.app.Shutdown(); err != nil {
pipe.logger.Error(err)
}
break Loop
default:
<-time.After(gracePeriod)
}
}
}()
return pipe.app.Listen(port)
}
// Stream is a method for adding a generic developer defined Stream.
// New Streams are created by the appropriately named NewStream function.
func (pipe *Pipe) Stream(stream Stream) Builder {
id := stream.ID()
pipe.streams[id] = stream
pipe.healthInfo[id] = &HealthInfo{
StreamID: id,
}
return pipe.streams[id].Builder()
}
// StreamHTTP a method that creates a Stream at the path /stream/:id
// which is hosted by the Pipe's fiber.App
func (pipe *Pipe) StreamHTTP(id string, opts ...*Option) Builder {
channel := make(chan []Data)
pipe.app.Post("/stream/"+id, func(ctx *fiber.Ctx) error {
payload := []Data{}
packet := Data{}
if err := ctx.BodyParser(&packet); err == nil {
payload = []Data{packet}
} else if err := ctx.BodyParser(&payload); err != nil {
return ctx.SendStatus(http.StatusBadRequest)
}
now := time.Now()
go func() {
pipe.healthInfo[id].mtx.Lock()
defer pipe.healthInfo[id].mtx.Unlock()
if now.After(pipe.healthInfo[id].LastPayload) {
pipe.healthInfo[id].LastPayload = now
}
}()
channel <- deepCopy(payload)
return ctx.SendStatus(http.StatusAccepted)
})
pipe.streams[id] = NewStream(id,
func(ctx context.Context) chan []Data {
return channel
},
opts...,
)
pipe.healthInfo[id] = &HealthInfo{
StreamID: id,
}
return pipe.streams[id].Builder()
}
// StreamSubscription is a method for creating a Stream based on the provided Subscription
// which has it's Read method called at the end of each interval period.
func (pipe *Pipe) StreamSubscription(id string, sub Subscription, interval time.Duration, opts ...*Option) Builder {
channel := make(chan []Data)
pipe.streams[id] = NewStream(id,
func(ctx context.Context) chan []Data {
go func() {
Loop:
for {
select {
case <-ctx.Done():
if err := sub.Close(); err != nil {
pipe.logger.Error(map[string]interface{}{
"subscription_id": id,
"message": "error closing subsciption",
"error": err,
})
}
break Loop
case <-time.After(interval):
now := time.Now()
go func() {
pipe.healthInfo[id].mtx.Lock()
defer pipe.healthInfo[id].mtx.Unlock()
if now.After(pipe.healthInfo[id].LastPayload) {
pipe.healthInfo[id].LastPayload = now
}
}()
channel <- sub.Read(ctx)
}
}
}()
return channel
},
opts...,
)
pipe.healthInfo[id] = &HealthInfo{
StreamID: id,
}
return pipe.streams[id].Builder()
}
// Use Wraps fiber.App.Use
//
// Use registers a middleware route that will match requests with the provided prefix (which is optional and defaults to "/").
//
// app.Use(func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", handler, func(c *fiber.Ctx) error {
// return c.Next()
// })
//
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
func (pipe *Pipe) Use(args ...interface{}) {
pipe.app.Use(args...)
}
func (pipe *Pipe) recorder(streamID string) recorder {
return func(vertexID, vertexType, state string, payload []*Packet) {
logs := make([]*Log, len(payload))
now := time.Now()
for i, packet := range payload {
logs[i] = &Log{
OwnerID: pipe.id,
StreamID: streamID,
VertexID: vertexID,
VertexType: vertexType,
State: state,
Packet: packet,
When: now,
}
}
if pipe.logger != nil {
pipe.logger.Info(logs)
}
if pipe.logStore != nil {
pipe.logStore.Write(logs...)
}
}
}
func (pipe *Pipe) injectionCallback(ctx context.Context) func(logs ...*Log) {
return func(logs ...*Log) {
for _, log := range logs {
if stream, ok := pipe.streams[log.StreamID]; ok {
stream.Inject(ctx, map[string][]*Packet{
log.VertexID: {log.Packet},
})
} else {
pipe.logger.Error(map[string]interface{}{
"message": "unknown stream",
"log": log,
})
}
}
}
}
// NewPipe is a function for creating a new Pipe. If logger or logStore are nil then
// the accosiated feature will be disabled.
func NewPipe(id string, logger *logrus.Logger, store LogStore, config ...fiber.Config) *Pipe {
if logger == nil {
logger = defaultLogger
}
pipe := &Pipe{
id: id,
app: fiber.New(config...),
streams: map[string]Stream{},
healthInfo: map[string]*HealthInfo{},
logStore: store,
logger: logger,
}
pipe.Use(recover.New())
pipe.app.Get("/health", func(c *fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(map[string]interface{}{
"pipe_id": pipe.id,
"health_info": pipe.healthInfo,
})
})
return pipe
}