markusthoemmes closed pull request #3272: improve loadbalancer's schedule algorithm URL: https://github.com/apache/incubator-openwhisk/pull/3272
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/core/controller/src/main/scala/whisk/core/loadBalancer/ContainerPoolBalancer.scala b/core/controller/src/main/scala/whisk/core/loadBalancer/ContainerPoolBalancer.scala index 193ed8da9f..a5327b0907 100644 --- a/core/controller/src/main/scala/whisk/core/loadBalancer/ContainerPoolBalancer.scala +++ b/core/controller/src/main/scala/whisk/core/loadBalancer/ContainerPoolBalancer.scala @@ -18,6 +18,7 @@ package whisk.core.loadBalancer import java.nio.charset.StandardCharsets +import java.util.concurrent.ThreadLocalRandom import akka.actor.{ActorSystem, Props} import akka.cluster.Cluster @@ -343,7 +344,11 @@ object ContainerPoolBalancer extends LoadBalancerProvider { .find(_._3 < invokerBusyThreshold) .orElse(invokerProgression.find(_._3 < invokerBusyThreshold * 2)) .orElse(invokerProgression.find(_._3 < invokerBusyThreshold * 3)) - .orElse(invokerProgression.headOption) + .orElse( + if (invokerProgression.isEmpty) + None + else + Some(invokerProgression(ThreadLocalRandom.current().nextInt(invokerProgression.size)))) .map(_._1) } else None } diff --git a/tests/src/test/scala/whisk/core/loadBalancer/test/ContainerPoolBalancerObjectTests.scala b/tests/src/test/scala/whisk/core/loadBalancer/test/ContainerPoolBalancerObjectTests.scala index 60eda84a79..fd3252d1c8 100644 --- a/tests/src/test/scala/whisk/core/loadBalancer/test/ContainerPoolBalancerObjectTests.scala +++ b/tests/src/test/scala/whisk/core/loadBalancer/test/ContainerPoolBalancerObjectTests.scala @@ -143,11 +143,14 @@ class ContainerPoolBalancerObjectTests extends FlatSpec with Matchers { ContainerPoolBalancer.schedule(invs, 16, hash) shouldBe Some(InstanceId(0)) } - it should "choose the home invoker if all invokers are overloaded even above the muliplied threshold" in { - val invs = IndexedSeq((InstanceId(0), Healthy, 51), (InstanceId(1), Healthy, 50), (InstanceId(2), Healthy, 49)) - val hash = 0 // home is 0, stepsize is 1 - - ContainerPoolBalancer.schedule(invs, 16, hash) shouldBe Some(InstanceId(0)) + it should "choose the random invoker if all invokers are overloaded even above the muliplied threshold" in { + val invs = IndexedSeq((InstanceId(0), Healthy, 33), (InstanceId(1), Healthy, 33), (InstanceId(2), Healthy, 33)) + val invokerBusyThreshold = 11 + val hash = 0 + val bruteResult = (0 to 100) map { _ => + ContainerPoolBalancer.schedule(invs, invokerBusyThreshold, hash).get.toInt + } + bruteResult should contain allOf (0, 1, 2) } it should "transparently work with partitioned sets of invokers" in { ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services