Skip to content

Commit 9cb33c2

Browse files
authored
Scale to zero for Async API (#2224)
1 parent 66c50fd commit 9cb33c2

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

docs/workloads/async/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
success_threshold: <int> # minimum consecutive successes for the probe to be considered successful after having failed (default: 1)
4343
failure_threshold: <int> # minimum consecutive failures for the probe to be considered failed after having succeeded (default: 3)
4444
autoscaling: # autoscaling configuration (default: see below)
45-
min_replicas: <int> # minimum number of replicas (default: 1)
45+
min_replicas: <int> # minimum number of replicas (default: 1; min value: 0)
4646
max_replicas: <int> # maximum number of replicas (default: 100)
4747
init_replicas: <int> # initial number of replicas (default: <min_replicas>)
4848
target_in_flight: <int> # desired number of in-flight requests per replica (including requests actively being processed as well as queued), which the autoscaler tries to maintain (default: 1)

pkg/operator/lib/autoscaler/autoscaler.go

-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ func AutoscaleFn(initialDeployment *kapps.Deployment, apiSpec *spec.API, getInFl
156156
recommendation = upscaleFactorCeil
157157
}
158158

159-
if recommendation < 1 {
160-
recommendation = 1
161-
}
162-
163159
if recommendation < autoscalingSpec.MinReplicas {
164160
recommendation = autoscalingSpec.MinReplicas
165161
}

pkg/operator/resources/asyncapi/autoscaler.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/aws/aws-sdk-go/service/sqs"
2727
"github.com/cortexlabs/cortex/pkg/config"
2828
"github.com/cortexlabs/cortex/pkg/lib/errors"
29+
"github.com/cortexlabs/cortex/pkg/lib/pointer"
2930
"github.com/cortexlabs/cortex/pkg/types/userconfig"
3031
"github.com/prometheus/client_golang/prometheus"
3132
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -111,12 +112,8 @@ func getMessagesInQueue(apiName string, window time.Duration) (*float64, error)
111112
return nil, errors.ErrorUnexpected("failed to convert prometheus metric to vector")
112113
}
113114

114-
// no values available
115-
if values.Len() == 0 {
116-
return nil, nil
115+
if values.Len() != 0 {
116+
return pointer.Float64(float64(values[0].Value)), nil
117117
}
118-
119-
avgMessagesInQueue := float64(values[0].Value)
120-
121-
return &avgMessagesInQueue, nil
118+
return nil, nil
122119
}

pkg/operator/resources/asyncapi/k8s_specs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func deploymentSpec(api spec.API, prevDeployment *kapps.Deployment, queueURL str
250250
func getRequestedReplicasFromDeployment(api spec.API, deployment *kapps.Deployment) int32 {
251251
requestedReplicas := api.Autoscaling.InitReplicas
252252

253-
if deployment != nil && deployment.Spec.Replicas != nil && *deployment.Spec.Replicas > 0 {
253+
if deployment != nil && deployment.Spec.Replicas != nil {
254254
requestedReplicas = *deployment.Spec.Replicas
255255
}
256256

pkg/types/spec/validations.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ func apiValidation(resource userconfig.Resource) *cr.StructValidation {
5454
structFieldValidations = append(resourceStructValidations,
5555
podValidation(userconfig.RealtimeAPIKind),
5656
networkingValidation(),
57-
autoscalingValidation(),
57+
autoscalingValidation(resource.Kind),
5858
updateStrategyValidation(),
5959
)
6060
case userconfig.AsyncAPIKind:
6161
structFieldValidations = append(resourceStructValidations,
6262
podValidation(userconfig.AsyncAPIKind),
6363
networkingValidation(),
64-
autoscalingValidation(),
64+
autoscalingValidation(resource.Kind),
6565
updateStrategyValidation(),
6666
)
6767
case userconfig.BatchAPIKind:
@@ -465,16 +465,21 @@ func computeValidation() *cr.StructFieldValidation {
465465
}
466466
}
467467

468-
func autoscalingValidation() *cr.StructFieldValidation {
468+
func autoscalingValidation(kind userconfig.Kind) *cr.StructFieldValidation {
469+
minReplicas := int32(1)
470+
if kind == userconfig.AsyncAPIKind {
471+
minReplicas = int32(0)
472+
}
473+
469474
return &cr.StructFieldValidation{
470475
StructField: "Autoscaling",
471476
StructValidation: &cr.StructValidation{
472477
StructFieldValidations: []*cr.StructFieldValidation{
473478
{
474479
StructField: "MinReplicas",
475480
Int32Validation: &cr.Int32Validation{
476-
Default: 1,
477-
GreaterThan: pointer.Int32(0),
481+
Default: 1,
482+
GreaterThanOrEqualTo: pointer.Int32(minReplicas),
478483
},
479484
},
480485
{
@@ -488,7 +493,7 @@ func autoscalingValidation() *cr.StructFieldValidation {
488493
StructField: "InitReplicas",
489494
DefaultField: "MinReplicas",
490495
Int32Validation: &cr.Int32Validation{
491-
GreaterThan: pointer.Int32(0),
496+
GreaterThanOrEqualTo: pointer.Int32(minReplicas),
492497
},
493498
},
494499
{

0 commit comments

Comments
 (0)