Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Limiting Max Child Processes Via an Exec Env Var API #69

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions src/Environment/Memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Environment;

use InvalidArgumentException;
use LogicException;

class Memory implements MemoryInterface
{
protected $EnvironmentMaximumMemoryValue;
protected $EnvironmentMaximumMemoryInputUnit;
protected $WorkerMaximumMemoryValue;
protected $WorkerMaximumMemoryInputUnit;
protected $MaximumNumberOfWorkers;

public function getMaximumNumberOfWorkers(): int
{
if ($this->MaximumNumberOfWorkers === null) {
$environmentMaximumMemoryValue = $this->getEnvironmentMaximumMemoryValue();
$workerMaximumMemoryValue = $this->getWorkerMaximumMemoryValue();
$maximumNumberOfWorkers = (int)floor($environmentMaximumMemoryValue / $workerMaximumMemoryValue);
$this->MaximumNumberOfWorkers = $maximumNumberOfWorkers;
}

return $this->MaximumNumberOfWorkers;
}

public function setEnvironmentMaximumMemoryValue(int $EnvironmentMaximumMemoryValue): MemoryInterface
{
if ($this->EnvironmentMaximumMemoryValue !== null) {
throw new LogicException('Environment Maximum Memory Value is already set.');
}

$this->EnvironmentMaximumMemoryValue = $this->getBytes(
$EnvironmentMaximumMemoryValue,
$this->getEnvironmentMaximumMemoryInputUnit()
);

return $this;
}

protected function getEnvironmentMaximumMemoryValue(): int
{
if ($this->EnvironmentMaximumMemoryValue === null) {
throw new LogicException('Environment Maximum Memory Value has not been set.');
}

return $this->EnvironmentMaximumMemoryValue;
}

public function setEnvironmentMaximumMemoryInputUnit(string $EnvironmentMaximumMemoryUnit): MemoryInterface
{
if ($this->EnvironmentMaximumMemoryInputUnit !== null) {
throw new LogicException('Environment Maximum Memory Input Unit is already set.');
}

$this->assertValidMemoryUnit($EnvironmentMaximumMemoryUnit);
$this->EnvironmentMaximumMemoryInputUnit = $EnvironmentMaximumMemoryUnit;

return $this;
}

protected function getEnvironmentMaximumMemoryInputUnit(): string
{
if ($this->EnvironmentMaximumMemoryInputUnit === null) {
throw new LogicException('Environment Maximum Memory Input Unit has not been set.');
}

return $this->EnvironmentMaximumMemoryInputUnit;
}

public function setWorkerMaximumMemoryValue(int $WorkerMaximumMemoryValue): MemoryInterface
{
if ($this->WorkerMaximumMemoryValue !== null) {
throw new LogicException('Worker Maximum Memory Value is already set.');
}

$this->WorkerMaximumMemoryValue = $this->getBytes(
$WorkerMaximumMemoryValue,
$this->getWorkerMaximumMemoryInputUnit()
);

return $this;
}

public function getWorkerMaximumMemoryValue(): int
{
if ($this->WorkerMaximumMemoryValue === null) {
throw new LogicException('Worker Maximum Memory Value has not been set.');
}

return $this->WorkerMaximumMemoryValue;
}

public function setWorkerMaximumMemoryInputUnit(string $WorkerMaximumMemoryUnit): MemoryInterface
{
if ($this->WorkerMaximumMemoryInputUnit !== null) {
throw new LogicException('Worker Maximum Memory Input Unit is already set.');
}

$this->assertValidMemoryUnit($WorkerMaximumMemoryUnit);

$this->WorkerMaximumMemoryInputUnit = $WorkerMaximumMemoryUnit;

return $this;
}

protected function getWorkerMaximumMemoryInputUnit(): string
{
if ($this->WorkerMaximumMemoryInputUnit === null) {
throw new LogicException('Worker Maximum Memory Input Unit has not been set.');
}

return $this->WorkerMaximumMemoryInputUnit;
}

protected function getBytes(int $Value, string $Unit): int
{
switch ($Unit) {
/** @noinspection PhpMissingBreakStatementInspection */
case self::Gibibyte:
$Value *= 1024;
/** @noinspection PhpMissingBreakStatementInspection */
case self::Mebibyte:
$Value *= 1024;
case self::Kibibyte:
$Value *= 1024;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have a default exceptional case here.

}

return $Value;
}

protected function assertValidMemoryUnit(string $MemoryUnit): MemoryInterface
{
switch ($MemoryUnit) {
case self::Kibibyte:
case self::Mebibyte:
case self::Gibibyte:
break;
default:
throw new InvalidArgumentException(sprintf('Memory Unit [%s] is not KiB, MiB, or GiB.', $MemoryUnit));
}

return $this;
}
}
41 changes: 41 additions & 0 deletions src/Environment/Memory/AwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Environment\Memory;

