Return all servers for a node as a paginated response
Avoids crashing the PHP process and avoids a bad runaway N+1 query issue that previously existed.
This commit is contained in:
parent
73b795faba
commit
c00e5b36a5
7 changed files with 56 additions and 57 deletions
|
@ -139,16 +139,4 @@ interface ServerRepositoryInterface extends RepositoryInterface
|
||||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||||
*/
|
*/
|
||||||
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator;
|
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns every server that exists for a given node.
|
|
||||||
*
|
|
||||||
* This is different from {@see loadAllServersForNode} because
|
|
||||||
* it does not paginate the response.
|
|
||||||
*
|
|
||||||
* @param int $node
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
|
|
||||||
*/
|
|
||||||
public function loadEveryServerForNode(int $node);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,13 @@
|
||||||
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
||||||
use Pterodactyl\Services\Eggs\EggConfigurationService;
|
use Pterodactyl\Services\Eggs\EggConfigurationService;
|
||||||
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
||||||
|
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
|
||||||
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
||||||
|
|
||||||
class ServerDetailsController extends Controller
|
class ServerDetailsController extends Controller
|
||||||
|
@ -27,11 +29,6 @@ class ServerDetailsController extends Controller
|
||||||
*/
|
*/
|
||||||
private $configurationStructureService;
|
private $configurationStructureService;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
|
|
||||||
*/
|
|
||||||
private $nodeRepository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ServerConfigurationController constructor.
|
* ServerConfigurationController constructor.
|
||||||
*
|
*
|
||||||
|
@ -49,7 +46,6 @@ class ServerDetailsController extends Controller
|
||||||
$this->eggConfigurationService = $eggConfigurationService;
|
$this->eggConfigurationService = $eggConfigurationService;
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->configurationStructureService = $configurationStructureService;
|
$this->configurationStructureService = $configurationStructureService;
|
||||||
$this->nodeRepository = $nodeRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +62,7 @@ class ServerDetailsController extends Controller
|
||||||
{
|
{
|
||||||
$server = $this->repository->getByUuid($uuid);
|
$server = $this->repository->getByUuid($uuid);
|
||||||
|
|
||||||
return JsonResponse::create([
|
return new JsonResponse([
|
||||||
'settings' => $this->configurationStructureService->handle($server),
|
'settings' => $this->configurationStructureService->handle($server),
|
||||||
'process_configuration' => $this->eggConfigurationService->handle($server),
|
'process_configuration' => $this->eggConfigurationService->handle($server),
|
||||||
]);
|
]);
|
||||||
|
@ -76,25 +72,19 @@ class ServerDetailsController extends Controller
|
||||||
* Lists all servers with their configurations that are assigned to the requesting node.
|
* Lists all servers with their configurations that are assigned to the requesting node.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
*
|
* @return \Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*
|
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function list(Request $request)
|
public function list(Request $request)
|
||||||
{
|
{
|
||||||
|
/** @var \Pterodactyl\Models\Node $node */
|
||||||
$node = $request->attributes->get('node');
|
$node = $request->attributes->get('node');
|
||||||
$servers = $this->repository->loadEveryServerForNode($node->id);
|
|
||||||
|
|
||||||
$configurations = [];
|
// Avoid run-away N+1 SQL queries by pre-loading the relationships that are used
|
||||||
|
// within each of the services called below.
|
||||||
|
$servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables')
|
||||||
|
->where('node_id', $node->id)
|
||||||
|
->paginate($request->input('per_page', 50));
|
||||||
|
|
||||||
foreach ($servers as $server) {
|
return new ServerConfigurationCollection($servers);
|
||||||
$configurations[$server->uuid] = [
|
|
||||||
'settings' => $this->configurationStructureService->handle($server),
|
|
||||||
'process_configuration' => $this->eggConfigurationService->handle($server),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonResponse::create($configurations);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ class DaemonAuthenticate
|
||||||
// Ensure that all of the correct parts are provided in the header.
|
// Ensure that all of the correct parts are provided in the header.
|
||||||
if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) {
|
if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) {
|
||||||
throw new BadRequestHttpException(
|
throw new BadRequestHttpException(
|
||||||
'The Authorization headed provided was not in a valid format.'
|
'The Authorization header provided was not in a valid format.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
app/Http/Resources/Wings/ServerConfigurationCollection.php
Normal file
35
app/Http/Resources/Wings/ServerConfigurationCollection.php
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Resources\Wings;
|
||||||
|
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
|
use Illuminate\Container\Container;
|
||||||
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||||
|
use Pterodactyl\Services\Eggs\EggConfigurationService;
|
||||||
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
||||||
|
|
||||||
|
class ServerConfigurationCollection extends ResourceCollection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Converts a collection of Server models into an array of configuration responses
|
||||||
|
* that can be understood by Wings. Make sure you've properly loaded the required
|
||||||
|
* relationships on the Server models before calling this function, otherwise you'll
|
||||||
|
* have some serious performance issues from all of the N+1 queries.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
$egg = Container::getInstance()->make(EggConfigurationService::class);
|
||||||
|
$configuration = Container::getInstance()->make(ServerConfigurationStructureService::class);
|
||||||
|
|
||||||
|
return $this->collection->map(function (Server $server) use ($configuration, $egg) {
|
||||||
|
return [
|
||||||
|
'uuid' => $server->uuid,
|
||||||
|
'settings' => $configuration->handle($server),
|
||||||
|
'process_configuration' => $egg->handle($server),
|
||||||
|
];
|
||||||
|
})->toArray();
|
||||||
|
}
|
||||||
|
}
|
|
@ -83,6 +83,13 @@ class Server extends Model
|
||||||
'oom_disabled' => true,
|
'oom_disabled' => true,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default relationships to load for all server models.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $with = ['allocation'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be mutated to dates.
|
* The attributes that should be mutated to dates.
|
||||||
*
|
*
|
||||||
|
|
|
@ -270,22 +270,4 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
|
||||||
->where('node_id', '=', $node)
|
->where('node_id', '=', $node)
|
||||||
->paginate($limit);
|
->paginate($limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns every server that exists for a given node.
|
|
||||||
*
|
|
||||||
* This is different from {@see loadAllServersForNode} because
|
|
||||||
* it does not paginate the response.
|
|
||||||
*
|
|
||||||
* @param int $node
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
|
|
||||||
*/
|
|
||||||
public function loadEveryServerForNode(int $node)
|
|
||||||
{
|
|
||||||
return $this->getBuilder()
|
|
||||||
->with('nest')
|
|
||||||
->where('node_id', '=', $node)
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@ use Pterodactyl\Models\Server;
|
||||||
|
|
||||||
class ServerConfigurationStructureService
|
class ServerConfigurationStructureService
|
||||||
{
|
{
|
||||||
const REQUIRED_RELATIONS = ['allocation', 'allocations', 'egg'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
||||||
*/
|
*/
|
||||||
|
@ -31,13 +29,11 @@ class ServerConfigurationStructureService
|
||||||
* daemon, if you modify the structure eggs will break unexpectedly.
|
* daemon, if you modify the structure eggs will break unexpectedly.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Models\Server $server
|
* @param \Pterodactyl\Models\Server $server
|
||||||
* @param bool $legacy
|
* @param bool $legacy deprecated
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function handle(Server $server, bool $legacy = false): array
|
public function handle(Server $server, bool $legacy = false): array
|
||||||
{
|
{
|
||||||
$server->loadMissing(self::REQUIRED_RELATIONS);
|
|
||||||
|
|
||||||
return $legacy ?
|
return $legacy ?
|
||||||
$this->returnLegacyFormat($server)
|
$this->returnLegacyFormat($server)
|
||||||
: $this->returnCurrentFormat($server);
|
: $this->returnCurrentFormat($server);
|
||||||
|
@ -93,6 +89,7 @@ class ServerConfigurationStructureService
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Models\Server $server
|
* @param \Pterodactyl\Models\Server $server
|
||||||
* @return array
|
* @return array
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
protected function returnLegacyFormat(Server $server)
|
protected function returnLegacyFormat(Server $server)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue