-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestration.go
527 lines (463 loc) · 11.9 KB
/
orchestration.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Copyright 2024 Outreach Corporation. All Rights Reserved.
// Description: This file contains task orchestration code
package plumber
import (
"context"
"errors"
"sync"
"sync/atomic"
"golang.org/x/sync/errgroup"
)
// DoneFunc is a func used to report that worker has finished
type DoneFunc func(error)
// ReadyFunc is a func used to report that worker is ready
type ReadyFunc func()
// Done reports back runner status using given error
func (f DoneFunc) Done(do func() error) {
f(do())
}
// Success reports back runner status as success. It is preferred then running Done(nil) to increase code readability.
func (f DoneFunc) Success() {
f(nil)
}
// ErrorCh is a channel of errors
type ErrorCh chan error
// Errors returns errors accumulated in the channel. This function blockes until the channel is closed
func (ec ErrorCh) Errors() []error {
errs := []error{}
for err := range ec {
errs = append(errs, err)
}
return errs
}
// Wait waits till channel is not close or till given context is ended and then returns all accumulated errors.
func (ec ErrorCh) Wait(ctx context.Context) []error {
var (
errs = []error{}
wg sync.WaitGroup
)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case err, ok := <-ec:
if !ok {
return
}
errs = append(errs, err)
}
}
}()
wg.Wait()
return errs
}
// CallbackFunc a callback function type for graceful runner
type CallbackFunc func(context.Context) error
// ReadyRunner creates a Runner based on supplied run function with callback to signal ready state
func ReadyRunner(run func(ctx context.Context, ready ReadyFunc) error) Runner {
signal := NewSignal()
return NewRunner(func(ctx context.Context) error {
return run(ctx, func() {
signal.Notify()
})
}, WithReady(signal))
}
// Closer creates a runner implementing Closer interface based on supplied close function with noop Run method
func Closer(closeFunc CallbackFunc) Runner {
return NewRunner(func(ctx context.Context) error {
<-ctx.Done()
return nil
}, WithClose(closeFunc))
}
// ParallelPipeline is a parallel runner closer orchestrator
// The runners are started and closed in concurrent fashion.
// The Run or Close are invoked independently
type ParallelPipeline struct {
runners []Runner
wg sync.WaitGroup
options *PipelineOptions
closing atomic.Bool
closed chan struct{}
closeOnce sync.Once
signal *Signal
errSignal *Signal
}
// Parallel creates a concurrent Runner executor.
// When started it will execute runners Run and Close methods in parallel.
// Run and Close will block till all runner's corresponding methods are returned.
func Parallel(runners ...Runner) *ParallelPipeline {
return &ParallelPipeline{
runners: runners,
options: &PipelineOptions{},
signal: NewSignal(),
errSignal: NewSignal(),
closed: make(chan struct{}),
}
}
func (r *ParallelPipeline) Errored() <-chan struct{} {
return r.errSignal.C()
}
func (r *ParallelPipeline) Ready() (<-chan struct{}, error) {
return r.signal.C(), nil
}
// Run executes Run method on internal runners in parallel.
// It partially implement Runner interface.
// The it returns when all runner's Run methods are returned.
func (r *ParallelPipeline) Run(ctx context.Context) error {
var (
readyCh = make(chan struct{}, len(r.runners))
errs = make(ErrorCh, len(r.runners))
)
r.wg.Add(len(r.runners))
go func() {
var counter = 0
for {
select {
case <-readyCh:
counter++
case <-r.closed:
break
case <-ctx.Done():
break
}
if counter == len(r.runners) {
r.signal.Notify()
break
}
}
r.wg.Wait()
close(errs)
}()
if !r.options.KeepRunningOnError {
closeOnError(ctx, r.errSignal, r)
}
for _, runner := range r.runners {
go func(runner Runner) {
defer r.wg.Done()
// Wait for the runner to be ready
go func() {
ready, err := RunnerReady(runner)
if err != nil {
errs <- err
r.Close(ctx)
return
}
select {
case <-ready:
readyCh <- struct{}{}
case <-ctx.Done():
}
}()
go forwardErrorSignal(ctx, runner, r.closed, r.errSignal)
err := runner.Run(ctx)
if err != nil && !r.closing.Load() {
r.errSignal.Notify()
}
errs <- err
}(runner)
}
return errors.Join(errs.Errors()...)
}
// Close executes Close method on internal runners in in parallel.
// It partially implement Closer interface.
// The it returns when all runner's Close methods are returned.
func (r *ParallelPipeline) Close(ctx context.Context) error {
r.closeOnce.Do(func() {
close(r.closed)
})
r.closing.Store(true)
var (
closeErrors = make(ErrorCh, len(r.runners))
wg sync.WaitGroup
)
wg.Add(len(r.runners))
for i := len(r.runners) - 1; i >= 0; i-- {
var runner = r.runners[i]
go func() {
defer wg.Done()
if err := RunnerClose(ctx, runner); err != nil {
closeErrors <- err
}
}()
}
wg.Wait()
close(closeErrors)
return errors.Join(closeErrors.Errors()...)
}
// With applies the pipeline options
func (r *ParallelPipeline) With(oo ...PipelineOption) *ParallelPipeline {
r.options.Apply(oo...)
return r
}
// SerialPipeline is a serial runner closer orchestrator
// The runners are started and closed in serial fashion.
// The Run or Close methods needs to return and only then next runner is evaluated
type SerialPipeline struct {
runners []Runner
options *PipelineOptions
closing atomic.Bool
closed chan struct{}
closeOnce sync.Once
signal *Signal
errSignal *Signal
}
// Pipeline creates a serial Runner executor.
// When started it will execute Run method on given runners one by one with given order.
// When closed it will execute Close method on given runners in revered order to achieve graceful shutdown sequence
func Pipeline(runners ...Runner) *SerialPipeline {
return &SerialPipeline{
runners: runners,
options: &PipelineOptions{},
closed: make(chan struct{}),
signal: NewSignal(),
errSignal: NewSignal(),
}
}
func (r *SerialPipeline) Errored() <-chan struct{} {
return r.errSignal.C()
}
func (r *SerialPipeline) Ready() (<-chan struct{}, error) {
return r.signal.C(), nil
}
// Run executes Run method on internal runners one by one with given order.
func (r *SerialPipeline) Run(ctx context.Context) error {
var (
wg sync.WaitGroup
errs = make(ErrorCh, len(r.runners))
readyCh = make(chan struct{}, 1)
errored atomic.Bool
)
if !r.options.KeepRunningOnError {
closeOnError(ctx, r.errSignal, r)
}
// orchestration go routine
go func() {
defer wg.Done()
var index = 0
for {
select {
case _, ok := <-readyCh:
// We are closed
if !ok {
return
}
// when all runners are running we cal report that pipeline is ready
if index == len(r.runners) {
r.signal.Notify()
return
}
// We need to check those again since select does not guarantee the priority
select {
case <-r.closed:
case <-ctx.Done():
default:
runner := r.runners[index]
index++
wg.Add(1)
// runner go routine
go func() {
defer wg.Done()
if errored.Load() && r.closing.Load() {
return
}
wg.Add(1)
// Wait for the runner to become ready
// ready checking goroutine
go func() {
defer wg.Done()
ready, err := RunnerReady(runner)
if err != nil {
errs <- err
r.Close(ctx)
return
}
select {
case <-r.closed:
case <-ctx.Done():
case <-ready:
readyCh <- struct{}{}
}
}()
go forwardErrorSignal(ctx, runner, r.closed, r.errSignal)
err := runner.Run(ctx)
if err != nil && !r.closing.Load() {
r.errSignal.Notify()
}
if err != nil {
errored.Store(true)
errs <- err
}
}()
}
case <-r.closed:
return
case <-ctx.Done():
return
}
}
}()
// Lets start first worker
wg.Add(1)
readyCh <- struct{}{}
wg.Wait()
close(errs)
close(readyCh)
return errors.Join(errs.Errors()...)
}
// Close executes Close method on internal runners in revered order to achieve graceful shutdown sequence
// It implements Closer interface
func (r *SerialPipeline) Close(ctx context.Context) error {
var closeErrors []error
r.closeOnce.Do(func() {
close(r.closed)
for i := len(r.runners) - 1; i >= 0; i-- {
var runner = r.runners[i]
if err := RunnerClose(ctx, runner); err != nil {
closeErrors = append(closeErrors, err)
}
}
})
r.closing.Store(true)
return errors.Join(closeErrors...)
}
// With applies the pipeline options
func (r *SerialPipeline) With(oo ...PipelineOption) *SerialPipeline {
r.options.Apply(oo...)
return r
}
// Start will execute given runner with optional configuration
func Start(ctx context.Context, runner Runner, opts ...Option) error {
var (
options = &Options{}
)
startCtx, startCancel := context.WithCancelCause(ctx)
defer startCancel(nil)
closers := newCloserContext(startCtx)
defer closers.cancel()
var (
errorCh = make(ErrorCh, 3)
closeCh = make(chan struct{}, 1)
once sync.Once
chanWriters sync.WaitGroup
closeChannels = func() {
closers.cancel()
close(closeCh)
chanWriters.Wait()
// We can really terminate since all channel writers are done
close(errorCh)
}
terminate = func(ctx context.Context, initiateClose bool) {
once.Do(func() {
if !initiateClose {
closeChannels()
return
}
closeCtx, closeCancel := options.closeContext(ctx)
defer closeCancel()
// go routine handling close async so it can be canceled
go func() {
err := RunnerClose(closeCtx, runner)
startCancel(closeCtx.Err())
errorCh <- err
closeChannels()
}()
select {
// Wait for close to finish
case <-closeCh:
break
// Wait for close context to be canceled
case <-closeCtx.Done():
startCancel(closeCtx.Err())
if err := closeCtx.Err(); err != nil {
errorCh <- err
}
break
}
})
}
)
chanWriters.Add(2)
options.close = func() {
go func() {
terminate(ctx, true)
}()
}
options.Apply(opts...)
closers.start(errorCh, &chanWriters, options.closers...)
if propagator, ok := runner.(ErrorNotifier); ok {
go func() {
select {
case <-startCtx.Done():
return
case <-propagator.Errored():
go terminate(ctx, true)
}
}()
}
// runner go routine
go func() {
defer chanWriters.Done()
err := runner.Run(startCtx)
if err != nil {
// runner sequence had a problems calling close
errorCh <- err
}
go terminate(ctx, false)
}()
return errors.Join(errorCh.Wait(ctx)...)
}
// newCloserContext return instance of *closerContext
func newCloserContext(startCtx context.Context) *closerContext {
var erg, closerCtx = errgroup.WithContext(startCtx)
closerCtx, closerCancel := context.WithCancel(closerCtx)
return &closerContext{
erg: erg,
cancel: closerCancel,
ctx: closerCtx,
}
}
// closerContext a helper struct managing pipeline closers
type closerContext struct {
erg *errgroup.Group
ctx context.Context
cancel func()
}
// start starts closers functions and captures an error
func (c *closerContext) start(errorCh chan error, chanWriters *sync.WaitGroup, closers ...func(context.Context) error) {
for _, closer := range closers {
closer := closer
c.erg.Go(func() error {
return closer(c.ctx)
})
}
// closers go routine
go func() {
defer chanWriters.Done()
err := c.erg.Wait()
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
errorCh <- err
}
}()
}
// PipelineOptions holds a pipeline options
type PipelineOptions struct {
KeepRunningOnError bool
}
// PipelineOption is a option patter struct for PipelineOptions
type PipelineOption func(*PipelineOptions)
// Apply given PipelineOption
func (o *PipelineOptions) Apply(oo ...PipelineOption) *PipelineOptions {
for _, op := range oo {
op(o)
}
return o
}
// KeepWhenErrored make the pipeline
func KeepRunningOnError() func(*PipelineOptions) {
return func(o *PipelineOptions) {
o.KeepRunningOnError = true
}
}