Skip to content

Commit

Permalink
fix: can downscale calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Nov 10, 2023
1 parent 9d8a999 commit 444e4c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/core/services/autoscaler/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -271,6 +272,7 @@ func TestCanDownscale(t *testing.T) {

policyParams := autoscaling.PolicyParameters{
RoomOccupancy: &autoscaling.RoomOccupancyParams{
ReadyTarget: readyTarget,
DownThreshold: downThreshold,
},
}
Expand All @@ -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,
Expand All @@ -296,6 +322,7 @@ func TestCanDownscale(t *testing.T) {

policyParams := autoscaling.PolicyParameters{
RoomOccupancy: &autoscaling.RoomOccupancyParams{
ReadyTarget: readyTarget,
DownThreshold: downThreshold,
},
}
Expand All @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down

0 comments on commit 444e4c1

Please sign in to comment.