use Neighborhoods\Kojo\Environment\MemoryInterface;

/** @codeCoverageIgnore */
trait AwareTrait
{
protected $NeighborhoodsKojoEnvironmentMemory;

public function setEnvironmentMemory(MemoryInterface $environmentMemory): self
{
assert(!$this->hasEnvironmentMemory(),
new \LogicException('NeighborhoodsKojoEnvironmentMemory is already set.'));
$this->NeighborhoodsKojoEnvironmentMemory = $environmentMemory;

return $this;
}

protected function getEnvironmentMemory(): MemoryInterface
{
assert($this->hasEnvironmentMemory(), new \LogicException('NeighborhoodsKojoEnvironmentMemory is not set.'));

return $this->NeighborhoodsKojoEnvironmentMemory;
}

protected function hasEnvironmentMemory(): bool
{
return isset($this->NeighborhoodsKojoEnvironmentMemory);
}

protected function unsetEnvironmentMemory(): self
{
assert($this->hasEnvironmentMemory(), new \LogicException('NeighborhoodsKojoEnvironmentMemory is not set.'));
unset($this->NeighborhoodsKojoEnvironmentMemory);

return $this;
}
}
23 changes: 23 additions & 0 deletions src/Environment/MemoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Environment;

interface MemoryInterface
{
public const Gibibyte = 'GiB';
public const Mebibyte = 'MiB';
public const Kibibyte = 'KiB';

public function setEnvironmentMaximumMemoryValue(int $EnvironmentMaximumMemoryValue): MemoryInterface;

public function setWorkerMaximumMemoryInputUnit(string $WorkerMaximumMemoryUnit): MemoryInterface;

public function getMaximumNumberOfWorkers(): int;

public function setWorkerMaximumMemoryValue(int $WorkerMaximumMemoryValue): MemoryInterface;

public function setEnvironmentMaximumMemoryInputUnit(string $EnvironmentMaximumMemoryUnit): MemoryInterface;

public function getWorkerMaximumMemoryValue(): int;
}
15 changes: 15 additions & 0 deletions src/Environment/MemoryInterface.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
Neighborhoods\Kojo\Environment\MemoryInterface.EnvironmentMaximumMemoryInputValue: '%env(ENVIRONMENT_MAXIMUM_MEMORY_INPUT_VALUE)%'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows missing values since this will be a 4.x release.
Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\EnvNotFoundException: Environment variable not found: "ENVIRONMENT_MAXIMUM_MEMORY_INPUT_VALUE". in /var/www/html/kojo_fitness.neighborhoods.com/UseCase46/vendor/symfony/dependency-injection/EnvVarProcessor.php on line 131

Neighborhoods\Kojo\Environment\MemoryInterface.EnvironmentMaximumMemoryInputUnit: '%env(ENVIRONMENT_MAXIMUM_MEMORY_INPUT_UNIT)%'
Neighborhoods\Kojo\Environment\MemoryInterface.WorkerMaximumMemoryValue: '%env(WORKER_MAXIMUM_MEMORY_INPUT_VALUE)%'
Neighborhoods\Kojo\Environment\MemoryInterface.WorkerMaximumMemoryUnit: '%env(WORKER_MAXIMUM_MEMORY_INPUT_UNIT)%'
services:
Neighborhoods\Kojo\Environment\MemoryInterface:
class: Neighborhoods\Kojo\Environment\Memory
shared: true
public: false
calls:
- [setEnvironmentMaximumMemoryValue, ['%Neighborhoods\Kojo\Environment\MemoryInterface.EnvironmentMaximumMemoryInputValue%']]
- [setEnvironmentMaximumMemoryInputUnit, ['%Neighborhoods\Kojo\Environment\MemoryInterface.EnvironmentMaximumMemoryInputUnit%']]
- [setWorkerMaximumMemoryValue, ['%Neighborhoods\Kojo\Environment\MemoryInterface.WorkerMaximumMemoryValue%']]
- [setWorkerMaximumMemoryInputUnit, ['%Neighborhoods\Kojo\Environment\MemoryInterface.WorkerMaximumMemoryUnit%']]
15 changes: 14 additions & 1 deletion src/Process/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Neighborhoods\Kojo\Process;
use Neighborhoods\Kojo\Scheduler;
use Neighborhoods\Kojo\Selector;
use Neighborhoods\Kojo\Environment;
use Throwable;

