From 444e4c14d76cdb5f2acdcd27139f8a5feec76f24 Mon Sep 17 00:00:00 2001 From: Reinaldo Oliveira Date: Fri, 10 Nov 2023 10:29:32 -0300 Subject: [PATCH] fix: can downscale calculation --- .../services/autoscaler/autoscaler_test.go | 4 +- .../policies/roomoccupancy/policy.go | 9 +++- .../policies/roomoccupancy/policy_test.go | 41 ++++++++++++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/internal/core/services/autoscaler/autoscaler_test.go b/internal/core/services/autoscaler/autoscaler_test.go index 8e01e1c8c..5c060251e 100644 --- a/internal/core/services/autoscaler/autoscaler_test.go +++ b/internal/core/services/autoscaler/autoscaler_test.go @@ -187,7 +187,7 @@ func TestCanDownscale(t *testing.T) { t.Run("When the occupation is below the threshold", func(t *testing.T) { mockRoomStorage := mock.NewMockRoomStorage(ctrl) - mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusReady).Return(1, nil) + mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusReady).Return(4, nil) mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusOccupied).Return(1, nil) policy := roomoccupancy.NewPolicy(mockRoomStorage) @@ -202,7 +202,7 @@ func TestCanDownscale(t *testing.T) { t.Run("When the occupation is above the threshold", func(t *testing.T) { mockRoomStorage := mock.NewMockRoomStorage(ctrl) - mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusReady).Return(1, nil) + mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusReady).Return(2, nil) mockRoomStorage.EXPECT().GetRoomCountByStatus(gomock.Any(), scheduler.Name, game_room.GameStatusOccupied).Return(3, nil) policy := roomoccupancy.NewPolicy(mockRoomStorage) diff --git a/internal/core/services/autoscaler/policies/roomoccupancy/policy.go b/internal/core/services/autoscaler/policies/roomoccupancy/policy.go index dd8635406..15acb2da3 100644 --- a/internal/core/services/autoscaler/policies/roomoccupancy/policy.go +++ b/internal/core/services/autoscaler/policies/roomoccupancy/policy.go @@ -108,6 +108,11 @@ func (p *Policy) CanDownscale(policyParameters autoscaling.PolicyParameters, cur return false, errors.New("Downscale threshold must be between 0 and 1") } + readyTarget := policyParameters.RoomOccupancy.ReadyTarget + if readyTarget >= float64(1) || readyTarget <= 0 { + return false, errors.New("Ready target must be between 0 and 1") + } + if _, ok := currentState[ReadyRoomsKey].(int); !ok { return false, errors.New("There are no readyRooms in the currentState") } @@ -119,5 +124,7 @@ func (p *Policy) CanDownscale(policyParameters autoscaling.PolicyParameters, cur readyRooms := currentState[ReadyRoomsKey].(int) occupiedRooms := currentState[OccupiedRoomsKey].(int) - return float64(occupiedRooms)/float64(readyRooms+occupiedRooms) < downThreshold, nil + occupationRate := float64(occupiedRooms) / float64(occupiedRooms+readyRooms) + + return (occupationRate / readyTarget) <= downThreshold, nil } diff --git a/internal/core/services/autoscaler/policies/roomoccupancy/policy_test.go b/internal/core/services/autoscaler/policies/roomoccupancy/policy_test.go index 3f324bed9..0ddcbed24 100644 --- a/internal/core/services/autoscaler/policies/roomoccupancy/policy_test.go +++ b/internal/core/services/autoscaler/policies/roomoccupancy/policy_test.go @@ -260,9 +260,10 @@ func TestCanDownscale(t *testing.T) { t.Parallel() t.Run("it is expected to not allow downscale", func(t *testing.T) { + readyTarget := float64(0.5) downThreshold := float64(0.6) occupiedRooms := 80 - readyRooms := 20 + readyRooms := 120 schedulerState := policies.CurrentState{ roomoccupancy.ReadyRoomsKey: readyRooms, @@ -271,6 +272,7 @@ func TestCanDownscale(t *testing.T) { policyParams := autoscaling.PolicyParameters{ RoomOccupancy: &autoscaling.RoomOccupancyParams{ + ReadyTarget: readyTarget, DownThreshold: downThreshold, }, } @@ -281,13 +283,37 @@ func TestCanDownscale(t *testing.T) { }) }) - t.Run("Success case - when current usage is below threshold", func(t *testing.T) { + t.Run("Success case - when current usage is equal or below threshold", func(t *testing.T) { t.Parallel() - t.Run("it is expected to allow downscale", func(t *testing.T) { + t.Run("it is expected to allow downscale when occupation is equal the threshold", func(t *testing.T) { + readyTarget := float64(0.5) + downThreshold := float64(0.7) + occupiedRooms := 70 + readyRooms := 130 + + schedulerState := policies.CurrentState{ + roomoccupancy.ReadyRoomsKey: readyRooms, + roomoccupancy.OccupiedRoomsKey: occupiedRooms, + } + + policyParams := autoscaling.PolicyParameters{ + RoomOccupancy: &autoscaling.RoomOccupancyParams{ + ReadyTarget: readyTarget, + DownThreshold: downThreshold, + }, + } + + allow, err := policy.CanDownscale(policyParams, schedulerState) + assert.NoError(t, err) + assert.Truef(t, allow, "downscale should be allowed") + }) + + t.Run("it is expected to allow downscale when occupation is equal the threshold", func(t *testing.T) { + readyTarget := float64(0.5) downThreshold := float64(0.6) - occupiedRooms := 59 - readyRooms := 41 + occupiedRooms := 60 + readyRooms := 140 schedulerState := policies.CurrentState{ roomoccupancy.ReadyRoomsKey: readyRooms, @@ -296,6 +322,7 @@ func TestCanDownscale(t *testing.T) { policyParams := autoscaling.PolicyParameters{ RoomOccupancy: &autoscaling.RoomOccupancyParams{ + ReadyTarget: readyTarget, DownThreshold: downThreshold, }, } @@ -320,9 +347,11 @@ func TestCanDownscale(t *testing.T) { roomoccupancy.OccupiedRoomsKey: 10, } + readyTarget := float64(0.3) downThreshold := float64(0.3) policyParams := autoscaling.PolicyParameters{ RoomOccupancy: &autoscaling.RoomOccupancyParams{ + ReadyTarget: readyTarget, DownThreshold: downThreshold, }, } @@ -336,9 +365,11 @@ func TestCanDownscale(t *testing.T) { roomoccupancy.ReadyRoomsKey: 10, } + readyTarget := float64(0.3) downThreshold := float64(0.3) policyParams := autoscaling.PolicyParameters{ RoomOccupancy: &autoscaling.RoomOccupancyParams{ + ReadyTarget: readyTarget, DownThreshold: downThreshold, }, }