| 143 | } |
| 144 | |
| 145 | func (a *Autoscaler) autoscaleFn(api userconfig.Resource) (func() error, error) { |
| 146 | log := a.logger.With( |
| 147 | zap.String("apiName", api.Name), |
| 148 | zap.String("apiKind", api.Kind.String()), |
| 149 | ) |
| 150 | |
| 151 | scaler, ok := a.scalers[api.Kind] |
| 152 | if !ok { |
| 153 | return nil, errors.ErrorUnexpected( |
| 154 | fmt.Sprintf("autoscaler does not have a scaler for the %s kind", api.Kind), |
| 155 | ) |
| 156 | } |
| 157 | |
| 158 | log.Info("autoscaler init") |
| 159 | |
| 160 | var startTime time.Time |
| 161 | a.recs[api.Name] = newRecommendations() |
| 162 | |
| 163 | return func() error { |
| 164 | autoscalingSpec, err := scaler.GetAutoscalingSpec(api.Name) |
| 165 | if err != nil { |
| 166 | return errors.Wrap(err, "failed to get autoscaling spec") |
| 167 | } |
| 168 | |
| 169 | currentRequestedReplicas, err := scaler.CurrentRequestedReplicas(api.Name) |
| 170 | if err != nil { |
| 171 | return errors.Wrap(err, "failed to get current replicas") |
| 172 | } |
| 173 | |
| 174 | if startTime.IsZero() { |
| 175 | startTime = time.Now() |
| 176 | } |
| 177 | |
| 178 | avgInFlight, err := scaler.GetInFlightRequests(api.Name, autoscalingSpec.Window) |
| 179 | if err != nil { |
| 180 | return errors.Wrap(err, "failed to get in-flight requests") |
| 181 | } |
| 182 | if avgInFlight == nil { |
| 183 | log.Debug("autoscaler tick: metrics not available yet") |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | rawRecommendation := *avgInFlight / *autoscalingSpec.TargetInFlight |
| 188 | recommendation := int32(math.Ceil(rawRecommendation)) |
| 189 | |
| 190 | if rawRecommendation < float64(currentRequestedReplicas) && rawRecommendation > float64(currentRequestedReplicas)*(1-autoscalingSpec.DownscaleTolerance) { |
| 191 | recommendation = currentRequestedReplicas |
| 192 | } |
| 193 | |
| 194 | if rawRecommendation > float64(currentRequestedReplicas) && rawRecommendation < float64(currentRequestedReplicas)*(1+autoscalingSpec.UpscaleTolerance) { |
| 195 | recommendation = currentRequestedReplicas |
| 196 | } |
| 197 | |
| 198 | // always allow subtraction of 1 |
| 199 | downscaleFactorFloor := libmath.MinInt32(currentRequestedReplicas-1, int32(math.Ceil(float64(currentRequestedReplicas)*autoscalingSpec.MaxDownscaleFactor))) |
| 200 | if recommendation < downscaleFactorFloor { |
| 201 | recommendation = downscaleFactorFloor |
| 202 | } |