-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
451 lines (421 loc) · 13.2 KB
/
runner.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
package gobulk
import (
"context"
"errors"
"fmt"
"sync"
"time"
"go.uber.org/zap"
)
const (
runnerBulkProcessingMetricName = "runner_bulk_processing"
runnerReadBulkMetricName = "runner_read_bulk"
)
var (
errFormatIsIntermitted = errors.New("format is intermitted")
)
// RunnerConfig represents a structure for the Runner config.
type RunnerConfig struct {
Format Format
ProcessIDPrefix string
ListenerState SwitcherState
ScanFromScratch bool
ProcessState SwitcherState
ParseChunkSize int
PlanChunkSize int
LoaderWorkersLimit int
}
// Validate validates the RunnerConfig fields.
func (c *RunnerConfig) Validate() error {
if c.ParseChunkSize <= 0 {
return fmt.Errorf("invalid ParseChunkSize value %d: should be more than 0", c.ParseChunkSize)
}
if c.PlanChunkSize <= 0 {
return fmt.Errorf("invalid PlanChunkSize value %d: should be more than 0", c.PlanChunkSize)
}
return nil
}
// NewRunner returns a preconfigured runner struct.
func NewRunner(ctx context.Context, cfg RunnerConfig) (*Runner, error) {
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("the passed RunnerConfig is invalid: %v", err)
}
logger := cfg.Format.Logger()
processID := buildProcessID(cfg.ProcessIDPrefix, cfg.Format.Name())
tracker, input, output, err := initFormatStorages(cfg.Format, ctx, processID, logger)
if err != nil {
return nil, err
}
iteration, err := GetIteration(cfg.Format, tracker)
if err != nil {
return nil, err
}
var marker *Container
if !cfg.ScanFromScratch {
marker = iteration.LastTrackedContainer
}
listener, err := NewListener(iteration, tracker, input, marker, logger, cfg.ListenerState, cfg.LoaderWorkersLimit)
if err != nil {
return nil, err
}
metricsTracker := cfg.Format.MetricsTracker()
metricsTracker.Add(runnerBulkProcessingMetricName, "Time taken to process a single containers bulk")
metricsTracker.Add(runnerReadBulkMetricName, "Time taken to read a single containers bulk")
reader := NewReader(tracker, input, iteration.Format.ContainerBulkSize(), iteration.Format.ReadStrategy(), logger, metricsTracker)
parser := NewParser(iteration.Format.Parse, cfg.ParseChunkSize, metricsTracker, logger)
planner := NewPlanner(iteration.Format.Plan, cfg.PlanChunkSize, metricsTracker, logger)
executor := NewExecutor(tracker, output, iteration.Format.ExecutorBulkSize(), logger, metricsTracker)
return &Runner{
Iteration: iteration,
Listener: listener,
Reader: reader,
Parser: parser,
Planner: planner,
Executor: executor,
switcher: newSwitcher(cfg.ProcessState),
metrics: metricsTracker,
logger: logger,
}, nil
}
// Run executes one or multiple runners. Once one runner has processed everything and is idle, the next runner starts.
// The interval defines how long to wait between walk throughs.
func Run(ctx context.Context, interval time.Duration, runners ...*Runner) error {
controller := newRunnersController(runners)
defer controller.ShutdownRunners()
for {
start := time.Now()
if err := controller.ExecRunners(ctx); err != nil {
return err
}
if interval == 0 {
return nil
}
if time.Since(start) < interval {
time.Sleep(interval)
}
}
}
// Runner bundles the logic to retrieve a Container from Tracker, parse, plan and finally import it.
type Runner struct {
Iteration *Iteration
Listener *Listener
Reader *Reader
Parser *Parser
Planner *Planner
Executor *Executor
*switcher
metrics MetricsTracker
logger *zap.Logger
}
// run in accordance with configuration runs or idles listen and import processes
// and keeps them switchable.
func (r *Runner) run(outCtx context.Context) error {
r.logger.Info(fmt.Sprintf("runner is running"))
var runErr error
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(outCtx)
wg.Add(1)
go func() {
defer wg.Done()
if err := r.Listener.Listen(ctx); err != nil {
r.logger.Error(fmt.Sprintf("listener failed with an error: %v", err))
cancel()
runErr = err
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := r.runImport(ctx); err != nil && err != errFormatIsIntermitted {
r.logger.Error(fmt.Sprintf("import failed with an error: %v", err))
runErr = err
}
cancel()
}()
wg.Wait()
return runErr
}
// runImport listens for the Reader to get new containers and processes all the gotten containers.
func (r *Runner) runImport(ctx context.Context) error {
if r.Listener.IsOn() && !r.Listener.Ready() {
if canceled := !r.waitForListenerTurnOn(ctx); canceled {
return nil
}
}
if r.IsOff() {
if canceled := !r.waitForTurnOn(ctx); canceled {
return nil
}
}
if r.Iteration.Format.ExecutionIsIntermitted() {
r.logger.Info(fmt.Sprintf("format %s is intermitted", r.Iteration.Format.Name()))
return errFormatIsIntermitted
} else {
if intermittence, err := r.Iteration.Format.ExecutionShouldBeIntermitted(); err != nil {
return err
} else if intermittence != nil {
r.Iteration.Format.SetIntermitUntil(intermittence)
r.logger.Info(fmt.Sprintf("format %s has been set to intermitted until %s", r.Iteration.Format.Name(), intermittence.Format(time.RFC1123Z)))
return errFormatIsIntermitted
}
}
for {
if r.IsOff() {
if canceled := !r.waitForTurnOn(ctx); canceled {
return nil
}
} else {
select {
case <-r.Off():
r.logger.Info("import has been turned off")
case <-ctx.Done():
r.logger.Info("import has been stopped by context")
return nil
default:
readStart := time.Now()
readResult, err := r.Reader.NextContainersBulk()
if err := r.handleProcessContainersResult(StepReader, readResult, err); err != nil {
return err
}
if readResult == nil || len(readResult.Succeeded) == 0 {
if err == nil {
r.logger.Info("import has been stopped: all existing containers have been imported")
return nil
}
continue
}
r.metrics.Set(runnerReadBulkMetricName, fmt.Sprintf("%d", time.Since(readStart).Microseconds()))
bulkProcessStart := time.Now()
parseResult, err := r.Parser.ParseBulkElements(readResult.Succeeded)
if err := r.handleProcessContainersResult(StepParser, parseResult, err); err != nil {
return err
}
if parseResult == nil || len(parseResult.Succeeded) == 0 {
continue
}
planResult, err := r.Planner.PlanBulkOperations(parseResult.Succeeded)
if err := r.handleProcessContainersResult(StepPlanner, planResult, err); err != nil {
return err
}
if planResult == nil || len(planResult.Succeeded) == 0 {
continue
}
r.completeContainerOperations(planResult.Succeeded)
failed, err := r.Executor.Import(planResult.Succeeded)
if err != nil {
return err
}
if err := r.handleImportIssues(StepExecutor, failed); err != nil {
return err
}
r.metrics.Set(runnerBulkProcessingMetricName, fmt.Sprintf("%d", time.Since(bulkProcessStart).Microseconds()))
}
}
}
}
// waitForTurnOn waits for a turn on switch for the runner and returns true, if it happened, and
// false, if the ctx has been stopped during the wait.
func (r *Runner) waitForTurnOn(ctx context.Context) bool {
r.logger.Info("import is off and waiting to be launched")
select {
case <-r.On():
r.logger.Info("starting import")
return true
case <-ctx.Done():
r.logger.Info("import has been stopped by context")
return false
}
}
// waitForListenerTurnOn waits for a turn on switch for the runner's listener and returns true, if
// it happened, and false, if the ctx has been stopped during the wait.
func (r *Runner) waitForListenerTurnOn(ctx context.Context) bool {
r.logger.Info("import is waiting for the listener to be ready")
select {
case <-r.Listener.ReadyChan():
r.logger.Info("import is no more blocked by listener")
return true
case <-ctx.Done():
r.logger.Info("import has been stopped by context")
return false
}
}
// setup setups the runner. It's called only once before executing the runner.
func (r *Runner) setup() error {
r.logger.Info("setting up the runner")
if err := r.Iteration.Format.Setup(); err != nil {
return err
}
r.logger.Info("runner has been set up")
return nil
}
// beforeRun runs BeforeRun for the runner storages.
func (r *Runner) beforeRun() error {
r.logger.Info("preparing the runner for a run")
if err := r.Iteration.Input.BeforeRun(); err != nil {
return err
}
if err := r.Iteration.Tracker.BeforeRun(); err != nil {
return err
}
if err := r.Iteration.Output.BeforeRun(); err != nil {
return err
}
r.Listener.Prepare()
r.logger.Info("runner has been prepared for a run")
return nil
}
// afterRun runs AfterRun for the runner storages and resets the runner listener.
func (r *Runner) afterRun() error {
r.logger.Info("finishing the runner after a run")
if err := r.Iteration.Input.AfterRun(); err != nil {
return err
}
if err := r.Iteration.Tracker.AfterRun(); err != nil {
return err
}
if err := r.Iteration.Output.AfterRun(); err != nil {
return err
}
r.Listener.Reset()
r.logger.Info("runner has been finished after a run")
return nil
}
// shutdown runs Shutdown for the runner storages.
func (r *Runner) shutdown() {
r.logger.Info("shutting down the runner")
r.Iteration.Input.Shutdown()
r.Iteration.Output.Shutdown()
r.Iteration.Tracker.Shutdown()
r.logger.Info("runner has been shut down")
}
// handleProcessContainersResult checks the process result and the corresponding error. If there is
// an error or an error occurred later during tracking the result issues, it's returned.
func (r *Runner) handleProcessContainersResult(step Step, result *ProcessContainersResult, err error) error {
if err != nil {
issue, ok := err.(*Issue)
if !ok {
return err
}
issue.complete(r.Iteration, nil, step)
if r.Iteration.Format.StopOnError() {
return issue
}
if err := r.Iteration.Tracker.TrackIssue(issue); err != nil {
return fmt.Errorf("failed to track issue: %v", err)
}
return nil
}
if result != nil && len(result.Failed) != 0 {
if err := r.handleImportIssues(step, result.Failed); err != nil {
return err
}
}
return nil
}
// completeOperations follows up containers operations fields population with the runner iteration
// and the corresponding container.
func (r *Runner) completeContainerOperations(containers []*Container) {
for _, container := range containers {
for _, operation := range container.Operations {
operation.Iteration = r.Iteration
operation.Container = container
}
}
}
// handleImportIssues populates the issues container and iteration id fields and tracks the result
// issues using the runner tracker.
func (r *Runner) handleImportIssues(step Step, issues ContainerIssues) error {
for container, containerIssues := range issues {
for _, issue := range containerIssues {
issue.complete(r.Iteration, container, step)
if r.Iteration.Format.StopOnError() {
return issue
}
if err := r.Iteration.Tracker.TrackIssue(issue); err != nil {
return fmt.Errorf("failed to track %d container issue: %v", container.ID, err)
}
}
}
return nil
}
// initFormatStorages returns the initialised format tracker, input and output.
func initFormatStorages(format Format, ctx context.Context, processID string, logger *zap.Logger) (Tracker, Input, Output, error) {
tracker := format.Tracker()
if err := initStorage(tracker, ctx, processID, logger); err != nil {
return nil, nil, nil, err
}
input := format.Input()
if err := initStorage(input, ctx, processID, logger); err != nil {
return nil, nil, nil, err
}
output := format.Output()
if err := initStorage(output, ctx, processID, logger); err != nil {
return nil, nil, nil, err
}
return tracker, input, output, nil
}
// buildProcessID concatenates the configured process id prefix with the current format name
// as the result process id.
func buildProcessID(prefix, formatName string) string {
if prefix != "" {
return prefix + "_" + formatName
}
return formatName
}
// newRunnersController creates a new instance of the runnersController.
func newRunnersController(runners []*Runner) *runnersController {
c := &runnersController{runners: make([]*runnerState, 0, len(runners))}
for _, r := range runners {
c.runners = append(c.runners, newRunnerState(r))
}
return c
}
// runnersController wraps interaction with runners.
type runnersController struct {
runners []*runnerState
}
// ExecRunner runs all the runners along with all the runners auxiliary functions.
func (c *runnersController) ExecRunners(ctx context.Context) error {
for _, runner := range c.runners {
if !runner.IsSetup() {
if err := runner.setup(); err != nil {
return err
}
runner.MarkAsSetup()
}
if err := runner.beforeRun(); err != nil {
return err
}
err := runner.run(ctx)
if err != nil {
return err
}
if err := runner.afterRun(); err != nil {
return err
}
}
return nil
}
// ShutdownRunners calls shutdown func for the runners which have been setup.
func (c *runnersController) ShutdownRunners() {
for _, runner := range c.runners {
runner.shutdown()
}
}
// newRunnerState wraps the runner with a state.
func newRunnerState(runner *Runner) *runnerState {
return &runnerState{runner, false}
}
// runnerState enhances a Runner with state properties.
type runnerState struct {
*Runner
isSetup bool
}
// IsSetup returns whether the runner has been setup.
func (s *runnerState) IsSetup() bool {
return s.isSetup
}
// MarkAsSetup marks the runner as setup.
func (s *runnerState) MarkAsSetup() {
s.isSetup = true
}