-
Notifications
You must be signed in to change notification settings - Fork 15
/
actuator.go
88 lines (77 loc) · 1.92 KB
/
actuator.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
// Copyright (c) 2016, M Bogus.
// This source file is part of the KUBE-AMQP-AUTOSCALE open source project
// Licensed under Apache License v2.0
// See LICENSE file for license information
package main
import (
"fmt"
"log"
"time"
"math"
"github.com/prometheus/client_golang/prometheus"
)
var (
desiredReplicas = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "desired_replicas",
Help: "Desired number of replicas.",
})
autoscaleErrors = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "autoscale_failures_total",
Help: "Number of failed autoscale operations.",
})
pollCount = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "polls_total",
Help: "Count of times autoscale polling loop runs.",
})
)
type queueStats func() (*queueMetrics, error)
type scaler func(int32) error
type scaleContext struct {
Threshold int
Coverage float64
Interval int
Scaler scaler
}
func autoscale(
fstats queueStats,
ctx *scaleContext,
quit <-chan struct{}) {
for {
select {
case <-quit:
return
case <-time.After(time.Duration(ctx.Interval) * time.Second):
pollCount.Inc()
qStats, err := fstats()
if err != nil {
log.Println(err)
autoscaleErrors.Inc()
continue
}
replicas, err := ctx.newSize(qStats.Average, qStats.Coverage)
if err != nil {
log.Println(err)
continue
}
desiredReplicas.Set(float64(replicas))
err = ctx.Scaler(replicas)
if err != nil {
log.Println(err)
autoscaleErrors.Inc()
}
}
}
}
func (ctx *scaleContext) newSize(avg float64, cov float64) (int32, error) {
var replicas int32
var err error
if cov < ctx.Coverage {
err = fmt.Errorf("not enough metrics to calculate new size, required at least %.2f was %.2f metrics ratio", ctx.Coverage, cov)
} else {
replicas = int32(math.Ceil(avg / float64(ctx.Threshold)))
}
return replicas, err
}