From dcebc1e00f5374d0ef42f8db30f7e40062695db5 Mon Sep 17 00:00:00 2001 From: Sander Dekkers Date: Wed, 7 Aug 2019 14:44:00 +0200 Subject: [PATCH 1/5] Fixed #622, added a return statement when timeToClearPerQueue = 0, to return minProcesses --- src/AutoScaler.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/AutoScaler.php b/src/AutoScaler.php index 7c3be16e..89df49db 100644 --- a/src/AutoScaler.php +++ b/src/AutoScaler.php @@ -96,8 +96,10 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $t return $timeToClear->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) { return $timeToClearAll > 0 && $supervisor->options->autoScaling() - ? [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)] - : [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]; + ? [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)] + : ($timeToClearAll == 0 && $supervisor->options->autoScaling() + ? [$queue => $supervisor->options->minProcesses] + : [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]); })->sort(); } From 095e7b8a728a6fda2566672f8d3c29298781c450 Mon Sep 17 00:00:00 2001 From: Sander Dekkers Date: Wed, 7 Aug 2019 15:26:40 +0200 Subject: [PATCH 2/5] Updated tests to reflect new behavior defaulting to the min amount of processes with an empty queue --- tests/Feature/AutoScalerTest.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/Feature/AutoScalerTest.php b/tests/Feature/AutoScalerTest.php index af920660..4819ae1b 100644 --- a/tests/Feature/AutoScalerTest.php +++ b/tests/Feature/AutoScalerTest.php @@ -51,18 +51,33 @@ public function test_balance_stays_even_when_queue_is_empty() $scaler->scale($supervisor); - $this->assertEquals(5, $supervisor->processPools['first']->totalProcessCount()); - $this->assertEquals(5, $supervisor->processPools['second']->totalProcessCount()); + $this->assertEquals(4, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(4, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + + $this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(3, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + + $this->assertEquals(2, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(2, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + + $this->assertEquals(1, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); } - public function test_balancing_a_single_queue_assigns_it_the_max_workers() + public function test_balancing_a_single_queue_assigns_it_the_min_workers_with_empty_queue() { [$scaler, $supervisor] = $this->with_scaling_scenario(5, [ - 'first' => ['current' => 4, 'size' => 0, 'runtime' => 0], + 'first' => ['current' => 2, 'size' => 0, 'runtime' => 0], ]); $scaler->scale($supervisor); - $this->assertEquals(5, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['first']->totalProcessCount()); } public function test_scaler_will_not_scale_past_max_process_threshold_under_high_load() From db4a3992c52e7dfb67ba9f400257098c239c1e98 Mon Sep 17 00:00:00 2001 From: Sander Dekkers Date: Wed, 7 Aug 2019 15:37:54 +0200 Subject: [PATCH 3/5] Added test to verify amount of processes goes up for busy queue --- tests/Feature/AutoScalerTest.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Feature/AutoScalerTest.php b/tests/Feature/AutoScalerTest.php index 4819ae1b..a54af322 100644 --- a/tests/Feature/AutoScalerTest.php +++ b/tests/Feature/AutoScalerTest.php @@ -70,6 +70,39 @@ public function test_balance_stays_even_when_queue_is_empty() $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); } + public function test_balancer_assigns_more_processes_on_busy_queue() + { + [$scaler, $supervisor] = $this->with_scaling_scenario(10, [ + 'first' => ['current' => 1, 'size' => 50, 'runtime' => 50], + 'second' => ['current' => 1, 'size' => 0, 'runtime' => 0], + ]); + + $scaler->scale($supervisor); + $scaler->scale($supervisor); + + $this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + $scaler->scale($supervisor); + + $this->assertEquals(5, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + $scaler->scale($supervisor); + + $this->assertEquals(7, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); + + $scaler->scale($supervisor); + $scaler->scale($supervisor); + $scaler->scale($supervisor); + + $this->assertEquals(9, $supervisor->processPools['first']->totalProcessCount()); + $this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount()); + } + public function test_balancing_a_single_queue_assigns_it_the_min_workers_with_empty_queue() { [$scaler, $supervisor] = $this->with_scaling_scenario(5, [ From c94e265e234ef95812b169c14bf9d6997ea149d7 Mon Sep 17 00:00:00 2001 From: Sander Dekkers Date: Tue, 20 Aug 2019 09:20:08 +0200 Subject: [PATCH 4/5] Updated AutoScaler::numberOfWorkersPerQueue() to not use ternary statements --- src/AutoScaler.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/AutoScaler.php b/src/AutoScaler.php index 89df49db..10a329c2 100644 --- a/src/AutoScaler.php +++ b/src/AutoScaler.php @@ -95,11 +95,17 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $t $timeToClearAll = $timeToClear->sum(); return $timeToClear->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) { - return $timeToClearAll > 0 && $supervisor->options->autoScaling() - ? [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)] - : ($timeToClearAll == 0 && $supervisor->options->autoScaling() - ? [$queue => $supervisor->options->minProcesses] - : [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]); + + if ($timeToClearAll > 0 && + $supervisor->options->autoScaling()) { + return [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)]; + } elseif ($timeToClearAll == 0 && + $supervisor->options->autoScaling()) { + return [$queue => $supervisor->options->minProcesses]; + } + + return [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]; + })->sort(); } From 2b50886b70e61c80c60f61ac671f4e21074949f6 Mon Sep 17 00:00:00 2001 From: Sander Dekkers Date: Tue, 20 Aug 2019 09:23:11 +0200 Subject: [PATCH 5/5] Removing empty lines for StyleCI --- src/AutoScaler.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/AutoScaler.php b/src/AutoScaler.php index 10a329c2..9946edbc 100644 --- a/src/AutoScaler.php +++ b/src/AutoScaler.php @@ -95,7 +95,6 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $t $timeToClearAll = $timeToClear->sum(); return $timeToClear->mapWithKeys(function ($timeToClear, $queue) use ($supervisor, $timeToClearAll) { - if ($timeToClearAll > 0 && $supervisor->options->autoScaling()) { return [$queue => (($timeToClear / $timeToClearAll) * $supervisor->options->maxProcesses)]; @@ -105,7 +104,6 @@ protected function numberOfWorkersPerQueue(Supervisor $supervisor, Collection $t } return [$queue => $supervisor->options->maxProcesses / count($supervisor->processPools)]; - })->sort(); }