Repository: cortexlabs/cortex (language: go)

Issue:
Fix pod autoscaler (when scaling down)
#### Description

When there's no traffic hitting an API and its replica number is higher than the `min_replicas` field, the down-scaling procedure will take place almost immediately (<10s). The problem is that the `downscale_stabilization_period` param is not taken into consideration.


Discussion / hints:

Fix patch:
diff --git a/pkg/operator/resources/realtimeapi/autoscaler.go b/pkg/operator/resources/realtimeapi/autoscaler.go
index 10979ecae6..5a41b3d904 100644
--- a/pkg/operator/resources/realtimeapi/autoscaler.go
+++ b/pkg/operator/resources/realtimeapi/autoscaler.go
@@ -166,23 +166,24 @@ func autoscaleFn(initialDeployment *kapps.Deployment, apiSpec *spec.API) (func()
 		recs.deleteOlderThan(libtime.MaxDuration(autoscalingSpec.DownscaleStabilizationPeriod, autoscalingSpec.UpscaleStabilizationPeriod))
 
 		request := recommendation
+		var downscaleStabilizationFloor *int32
+		var upscaleStabilizationCeil *int32
 
-		downscaleStabilizationFloor := recs.maxSince(autoscalingSpec.DownscaleStabilizationPeriod)
-		if time.Since(startTime) < autoscalingSpec.DownscaleStabilizationPeriod {
-			if request < currentReplicas {
+		if request < currentReplicas {
+			downscaleStabilizationFloor = recs.maxSince(autoscalingSpec.DownscaleStabilizationPeriod)
+			if time.Since(startTime) < autoscalingSpec.DownscaleStabilizationPeriod {
 				request = currentReplicas
+			} else if downscaleStabilizationFloor != nil && request < *downscaleStabilizationFloor {
+				request = *downscaleStabilizationFloor
 			}
-		} else if downscaleStabilizationFloor != nil && request < *downscaleStabilizationFloor {
-			request = *downscaleStabilizationFloor
 		}
-
-		upscaleStabilizationCeil := recs.minSince(autoscalingSpec.UpscaleStabilizationPeriod)
-		if time.Since(startTime) < autoscalingSpec.UpscaleStabilizationPeriod {
-			if request > currentReplicas {
+		if request > currentReplicas {
+			upscaleStabilizationCeil = recs.minSince(autoscalingSpec.UpscaleStabilizationPeriod)
+			if time.Since(startTime) < autoscalingSpec.UpscaleStabilizationPeriod {
 				request = currentReplicas
+			} else if upscaleStabilizationCeil != nil && request > *upscaleStabilizationCeil {
+				request = *upscaleStabilizationCeil
 			}
-		} else if upscaleStabilizationCeil != nil && request > *upscaleStabilizationCeil {
-			request = *upscaleStabilizationCeil
 		}
 
 		apiLogger.Debugw(fmt.Sprintf("%s autoscaler tick", apiName),


Test patch: