forked from knative/serving
/
config.go
234 lines (204 loc) · 7.4 KB
/
config.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
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package autoscaler
import (
"fmt"
"strconv"
"strings"
"time"
"knative.dev/serving/pkg/apis/autoscaling"
corev1 "k8s.io/api/core/v1"
)
const (
// ConfigName is the name of the config map of the autoscaler.
ConfigName = "config-autoscaler"
defaultTargetUtilization = 0.7
)
// Config defines the tunable autoscaler parameters
// +k8s:deepcopy-gen=true
type Config struct {
// Feature flags.
EnableScaleToZero bool
// Enable connection-aware pod scaledown
EnableGracefulScaledown bool
// Target concurrency knobs for different container concurrency configurations.
ContainerConcurrencyTargetFraction float64
ContainerConcurrencyTargetDefault float64
// TargetUtilization is used for the metrics other than concurrency. This is not
// configurable now. Customers can override it by specifying
// autoscaling.knative.dev/targetUtilizationPercentage in Revision annotation.
// TODO(yanweiguo): Expose this to config-autoscaler configmap and eventually
// deprecate ContainerConcurrencyTargetFraction.
TargetUtilization float64
// RPSTargetDefault is the default target value for requests per second.
RPSTargetDefault float64
// NB: most of our computations are in floats, so this is float to avoid casting.
TargetBurstCapacity float64
// General autoscaler algorithm configuration.
MaxScaleUpRate float64
MaxScaleDownRate float64
StableWindow time.Duration
PanicWindowPercentage float64
PanicThresholdPercentage float64
TickInterval time.Duration
ScaleToZeroGracePeriod time.Duration
}
// NewConfigFromMap creates a Config from the supplied map
func NewConfigFromMap(data map[string]string) (*Config, error) {
lc := &Config{
TargetUtilization: defaultTargetUtilization,
}
// Process bool fields.
for _, b := range []struct {
key string
field *bool
defaultValue bool
}{
{
key: "enable-scale-to-zero",
field: &lc.EnableScaleToZero,
defaultValue: true,
},
{
key: "enable-graceful-scaledown",
field: &lc.EnableGracefulScaledown,
defaultValue: false,
}} {
if raw, ok := data[b.key]; !ok {
*b.field = b.defaultValue
} else {
*b.field = strings.EqualFold(raw, "true")
}
}
// Process Float64 fields
for _, f64 := range []struct {
key string
field *float64
// specified exactly when optional
defaultValue float64
}{{
key: "max-scale-up-rate",
field: &lc.MaxScaleUpRate,
defaultValue: 1000.0,
}, {
key: "max-scale-down-rate",
field: &lc.MaxScaleDownRate,
defaultValue: 2.0,
}, {
key: "container-concurrency-target-percentage",
field: &lc.ContainerConcurrencyTargetFraction,
// TODO(#1956): Tune target usage based on empirical data.
defaultValue: defaultTargetUtilization,
}, {
key: "container-concurrency-target-default",
field: &lc.ContainerConcurrencyTargetDefault,
defaultValue: 100.0,
}, {
key: "requests-per-second-target-default",
field: &lc.RPSTargetDefault,
defaultValue: 200.0,
}, {
key: "target-burst-capacity",
field: &lc.TargetBurstCapacity,
defaultValue: 200,
}, {
key: "panic-window-percentage",
field: &lc.PanicWindowPercentage,
defaultValue: 10.0,
}, {
key: "panic-threshold-percentage",
field: &lc.PanicThresholdPercentage,
defaultValue: 200.0,
}} {
if raw, ok := data[f64.key]; !ok {
*f64.field = f64.defaultValue
} else if val, err := strconv.ParseFloat(raw, 64); err != nil {
return nil, err
} else {
*f64.field = val
}
}
// Adjust % ⇒ fractions: for legacy reasons we allow values in the
// (0, 1] interval, so minimal percentage must be greater than 1.0.
// Internally we want to have fractions, since otherwise we'll have
// to perform division on each computation.
if lc.ContainerConcurrencyTargetFraction > 1.0 {
lc.ContainerConcurrencyTargetFraction /= 100.0
}
// Process Duration fields
for _, dur := range []struct {
key string
field *time.Duration
defaultValue time.Duration
}{{
key: "stable-window",
field: &lc.StableWindow,
defaultValue: 60 * time.Second,
}, {
key: "scale-to-zero-grace-period",
field: &lc.ScaleToZeroGracePeriod,
defaultValue: 30 * time.Second,
}, {
key: "tick-interval",
field: &lc.TickInterval,
defaultValue: 2 * time.Second,
}} {
if raw, ok := data[dur.key]; !ok {
*dur.field = dur.defaultValue
} else if val, err := time.ParseDuration(raw); err != nil {
return nil, err
} else {
*dur.field = val
}
}
return validate(lc)
}
func validate(lc *Config) (*Config, error) {
if lc.ScaleToZeroGracePeriod < autoscaling.WindowMin {
return nil, fmt.Errorf("scale-to-zero-grace-period must be at least %v, got %v", autoscaling.WindowMin, lc.ScaleToZeroGracePeriod)
}
if lc.TargetBurstCapacity < 0 && lc.TargetBurstCapacity != -1 {
return nil, fmt.Errorf("target-burst-capacity must be non-negative, got %f", lc.TargetBurstCapacity)
}
if lc.ContainerConcurrencyTargetFraction <= 0 || lc.ContainerConcurrencyTargetFraction > 1 {
return nil, fmt.Errorf("container-concurrency-target-percentage = %f is outside of valid range of (0, 100]", lc.ContainerConcurrencyTargetFraction)
}
if x := lc.ContainerConcurrencyTargetFraction * lc.ContainerConcurrencyTargetDefault; x < autoscaling.TargetMin {
return nil, fmt.Errorf("container-concurrency-target-percentage and container-concurrency-target-default yield target concurrency of %f, can't be less than 1", x)
}
if lc.RPSTargetDefault < autoscaling.TargetMin {
return nil, fmt.Errorf("requests-per-second-target-default must be at least %v, got %v", autoscaling.TargetMin, lc.RPSTargetDefault)
}
if lc.MaxScaleUpRate <= 1.0 {
return nil, fmt.Errorf("max-scale-up-rate = %v, must be greater than 1.0", lc.MaxScaleUpRate)
}
if lc.MaxScaleDownRate <= 1.0 {
return nil, fmt.Errorf("max-scale-down-rate = %v, must be greater than 1.0", lc.MaxScaleDownRate)
}
// We can't permit stable window be less than our aggregation window for correctness.
if lc.StableWindow < autoscaling.WindowMin {
return nil, fmt.Errorf("stable-window = %v, must be at least %v", lc.StableWindow, autoscaling.WindowMin)
}
if lc.StableWindow.Round(time.Second) != lc.StableWindow {
return nil, fmt.Errorf("stable-window = %v, must be specified with at most second precision", lc.StableWindow)
}
effPW := time.Duration(lc.PanicWindowPercentage / 100 * float64(lc.StableWindow))
if effPW < BucketSize || effPW > lc.StableWindow {
return nil, fmt.Errorf("panic-window-percentage = %v, must be in [%v, 100] interval", lc.PanicWindowPercentage, 100*float64(BucketSize)/float64(lc.StableWindow))
}
return lc, nil
}
// NewConfigFromConfigMap creates a Config from the supplied ConfigMap
func NewConfigFromConfigMap(configMap *corev1.ConfigMap) (*Config, error) {
return NewConfigFromMap(configMap.Data)
}