Skip to content

Commit

Permalink
Ensure Autoscaler never reduces processes to less than 1 (#1221)
Browse files Browse the repository at this point in the history
* Don't tolerate negative `$maxUpShift`

* Fix autoscaling letting processes hit 0

* Fix autoscaling letting processes hit 0
  • Loading branch information
LasseRafn committed Nov 25, 2022
1 parent 9e65557 commit 01b26da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AutoScaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function scalePool(Supervisor $supervisor, $pool, $workers)

if ($desiredProcessCount > $totalProcessCount) {
$maxUpShift = min(
$supervisor->options->maxProcesses - $supervisor->totalProcessCount(),
max(0, $supervisor->options->maxProcesses - $supervisor->totalProcessCount()),
$supervisor->options->balanceMaxShift
);

Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/AutoScalerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,26 @@ public function test_scaler_considers_max_shift_and_attempts_to_get_closer_to_pr
$this->assertEquals(100, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(50, $supervisor->processPools['second']->totalProcessCount());
}

public function test_scaler_does_not_permit_going_to_zero_processes_despite_exceeding_max_processes()
{
$external = Mockery::mock(SystemProcessCounter::class);
$external->shouldReceive('get')->with('name')->andReturn(5);
$this->app->instance(SystemProcessCounter::class, $external);

[$scaler, $supervisor] = $this->with_scaling_scenario(15, [
'first' => ['current' => 16, 'size' => 1, 'runtime' => 1],
'second' => ['current' => 1, 'size' => 1, 'runtime' => 1],
], ['minProcesses' => 1]);

$scaler->scale($supervisor);

$this->assertSame(15, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertSame(14, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());
}
}

0 comments on commit 01b26da

Please sign in to comment.