-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathplugin.go
294 lines (243 loc) · 8.29 KB
/
plugin.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
package plugin
import (
"fmt"
"math"
"strconv"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad-autoscaler/plugins"
"github.com/hashicorp/nomad-autoscaler/plugins/base"
"github.com/hashicorp/nomad-autoscaler/plugins/strategy"
"github.com/hashicorp/nomad-autoscaler/sdk"
)
const (
// pluginName is the unique name of the this plugin amongst strategy
// plugins.
pluginName = "threshold"
// These are the keys read from the RunRequest.Config map.
runConfigKeyUpperBound = "upper_bound"
runConfigKeyLowerBound = "lower_bound"
runConfigKeyDelta = "delta"
runConfigKeyPercentage = "percentage"
runConfigKeyValue = "value"
runConfigKeyWithinBoundsTrigger = "within_bounds_trigger"
// defaultWithinBoundsTrigger is the default value for the
// within_bounds_trigger check run config.
defaultWithinBoundsTrigger = 5
)
var (
PluginID = plugins.PluginID{
Name: pluginName,
PluginType: sdk.PluginTypeStrategy,
}
PluginConfig = &plugins.InternalPluginConfig{
Factory: func(l hclog.Logger) interface{} { return NewThresholdPlugin(l) },
}
pluginInfo = &base.PluginInfo{
Name: pluginName,
PluginType: sdk.PluginTypeStrategy,
}
)
// thresholdPluginRunConfig are the parsed values for a threshold plugin run.
type thresholdPluginRunConfig struct {
upperBound float64
lowerBound float64
actionType string
actionValue float64
withinboundsTrigger int
}
// Assert that StrategyPlugin meets the strategy.Strategy interface.
var _ strategy.Strategy = (*StrategyPlugin)(nil)
// StrategyPlugin is the Threshold implementation of the strategy.Strategy
// interface.
type StrategyPlugin struct {
logger hclog.Logger
}
// NewThresholdPlugin returns the Threshold implementation of the
// strategy.Strategy interface.
func NewThresholdPlugin(log hclog.Logger) strategy.Strategy {
return &StrategyPlugin{
logger: log,
}
}
// SetConfig satisfies the SetConfig function on the base.Base interface.
func (s *StrategyPlugin) SetConfig(_ map[string]string) error {
return nil
}
// PluginInfo satisfies the PluginInfo function on the base.Base interface.
func (s *StrategyPlugin) PluginInfo() (*base.PluginInfo, error) {
return pluginInfo, nil
}
// Run satisfies the Run function on the strategy.Strategy interface.
func (s *StrategyPlugin) Run(eval *sdk.ScalingCheckEvaluation, count int64) (*sdk.ScalingCheckEvaluation, error) {
if len(eval.Metrics) == 0 {
s.logger.Trace("no metrics available")
return nil, nil
}
// Parse check config.
config, err := parseConfig(eval.Check.Strategy.Config)
if err != nil {
return nil, err
}
logger := s.logger.With("check_name", eval.Check.Name, "current_count", count,
"lower_bound", config.lowerBound, "upper_bound", config.upperBound,
"actionType", config.actionType)
// Check if we have enough data points within bounds.
if !withinBounds(logger, eval.Metrics, config) {
logger.Trace("not enough data points within bounds")
return nil, nil
}
// Calculate new count.
logger.Trace("calculating new count")
var newCount int64
switch config.actionType {
case runConfigKeyDelta:
newCount = runDelta(count, config.actionValue)
case runConfigKeyPercentage:
newCount = runPercentage(count, config.actionValue)
case runConfigKeyValue:
newCount = runValue(config.actionValue)
}
// Identify the direction of scaling, and exit early if none.
eval.Action.Direction = calculateDirection(count, newCount)
if eval.Action.Direction == sdk.ScaleDirectionNone {
return eval, nil
}
logger.Trace("calculated scaling strategy results",
"new_count", newCount, "direction", eval.Action.Direction)
eval.Action.Count = newCount
eval.Action.Reason = fmt.Sprintf("scaling %s because metric is within bounds", eval.Action.Direction)
return eval, nil
}
// parseConfig parses and validates the policy check config.
func parseConfig(config map[string]string) (*thresholdPluginRunConfig, error) {
c := &thresholdPluginRunConfig{}
// Read and parse threshold bounds from check config.
upperStr := config[runConfigKeyUpperBound]
lowerStr := config[runConfigKeyLowerBound]
if upperStr == "" && lowerStr == "" {
return nil, fmt.Errorf("missing required field, must have either %q or %q", runConfigKeyLowerBound, runConfigKeyUpperBound)
}
upper, err := parseBound(runConfigKeyUpperBound, upperStr)
if err != nil {
return nil, err
}
c.upperBound = upper
lower, err := parseBound(runConfigKeyLowerBound, lowerStr)
if err != nil {
return nil, err
}
c.lowerBound = lower
// Read and parse within bounds trigger from check config.
triggerStr := config[runConfigKeyWithinBoundsTrigger]
trigger := defaultWithinBoundsTrigger
if triggerStr != "" {
t, err := strconv.ParseInt(triggerStr, 10, 0)
if err != nil {
return nil, fmt.Errorf("invalid value for %q: %v (%T)", runConfigKeyWithinBoundsTrigger, triggerStr, triggerStr)
}
trigger = int(t)
}
c.withinboundsTrigger = trigger
// Read and validate action type from check config.
deltaStr := config[runConfigKeyDelta]
percentageStr := config[runConfigKeyPercentage]
valueStr := config[runConfigKeyValue]
nonEmpty := 0
for _, s := range []string{deltaStr, percentageStr, valueStr} {
if s != "" {
nonEmpty++
}
}
if nonEmpty == 0 {
return nil, fmt.Errorf("missing required field, must have either %q, %q or %q", runConfigKeyDelta, runConfigKeyPercentage, runConfigKeyValue)
}
if nonEmpty != 1 {
return nil, fmt.Errorf("only one of %q, %q or %q must be provided", runConfigKeyDelta, runConfigKeyPercentage, runConfigKeyValue)
}
if deltaStr != "" {
// Delta must be an integer.
d, err := strconv.ParseInt(deltaStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("%q value %v is not an interger", runConfigKeyDelta, deltaStr)
}
c.actionType = runConfigKeyDelta
c.actionValue = float64(d)
}
if percentageStr != "" {
// Percentage must be a float.
p, err := strconv.ParseFloat(percentageStr, 64)
if err != nil {
return nil, fmt.Errorf("%q value %v is not a number", runConfigKeyPercentage, percentageStr)
}
c.actionType = runConfigKeyPercentage
c.actionValue = p
}
if valueStr != "" {
// Value must be a positive interger.
v, err := strconv.ParseInt(valueStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("%q value %v is not an interger", runConfigKeyValue, valueStr)
}
if v < 0 {
return nil, fmt.Errorf("%q value %v is negative", runConfigKeyValue, valueStr)
}
c.actionType = runConfigKeyValue
c.actionValue = float64(v)
}
return c, nil
}
// parseBound parses and validates the value for a bound.
func parseBound(bound string, input string) (float64, error) {
var defaultValue float64
switch bound {
case runConfigKeyLowerBound:
defaultValue = -math.MaxFloat64
case runConfigKeyUpperBound:
defaultValue = math.MaxFloat64
}
if input == "" {
return defaultValue, nil
}
value, err := strconv.ParseFloat(input, 64)
if err != nil {
return 0, fmt.Errorf("invalid value for %q: %v (%T)", bound, input, input)
}
return value, nil
}
// withinBounds returns true if the metric result is considered within bounds.
func withinBounds(logger hclog.Logger, metrics sdk.TimestampedMetrics, config *thresholdPluginRunConfig) bool {
logger.Trace("checking how many data points are within bounds")
withinBoundsCounter := 0
for _, metric := range metrics {
if metric.Value >= config.lowerBound && metric.Value < config.upperBound {
withinBoundsCounter++
}
}
logger.Trace(fmt.Sprintf("found %d data points within bounds", withinBoundsCounter))
return withinBoundsCounter >= config.withinboundsTrigger
}
// runDelta returns the next count for a delta check.
func runDelta(count int64, d float64) int64 {
return count + int64(d)
}
// runPercentage returns the next count for a percentage check.
func runPercentage(count int64, pct float64) int64 {
newCount := float64(count) * (1 + pct/100)
return int64(math.Ceil(newCount))
}
// runValue returns the next count for a value check.
func runValue(v float64) int64 {
return int64(v)
}
// calculateDirection is used to calculate the direction of scaling that should
// occur, if any at all.
func calculateDirection(currentCount, newCount int64) sdk.ScaleDirection {
switch {
case newCount > currentCount:
return sdk.ScaleDirectionUp
case newCount < currentCount:
return sdk.ScaleDirectionDown
default:
return sdk.ScaleDirectionNone
}
}