Repository navigation
[Bug] Scale KPA on the total load, not the per-pod mean - #2884
Conversation
The metrics client records the per-pod mean for pod, resource and custom sources, but KPA divided that mean by the per-pod target as if it were a total. Four pods at 90 against a target of 50 scaled down to 2. Multiply by the pod count for those sources (external and domain sources already report one total), as APA does. Signed-off-by: Ankit Jha <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates the KPA scaling algorithm to correctly distinguish between per-pod mean metrics and total metrics. For per-pod, resource, and custom metric sources, the target replica calculation now scales the observed value by the number of ready pods to compute the total load, while external and domain sources continue to use the total value directly. The ScalingContext interface and its implementations have been updated to support retrieving the metric source type, and comprehensive unit tests have been added to verify the new scaling behavior. There are no review comments, and I have no feedback to provide.
varungup90
left a comment
There was a problem hiding this comment.
Reviewed commit a31e12ac8a95d1d30073c303696e352f562ac75b. Found one P2 correctness gap: the pending-replica guard still assumes a total metric on KPA scale-up and can override the corrected recommendation.
Validation:
go test ./pkg/controller/podautoscaler/algorithm ./pkg/controller/podautoscaler/context -count=1passed.go test ./pkg/controller/podautoscaler/... -count=1passed on the unmodified PR head.- An additional full-pipeline regression case (4 current replicas, 3 ready, 1 pending, per-pod metric 90, target 50) failed for KPA with both POD and RESOURCE sources: expected 6 replicas, actual 4. The same case passed for APA.
- Temporarily multiplying the KPA guard's adjusted mean by currentReplicas made those regression cases pass. All temporary changes were confined to an isolated checkout and removed afterward.
The algorithm-level fix is correct for the covered cases, but the pending-replica path needs the same normalization and integration coverage.
| var dspc, dppc float64 | ||
| if observedStableValue > scaleUpThreshold || observedStableValue < scaleDownThreshold { | ||
| dspc = math.Ceil(observedStableValue / targetValue) | ||
| dspc = math.Ceil(load * observedStableValue / targetValue) |
There was a problem hiding this comment.
[P2] Apply the per-pod formula in the pending-replica guard too
The corrected recommendation is still overwritten by applyPendingReplicaGuard: its KPA scale-up branch in autoscaler.go:323 uses ceil(adjustedMetricValue / targetValue), even though the adjusted value is a per-pod mean. I reproduced this through ComputeDesiredReplicas with 4 current replicas, 3 ready pods each at 90, 1 pending pod, and target 50. This calculation recommends 8, but the guard computes ceil((90*3/4)/50)=2 and clamps it to 4, suppressing scale-up even though the ready pods alone need ceil(3*90/50)=6. Both POD and RESOURCE sources are affected; APA returns 6 for the same input. Please update the KPA guard to ceil(currentReplicas * adjustedMetricValue / targetValue) and add a full-pipeline pending-replica test, updating the existing total-based guard fixtures as needed. This pending-replica inconsistency is also called out in #2879.
There was a problem hiding this comment.
Good catch, thanks. The guard's scale-up branch now uses ceil(currentReplicas * adjusted / target) for per-pod sources (autoscaler.go), as APA does. External and domain sources keep the old formula. Your case (4 current, 3 ready, 1 pending, 90 vs 50) now gives 6 for KPA with POD and RESOURCE sources, and the same for APA. I updated the two total-based fixtures (kpa scale-up adjusted is now 10, dampened uses 100 so the guard still bites) and added the POD, RESOURCE and APA rows. Without the change the new rows fail. Tip 78a7b32.
The guard recomputed the scale-up with ceil(adjusted / target), which assumed a total and could override the corrected KPA recommendation. Use ceil(currentReplicas * adjusted / target) for per-pod sources, as APA does. Signed-off-by: Ankit Jha <[email protected]>
The projection mirrors the reactive formula. KPA scales the windowed per-pod mean by the pod count, external and domain sources keep the total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884.
The projection mirrors the reactive formula. KPA scales the windowed per-pod mean by the pod count, external and domain sources keep the total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884. Signed-off-by: bolubo <[email protected]>
The projection mirrors the reactive formula. KPA scales the windowed per-pod mean by the pod count, external and domain sources keep the total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884. Signed-off-by: bolubo <[email protected]>
The projection mirrors the reactive formula. KPA scales the windowed per-pod mean by the pod count, external and domain sources keep the total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884. Signed-off-by: bolubo <[email protected]>
Pull Request Description
Problem. KPA's replica count ignores how many pods carry the load. Four pods each at 90 against a target of 50 (80% over) scale down to 2. The shipped
samples/autoscaling/kpa.yaml(gpu_cache_usage_perc, target 0.5) can never recommend more thanceil(1/0.5) = 2replicas.Root cause.
MetricsClient.UpdateMetricsrecords the mean of the per-pod values for pod, resource and custom sources.KPAAlgorithm.computeTargetReplicasthen computedceil(observed / target), which is Knative's formula for a total across ready pods. APA already multiplies the mean by the pod count.Fix. For pod, resource and custom sources, multiply the windowed value by
max(1, readyPods)before dividing by the target. External and domain sources report one total, so they keepceil(total / target). The tolerance gate still compares the mean with the per-pod target, so its behavior is unchanged. This addsGetMetricSourceTypeForMetrictoScalingContextto read the source type already stored inMetricTarget.Pending-replica guard.
applyPendingReplicaGuardrecomputed the KPA scale-up withceil(adjusted / target)and could override the corrected result. It now usesceil(currentReplicas * adjusted / target)for per-pod sources.Tests.
per_pod_mean_over_target_scales_up: 4 pods at 90 vs 50 gives 8 (2 before the fix).ratio_metric_can_exceed_two_replicas: 4 pods at 0.9 vs 0.5, max 8, gives 8 (2 before the fix).external_source_value_is_already_a_total: an external total of 90 vs 50 still gives 2.Checks run:
go test ./pkg/controller/podautoscaler/...passes. On macOS I needed-ldflags=-linkmode=externalto avoid amissing LC_UUIDabort.golangci-lint run ./pkg/controller/podautoscaler/...reports 0 issues.mainand pass here.Related Issues
Resolves: #2879