Updated deploy torrent. Refactor.
parent
c29af77466
commit
6dd428412f
|
|
@ -89,126 +89,152 @@ class DeployImageAction extends AbstractController
|
|||
|
||||
private function handleUnicastDeployment(DeployImageInput $input, ImageImageRepository $image, ?Trace $trace = null): array
|
||||
{
|
||||
$clientJobs = [];
|
||||
|
||||
foreach ($input->clients as $client) {
|
||||
try {
|
||||
$inputData = $this->createInputData($input, $image, $client->getEntity());
|
||||
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::UNICAST, $trace);
|
||||
|
||||
if ($jobId) {
|
||||
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->warning('Error deploying to client (unicast)', [
|
||||
'client_uuid' => $client->getEntity()->getUuid(),
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
if ($input->queue) {
|
||||
$inputData = $this->createInputData($input, $image, $client->getEntity());
|
||||
$this->createService->__invoke($client->getEntity(), CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $clientJobs;
|
||||
return $this->processDeployments($input, $image, [], 'unicast', $trace);
|
||||
}
|
||||
|
||||
private function handleMulticastDeployment(DeployImageInput $input, ImageImageRepository $image, ?Trace $trace = null): array
|
||||
{
|
||||
$clientJobs = [];
|
||||
$repositoryErrors = $this->prepareRepositoryTransmissions($input, $image, 'multicast');
|
||||
|
||||
foreach ($input->clients as $client) {
|
||||
return $this->processDeployments($input, $image, $repositoryErrors, 'multicast', $trace);
|
||||
}
|
||||
|
||||
private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image, ?Trace $trace = null): array
|
||||
{
|
||||
$repositoryErrors = $this->prepareRepositoryTransmissions($input, $image, 'torrent');
|
||||
|
||||
return $this->processDeployments($input, $image, $repositoryErrors, 'torrent', $trace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reparte la orden de despliegue entre todos los equipos en dos fases: primero se lanzan todas
|
||||
* las peticiones a los ogAgent (que viajan en paralelo, porque el HttpClient de Symfony no
|
||||
* bloquea hasta que se lee la respuesta) y después se recogen los resultados.
|
||||
*
|
||||
* Antes se hacía equipo por equipo, así que el tiempo total era la suma de todos y cada equipo
|
||||
* apagado se comía su timeout completo antes de pasar al siguiente.
|
||||
*
|
||||
* @param array<string, string> $repositoryErrors error de transmisión por clave de repositorio
|
||||
*/
|
||||
private function processDeployments(
|
||||
DeployImageInput $input,
|
||||
ImageImageRepository $image,
|
||||
array $repositoryErrors,
|
||||
string $methodLabel,
|
||||
?Trace $trace = null
|
||||
): array {
|
||||
$clientJobs = [];
|
||||
$pending = [];
|
||||
|
||||
// Fase 1: lanzar la orden a todos los ogAgent sin esperar su respuesta.
|
||||
foreach ($input->clients as $clientOutput) {
|
||||
$client = $clientOutput->getEntity();
|
||||
$inputData = $this->createInputDataForMethod($input, $image, $client);
|
||||
|
||||
$repositoryError = $repositoryErrors[$this->getRepositoryKey($client)] ?? null;
|
||||
|
||||
if ($repositoryError !== null) {
|
||||
$this->logger->warning(sprintf('Error with OgRepository for client (%s)', $methodLabel), [
|
||||
'client_uuid' => $client->getUuid(),
|
||||
'error' => $repositoryError
|
||||
]);
|
||||
|
||||
$this->queueDeployment($client, $input, $inputData);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$inputData = $this->createMulticastInputData($input, $image, $client->getEntity());
|
||||
|
||||
$repositoryError = $repositoryErrors[$this->getRepositoryKey($client->getEntity())] ?? null;
|
||||
|
||||
if ($repositoryError !== null) {
|
||||
$this->logger->warning('Error with OgRepository for client (multicast)', [
|
||||
'client_uuid' => $client->getEntity()->getUuid(),
|
||||
'error' => $repositoryError
|
||||
]);
|
||||
|
||||
if ($input->queue) {
|
||||
$this->createService->__invoke($client->getEntity(), CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::MULTICAST, $trace);
|
||||
|
||||
if ($jobId) {
|
||||
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
|
||||
}
|
||||
$pending[] = [
|
||||
'client' => $client,
|
||||
'inputData' => $inputData,
|
||||
'response' => $this->deployImageOgAgentAction->startDeploy($image, $input, $client),
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->warning('Error deploying to client (multicast)', [
|
||||
'client_uuid' => $client->getEntity()->getUuid(),
|
||||
$this->logger->warning(sprintf('Error deploying to client (%s)', $methodLabel), [
|
||||
'client_uuid' => $client->getUuid(),
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
if ($input->queue) {
|
||||
$inputData = $this->createMulticastInputData($input, $image, $client->getEntity());
|
||||
$this->createService->__invoke($client->getEntity(), CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
|
||||
$this->queueDeployment($client, $input, $inputData);
|
||||
}
|
||||
}
|
||||
|
||||
// Fase 2: recoger las respuestas, que han ido avanzando en paralelo.
|
||||
foreach ($pending as ['client' => $client, 'inputData' => $inputData, 'response' => $response]) {
|
||||
try {
|
||||
$jobId = $this->deployImageOgAgentAction->completeDeploy($response, $client);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->warning(sprintf('Error deploying to client (%s)', $methodLabel), [
|
||||
'client_uuid' => $client->getUuid(),
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
$this->queueDeployment($client, $input, $inputData);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->registerDeployment($client, $input, $inputData, $jobId, $trace);
|
||||
|
||||
if ($jobId) {
|
||||
$clientJobs[(string) '/clients/' . $client->getUuid()] = $jobId;
|
||||
}
|
||||
}
|
||||
|
||||
return $clientJobs;
|
||||
}
|
||||
|
||||
private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image, ?Trace $trace = null): array
|
||||
/** Deja el despliegue en cola para que lo reintente ExecutePendingTracesCommand. */
|
||||
private function queueDeployment($client, DeployImageInput $input, array $inputData): void
|
||||
{
|
||||
$clientJobs = [];
|
||||
$repositoryErrors = $this->prepareRepositoryTransmissions($input, $image, 'torrent');
|
||||
|
||||
foreach ($input->clients as $client) {
|
||||
try {
|
||||
$inputData = $this->createTorrentInputData($input, $image, $client->getEntity());
|
||||
|
||||
$repositoryError = $repositoryErrors[$this->getRepositoryKey($client->getEntity())] ?? null;
|
||||
|
||||
if ($repositoryError !== null) {
|
||||
$this->logger->warning('Error with OgRepository for client (torrent)', [
|
||||
'client_uuid' => $client->getEntity()->getUuid(),
|
||||
'error' => $repositoryError
|
||||
]);
|
||||
|
||||
if ($input->queue) {
|
||||
$this->createService->__invoke($client->getEntity(), CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::TORRENT, $trace);
|
||||
|
||||
if ($jobId) {
|
||||
$clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->warning('Error deploying to client (torrent)', [
|
||||
'client_uuid' => $client->getEntity()->getUuid(),
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
if ($input->queue) {
|
||||
$inputData = $this->createTorrentInputData($input, $image, $client->getEntity());
|
||||
$this->createService->__invoke($client->getEntity(), CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if (!$input->queue) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $clientJobs;
|
||||
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
/** Anota la traza del despliegue: en curso si el ogAgent aceptó la orden, en cola si no. */
|
||||
private function registerDeployment($client, DeployImageInput $input, array $inputData, ?string $jobId, ?Trace $trace = null): void
|
||||
{
|
||||
if (!$jobId) {
|
||||
if (!$input->queue) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($trace) {
|
||||
$trace->setStatus(TraceStatus::PENDING);
|
||||
$trace->setJobId(null);
|
||||
$trace->setInput($inputData);
|
||||
$this->entityManager->persist($trace);
|
||||
$this->entityManager->flush();
|
||||
} else {
|
||||
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($trace) {
|
||||
$trace->setStatus(TraceStatus::IN_PROGRESS);
|
||||
$trace->setJobId($jobId);
|
||||
$trace->setInput($inputData);
|
||||
$this->entityManager->persist($trace);
|
||||
$this->entityManager->flush();
|
||||
} else {
|
||||
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $jobId, $inputData);
|
||||
}
|
||||
}
|
||||
|
||||
private function createInputDataForMethod(DeployImageInput $input, ImageImageRepository $image, $client): array
|
||||
{
|
||||
return match ($input->method) {
|
||||
DeployMethodTypes::MULTICAST, DeployMethodTypes::MULTICAST_UFTP, DeployMethodTypes::MULTICAST_UFTP_DIRECT,
|
||||
DeployMethodTypes::MULTICAST_UDPCAST, DeployMethodTypes::MULTICAST_UDPCAST_DIRECT => $this->createMulticastInputData($input, $image, $client),
|
||||
DeployMethodTypes::TORRENT => $this->createTorrentInputData($input, $image, $client),
|
||||
default => $this->createInputData($input, $image, $client),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -258,38 +284,6 @@ class DeployImageAction extends AbstractController
|
|||
return $repository ? (string) $repository->getUuid() : self::NO_REPOSITORY;
|
||||
}
|
||||
|
||||
private function processDeployment($client, DeployImageInput $input, ImageImageRepository $image, array $inputData, string $deployType, ?Trace $trace = null): ?string
|
||||
{
|
||||
$agentJobId = $this->deployImageOgAgentAction->__invoke($image, $input, $client, $deployType);
|
||||
|
||||
if (!$agentJobId) {
|
||||
if ($input->queue) {
|
||||
if ($trace) {
|
||||
$trace->setStatus(TraceStatus::PENDING);
|
||||
$trace->setJobId(null);
|
||||
$trace->setInput($inputData);
|
||||
$this->entityManager->persist($trace);
|
||||
$this->entityManager->flush();
|
||||
} else {
|
||||
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($trace) {
|
||||
$trace->setStatus(TraceStatus::IN_PROGRESS);
|
||||
$trace->setJobId($agentJobId);
|
||||
$trace->setInput($inputData);
|
||||
$this->entityManager->persist($trace);
|
||||
$this->entityManager->flush();
|
||||
} else {
|
||||
$this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $agentJobId, $inputData);
|
||||
}
|
||||
|
||||
return $agentJobId;
|
||||
}
|
||||
|
||||
private function createInputData(DeployImageInput $input, ImageImageRepository $image, $client): array
|
||||
{
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
#[AsController]
|
||||
abstract class AbstractOgAgentController extends AbstractController
|
||||
|
|
@ -71,6 +72,26 @@ abstract class AbstractOgAgentController extends AbstractController
|
|||
|
||||
try {
|
||||
$response = $this->httpClient->request($method, $url, $params);
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
|
||||
|
||||
return [
|
||||
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
|
||||
'error' => 'Transport error',
|
||||
'details' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->readResponse($response, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpreta la respuesta del ogAgent. Está separado de createRequest() para poder lanzar
|
||||
* varias peticiones a la vez y leerlas después, en vez de esperar a cada una por turno.
|
||||
*/
|
||||
public function readResponse(ResponseInterface $response, string $url): array
|
||||
{
|
||||
try {
|
||||
return json_decode($response->getContent(), true);
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
|
||||
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 409) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
class DeployImageAction extends AbstractOgAgentController
|
||||
{
|
||||
|
|
@ -25,6 +26,22 @@ class DeployImageAction extends AbstractOgAgentController
|
|||
* @throws ServerExceptionInterface
|
||||
*/
|
||||
public function __invoke(ImageImageRepository $imageImageRepository, DeployImageInput $input, Client $client, ?string $method = null)
|
||||
{
|
||||
return $this->completeDeploy(
|
||||
$this->startDeploy($imageImageRepository, $input, $client),
|
||||
$client
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Envía la orden al ogAgent sin esperar la respuesta. El HttpClient de Symfony gestiona las
|
||||
* peticiones de forma concurrente mientras no se lea su contenido, así que lanzando primero
|
||||
* todas las de un despliegue el coste total pasa de ser la suma de todos los equipos a ser
|
||||
* el del equipo más lento.
|
||||
*
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function startDeploy(ImageImageRepository $imageImageRepository, DeployImageInput $input, Client $client): ResponseInterface
|
||||
{
|
||||
$image = $imageImageRepository->getImage();
|
||||
|
||||
|
|
@ -77,26 +94,32 @@ class DeployImageAction extends AbstractOgAgentController
|
|||
|
||||
$url = 'https://'.$client->getIp().':8000/opengnsys/RestaurarImagen';
|
||||
|
||||
$response = $this->createRequest(
|
||||
method: 'POST',
|
||||
url: $url,
|
||||
params: [
|
||||
'json' => $data,
|
||||
],
|
||||
token: $client->getToken(),
|
||||
);
|
||||
|
||||
$this->logger->info('Deploying image', [
|
||||
'image' => $imageImageRepository->getName(),
|
||||
$this->logger->info('Deploying image', [
|
||||
'image' => $imageImageRepository->getName(),
|
||||
'repository' => $repository->getIp(),
|
||||
'client' => $client->getIp()
|
||||
]);
|
||||
|
||||
if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
|
||||
return $this->httpClient->request(
|
||||
method: 'POST',
|
||||
url: $url,
|
||||
options: $this->buildRequestOptions(['json' => $data], $client->getToken()),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lee la respuesta de un ogAgent y devuelve su job_id, o null si el equipo no ha aceptado la
|
||||
* orden (por ejemplo un 409 porque ya está ocupado).
|
||||
*/
|
||||
public function completeDeploy(ResponseInterface $response, Client $client): ?string
|
||||
{
|
||||
$content = $this->readResponse($response, (string) $response->getInfo('url'));
|
||||
|
||||
if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
|
||||
throw new BadRequestHttpException('Error deploying image');
|
||||
}
|
||||
|
||||
$jobId = $response['job_id'];
|
||||
$jobId = $content['job_id'] ?? null;
|
||||
|
||||
$client->setStatus(ClientStatus::BUSY);
|
||||
$this->entityManager->persist($client);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,317 @@
|
|||
<?php
|
||||
|
||||
namespace Functional;
|
||||
|
||||
use App\Entity\Client;
|
||||
use App\Entity\Image;
|
||||
use App\Entity\ImageImageRepository;
|
||||
use App\Entity\ImageRepository;
|
||||
use App\Entity\OrganizationalUnit;
|
||||
use App\Factory\UserFactory;
|
||||
use App\Model\DeployMethodTypes;
|
||||
use App\Model\ImageStatus;
|
||||
use App\Model\OrganizationalUnitP2PModes;
|
||||
use App\Model\OrganizationalUnitTypes;
|
||||
use App\Model\UserGroupPermissions;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
|
||||
/**
|
||||
* La creación de la transmisión en el ogRepository es una operación por repositorio, no por cliente.
|
||||
* Antes se lanzaba dentro del bucle de clientes, así que un despliegue de N equipos hacía N veces la
|
||||
* misma petición (y esa petición tarda ~10s en real), de ahí que el tiempo creciera con el nº de PCs.
|
||||
*
|
||||
* @see \App\Controller\DeployImageAction::prepareRepositoryTransmissions()
|
||||
*/
|
||||
class DeployImageTorrentTest extends AbstractTest
|
||||
{
|
||||
private const ADMIN_USER = 'deploy-super-admin';
|
||||
|
||||
/** @var string[] URLs invocadas sobre el httpClient, en orden */
|
||||
private array $requestedUrls = [];
|
||||
|
||||
/** Contadores para no repetir IPs, que son únicas en BD. */
|
||||
private int $clientIpCounter = 0;
|
||||
|
||||
private int $repositoryIpCounter = 0;
|
||||
|
||||
private ?string $adminToken = null;
|
||||
|
||||
/** @var string[] Orden de "start:" (peticion lanzada) y "read:" (respuesta leida) */
|
||||
private array $callOrder = [];
|
||||
|
||||
/** Permite que un test elija el mock que registra el orden de las llamadas. */
|
||||
private bool $trackCallOrder = false;
|
||||
|
||||
private function getEntityManager(): EntityManagerInterface
|
||||
{
|
||||
return static::getContainer()->get('doctrine')->getManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sustituye el cliente HTTP por un mock que registra cada URL invocada, para poder contar
|
||||
* cuántas peticiones se hacen al ogRepository y cuántas a los ogAgent.
|
||||
*/
|
||||
private function mockHttpClient(): void
|
||||
{
|
||||
$this->requestedUrls = [];
|
||||
|
||||
$mock = new MockHttpClient(function (string $method, string $url): MockResponse {
|
||||
$this->requestedUrls[] = $url;
|
||||
|
||||
if (str_contains($url, '/ogrepository/v1/')) {
|
||||
return new MockResponse(
|
||||
json_encode(['success' => true, 'details' => 'Tracker and Seeder serving image correctly']),
|
||||
['response_headers' => ['content-type' => 'application/json']]
|
||||
);
|
||||
}
|
||||
|
||||
return new MockResponse(
|
||||
json_encode(['job_id' => 'RestaurarImagen-'.substr(md5($url.count($this->requestedUrls)), 0, 8)]),
|
||||
['response_headers' => ['content-type' => 'application/json']]
|
||||
);
|
||||
});
|
||||
|
||||
static::getContainer()->set('http_client', $mock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Igual que mockHttpClient(), pero además anota el orden en que se crean las peticiones y en
|
||||
* que se leen sus respuestas. El cuerpo se entrega con un generador, que MockHttpClient sólo
|
||||
* consume cuando alguien lee la respuesta: así se distingue "lanzada" de "leída".
|
||||
*/
|
||||
private function mockHttpClientTrackingOrder(): void
|
||||
{
|
||||
$this->requestedUrls = [];
|
||||
$this->callOrder = [];
|
||||
|
||||
$mock = new MockHttpClient(function (string $method, string $url): MockResponse {
|
||||
$this->requestedUrls[] = $url;
|
||||
|
||||
$isAgent = str_contains($url, '/opengnsys/RestaurarImagen');
|
||||
|
||||
if ($isAgent) {
|
||||
$this->callOrder[] = 'start:'.$url;
|
||||
}
|
||||
|
||||
$body = function () use ($url, $isAgent): \Generator {
|
||||
if ($isAgent) {
|
||||
$this->callOrder[] = 'read:'.$url;
|
||||
|
||||
yield json_encode(['job_id' => 'job-'.substr(md5($url), 0, 8)]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
yield json_encode(['success' => true, 'details' => 'Tracker and Seeder serving image correctly']);
|
||||
};
|
||||
|
||||
return new MockResponse($body(), ['response_headers' => ['content-type' => 'application/json']]);
|
||||
});
|
||||
|
||||
static::getContainer()->set('http_client', $mock);
|
||||
}
|
||||
|
||||
/** @return array{image: ImageImageRepository, clients: Client[]} */
|
||||
private function createDeployScenario(int $numberOfClients, ?ImageRepository $repository = null): array
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
|
||||
$organizationalUnit = new OrganizationalUnit();
|
||||
$organizationalUnit->setName('deploy-ou-'.uniqid());
|
||||
$organizationalUnit->setType(OrganizationalUnitTypes::CLASSROOM);
|
||||
$entityManager->persist($organizationalUnit);
|
||||
|
||||
if ($repository === null) {
|
||||
$repository = new ImageRepository();
|
||||
$repository->setName('repo-'.uniqid());
|
||||
$repository->setIp('10.200.0.'.(++$this->repositoryIpCounter));
|
||||
$entityManager->persist($repository);
|
||||
}
|
||||
|
||||
$image = new Image();
|
||||
$image->setName('image-'.uniqid());
|
||||
$image->setType('monolithic');
|
||||
$image->setIsGlobal(false);
|
||||
$entityManager->persist($image);
|
||||
|
||||
$imageImageRepository = new ImageImageRepository();
|
||||
$imageImageRepository->setImage($image);
|
||||
$imageImageRepository->setRepository($repository);
|
||||
$imageImageRepository->setName($image->getName());
|
||||
$imageImageRepository->setStatus(ImageStatus::SUCCESS);
|
||||
$imageImageRepository->setImageFullsum(md5($image->getName()));
|
||||
$entityManager->persist($imageImageRepository);
|
||||
|
||||
$clients = [];
|
||||
for ($i = 0; $i < $numberOfClients; $i++) {
|
||||
$client = new Client();
|
||||
$client->setName('deploy-client-'.$i.'-'.uniqid());
|
||||
$client->setMaintenance(false);
|
||||
$client->setOrganizationalUnit($organizationalUnit);
|
||||
$client->setIp('10.'.intdiv($this->clientIpCounter, 254).'.1.'.(($this->clientIpCounter++ % 254) + 1));
|
||||
$client->setToken('token-'.$i);
|
||||
$client->setRepository($repository);
|
||||
$entityManager->persist($client);
|
||||
|
||||
$clients[] = $client;
|
||||
}
|
||||
|
||||
$entityManager->flush();
|
||||
|
||||
return ['image' => $imageImageRepository, 'clients' => $clients];
|
||||
}
|
||||
|
||||
/** Lanza el despliegue torrent y devuelve el código de respuesta. */
|
||||
private function deployTorrent(ImageImageRepository $image, array $clients): int
|
||||
{
|
||||
if ($this->adminToken === null) {
|
||||
UserFactory::createOne(['username' => self::ADMIN_USER, 'roles' => [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
|
||||
$this->adminToken = $this->getToken([
|
||||
'username' => self::ADMIN_USER,
|
||||
'password' => UserFactory::PLAIN_PASSWORD,
|
||||
]);
|
||||
}
|
||||
|
||||
$token = $this->adminToken;
|
||||
|
||||
$clientIris = array_map(
|
||||
static fn (Client $client): string => '/clients/'.$client->getUuid(),
|
||||
$clients
|
||||
);
|
||||
|
||||
$imageUuid = (string) $image->getUuid();
|
||||
|
||||
// El mock debe instalarse sobre el kernel que atenderá la petición: createClient() lo reinicia.
|
||||
$apiClient = $this->createClientWithCredentials($token);
|
||||
|
||||
if ($this->trackCallOrder) {
|
||||
$this->mockHttpClientTrackingOrder();
|
||||
} else {
|
||||
$this->mockHttpClient();
|
||||
}
|
||||
|
||||
$apiClient->request('POST', '/image-image-repositories/'.$imageUuid.'/deploy-image', [
|
||||
'json' => [
|
||||
'type' => 'monolithic',
|
||||
'method' => DeployMethodTypes::TORRENT,
|
||||
'clients' => $clientIris,
|
||||
'diskNumber' => 1,
|
||||
'partitionNumber' => 1,
|
||||
'p2pMode' => OrganizationalUnitP2PModes::P2P_MODE_PEER,
|
||||
'p2pTime' => 0,
|
||||
],
|
||||
]);
|
||||
|
||||
return $apiClient->getResponse()->getStatusCode();
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
private function repositoryRequests(): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->requestedUrls,
|
||||
static fn (string $url): bool => str_contains($url, '/ogrepository/v1/')
|
||||
));
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
private function agentRequests(): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->requestedUrls,
|
||||
static fn (string $url): bool => str_contains($url, '/opengnsys/RestaurarImagen')
|
||||
));
|
||||
}
|
||||
|
||||
/** Con 10 equipos solo debe crearse la transmisión una vez, y avisarse a los 10 ogAgent. */
|
||||
public function testTorrentDeploymentCallsRepositoryOncePerDeployment(): void
|
||||
{
|
||||
$scenario = $this->createDeployScenario(10);
|
||||
|
||||
$statusCode = $this->deployTorrent($scenario['image'], $scenario['clients']);
|
||||
$this->assertSame(200, $statusCode);
|
||||
|
||||
$this->assertCount(
|
||||
1,
|
||||
$this->repositoryRequests(),
|
||||
'La transmisión p2p debe crearse una sola vez, no una por equipo'
|
||||
);
|
||||
$this->assertStringContainsString('/ogrepository/v1/p2p', $this->repositoryRequests()[0]);
|
||||
$this->assertCount(10, $this->agentRequests(), 'Cada equipo debe recibir su orden de despliegue');
|
||||
}
|
||||
|
||||
/** El número de llamadas al ogRepository no debe crecer con el número de equipos. */
|
||||
public function testRepositoryCallsDoNotGrowWithClientCount(): void
|
||||
{
|
||||
$scenario = $this->createDeployScenario(3);
|
||||
$this->deployTorrent($scenario['image'], $scenario['clients']);
|
||||
$repositoryCallsWithThreeClients = count($this->repositoryRequests());
|
||||
|
||||
$scenario = $this->createDeployScenario(30);
|
||||
$this->deployTorrent($scenario['image'], $scenario['clients']);
|
||||
$repositoryCallsWithThirtyClients = count($this->repositoryRequests());
|
||||
|
||||
$this->assertSame(1, $repositoryCallsWithThreeClients);
|
||||
$this->assertSame(
|
||||
$repositoryCallsWithThreeClients,
|
||||
$repositoryCallsWithThirtyClients,
|
||||
'El coste en ogRepository debe ser constante, no proporcional al nº de equipos'
|
||||
);
|
||||
}
|
||||
|
||||
/** Si los equipos resuelven a repositorios distintos, se crea una transmisión en cada uno. */
|
||||
public function testOneRepositoryCallPerDistinctRepository(): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
|
||||
$firstScenario = $this->createDeployScenario(4);
|
||||
|
||||
$secondRepository = new ImageRepository();
|
||||
$secondRepository->setName('repo-second-'.uniqid());
|
||||
$secondRepository->setIp('10.0.0.2');
|
||||
$entityManager->persist($secondRepository);
|
||||
$entityManager->flush();
|
||||
|
||||
// Cuatro equipos más, apuntando al segundo repositorio, sobre la misma imagen.
|
||||
$secondScenario = $this->createDeployScenario(4, $secondRepository);
|
||||
|
||||
$clients = array_merge($firstScenario['clients'], $secondScenario['clients']);
|
||||
|
||||
$this->deployTorrent($firstScenario['image'], $clients);
|
||||
|
||||
$this->assertCount(2, $this->repositoryRequests(), 'Una transmisión por repositorio distinto');
|
||||
$this->assertCount(8, $this->agentRequests());
|
||||
}
|
||||
|
||||
/**
|
||||
* Las N peticiones a los ogAgent deben lanzarse todas antes de leer la primera respuesta: es lo
|
||||
* que permite que viajen en paralelo. Si se procesaran de una en una, el orden sería
|
||||
* start/read/start/read... y el tiempo total la suma de todos los equipos.
|
||||
*/
|
||||
public function testAgentRequestsAreAllStartedBeforeReadingAnyResponse(): void
|
||||
{
|
||||
$this->trackCallOrder = true;
|
||||
$scenario = $this->createDeployScenario(12);
|
||||
|
||||
$this->assertSame(200, $this->deployTorrent($scenario['image'], $scenario['clients']));
|
||||
|
||||
$starts = array_keys(array_filter(
|
||||
$this->callOrder,
|
||||
static fn (string $entry): bool => str_starts_with($entry, 'start:')
|
||||
));
|
||||
$reads = array_keys(array_filter(
|
||||
$this->callOrder,
|
||||
static fn (string $entry): bool => str_starts_with($entry, 'read:')
|
||||
));
|
||||
|
||||
$this->assertCount(12, $starts);
|
||||
$this->assertCount(12, $reads);
|
||||
$this->assertLessThan(
|
||||
min($reads),
|
||||
max($starts),
|
||||
'Todas las peticiones deben estar lanzadas antes de leer la primera respuesta'
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue