Skip to content

Commit c00e5b3

Browse files
committed
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.
1 parent 73b795f commit c00e5b3

File tree

7 files changed

+56
-57
lines changed

7 files changed

+56
-57
lines changed

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,4 @@ public function getSuspendedServersCount(): int;
139139
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
140140
*/
141141
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator;
142-
143-
/**
144-
* Returns every server that exists for a given node.
145-
*
146-
* This is different from {@see loadAllServersForNode} because
147-
* it does not paginate the response.
148-
*
149-
* @param int $node
150-
*
151-
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
152-
*/
153-
public function loadEveryServerForNode(int $node);
154142
}

app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
44

55
use Illuminate\Http\Request;
6+
use Pterodactyl\Models\Server;
67
use Illuminate\Http\JsonResponse;
78
use Pterodactyl\Http\Controllers\Controller;
89
use Pterodactyl\Repositories\Eloquent\NodeRepository;
910
use Pterodactyl\Services\Eggs\EggConfigurationService;
1011
use Pterodactyl\Repositories\Eloquent\ServerRepository;
12+
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
1113
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
1214

1315
class ServerDetailsController extends Controller
@@ -27,11 +29,6 @@ class ServerDetailsController extends Controller
2729
*/
2830
private $configurationStructureService;
2931

30-
/**
31-
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
32-
*/
33-
private $nodeRepository;
34-
3532
/**
3633
* ServerConfigurationController constructor.
3734
*
@@ -49,7 +46,6 @@ public function __construct(
4946
$this->eggConfigurationService = $eggConfigurationService;
5047
$this->repository = $repository;
5148
$this->configurationStructureService = $configurationStructureService;
52-
$this->nodeRepository = $nodeRepository;
5349
}
5450

5551
/**
@@ -66,7 +62,7 @@ public function __invoke(Request $request, $uuid)
6662
{
6763
$server = $this->repository->getByUuid($uuid);
6864

69-
return JsonResponse::create([
65+
return new JsonResponse([
7066
'settings' => $this->configurationStructureService->handle($server),
7167
'process_configuration' => $this->eggConfigurationService->handle($server),
7268
]);
@@ -76,25 +72,19 @@ public function __invoke(Request $request, $uuid)
7672
* Lists all servers with their configurations that are assigned to the requesting node.
7773
*
7874
* @param \Illuminate\Http\Request $request
79-
*
80-
* @return \Illuminate\Http\JsonResponse
81-
*
82-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
75+
* @return \Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection
8376
*/
8477
public function list(Request $request)
8578
{
79+
/** @var \Pterodactyl\Models\Node $node */
8680
$node = $request->attributes->get('node');
87-
$servers = $this->repository->loadEveryServerForNode($node->id);
88-
89-
$configurations = [];
9081

91-
foreach ($servers as $server) {
92-
$configurations[$server->uuid] = [
93-
'settings' => $this->configurationStructureService->handle($server),
94-
'process_configuration' => $this->eggConfigurationService->handle($server),
95-
];
96-
}
82+
// Avoid run-away N+1 SQL queries by pre-loading the relationships that are used
83+
// within each of the services called below.
84+
$servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables')
85+
->where('node_id', $node->id)
86+
->paginate($request->input('per_page', 50));
9787

98-
return JsonResponse::create($configurations);
88+
return new ServerConfigurationCollection($servers);
9989
}
10090
}

app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function handle(Request $request, Closure $next)
6969
// Ensure that all of the correct parts are provided in the header.
7070
if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) {
7171
throw new BadRequestHttpException(
72-
'The Authorization headed provided was not in a valid format.'
72+
'The Authorization header provided was not in a valid format.'
7373
);
7474
}
7575

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Resources\Wings;
4+
5+
use Pterodactyl\Models\Server;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Http\Resources\Json\ResourceCollection;
8+
use Pterodactyl\Services\Eggs\EggConfigurationService;
9+
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
10+
11+
class ServerConfigurationCollection extends ResourceCollection
12+
{
13+
/**
14+
* Converts a collection of Server models into an array of configuration responses
15+
* that can be understood by Wings. Make sure you've properly loaded the required
16+
* relationships on the Server models before calling this function, otherwise you'll
17+
* have some serious performance issues from all of the N+1 queries.
18+
*
19+
* @param \Illuminate\Http\Request $request
20+
* @return array
21+
*/
22+
public function toArray($request)
23+
{
24+
$egg = Container::getInstance()->make(EggConfigurationService::class);
25+
$configuration = Container::getInstance()->make(ServerConfigurationStructureService::class);
26+
27+
return $this->collection->map(function (Server $server) use ($configuration, $egg) {
28+
return [
29+
'uuid' => $server->uuid,
30+
'settings' => $configuration->handle($server),
31+
'process_configuration' => $egg->handle($server),
32+
];
33+
})->toArray();
34+
}
35+
}

app/Models/Server.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class Server extends Model
8383
'oom_disabled' => true,
8484
];
8585

86+
/**
87+
* The default relationships to load for all server models.
88+
*
89+
* @var string[]
90+
*/
91+
protected $with = ['allocation'];
92+
8693
/**
8794
* The attributes that should be mutated to dates.
8895
*

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,4 @@ public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginat
270270
->where('node_id', '=', $node)
271271
->paginate($limit);
272272
}
273-
274-
/**
275-
* Returns every server that exists for a given node.
276-
*
277-
* This is different from {@see loadAllServersForNode} because
278-
* it does not paginate the response.
279-
*
280-
* @param int $node
281-
*
282-
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
283-
*/
284-
public function loadEveryServerForNode(int $node)
285-
{
286-
return $this->getBuilder()
287-
->with('nest')
288-
->where('node_id', '=', $node)
289-
->get();
290-
}
291273
}

app/Services/Servers/ServerConfigurationStructureService.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
class ServerConfigurationStructureService
99
{
10-
const REQUIRED_RELATIONS = ['allocation', 'allocations', 'egg'];
11-
1210
/**
1311
* @var \Pterodactyl\Services\Servers\EnvironmentService
1412
*/
@@ -31,13 +29,11 @@ public function __construct(EnvironmentService $environment)
3129
* daemon, if you modify the structure eggs will break unexpectedly.
3230
*
3331
* @param \Pterodactyl\Models\Server $server
34-
* @param bool $legacy
32+
* @param bool $legacy deprecated
3533
* @return array
3634
*/
3735
public function handle(Server $server, bool $legacy = false): array
3836
{
39-
$server->loadMissing(self::REQUIRED_RELATIONS);
40-
4137
return $legacy ?
4238
$this->returnLegacyFormat($server)
4339
: $this->returnCurrentFormat($server);
@@ -93,6 +89,7 @@ protected function returnCurrentFormat(Server $server)
9389
*
9490
* @param \Pterodactyl\Models\Server $server
9591
* @return array
92+
* @deprecated
9693
*/
9794
protected function returnLegacyFormat(Server $server)
9895
{

0 commit comments

Comments
 (0)