class Job extends Forked implements JobInterface
{
Expand All @@ -16,6 +18,7 @@ class Job extends Forked implements JobInterface
use Scheduler\AwareTrait;
use Selector\AwareTrait;
use Process\Pool\Factory\AwareTrait;
use Environment\Memory\AwareTrait;

protected function _run(): Forked
{
Expand All @@ -26,12 +29,22 @@ protected function _run(): Forked
$this->_getScheduler()->scheduleStaticJobs();
$this->_getMaintainer()->updatePendingJobs();
$this->_getMaintainer()->deleteCompletedJobs();
$this->applyMemoryLimit();
$this->_getForeman()->workWorker();
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
$this->_setOrReplaceExitCode(255);
}

return $this;
}

protected function applyMemoryLimit(): JobInterface
{
$processMaximumMemoryValue = $this->getEnvironmentMemory()->getWorkerMaximumMemoryValue();
$this->_getLogger()->debug(sprintf('Applying memory limit of [%s] bytes.', $processMaximumMemoryValue));
ini_set('memory_limit', $processMaximumMemoryValue);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that PHP will set memory_limit to be 2MiB if it is set to any smaller value. 2MiB is the smallest PHP allows.


return $this;
}
}
3 changes: 2 additions & 1 deletion src/Process/Job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ services:
- [setUuidMaximumInteger, [9999999999]]
- [setProcessPoolFactory, ['@process.pool.factory-job']]
- [setTitlePrefix, ['%process.title.prefix%']]
- [setEnvironmentMemory, ['@Neighborhoods\Kojo\Environment\MemoryInterface']]
process.job:
alias: neighborhoods.kojo.process.job
alias: neighborhoods.kojo.process.job
2 changes: 1 addition & 1 deletion src/Process/Pool/Factory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ services:
- [setProcessPoolStrategy, ['@process.pool.strategy-job']]
- [setProcessCollection, ['@process.collection-empty']]
process.pool.factory-empty:
alias: neighborhoods.kojo.process.pool.factory-empty
alias: neighborhoods.kojo.process.pool.factory-empty
2 changes: 1 addition & 1 deletion src/Process/Pool/Logger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
- [setProcessPoolLoggerMessageFactory, ['@neighborhoods.kojo.process.pool.logger.message.factory']]
- [setLevelFilterMask, ['%neighborhoods.kojo.process.pool.logger.level_filter_mask%']]
process.pool.logger:
alias: neighborhoods.kojo.process.pool.logger
alias: neighborhoods.kojo.process.pool.logger
8 changes: 4 additions & 4 deletions src/Process/Pool/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function _listenerProcessExited(ListenerInterface $listenerProcess): S
while (
$listenerProcess->hasMessages()
&& !$this->_getProcessPool()->isFull()
&& $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()
&& $this->canEnvironmentSustainAdditionalProcesses()
) {
$listenerProcess->processMessages();
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public function currentPendingChildExitsCompleted(): StrategyInterface
protected function _jobProcessExited(JobInterface $jobProcess): Strategy
{
$this->_getProcessPool()->freeChildProcess($jobProcess->getProcessId());
if ($jobProcess->getExitCode() !== 0 && $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()) {
if ($jobProcess->getExitCode() !== 0 && $this->canEnvironmentSustainAdditionalProcesses()) {
$typeCode = $jobProcess->getTypeCode();
$replacementProcess = $this->_getProcessCollection()->getProcessPrototypeClone($typeCode);
$replacementProcess->setThrottle($this->getChildProcessWaitThrottle());
Expand All @@ -97,7 +97,7 @@ protected function _jobProcessExited(JobInterface $jobProcess): Strategy

public function receivedAlarm(): StrategyInterface
{
if (!$this->_getProcessPool()->isFull() && $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()) {
if (!$this->_getProcessPool()->isFull() && $this->canEnvironmentSustainAdditionalProcesses()) {
if ($this->_hasPausedListenerProcess()) {
$this->_unPauseListenerProcesses();
} else {
Expand Down Expand Up @@ -131,7 +131,7 @@ public function initializePool(): StrategyInterface
}
}
}
if ($this->_hasFillProcessTypeCode() && $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()) {
if ($this->_hasFillProcessTypeCode() && $this->canEnvironmentSustainAdditionalProcesses()) {
while (!$this->_getProcessPool()->isFull()) {
$fillProcessTypeCode = $this->_getFillProcessTypeCode();
$fillProcess = $this->_getProcessCollection()->getProcessPrototypeClone($fillProcessTypeCode);
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Pool/Strategy/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function initializePool(): StrategyInterface
$this->_getProcessPool()->getProcess()->exit();
}
}
if ($this->_hasFillProcessTypeCode() && $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()) {
if ($this->_hasFillProcessTypeCode() && $this->canEnvironmentSustainAdditionalProcesses()) {
while (!$this->_getProcessPool()->isFull()) {
$fillProcessTypeCode = $this->_getFillProcessTypeCode();
$fillProcess = $this->_getProcessCollection()->getProcessPrototypeClone($fillProcessTypeCode);
Expand Down
Loading