From f5939c9a60872f23a393f5d2bc7af077d7dab90e Mon Sep 17 00:00:00 2001 From: "Alex Ellis (VMware)" Date: Thu, 25 Oct 2018 09:54:02 +0100 Subject: [PATCH] Update for scaling edge-case - as reported on Slack and in issue #931 the gateway scaling code was scaling to zero replicas as a result of the "proportional scaling" added by @Templum's PR. This commit added a failing test which was fixed by adding boundary checking - now if the scaling amount is "0" we keep the current amount of replicas. Signed-off-by: Alex Ellis (VMware) --- gateway/handlers/alerthandler.go | 5 ++--- gateway/handlers/alerthandler_test.go | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gateway/handlers/alerthandler.go b/gateway/handlers/alerthandler.go index ce908ced..60a5c0fe 100644 --- a/gateway/handlers/alerthandler.go +++ b/gateway/handlers/alerthandler.go @@ -5,12 +5,11 @@ package handlers import ( "encoding/json" + "fmt" "io/ioutil" "log" "net/http" - "fmt" - "github.com/openfaas/faas/gateway/requests" ) @@ -119,7 +118,7 @@ func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64 newReplicas := currentReplicas step := uint64((float64(maxReplicas) / 100) * float64(scalingFactor)) - if status == "firing" { + if status == "firing" && step > 0 { if currentReplicas == 1 { newReplicas = step } else { diff --git a/gateway/handlers/alerthandler_test.go b/gateway/handlers/alerthandler_test.go index dc1c2c04..7ac864f6 100644 --- a/gateway/handlers/alerthandler_test.go +++ b/gateway/handlers/alerthandler_test.go @@ -11,8 +11,8 @@ func TestDisabledScale(t *testing.T) { minReplicas := uint64(1) scalingFactor := uint64(0) newReplicas := CalculateReplicas("firing", DefaultMinReplicas, DefaultMaxReplicas, minReplicas, scalingFactor) - if newReplicas != 0 { - t.Log("Expected not to scale") + if newReplicas != minReplicas { + t.Logf("Expected not to scale, but replicas were: %d", newReplicas) t.Fail() } } @@ -27,6 +27,17 @@ func TestParameterEdge(t *testing.T) { } } +func TestScalingWithSameUpperLowerLimit(t *testing.T) { + minReplicas := uint64(1) + scalingFactor := uint64(20) + // status string, currentReplicas uint64, maxReplicas uint64, minReplicas uint64, scalingFactor uint64) + newReplicas := CalculateReplicas("firing", minReplicas, minReplicas, minReplicas, scalingFactor) + if newReplicas != 1 { + t.Logf("Replicas - want: %d, got: %d", minReplicas, newReplicas) + t.Fail() + } +} + func TestMaxScale(t *testing.T) { minReplicas := uint64(1) scalingFactor := uint64(100)