Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/controller/podautoscaler/algorithm/apa.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ func (a *APAAlgorithm) computeTargetReplicas(currentPodCount float64, context sc

// Tolerance check: only scale if metric exceeds tolerance thresholds
if currentUsePerPod/expectedUse > (1 + upTolerance) {
maxScaleUp := math.Ceil(context.GetMaxScaleUpRate() * currentPodCount)
maxScaleUp := int32(math.Ceil(context.GetMaxScaleUpRate() * currentPodCount))
expectedPods := int32(math.Ceil(currentPodCount * (currentUsePerPod / expectedUse)))
if float64(expectedPods) > maxScaleUp {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make sure one thing: we convert from float -> int to ensure the stability of the unit test, right?

Copy link
Contributor Author

@nurali-techie nurali-techie Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure on what you are referring about stability of unit test. Unit tests will work without this change (commit_link).

This change is made for better code readability. If you see, math.Ceil() and math.Floor() always return int (check documentation) so it's safe to type cast to int.

Once we type cast both variable maxScaleUp and expectedPods to int; we can work with them without any further type cast which simplify the code and improve readability.

Copy link
Collaborator

@Jeffwan Jeffwan Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, math,Ceil() math.Floor() still return float but it's integer. The refactoring is about earlier type conversion, not about the return types here. so this is still ok

expectedPods = int32(maxScaleUp)
if expectedPods > maxScaleUp {
expectedPods = maxScaleUp
}
klog.V(4).InfoS("APA scaling up", "currentPods", currentPodCount, "expectedPods", expectedPods)
return expectedPods
} else if currentUsePerPod/expectedUse < (1 - downTolerance) {
maxScaleDown := math.Floor(currentPodCount / context.GetMaxScaleDownRate())
maxScaleDown := int32(math.Floor(currentPodCount / context.GetMaxScaleDownRate()))
expectedPods := int32(math.Ceil(currentPodCount * (currentUsePerPod / expectedUse)))
if float64(expectedPods) < maxScaleDown {
expectedPods = int32(maxScaleDown)
if expectedPods < maxScaleDown {
expectedPods = maxScaleDown
}
klog.V(4).InfoS("APA scaling down", "currentPods", currentPodCount, "expectedPods", expectedPods)
return expectedPods
Expand Down
175 changes: 175 additions & 0 deletions pkg/controller/podautoscaler/algorithm/apa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
Copyright 2025 The Aibrix Team.

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 algorithm

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAPAAlgorithm_ComputeTargetReplicas(t *testing.T) {
algorithm := &APAAlgorithm{}

tests := []struct {
name string
currentPodCount float64
context *mockScalingContext
expected int32
}{
// scale up tests
{
name: "basic_scale_up",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 45.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 3,
},
{
name: "scale_up_upto_ceiling",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 35.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 3,
},
{
name: "scale_up_upto_max_scale_up",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 90.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 4,
},
{
name: "scale_up_upto_max_scale_up_ceiling",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 90.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2.5,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 5,
},
// scale down tests
{
name: "basic_scale_down",
currentPodCount: 6.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 20.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 4,
},
{
name: "scale_down_upto_ceiling",
currentPodCount: 6.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 18.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 4,
},
{
name: "scale_down_upto_max_scale_down",
currentPodCount: 6.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 10.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 3,
},
{
name: "scale_down_upto_max_scale_down_floor",
currentPodCount: 6.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 5.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2.5,
},
expected: 2,
},
// no scaling tests
{
name: "no_scaling_as_within_up_tolerance",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 33.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 2,
},
{
name: "no_scaling_as_within_down_tolerance",
currentPodCount: 2.0,
context: &mockScalingContext{
TargetValue: 30.0,
CurrentUsePerPod: 27.0,
UpFluctuationTolerance: 0.1,
MaxScaleUpRate: 2,
DownFluctuationTolerance: 0.1,
MaxScaleDownRate: 2,
},
expected: 2,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := algorithm.computeTargetReplicas(tt.currentPodCount, tt.context)
assert.Equal(t, tt.expected, result)
})
}
}
Loading