Simplify viable nodes service

This commit is contained in:
Lance Pioch 2024-05-21 21:44:49 -04:00
parent da698a3666
commit f9aa8cf218
4 changed files with 27 additions and 95 deletions

View File

@ -9,9 +9,6 @@ use App\Http\Requests\Api\Application\Nodes\GetDeployableNodesRequest;
class NodeDeploymentController extends ApplicationApiController class NodeDeploymentController extends ApplicationApiController
{ {
/**
* NodeDeploymentController constructor.
*/
public function __construct(private FindViableNodesService $viableNodesService) public function __construct(private FindViableNodesService $viableNodesService)
{ {
parent::__construct(); parent::__construct();
@ -25,10 +22,13 @@ class NodeDeploymentController extends ApplicationApiController
public function __invoke(GetDeployableNodesRequest $request): array public function __invoke(GetDeployableNodesRequest $request): array
{ {
$data = $request->validated(); $data = $request->validated();
$nodes = $this->viableNodesService
->setMemory($data['memory']) $nodes = $this->viableNodesService->handle(
->setDisk($data['disk']) $data['disk'] ?? 0,
->handle((int) $request->query('per_page'), (int) $request->query('page')); $data['memory'] ?? 0,
$data['cpu'] ?? 0,
$data['location_ids'] ?? $data['tags'] ?? [],
);
return $this->fractal->collection($nodes) return $this->fractal->collection($nodes)
->transformWith($this->getTransformer(NodeTransformer::class)) ->transformWith($this->getTransformer(NodeTransformer::class))

View File

@ -3,37 +3,10 @@
namespace App\Services\Deployment; namespace App\Services\Deployment;
use App\Models\Node; use App\Models\Node;
use Webmozart\Assert\Assert;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class FindViableNodesService class FindViableNodesService
{ {
protected ?int $disk = null;
protected ?int $memory = null;
/**
* Set the amount of disk that will be used by the server being created. Nodes will be
* filtered out if they do not have enough available free disk space for this server
* to be placed on.
*/
public function setDisk(int $disk): self
{
$this->disk = $disk;
return $this;
}
/**
* Set the amount of memory that this server will be using. As with disk space, nodes that
* do not have enough free memory will be filtered out.
*/
public function setMemory(int $memory): self
{
$this->memory = $memory;
return $this;
}
/** /**
* Returns a collection of nodes that meet the provided requirements and can then * Returns a collection of nodes that meet the provided requirements and can then
* be passed to the AllocationSelectionService to return a single allocation. * be passed to the AllocationSelectionService to return a single allocation.
@ -44,21 +17,20 @@ class FindViableNodesService
* are tossed out, as are any nodes marked as non-public, meaning automatic * are tossed out, as are any nodes marked as non-public, meaning automatic
* deployments should not be done against them. * deployments should not be done against them.
*/ */
public function handle(int $disk = null, int $memory = null, int $cpu = null): Collection public function handle(int $disk = 0, int $memory = 0, int $cpu = 0, $tags = []): Collection
{ {
Assert::integer($this->disk, 'Disk space must be an int, got %s'); $nodes = Node::query()
Assert::integer($this->memory, 'Memory usage must be an int, got %s'); ->withSum('servers', 'disk')
->withSum('servers', 'memory')
->withSum('servers', 'cpu')
->where('public', true)
->get();
$query = Node::query()->select('nodes.*') return $nodes
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory') ->filter(fn (Node $node) => !$tags || collect($node->tags)->intersect($tags))
->selectRaw('IFNULL(SUM(servers.disk), 0) as sum_disk') ->filter(fn (Node $node) => $node->servers_sum_disk + $disk <= $node->disk * (1 + $node->disk_overallocate / 100))
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id') ->filter(fn (Node $node) => $node->servers_sum_memory + $memory <= $node->memory * (1 + $node->memory_overallocate / 100))
->where('nodes.public', 1); ->filter(fn (Node $node) => $node->servers_sum_cpu + $cpu <= $node->cpu * (1 + $node->cpu_overallocate / 100))
;
$results = $query->groupBy('nodes.id')
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$this->memory])
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$this->disk]);
return $results->get();
} }
} }

View File

@ -107,11 +107,13 @@ class ServerCreationService
*/ */
private function configureDeployment(array $data, DeploymentObject $deployment): Allocation private function configureDeployment(array $data, DeploymentObject $deployment): Allocation
{ {
/** @var \Illuminate\Support\Collection $nodes */ /** @var Collection<\App\Models\Node> $nodes */
$nodes = $this->findViableNodesService $nodes = $this->findViableNodesService->handle(
->setDisk(Arr::get($data, 'disk')) Arr::get($data, 'disk', 0),
->setMemory(Arr::get($data, 'memory')) Arr::get($data, 'memory', 0),
->handle(); Arr::get($data, 'cpu', 0),
Arr::get($data, 'tags', []),
);
return $this->allocationSelectionService->setDedicated($deployment->isDedicated()) return $this->allocationSelectionService->setDedicated($deployment->isDedicated())
->setNodes($nodes->pluck('id')->toArray()) ->setNodes($nodes->pluck('id')->toArray())

View File

@ -1,42 +0,0 @@
<?php
namespace App\Tests\Integration\Services\Deployment;
use App\Models\Node;
use App\Models\Server;
use App\Models\Database;
use App\Tests\Integration\IntegrationTestCase;
use App\Services\Deployment\FindViableNodesService;
class FindViableNodesServiceTest extends IntegrationTestCase
{
protected function setUp(): void
{
parent::setUp();
Database::query()->delete();
Server::query()->delete();
Node::query()->delete();
}
public function testExceptionIsThrownIfNoDiskSpaceHasBeenSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Disk space must be an int, got NULL');
$this->getService()->handle();
}
public function testExceptionIsThrownIfNoMemoryHasBeenSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Memory usage must be an int, got NULL');
$this->getService()->setDisk(10)->handle();
}
private function getService(): FindViableNodesService
{
return $this->app->make(FindViableNodesService::class);
}
}