-
Notifications
You must be signed in to change notification settings - Fork 24
/
load-scheduler.go
466 lines (409 loc) · 14.9 KB
/
load-scheduler.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
package loadscheduler
import (
"context"
"errors"
"fmt"
"path"
"time"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"go.uber.org/multierr"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/sync/v1"
agentinfo "github.com/fluxninja/aperture/v2/pkg/agent-info"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
etcdwatcher "github.com/fluxninja/aperture/v2/pkg/etcd/watcher"
"github.com/fluxninja/aperture/v2/pkg/metrics"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
workloadscheduler "github.com/fluxninja/aperture/v2/pkg/policies/flowcontrol/actuators/workload-scheduler"
"github.com/fluxninja/aperture/v2/pkg/policies/flowcontrol/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/paths"
"github.com/fluxninja/aperture/v2/pkg/scheduler"
"github.com/fluxninja/aperture/v2/pkg/status"
)
// FxNameTag is Load Scheduler Watcher's Fx Tag.
var fxNameTag = config.NameTag("load_scheduler_watcher")
// loadSchedulerModule returns the fx options for flowcontrol side pieces of load scheduler in the main fx app.
func loadSchedulerModule() fx.Option {
return fx.Options(
// Tag the watcher so that other modules can find it.
fx.Provide(
fx.Annotate(
provideWatcher,
fx.ResultTags(fxNameTag),
),
),
fx.Invoke(
fx.Annotate(
setupLoadSchedulerFactory,
fx.ParamTags(fxNameTag),
),
),
)
}
// provideWatcher provides pointer to load scheduler watcher.
func provideWatcher(
etcdClient *etcdclient.Client,
ai *agentinfo.AgentInfo,
) (notifiers.Watcher, error) {
// Get Agent Group from host info gatherer
agentGroupName := ai.GetAgentGroup()
// Scope the sync to the agent group.
etcdPath := path.Join(paths.LoadSchedulerConfigPath,
paths.AgentGroupPrefix(agentGroupName))
watcher, err := etcdwatcher.NewWatcher(etcdClient, etcdPath)
if err != nil {
return nil, err
}
return watcher, nil
}
type loadSchedulerFactory struct {
engineAPI iface.Engine
registry status.Registry
loadDecisionWatcher notifiers.Watcher
tokenBucketLMGaugeVec *prometheus.GaugeVec
tokenBucketFillRateGaugeVec *prometheus.GaugeVec
tokenBucketBucketCapacityGaugeVec *prometheus.GaugeVec
tokenBucketAvailableTokensGaugeVec *prometheus.GaugeVec
wsFactory *workloadscheduler.Factory
agentGroupName string
}
// setupLoadSchedulerFactory sets up the load scheduler module in the main fx app.
func setupLoadSchedulerFactory(
watcher notifiers.Watcher,
lifecycle fx.Lifecycle,
e iface.Engine,
registry status.Registry,
prometheusRegistry *prometheus.Registry,
etcdClient *etcdclient.Client,
ai *agentinfo.AgentInfo,
wsFactory *workloadscheduler.Factory,
) error {
reg := registry.Child("component", "load_scheduler")
agentGroup := ai.GetAgentGroup()
// Scope the sync to the agent group.
etcdDecisionsPath := path.Join(paths.LoadSchedulerDecisionsPath, paths.AgentGroupPrefix(agentGroup))
loadDecisionWatcher, err := etcdwatcher.NewWatcher(etcdClient, etcdDecisionsPath)
if err != nil {
return err
}
lsFactory := &loadSchedulerFactory{
engineAPI: e,
registry: reg,
wsFactory: wsFactory,
loadDecisionWatcher: loadDecisionWatcher,
agentGroupName: ai.GetAgentGroup(),
}
// Initialize and register the WFQ and Token Bucket Metric Vectors
lsFactory.tokenBucketLMGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.TokenBucketLMMetricName,
Help: "A gauge that tracks the load multiplier",
},
workloadscheduler.MetricLabelKeys,
)
lsFactory.tokenBucketFillRateGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.TokenBucketFillRateMetricName,
Help: "A gauge that tracks the fill rate of token bucket in tokens/sec",
},
workloadscheduler.MetricLabelKeys,
)
lsFactory.tokenBucketBucketCapacityGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.TokenBucketCapacityMetricName,
Help: "A gauge that tracks the capacity of token bucket",
},
workloadscheduler.MetricLabelKeys,
)
lsFactory.tokenBucketAvailableTokensGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.TokenBucketAvailableMetricName,
Help: "A gauge that tracks the number of tokens available in token bucket",
},
workloadscheduler.MetricLabelKeys,
)
fxDriver, err := notifiers.NewFxDriver(reg, prometheusRegistry,
config.NewProtobufUnmarshaller,
[]notifiers.FxOptionsFunc{lsFactory.newLoadSchedulerOptions},
)
if err != nil {
return err
}
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
var merr error
err := prometheusRegistry.Register(lsFactory.tokenBucketLMGaugeVec)
if err != nil {
return err
}
err = prometheusRegistry.Register(lsFactory.tokenBucketFillRateGaugeVec)
if err != nil {
return err
}
err = prometheusRegistry.Register(lsFactory.tokenBucketBucketCapacityGaugeVec)
if err != nil {
return err
}
err = prometheusRegistry.Register(lsFactory.tokenBucketAvailableTokensGaugeVec)
if err != nil {
return err
}
err = lsFactory.loadDecisionWatcher.Start()
if err != nil {
return err
}
return merr
},
OnStop: func(_ context.Context) error {
var merr error
err := lsFactory.loadDecisionWatcher.Stop()
if err != nil {
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(lsFactory.tokenBucketLMGaugeVec) {
err := fmt.Errorf("failed to unregister " + metrics.TokenBucketLMMetricName)
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(lsFactory.tokenBucketFillRateGaugeVec) {
err := fmt.Errorf("failed to unregister " + metrics.TokenBucketFillRateMetricName)
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(lsFactory.tokenBucketBucketCapacityGaugeVec) {
err := fmt.Errorf("failed to unregister " + metrics.TokenBucketCapacityMetricName)
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(lsFactory.tokenBucketAvailableTokensGaugeVec) {
err := fmt.Errorf("failed to unregister " + metrics.TokenBucketAvailableMetricName)
merr = multierr.Append(merr, err)
}
return merr
},
})
notifiers.WatcherLifecycle(lifecycle, watcher, []notifiers.PrefixNotifier{fxDriver})
return nil
}
// newLoadSchedulerOptions returns fx options for the load scheduler fx app.
func (lsFactory *loadSchedulerFactory) newLoadSchedulerOptions(
key notifiers.Key,
unmarshaller config.Unmarshaller,
registry status.Registry,
) (fx.Option, error) {
logger := lsFactory.registry.GetLogger()
wrapperMessage := &policysyncv1.LoadSchedulerWrapper{}
err := unmarshaller.Unmarshal(wrapperMessage)
loadSchedulerProto := wrapperMessage.LoadScheduler
if err != nil || loadSchedulerProto == nil {
registry.SetStatus(status.NewStatus(nil, err))
logger.Warn().Err(err).Msg("Failed to unmarshal load scheduler config wrapper")
return fx.Options(), err
}
loadSchedulerProto.Parameters.Scheduler, err = workloadscheduler.SanitizeSchedulerProto(loadSchedulerProto.Parameters.Scheduler)
if err != nil {
logger.Error().Err(err).Msg("Failed to sanitize scheduler proto")
return fx.Options(), err
}
ls := &loadScheduler{
Component: wrapperMessage.GetCommonAttributes(),
proto: loadSchedulerProto,
registry: registry,
loadSchedulerFactory: lsFactory,
clock: clockwork.NewRealClock(),
}
return fx.Options(
fx.Invoke(
ls.setup,
),
), nil
}
// loadScheduler implements load scheduler on the flowcontrol side.
type loadScheduler struct {
*workloadscheduler.Scheduler
iface.Component
registry status.Registry
proto *policylangv1.LoadScheduler
loadSchedulerFactory *loadSchedulerFactory
clock clockwork.Clock
tokenBucket *scheduler.LoadMultiplierTokenBucket
schedulerMetrics *workloadscheduler.SchedulerMetrics
}
// Make sure LoadScheduler implements the iface.LoadScheduler.
var _ iface.Limiter = &loadScheduler{}
func (ls *loadScheduler) setup(lifecycle fx.Lifecycle) error {
// Factories
lsFactory := ls.loadSchedulerFactory
wsFactory := lsFactory.wsFactory
// Form metric labels
metricLabels := make(prometheus.Labels)
metricLabels[metrics.PolicyNameLabel] = ls.GetPolicyName()
metricLabels[metrics.PolicyHashLabel] = ls.GetPolicyHash()
metricLabels[metrics.ComponentIDLabel] = ls.GetComponentId()
etcdKey := paths.AgentComponentKey(lsFactory.agentGroupName,
ls.GetPolicyName(),
ls.GetComponentId())
decisionUnmarshaller, protoErr := config.NewProtobufUnmarshaller(nil)
if protoErr != nil {
return protoErr
}
// decision notifier
decisionNotifier, err := notifiers.NewUnmarshalKeyNotifier(
notifiers.Key(etcdKey),
decisionUnmarshaller,
ls.decisionUpdateCallback,
)
if err != nil {
return err
}
engineAPI := lsFactory.engineAPI
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
retErr := func(err error) error {
ls.registry.SetStatus(status.NewStatus(nil, err))
return err
}
tbLMGauge, err := lsFactory.tokenBucketLMGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return retErr(fmt.Errorf("%w: Failed to get token bucket LM gauge", err))
}
tokenBucketFillRateGauge, err := lsFactory.tokenBucketFillRateGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return retErr(fmt.Errorf("%w: Failed to get token bucket fill rate gauge", err))
}
tokenBucketBucketCapacityGauge, err := lsFactory.tokenBucketBucketCapacityGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return retErr(fmt.Errorf("%w: Failed to get token bucket bucket capacity gauge", err))
}
tokenBucketAvailableTokensGauge, err := lsFactory.tokenBucketAvailableTokensGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return retErr(fmt.Errorf("%w: Failed to get token bucket available tokens gauge", err))
}
tbMetrics := &scheduler.TokenBucketMetrics{
FillRateGauge: tokenBucketFillRateGauge,
BucketCapacityGauge: tokenBucketBucketCapacityGauge,
AvailableTokensGauge: tokenBucketAvailableTokensGauge,
}
// Initialize the token bucket (non continuous tracking mode)
// TODO: 30s is also used by the controller. Define the constant at a common location.
ls.tokenBucket = scheduler.NewLoadMultiplierTokenBucket(ls.clock, 30, time.Second, tbLMGauge, tbMetrics)
ls.schedulerMetrics, err = wsFactory.NewSchedulerMetrics(metricLabels)
if err != nil {
return retErr(err)
}
// Create a new scheduler
ls.Scheduler, err = wsFactory.NewScheduler(
ls.clock,
ls.registry,
ls.proto.Parameters.Scheduler,
ls,
ls.tokenBucket,
ls.schedulerMetrics,
)
if err != nil {
return retErr(err)
}
err = lsFactory.loadDecisionWatcher.AddKeyNotifier(decisionNotifier)
if err != nil {
return retErr(err)
}
err = engineAPI.RegisterScheduler(ls)
if err != nil {
return retErr(err)
}
return retErr(nil)
},
OnStop: func(context.Context) error {
var errMulti error
err := engineAPI.UnregisterScheduler(ls)
if err != nil {
errMulti = multierr.Append(errMulti, err)
}
protoErr = lsFactory.loadDecisionWatcher.RemoveKeyNotifier(decisionNotifier)
if protoErr != nil {
errMulti = multierr.Append(errMulti, protoErr)
}
// delete the metrics
if ls.schedulerMetrics != nil {
err = ls.schedulerMetrics.Delete()
if err != nil {
errMulti = multierr.Append(errMulti, err)
}
}
deleted := lsFactory.tokenBucketLMGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete "+metrics.TokenBucketLMMetricName+" from its metric vector"))
}
deleted = lsFactory.tokenBucketFillRateGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete "+metrics.TokenBucketFillRateMetricName+" gauge from its metric vector"))
}
deleted = lsFactory.tokenBucketBucketCapacityGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete "+metrics.TokenBucketCapacityMetricName+" gauge from its metric vector"))
}
deleted = lsFactory.tokenBucketAvailableTokensGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete "+metrics.TokenBucketAvailableMetricName+" gauge from its metric vector"))
}
ls.registry.SetStatus(status.NewStatus(nil, errMulti))
return errMulti
},
})
return nil
}
// GetSelectors returns selectors.
func (ls *loadScheduler) GetSelectors() []*policylangv1.Selector {
return ls.proto.Parameters.GetSelectors()
}
// GetLimiterID returns the limiter ID.
func (ls *loadScheduler) GetLimiterID() iface.LimiterID {
// TODO: move this to limiter base.
return iface.LimiterID{
PolicyName: ls.GetPolicyName(),
PolicyHash: ls.GetPolicyHash(),
ComponentID: ls.GetComponentId(),
}
}
func (ls *loadScheduler) decisionUpdateCallback(event notifiers.Event, unmarshaller config.Unmarshaller) {
logger := ls.registry.GetLogger()
if event.Type == notifiers.Remove {
logger.Debug().Msg("Decision was removed, set pass through mode")
ls.tokenBucket.SetPassThrough(true)
return
}
var wrapperMessage policysyncv1.LoadDecisionWrapper
err := unmarshaller.Unmarshal(&wrapperMessage)
if err != nil {
statusMsg := "Failed to unmarshal config wrapper"
logger.Warn().Err(err).Msg(statusMsg)
ls.registry.SetStatus(status.NewStatus(nil, err))
return
}
loadDecision := wrapperMessage.GetLoadDecision()
if loadDecision == nil {
statusMsg := "load decision is nil"
logger.Error().Msg(statusMsg)
ls.registry.SetStatus(status.NewStatus(nil, fmt.Errorf("failed to get load decision from LoadDecisionWrapper: %s", statusMsg)))
return
}
commonAttributes := wrapperMessage.GetCommonAttributes()
if commonAttributes == nil {
statusMsg := "common attributes is nil"
logger.Error().Msg(statusMsg)
ls.registry.SetStatus(status.NewStatus(nil, fmt.Errorf("failed to get common attributes from LoadDecisionWrapper: %s", statusMsg)))
return
}
// check if this decision is for the same policy id as what we have
if commonAttributes.PolicyHash != ls.GetPolicyHash() {
err = errors.New("policy id mismatch")
statusMsg := fmt.Sprintf("Expected policy hash: %s, Got: %s", ls.GetPolicyHash(), commonAttributes.PolicyHash)
logger.Warn().Err(err).Msg(statusMsg)
ls.registry.SetStatus(status.NewStatus(nil, err))
return
}
logger.Autosample().Debug().Bool("passThrough", loadDecision.PassThrough).Float64("loadMultiplier", loadDecision.LoadMultiplier).Msg("Setting load multiplier")
ls.tokenBucket.SetLoadDecisionValues(loadDecision)
ls.tokenBucket.SetPassThrough(loadDecision.PassThrough)
ls.SetEstimatedTokens(loadDecision.TokensByWorkloadIndex)
}