-
Notifications
You must be signed in to change notification settings - Fork 2
/
stage.go
246 lines (198 loc) · 6 KB
/
stage.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 stage
import (
"context"
"expvar"
"github.com/thalesfsp/concurrentloop"
"github.com/thalesfsp/customerror"
"github.com/thalesfsp/status"
"github.com/thalesfsp/sypl"
"github.com/thalesfsp/sypl/level"
"github.com/thalesfsp/validation"
"github.com/thalesfsp/etler/v2/internal/customapm"
"github.com/thalesfsp/etler/v2/internal/logging"
"github.com/thalesfsp/etler/v2/internal/metrics"
"github.com/thalesfsp/etler/v2/processor"
)
//////
// Consts, vars and types.
//////
// Type of the entity.
const Type = "stage"
// Stage definition.
type Stage[In, Out any] struct {
// Description of the processor.
Description string `json:"description"`
// Conversor to be used in the stage.
Conversor concurrentloop.MapFunc[In, Out] `json:"-" validate:"required"`
// Logger is the pipeline logger.
Logger sypl.ISypl `json:"-" validate:"required"`
// Name of the stage.
Name string `json:"name" validate:"required"`
// OnFinished is the function that is called when a processor finishes its
// execution.
OnFinished OnFinished[In, Out] `json:"-"`
// Processors to be run in the stage.
Processors []processor.IProcessor[In] `json:"processors" validate:"required,gt=0"`
// Progress of the stage.
Progress int `json:"progress"`
// Metrics.
CounterCreated *expvar.Int `json:"counterCreated"`
CounterRunning *expvar.Int `json:"counterRunning"`
CounterFailed *expvar.Int `json:"counterFailed"`
CounterDone *expvar.Int `json:"counterDone"`
Status *expvar.String `json:"status"`
}
//////
// Methods.
//////
// GetDescription returns the `Description` of the processor.
func (s *Stage[In, Out]) GetDescription() string {
return s.Description
}
// GetLogger returns the `Logger` of the processor.
func (s *Stage[In, Out]) GetLogger() sypl.ISypl {
return s.Logger
}
// GetName returns the `Name` of the stage.
func (s *Stage[In, Out]) GetName() string {
return s.Name
}
// GetCounterCreated returns the `CounterCreated` of the processor.
func (s *Stage[In, Out]) GetCounterCreated() *expvar.Int {
return s.CounterCreated
}
// GetCounterRunning returns the `CounterRunning` of the processor.
func (s *Stage[In, Out]) GetCounterRunning() *expvar.Int {
return s.CounterRunning
}
// GetCounterFailed returns the `CounterFailed` of the processor.
func (s *Stage[In, Out]) GetCounterFailed() *expvar.Int {
return s.CounterFailed
}
// GetCounterDone returns the `CounterDone` of the processor.
func (s *Stage[In, Out]) GetCounterDone() *expvar.Int {
return s.CounterDone
}
// GetStatus returns the `Status` metric.
func (s *Stage[In, Out]) GetStatus() *expvar.String {
return s.Status
}
// GetOnFinished returns the `OnFinished` function.
func (s *Stage[In, Out]) GetOnFinished() OnFinished[In, Out] {
return s.OnFinished
}
// SetOnFinished sets the `OnFinished` function.
func (s *Stage[In, Out]) SetOnFinished(onFinished OnFinished[In, Out]) {
s.OnFinished = onFinished
}
// GetType returns the entity type.
func (s *Stage[In, Out]) GetType() string {
return Type
}
// Run the transform function.
func (s *Stage[In, Out]) Run(ctx context.Context, in []In) ([]Out, error) {
//////
// Observability: logging, metrics, and tracing.
//////
tracedContext, span := customapm.Trace(
ctx,
Type,
s.GetName(),
status.Runnning,
s.Logger,
s.CounterRunning,
)
defer span.End()
// Update the status.
s.GetStatus().Set(status.Runnning.String())
//////
// Stage's processors.
//////
// Initialize the output.
out := make([]Out, 0)
// Store in as reference to be used as the input of the next stage.
retroFeedIn := in
// NOTE: It process the data sequentially.
for _, proc := range s.Processors {
// Re-use the output of the previous stage as the input of the
// next stage ensuring that the data is processed sequentially.
rFI, err := proc.Run(tracedContext, retroFeedIn)
if err != nil {
// Observability: logging, metrics.
s.GetStatus().Set(status.Failed.String())
// Returns whatever is in `out` and the error.
//
// Don't need tracing, it's already traced.
return out, customapm.TraceError(
tracedContext,
customerror.New(
"failed to run processor",
customerror.WithError(err),
customerror.WithField(Type, s.Name),
),
s.GetLogger(),
s.GetCounterFailed(),
)
}
// Update the input with the output.
retroFeedIn = rFI
}
//////
// Stage's conversor.
//////
mapOut, errs := concurrentloop.Map(tracedContext, retroFeedIn, s.Conversor)
if errs != nil {
// Observability: logging, metrics.
s.GetStatus().Set(status.Failed.String())
// Returns whatever is in `out` and the error.
//
// Observability: logging, metrics, and tracing.
return out, customapm.TraceError(
tracedContext,
customerror.NewFailedToError(
"convert",
customerror.WithError(errs),
customerror.WithField(Type, s.Name),
),
s.GetLogger(),
s.GetCounterFailed(),
)
}
// Observability: logging, metrics.
s.GetStatus().Set(status.Done.String())
s.GetCounterDone().Add(1)
// Run onEvent callback.
if s.GetOnFinished() != nil {
s.GetOnFinished()(ctx, s, in, mapOut)
}
return mapOut, nil
}
//////
// Factory.
//////
// New returns a new stage.
func New[In, Out any](
name string,
conversor concurrentloop.MapFunc[In, Out],
processors ...processor.IProcessor[In],
) (IStage[In, Out], error) {
s := &Stage[In, Out]{
Conversor: conversor,
Logger: logging.Get().New(name).SetTags(Type, name),
Name: name,
Processors: processors,
CounterCreated: metrics.NewIntWithPattern(Type, name, status.Created),
CounterDone: metrics.NewIntWithPattern(Type, name, status.Done),
CounterFailed: metrics.NewIntWithPattern(Type, name, status.Failed),
CounterRunning: metrics.NewIntWithPattern(Type, name, status.Runnning),
Status: metrics.NewStringWithPattern(Type, name, status.Name),
}
// Validation.
if err := validation.Validate(s); err != nil {
return nil, err
}
// Observability: logging, metrics.
s.GetCounterCreated().Add(1)
s.GetLogger().PrintlnWithOptions(level.Debug, status.Created.String())
return s, nil
}