-
Notifications
You must be signed in to change notification settings - Fork 24
/
circuit.go
464 lines (430 loc) · 15.2 KB
/
circuit.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
package runtime
import (
"context"
"errors"
"fmt"
"math"
"sync"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"go.uber.org/multierr"
policymonitoringv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/monitoring/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/metrics"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/status"
)
// CircuitModule returns fx options of Circuit for the main app.
func CircuitModule() fx.Option {
return fx.Options(
fx.Invoke(setupCircuitMetrics),
)
}
// CircuitMetrics holds prometheus metrics related circuit.
type CircuitMetrics struct {
SignalSummaryVec *prometheus.SummaryVec
InvalidSignalReadingsTotal *prometheus.CounterVec
InfiniteSignalReadingsTotal *prometheus.CounterVec
}
var circuitMetrics = newCircuitMetrics()
func newCircuitMetrics() *CircuitMetrics {
circuitMetricsLabels := []string{
metrics.SignalNameLabel,
metrics.SubCircuitIDLabel,
metrics.PolicyNameLabel,
}
circuitMetrics := CircuitMetrics{
SignalSummaryVec: prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: metrics.SignalReadingMetricName,
Help: "The reading from a signal",
Objectives: map[float64]float64{
0: 0,
0.01: 0.001,
0.05: 0.01,
0.5: 0.05,
0.9: 0.01,
0.99: 0.001,
1: 0,
},
},
circuitMetricsLabels,
),
InvalidSignalReadingsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: metrics.InvalidSignalReadingsTotalMetricName,
Help: "The number of invalid readings from a signal",
},
circuitMetricsLabels,
),
InfiniteSignalReadingsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: metrics.InfiniteSignalReadingsTotalMetricName,
Help: "The number of infinite readings from a signal",
},
circuitMetricsLabels,
),
}
return &circuitMetrics
}
func setupCircuitMetrics(prometheusRegistry *prometheus.Registry, lifecycle fx.Lifecycle) {
type metric struct {
metric prometheus.Collector
name string
}
circuitMetrics := []metric{
{circuitMetrics.SignalSummaryVec, metrics.SignalReadingMetricName},
{circuitMetrics.InvalidSignalReadingsTotal, metrics.InvalidSignalReadingsTotalMetricName},
{circuitMetrics.InfiniteSignalReadingsTotal, metrics.InfiniteSignalReadingsTotalMetricName},
}
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
for _, m := range circuitMetrics {
err := prometheusRegistry.Register(m.metric)
if err != nil {
return fmt.Errorf("failed to register metric %s: %w", m.name, err)
}
}
return nil
},
OnStop: func(context.Context) error {
for _, m := range circuitMetrics {
unregistered := prometheusRegistry.Unregister(m.metric)
if !unregistered {
return fmt.Errorf("failed to unregister metric %s", m.name)
}
}
return nil
},
})
}
type signalToReading map[Signal]Reading
// Circuit manages the runtime state of a set of components and their inter linkages via signals.
type Circuit struct {
// Policy Read API
iface.Policy
// Status registry to track signal values
statusRegistry status.Registry
// Current tick info
tickInfo TickInfo
// Looped signals persistence across ticks
loopedSignals signalToReading
// Background scheduler
backgroundScheduler *backgroundScheduler
// Components
components []*ConfiguredComponent
// Tick end callbacks
tickEndCallbacks []TickEndCallback
// Tick start callbacks
tickStartCallbacks []TickStartCallback
// Execution lock is taken when circuit needs to execute
executionLock sync.Mutex
}
// Make sure Circuit complies with CircuitAPI interface.
var _ CircuitSuperAPI = &Circuit{}
// NewCircuitAndOptions create a new Circuit struct along with fx options.
func NewCircuitAndOptions(
configuredComponents []*ConfiguredComponent,
policyReadAPI iface.Policy,
) (*Circuit, fx.Option) {
reg := policyReadAPI.GetStatusRegistry().Child("circuit", "circuit_signals")
circuit := &Circuit{
Policy: policyReadAPI,
loopedSignals: make(signalToReading),
components: configuredComponents,
statusRegistry: reg,
}
// Populate loopedSignals
for _, component := range circuit.components {
for _, outPort := range component.PortMapping.Outs {
for _, signal := range outPort {
if signal.Looped {
circuit.loopedSignals[signal] = InvalidReading()
}
}
}
}
return circuit, fx.Options(
fx.Invoke(circuit.setup),
)
}
// Setup handle lifecycle of the inner metrics of Circuit.
func (circuit *Circuit) setup(backgroundScheduler *backgroundScheduler, lifecycle fx.Lifecycle) {
circuit.backgroundScheduler = backgroundScheduler
var circuitMetricsLabels []prometheus.Labels
for _, component := range circuit.components {
for _, outPort := range component.PortMapping.Outs {
for _, signal := range outPort {
circuitMetricsLabels = append(circuitMetricsLabels,
prometheus.Labels{
metrics.SignalNameLabel: signal.SignalName,
metrics.SubCircuitIDLabel: signal.SubCircuitID,
metrics.PolicyNameLabel: circuit.GetPolicyName(),
},
)
}
}
}
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
var merr error
for _, labels := range circuitMetricsLabels {
_, err := circuitMetrics.SignalSummaryVec.GetMetricWith(labels)
if err != nil {
err = fmt.Errorf("%w: failed to create metrics for %+v", err, labels)
merr = multierr.Append(merr, err)
}
}
return merr
},
OnStop: func(context.Context) error {
var merr error
for _, labels := range circuitMetricsLabels {
deleted := circuitMetrics.SignalSummaryVec.Delete(labels)
if !deleted {
err := fmt.Errorf("failed to delete metrics for %+v", labels)
merr = multierr.Append(merr, err)
}
}
if merr != nil {
log.Info().Msgf("stopping circuit: %v", merr)
}
return nil
},
})
}
// Execute runs one tick of computations of all the Components in the Circuit.
func (circuit *Circuit) Execute(tickInfo TickInfo) error {
logger := circuit.GetStatusRegistry().GetLogger()
// Save tickInfo
circuit.tickInfo = tickInfo
// Lock execution
circuit.LockExecution()
// Defer unlock
defer circuit.UnlockExecution()
// errMulti appends errors from Executing all the components
var errMulti error
// Invoke TickStartCallback(s)
for _, sc := range circuit.tickStartCallbacks {
err := sc(circuit)
errMulti = multierr.Append(errMulti, err)
}
policyID := fmt.Sprintf("%s-%s", circuit.GetPolicyName(), circuit.GetPolicyHash())
reg := circuit.statusRegistry.Child("policy", policyID)
// Signals for this tick
circuitSignalReadings := make(signalToReading)
defer func() {
signalInfo := &policymonitoringv1.SignalMetricsInfo{
PolicyName: circuit.GetPolicyName(),
PolicyHash: circuit.GetPolicyHash(),
SignalReading: make([]*policymonitoringv1.SignalReading, 0),
}
// log all circuitSignalReadings
for signal, reading := range circuitSignalReadings {
circuitMetricsLabels := prometheus.Labels{
metrics.SignalNameLabel: signal.SignalName,
metrics.SubCircuitIDLabel: signal.SubCircuitID,
metrics.PolicyNameLabel: circuit.Policy.GetPolicyName(),
}
signalReadingProto := &policymonitoringv1.SignalReading{
SignalName: signal.SignalName,
Valid: reading.Valid(),
Value: reading.Value(),
}
signalInfo.SignalReading = append(signalInfo.SignalReading, signalReadingProto)
if reading.Valid() {
if math.IsInf(reading.Value(), 0) {
infReadings, err := circuitMetrics.InfiniteSignalReadingsTotal.GetMetricWith(circuitMetricsLabels)
if err != nil {
logger.Error().Err(err).Msg("Failed to get InfiniteSignalReadingsTotal metric")
panic(err)
}
infReadings.Inc()
} else {
signalSummary, err := circuitMetrics.SignalSummaryVec.GetMetricWith(circuitMetricsLabels)
if err != nil {
logger.Error().Err(err).Msg("Failed to get signal metric")
panic(err)
}
signalSummary.Observe(reading.Value())
}
} else {
invReadings, err := circuitMetrics.InvalidSignalReadingsTotal.GetMetricWith(circuitMetricsLabels)
if err != nil {
logger.Error().Err(err).Msg("Failed to get InvalidSignalReadingsTotal metric")
panic(err)
}
invReadings.Inc()
}
}
signalStatus := status.NewStatus(signalInfo, nil)
reg.SetStatus(signalStatus)
}()
// Populate with last run's looped signal
for sig, reading := range circuit.loopedSignals {
circuitSignalReadings[sig] = reading
}
// Clear looped signals for next tick
circuit.loopedSignals = signalToReading{}
// Map of executed components for this tick
executedComponents := make(map[int]bool)
// Number of components executed
var numExecutedBefore, numExecutedAfter int
// Loop rounds until no components are executed
for len(executedComponents) < len(circuit.components) {
numExecutedBefore = len(executedComponents)
OUTER:
// Check readiness by component and execute if ready
for cmpIdx, cmp := range circuit.components {
componentInPortReadings := make(PortToReading)
// Skip if already executed
if executedComponents[cmpIdx] {
continue
}
// Check readiness of cmp by checking in_ports
for port, sigs := range cmp.PortMapping.Ins {
// Reading list for this port
readingList := make([]Reading, len(sigs))
// Check if all the sig(s) in sigs are ready
for index, sig := range sigs {
if sig.SignalType() == SignalTypeConstant {
readingList[index] = NewReading(sig.ConstantSignalValue())
} else if sigReading, ok := circuitSignalReadings[sig]; ok {
// Set sigReading in readingList at index
readingList[index] = sigReading
} else {
// not ready yet
continue OUTER
}
}
// All the sig(s) in sigs are ready, set readingList in componentInPortReadings
componentInPortReadings[port] = readingList
}
// log the component being executed
logger.Trace().Str("component", cmp.Name()).
Int("tick", tickInfo.Tick()).
Interface("in_ports", componentInPortReadings).
Interface("InPortToSignalsMap", cmp.PortMapping.Ins).
Msg("Executing component")
// If control reaches this point, the component is ready to execute
componentOutPortReadings, err := cmp.Execute(
/* pass signal */
componentInPortReadings,
/* pass circuit api */
circuit,
)
if componentOutPortReadings == nil {
componentOutPortReadings = make(PortToReading)
}
// Update executedComponents
executedComponents[cmpIdx] = true
if err != nil {
// Append err to errMulti
errMulti = multierr.Append(errMulti, err)
}
// Fill any missing values from cmp.outPortsMapping in componentOutPortReadings with invalid readings
for port, signals := range cmp.PortMapping.Outs {
if _, ok := componentOutPortReadings[port]; !ok {
// Fill with invalid readings
componentOutPortReadings[port] = make([]Reading, len(signals))
for index := range signals {
componentOutPortReadings[port][index] = InvalidReading()
}
} else if len(componentOutPortReadings[port]) < len(signals) {
// The reading list has fewer reading, currentTick, ticksPerExecution ints compared to portOutsSpec
// Fill with invalid readings
for index := len(componentOutPortReadings[port]); index < len(signals); index++ {
componentOutPortReadings[port][index] = InvalidReading()
}
}
}
// Update circuitSignalReadings with componentOutPortReadings while iterating through outPortsMapping
for port, signals := range cmp.PortMapping.Outs {
for index, sig := range signals {
readings, ok := componentOutPortReadings[port]
if !ok {
// Create error message
errMsg := fmt.Sprintf("unexpected state: port %s is not defined in componentOutPortReadings. abort circuit execution", port)
// Log error
logger.Error().Msg(errMsg)
return errors.New(errMsg)
}
// check presence of index in readings
if index >= len(readings) {
// Create error message
errMsg := fmt.Sprintf("unexpected state: index %d is out of range in port %s. abort circuit execution", index, port)
// Log error
logger.Error().Msg(errMsg)
return errors.New(errMsg)
}
if sig.Looped {
// Looped signals are stored in circuit.loopedSignals for the next round
circuit.loopedSignals[sig] = readings[index]
// Store the reading in circuitSignalReadings under the same signal name without the looped flag
sigNoLoop := sig
sigNoLoop.Looped = false
circuitSignalReadings[sigNoLoop] = readings[index]
} else {
// Store the reading in circuitSignalReadings
circuitSignalReadings[sig] = readings[index]
}
}
}
} // this is the component for loop
numExecutedAfter = len(executedComponents)
// Return with error if there is no change in number of executed components.
if numExecutedBefore == numExecutedAfter {
errMsg := fmt.Sprintf("circuit execution failed. number of executed components (%d) remained same across consecutive rounds", numExecutedBefore)
logger.Error().Msg(errMsg)
return errors.New(errMsg)
}
}
// Invoke TickEndCallback(s)
for _, ec := range circuit.tickEndCallbacks {
err := ec(circuit)
errMulti = multierr.Append(errMulti, err)
}
return errMulti
}
// DynamicConfigUpdate updates the circuit with the new dynamic config.
func (circuit *Circuit) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
// Take Circuit execution lock
circuit.LockExecution()
defer circuit.UnlockExecution()
// loop through all the components
for _, cmp := range circuit.components {
// update the dynamic config
cmp.DynamicConfigUpdate(event, unmarshaller)
}
}
// LockExecution locks the execution of the circuit.
func (circuit *Circuit) LockExecution() {
circuit.executionLock.Lock()
}
// UnlockExecution unlocks the execution of the circuit.
func (circuit *Circuit) UnlockExecution() {
circuit.executionLock.Unlock()
}
// RegisterTickEndCallback adds a callback function to be called when a tick ends.
func (circuit *Circuit) RegisterTickEndCallback(ec TickEndCallback) {
circuit.tickEndCallbacks = append(circuit.tickEndCallbacks, ec)
}
// RegisterTickStartCallback adds a callback function to be called when a tick starts.
func (circuit *Circuit) RegisterTickStartCallback(sc TickStartCallback) {
circuit.tickStartCallbacks = append(circuit.tickStartCallbacks, sc)
}
// GetTickInfo returns the current tick info.
func (circuit *Circuit) GetTickInfo() TickInfo {
return circuit.tickInfo
}
// ScheduleConditionalBackgroundJob schedules a background job for one time execution. The job gets scheduled only if currentTick is a multiple of ticksPerExecution. There can be at most a single job with a certain name pending to be run at a time. Subsequent invocations with the same job name overwrite the previous one.
// Warning: This method should only be called by the Components during a tick execution.
func (circuit *Circuit) ScheduleConditionalBackgroundJob(job BackgroundJob, ticksPerExecution int) {
// Do not schedule if currentTick is not a multiple of ticksPerExecution
if circuit.tickInfo.Tick()%ticksPerExecution != 0 {
return
}
circuit.backgroundScheduler.scheduleJob(job)
}