|
|
|
|
@ -0,0 +1,735 @@
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Models\CloudJob;
|
|
|
|
|
use App\Models\CloudVpc;
|
|
|
|
|
use App\Models\CloudVpcHostState;
|
|
|
|
|
use App\Models\CloudVpcMembershipBatch;
|
|
|
|
|
use App\Models\CloudVpcMembershipBatchItem;
|
|
|
|
|
use App\Models\CloudVpcPort;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* One durable server-side VPC membership batch.
|
|
|
|
|
*
|
|
|
|
|
* The browser submits only intent. This module owns the complete barrier:
|
|
|
|
|
* attach = one route generation -> all port jobs; detach = all port jobs ->
|
|
|
|
|
* one route generation. There is no selection-count limit in this domain.
|
|
|
|
|
*/
|
|
|
|
|
final class CloudVpcMembershipBatchService
|
|
|
|
|
{
|
|
|
|
|
private const PARENT_KIND = 'vpc_membership_batch';
|
|
|
|
|
private const TERMINAL_STATES = ['succeeded', 'failed'];
|
|
|
|
|
|
|
|
|
|
public static function start(array $actor, string $vpcKey,
|
|
|
|
|
array $input): array
|
|
|
|
|
{
|
|
|
|
|
CloudInput::only($input, [
|
|
|
|
|
'action', 'instance_ids', 'confirm_restart',
|
|
|
|
|
]);
|
|
|
|
|
if (preg_match('/^[a-f0-9]{32}$/D', $vpcKey) !== 1) {
|
|
|
|
|
throw new CloudError('VPC 标识无效', 404);
|
|
|
|
|
}
|
|
|
|
|
$action = $input['action'] ?? null;
|
|
|
|
|
if (!in_array($action, ['attach', 'detach'], true)) {
|
|
|
|
|
throw new CloudError('VPC 批量操作类型无效');
|
|
|
|
|
}
|
|
|
|
|
if (($input['confirm_restart'] ?? null) !== true) {
|
|
|
|
|
throw new CloudError('请确认所选实例会自动关机并冷启动', 409);
|
|
|
|
|
}
|
|
|
|
|
$instanceIds = self::instanceIds($input['instance_ids'] ?? null);
|
|
|
|
|
$userId = (int) ($actor['user_id'] ?? 0);
|
|
|
|
|
$requestKey = $actor['request_idempotency_key'] ?? null;
|
|
|
|
|
if ($userId < 1 || !is_string($requestKey)
|
|
|
|
|
|| preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $requestKey) !== 1) {
|
|
|
|
|
throw new CloudError('请求幂等标识无效', 400);
|
|
|
|
|
}
|
|
|
|
|
$fingerprint = hash('sha256', json_encode([
|
|
|
|
|
'kind' => self::PARENT_KIND,
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
'vpc_key' => $vpcKey,
|
|
|
|
|
'action' => $action,
|
|
|
|
|
'instance_ids' => $instanceIds,
|
|
|
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
|
|
|
| JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$batchKey = substr(hash('sha256', "vpc-membership-batch\0"
|
|
|
|
|
. $userId . "\0" . $vpcKey . "\0" . $requestKey), 0, 32);
|
|
|
|
|
|
|
|
|
|
return (new CloudVpcMembershipBatch())->getConnection()->transaction(
|
|
|
|
|
static function () use (
|
|
|
|
|
$actor, $userId, $requestKey, $fingerprint, $batchKey,
|
|
|
|
|
$vpcKey, $action, $instanceIds
|
|
|
|
|
): array {
|
|
|
|
|
$vpc = CloudVpc::query()->where('vpc_key', $vpcKey)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($vpc === null || (int) $vpc->user_id !== $userId
|
|
|
|
|
|| $vpc->active_claim === null) {
|
|
|
|
|
throw new CloudError('VPC 不存在或当前不可用', 404);
|
|
|
|
|
}
|
|
|
|
|
$prior = CloudJob::query()->where('user_id', $userId)
|
|
|
|
|
->where('request_idempotency_key', $requestKey)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($prior !== null) {
|
|
|
|
|
if ((string) $prior->kind !== self::PARENT_KIND
|
|
|
|
|
|| !hash_equals((string) $prior->request_fingerprint,
|
|
|
|
|
$fingerprint)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'同一请求标识不能用于不同的批量操作', 409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$batch = CloudVpcMembershipBatch::query()
|
|
|
|
|
->where('parent_job_id', (int) $prior->id)->first();
|
|
|
|
|
if ($batch === null || (int) $batch->vpc_id !== (int) $vpc->id) {
|
|
|
|
|
throw new CloudError('VPC 批次幂等账本不完整', 409);
|
|
|
|
|
}
|
|
|
|
|
return ['job' => $prior, 'batch' => self::dto($batch),
|
|
|
|
|
'unchanged' => true];
|
|
|
|
|
}
|
|
|
|
|
if (CloudVpcMembershipBatch::query()
|
|
|
|
|
->where('vpc_id', (int) $vpc->id)
|
|
|
|
|
->where('active_claim', 1)->lockForUpdate()->exists()) {
|
|
|
|
|
throw new CloudError('该 VPC 已有批量操作正在执行', 409);
|
|
|
|
|
}
|
|
|
|
|
CloudVpcPortService::assertBatchVpcSettledLocked($vpc);
|
|
|
|
|
|
|
|
|
|
$instances = CloudInstance::query()->whereIn('id', $instanceIds)
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get()->keyBy('id');
|
|
|
|
|
if ($instances->count() !== count($instanceIds)) {
|
|
|
|
|
throw new CloudError('部分实例不存在或状态已变化', 409);
|
|
|
|
|
}
|
|
|
|
|
foreach ($instanceIds as $instanceId) {
|
|
|
|
|
$instance = $instances->get($instanceId);
|
|
|
|
|
CloudVpcPortService::assertBatchInstanceLocked(
|
|
|
|
|
$instance, $actor
|
|
|
|
|
);
|
|
|
|
|
if (CloudVpcPortService::batchInstanceRegionId($instance)
|
|
|
|
|
!== (int) $vpc->region_id) {
|
|
|
|
|
throw new CloudError('所选实例必须与 VPC 位于同一地区', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parent = self::newParentJob(
|
|
|
|
|
$userId, $requestKey, $fingerprint, $vpc, $action,
|
|
|
|
|
$instanceIds
|
|
|
|
|
);
|
|
|
|
|
$now = time();
|
|
|
|
|
$batch = new CloudVpcMembershipBatch();
|
|
|
|
|
$batch->batch_key = $batchKey;
|
|
|
|
|
$batch->vpc_id = (int) $vpc->id;
|
|
|
|
|
$batch->user_id = $userId;
|
|
|
|
|
$batch->parent_job_id = (int) $parent->id;
|
|
|
|
|
$batch->kind = $action;
|
|
|
|
|
$batch->state = $action === 'attach' ? 'routing' : 'ports';
|
|
|
|
|
$batch->desired_revision = (int) $vpc->desired_revision;
|
|
|
|
|
$batch->active_claim = 1;
|
|
|
|
|
$batch->last_error_code = '';
|
|
|
|
|
$batch->terminal_at = null;
|
|
|
|
|
$batch->created_at = $batch->updated_at = $now;
|
|
|
|
|
$batch->save();
|
|
|
|
|
|
|
|
|
|
if ($action === 'attach') {
|
|
|
|
|
self::startAttachLocked(
|
|
|
|
|
$batch, $vpc, $instances, $instanceIds
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
self::startDetachLocked(
|
|
|
|
|
$batch, $vpc, $instances, $instanceIds
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return ['job' => $parent, 'batch' => self::dto($batch),
|
|
|
|
|
'unchanged' => false];
|
|
|
|
|
},
|
|
|
|
|
3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return list<array<string,mixed>> */
|
|
|
|
|
public static function activeForOwner(int $userId): array
|
|
|
|
|
{
|
|
|
|
|
if ($userId < 1) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$batches = CloudVpcMembershipBatch::query()
|
|
|
|
|
->where('user_id', $userId)->where('active_claim', 1)
|
|
|
|
|
->orderBy('id')->get();
|
|
|
|
|
return $batches->map(static fn ($batch): array => self::dto($batch))
|
|
|
|
|
->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Called inside the worker's child-finalization transaction. */
|
|
|
|
|
public static function finishChildLocked(CloudJob $job, string $status,
|
|
|
|
|
$result, array $operation, CloudVpc $vpc, CloudVpcPort $port,
|
|
|
|
|
CloudInstance $instance): void
|
|
|
|
|
{
|
|
|
|
|
$batch = CloudVpcMembershipBatch::query()
|
|
|
|
|
->where('id', (int) $job->vpc_batch_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($batch === null || (int) $batch->vpc_id !== (int) $vpc->id
|
|
|
|
|
|| (int) $batch->user_id !== (int) $job->user_id) {
|
|
|
|
|
throw new CloudError('VPC 批次与子任务账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if (in_array((string) $batch->state, self::TERMINAL_STATES, true)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$parent = CloudJob::query()->where('id', (int) $batch->parent_job_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($parent === null || (string) $parent->kind !== self::PARENT_KIND
|
|
|
|
|
|| (string) $parent->phase === 'terminal') {
|
|
|
|
|
throw new CloudError('VPC 批次父任务账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_array((string) $job->kind,
|
|
|
|
|
['vpc_host_apply', 'vpc_host_remove'], true)) {
|
|
|
|
|
$code = CloudVpcPortService::settleBatchHostJobLocked(
|
|
|
|
|
$job, $status, $result, $operation, $vpc
|
|
|
|
|
);
|
|
|
|
|
self::advanceHostStageLocked(
|
|
|
|
|
$batch, $parent, $job, $status, $code, $vpc
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self::settlePortChildLocked(
|
|
|
|
|
$batch, $parent, $job, $status, $result, $operation,
|
|
|
|
|
$vpc, $port, $instance
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Preserve the batch fence when a child is terminalized as unknown. */
|
|
|
|
|
public static function markChildUncertainLocked(CloudJob $job): void
|
|
|
|
|
{
|
|
|
|
|
if ($job->vpc_batch_id === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$batch = CloudVpcMembershipBatch::query()
|
|
|
|
|
->where('id', (int) $job->vpc_batch_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($batch === null
|
|
|
|
|
|| in_array((string) $batch->state, self::TERMINAL_STATES, true)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (in_array((string) $job->kind,
|
|
|
|
|
['vpc_port_attach', 'vpc_port_detach'], true)) {
|
|
|
|
|
CloudVpcMembershipBatchItem::query()
|
|
|
|
|
->where('batch_id', (int) $batch->id)
|
|
|
|
|
->where('child_job_id', (int) $job->id)->update([
|
|
|
|
|
'state' => 'unknown',
|
|
|
|
|
'last_error_code' => 'vpc_result_uncertain',
|
|
|
|
|
'updated_at' => time(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$batch->state = 'blocked';
|
|
|
|
|
$batch->last_error_code = 'vpc_result_uncertain';
|
|
|
|
|
$batch->updated_at = time();
|
|
|
|
|
$batch->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function startAttachLocked(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudVpc $vpc, $instances, array $instanceIds): void
|
|
|
|
|
{
|
|
|
|
|
$revision = (int) $vpc->desired_revision + 1;
|
|
|
|
|
$focus = null;
|
|
|
|
|
foreach ($instanceIds as $instanceId) {
|
|
|
|
|
$instance = $instances->get($instanceId);
|
|
|
|
|
$port = CloudVpcPortService::reserveBatchPortLocked(
|
|
|
|
|
$instance, $vpc, (string) $batch->batch_key, $revision
|
|
|
|
|
);
|
|
|
|
|
self::newItem($batch, $instance, $port, 'waiting_route');
|
|
|
|
|
$focus ??= $port;
|
|
|
|
|
}
|
|
|
|
|
if ($focus === null) {
|
|
|
|
|
throw new CloudError('VPC 批次没有可执行实例', 409);
|
|
|
|
|
}
|
|
|
|
|
$vpc->desired_revision = $revision;
|
|
|
|
|
$vpc->state = 'provisioning';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
$batch->desired_revision = $revision;
|
|
|
|
|
$batch->updated_at = time();
|
|
|
|
|
$batch->save();
|
|
|
|
|
if (CloudVpcPortService::queueBatchProjectionLocked(
|
|
|
|
|
$vpc, $focus, $revision, (int) $batch->id
|
|
|
|
|
) === []) {
|
|
|
|
|
throw new CloudError('VPC 路由投影没有可执行宿主机', 503);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function startDetachLocked(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudVpc $vpc, $instances, array $instanceIds): void
|
|
|
|
|
{
|
|
|
|
|
$ports = CloudVpcPort::query()->whereIn('instance_id', $instanceIds)
|
|
|
|
|
->where('active_claim', 1)->orderBy('instance_id')
|
|
|
|
|
->lockForUpdate()->get()->keyBy('instance_id');
|
|
|
|
|
if ($ports->count() !== count($instanceIds)) {
|
|
|
|
|
throw new CloudError('部分实例未连接该 VPC', 409);
|
|
|
|
|
}
|
|
|
|
|
foreach ($instanceIds as $instanceId) {
|
|
|
|
|
$instance = $instances->get($instanceId);
|
|
|
|
|
$port = $ports->get($instanceId);
|
|
|
|
|
if ((int) $port->vpc_id !== (int) $vpc->id
|
|
|
|
|
|| (int) $port->user_id !== (int) $vpc->user_id
|
|
|
|
|
|| (string) $port->state !== 'attached') {
|
|
|
|
|
throw new CloudError('所选实例的 VPC 连接状态已变化', 409);
|
|
|
|
|
}
|
|
|
|
|
$state = CloudVpcHostState::query()
|
|
|
|
|
->where('vpc_id', (int) $vpc->id)
|
|
|
|
|
->where('host_id', (int) $instance->host_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if (!CloudVpcPortService::batchHostReady($state)) {
|
|
|
|
|
throw new CloudError('实例所在宿主机的 VPC 路由尚未收敛', 409);
|
|
|
|
|
}
|
|
|
|
|
$port->state = 'detaching';
|
|
|
|
|
$port->last_error_code = '';
|
|
|
|
|
$port->updated_at = time();
|
|
|
|
|
$port->save();
|
|
|
|
|
$item = self::newItem($batch, $instance, $port, 'queued');
|
|
|
|
|
$job = CloudVpcPortService::queueBatchPortJobLocked(
|
|
|
|
|
'vpc_port_detach', $instance, $vpc, $port, $state,
|
|
|
|
|
(int) $batch->id
|
|
|
|
|
);
|
|
|
|
|
$item->child_job_id = (int) $job->id;
|
|
|
|
|
$item->updated_at = time();
|
|
|
|
|
$item->save();
|
|
|
|
|
}
|
|
|
|
|
$vpc->state = 'provisioning';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function settlePortChildLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent,
|
|
|
|
|
CloudJob $job,
|
|
|
|
|
string $status,
|
|
|
|
|
$result,
|
|
|
|
|
array $operation,
|
|
|
|
|
CloudVpc $vpc,
|
|
|
|
|
CloudVpcPort $port,
|
|
|
|
|
CloudInstance $instance
|
|
|
|
|
): void {
|
|
|
|
|
$item = CloudVpcMembershipBatchItem::query()
|
|
|
|
|
->where('batch_id', (int) $batch->id)
|
|
|
|
|
->where('instance_id', (int) $instance->id)
|
|
|
|
|
->where('vpc_port_id', (int) $port->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($item === null || (int) ($item->child_job_id ?? 0) !== (int) $job->id) {
|
|
|
|
|
throw new CloudError('VPC 批次实例明细与子任务不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$now = time();
|
|
|
|
|
if ($status === 'succeeded') {
|
|
|
|
|
CloudVpcPortService::assertBatchPortResult(
|
|
|
|
|
$job, $vpc, $port, $instance, $result
|
|
|
|
|
);
|
|
|
|
|
if ((string) $batch->kind === 'attach') {
|
|
|
|
|
$port->state = 'attached';
|
|
|
|
|
$port->applied_revision = (int) $port->desired_revision;
|
|
|
|
|
$port->attached_at = $now;
|
|
|
|
|
} else {
|
|
|
|
|
$port->state = 'release_pending';
|
|
|
|
|
}
|
|
|
|
|
$port->last_error_code = '';
|
|
|
|
|
$port->updated_at = $now;
|
|
|
|
|
$port->save();
|
|
|
|
|
$item->state = 'succeeded';
|
|
|
|
|
$item->last_error_code = '';
|
|
|
|
|
} elseif ($status === 'uncertain') {
|
|
|
|
|
$item->state = 'unknown';
|
|
|
|
|
$item->last_error_code = 'vpc_result_uncertain';
|
|
|
|
|
$port->last_error_code = 'vpc_result_uncertain';
|
|
|
|
|
$port->updated_at = $now;
|
|
|
|
|
$port->save();
|
|
|
|
|
} else {
|
|
|
|
|
$code = CloudOperationErrorCatalog::workerSummary(
|
|
|
|
|
(string) $job->kind, 'failed', $operation
|
|
|
|
|
) ?? 'vpc_port_change_failed';
|
|
|
|
|
if ((string) $batch->kind === 'detach') {
|
|
|
|
|
$port->state = 'attached';
|
|
|
|
|
}
|
|
|
|
|
$port->last_error_code = substr($code, 0, 64);
|
|
|
|
|
$port->updated_at = $now;
|
|
|
|
|
$port->save();
|
|
|
|
|
$item->state = 'failed';
|
|
|
|
|
$item->last_error_code = substr($code, 0, 64);
|
|
|
|
|
}
|
|
|
|
|
$item->updated_at = $now;
|
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
|
|
$items = self::itemsLocked($batch);
|
|
|
|
|
if ($items->contains(static fn ($row): bool =>
|
|
|
|
|
(string) $row->state === 'queued')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($items->contains(static fn ($row): bool =>
|
|
|
|
|
(string) $row->state === 'unknown')) {
|
|
|
|
|
self::blockLocked($batch, $vpc, 'vpc_result_uncertain');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $batch->kind === 'attach') {
|
|
|
|
|
self::finishAttachPortsLocked($batch, $parent, $vpc, $items);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self::beginDetachCleanupLocked($batch, $parent, $vpc, $items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function advanceHostStageLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent,
|
|
|
|
|
CloudJob $current,
|
|
|
|
|
string $currentStatus,
|
|
|
|
|
string $code,
|
|
|
|
|
CloudVpc $vpc
|
|
|
|
|
): void {
|
|
|
|
|
$outcomes = self::hostStageOutcomes(
|
|
|
|
|
$batch, $current, $currentStatus
|
|
|
|
|
);
|
|
|
|
|
if ($outcomes['pending'] > 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($outcomes['unknown'] > 0) {
|
|
|
|
|
self::blockLocked($batch, $vpc,
|
|
|
|
|
$code !== '' ? $code : 'vpc_result_uncertain');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($outcomes['failed'] > 0) {
|
|
|
|
|
if ((string) $batch->kind === 'attach') {
|
|
|
|
|
CloudVpcMembershipBatchItem::query()
|
|
|
|
|
->where('batch_id', (int) $batch->id)
|
|
|
|
|
->where('state', 'waiting_route')->update([
|
|
|
|
|
'state' => 'failed',
|
|
|
|
|
'last_error_code' => $code !== ''
|
|
|
|
|
? $code : 'vpc_host_apply_failed',
|
|
|
|
|
'updated_at' => time(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$vpc->state = 'degraded';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
self::finishLocked($batch, $parent, false,
|
|
|
|
|
$outcomes['succeeded'] > 0,
|
|
|
|
|
$outcomes['succeeded'] === 0,
|
|
|
|
|
$code !== '' ? $code : 'vpc_host_apply_failed');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $batch->kind === 'attach') {
|
|
|
|
|
self::releaseAttachPortJobsLocked($batch, $vpc);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self::finishDetachCleanupLocked($batch, $parent, $vpc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function releaseAttachPortJobsLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudVpc $vpc
|
|
|
|
|
): void {
|
|
|
|
|
$items = self::itemsLocked($batch);
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
if ((string) $item->state !== 'waiting_route') {
|
|
|
|
|
throw new CloudError('VPC 加入批次明细状态不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$instance = CloudInstance::query()
|
|
|
|
|
->where('id', (int) $item->instance_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$port = CloudVpcPort::query()
|
|
|
|
|
->where('id', (int) $item->vpc_port_id)
|
|
|
|
|
->where('active_claim', 1)->lockForUpdate()->first();
|
|
|
|
|
$state = $instance === null ? null : CloudVpcHostState::query()
|
|
|
|
|
->where('vpc_id', (int) $vpc->id)
|
|
|
|
|
->where('host_id', (int) $instance->host_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($instance === null || $port === null
|
|
|
|
|
|| !CloudVpcPortService::batchHostReady($state)) {
|
|
|
|
|
throw new CloudError('VPC 加入批次路由屏障未收敛', 409);
|
|
|
|
|
}
|
|
|
|
|
$job = CloudVpcPortService::queueBatchPortJobLocked(
|
|
|
|
|
'vpc_port_attach', $instance, $vpc, $port, $state,
|
|
|
|
|
(int) $batch->id
|
|
|
|
|
);
|
|
|
|
|
$item->child_job_id = (int) $job->id;
|
|
|
|
|
$item->state = 'queued';
|
|
|
|
|
$item->updated_at = time();
|
|
|
|
|
$item->save();
|
|
|
|
|
}
|
|
|
|
|
$batch->state = 'ports';
|
|
|
|
|
$batch->last_error_code = '';
|
|
|
|
|
$batch->updated_at = time();
|
|
|
|
|
$batch->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function finishAttachPortsLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent,
|
|
|
|
|
CloudVpc $vpc,
|
|
|
|
|
$items
|
|
|
|
|
): void {
|
|
|
|
|
$failed = $items->where('state', 'failed')->count();
|
|
|
|
|
$succeeded = $items->where('state', 'succeeded')->count();
|
|
|
|
|
$vpc->state = $failed === 0 ? 'active' : 'degraded';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
if ($failed === 0) {
|
|
|
|
|
self::finishLocked($batch, $parent, true, false, false, '');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self::finishLocked($batch, $parent, false, $succeeded > 0,
|
|
|
|
|
$succeeded === 0, 'vpc_batch_attach_failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function beginDetachCleanupLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent,
|
|
|
|
|
CloudVpc $vpc,
|
|
|
|
|
$items
|
|
|
|
|
): void {
|
|
|
|
|
$succeeded = $items->where('state', 'succeeded');
|
|
|
|
|
if ($succeeded->isEmpty()) {
|
|
|
|
|
$vpc->state = 'degraded';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
self::finishLocked($batch, $parent, false, false, true,
|
|
|
|
|
'vpc_batch_detach_failed');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$revision = (int) $vpc->desired_revision + 1;
|
|
|
|
|
CloudVpcPort::query()->whereIn('id', $succeeded->pluck('vpc_port_id'))
|
|
|
|
|
->update(['desired_revision' => $revision, 'updated_at' => time()]);
|
|
|
|
|
$vpc->desired_revision = $revision;
|
|
|
|
|
$vpc->state = 'provisioning';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
$batch->state = 'cleanup';
|
|
|
|
|
$batch->desired_revision = $revision;
|
|
|
|
|
$batch->updated_at = time();
|
|
|
|
|
$batch->save();
|
|
|
|
|
$focus = CloudVpcPort::query()
|
|
|
|
|
->where('id', (int) $succeeded->first()->vpc_port_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($focus === null) {
|
|
|
|
|
throw new CloudError('VPC 移出批次清理端口缺失', 409);
|
|
|
|
|
}
|
|
|
|
|
$jobs = CloudVpcPortService::queueBatchProjectionLocked(
|
|
|
|
|
$vpc, $focus, $revision, (int) $batch->id
|
|
|
|
|
);
|
|
|
|
|
if ($jobs === []) {
|
|
|
|
|
self::finishDetachCleanupLocked($batch, $parent, $vpc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function finishDetachCleanupLocked(
|
|
|
|
|
CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent,
|
|
|
|
|
CloudVpc $vpc
|
|
|
|
|
): void {
|
|
|
|
|
$items = self::itemsLocked($batch);
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach ($items->where('state', 'succeeded') as $item) {
|
|
|
|
|
$port = CloudVpcPort::query()
|
|
|
|
|
->where('id', (int) $item->vpc_port_id)
|
|
|
|
|
->where('active_claim', 1)->lockForUpdate()->first();
|
|
|
|
|
if ($port === null || (string) $port->state !== 'release_pending') {
|
|
|
|
|
throw new CloudError('VPC 移出批次端口释放账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$port->state = 'released';
|
|
|
|
|
$port->active_claim = null;
|
|
|
|
|
$port->last_operation_id = null;
|
|
|
|
|
$port->last_error_code = '';
|
|
|
|
|
$port->released_at = $now;
|
|
|
|
|
$port->updated_at = $now;
|
|
|
|
|
$port->save();
|
|
|
|
|
}
|
|
|
|
|
$failed = $items->where('state', 'failed')->count();
|
|
|
|
|
$vpc->state = $failed === 0 ? 'active' : 'degraded';
|
|
|
|
|
$vpc->updated_at = $now;
|
|
|
|
|
$vpc->save();
|
|
|
|
|
if ($failed === 0) {
|
|
|
|
|
self::finishLocked($batch, $parent, true, false, false, '');
|
|
|
|
|
} else {
|
|
|
|
|
self::finishLocked($batch, $parent, false, true, false,
|
|
|
|
|
'vpc_batch_detach_partial');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function finishLocked(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $parent, bool $success, bool $effect, bool $safe,
|
|
|
|
|
string $code): void
|
|
|
|
|
{
|
|
|
|
|
$items = self::itemsLocked($batch);
|
|
|
|
|
$now = time();
|
|
|
|
|
$batch->state = $success ? 'succeeded' : 'failed';
|
|
|
|
|
$batch->active_claim = null;
|
|
|
|
|
$batch->last_error_code = $success ? '' : substr($code, 0, 64);
|
|
|
|
|
$batch->terminal_at = $now;
|
|
|
|
|
$batch->updated_at = $now;
|
|
|
|
|
$batch->save();
|
|
|
|
|
CloudJobService::completeAggregateLocked(
|
|
|
|
|
$parent,
|
|
|
|
|
$success ? 'succeeded' : 'failed',
|
|
|
|
|
$effect,
|
|
|
|
|
$safe,
|
|
|
|
|
$success ? '' : $code,
|
|
|
|
|
[
|
|
|
|
|
'batch_key' => (string) $batch->batch_key,
|
|
|
|
|
'action' => (string) $batch->kind,
|
|
|
|
|
'total' => $items->count(),
|
|
|
|
|
'succeeded' => $items->where('state', 'succeeded')->count(),
|
|
|
|
|
'failed' => $items->where('state', 'failed')->count(),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
// A partial batch failure is a known mixed result, not an ambiguous
|
|
|
|
|
// dispatch. Successful children are already reflected in the port and
|
|
|
|
|
// route ledgers, while every failed child is proven not applied.
|
|
|
|
|
if (!$success && $effect) {
|
|
|
|
|
$parent->effect_resolution = 'applied';
|
|
|
|
|
$parent->effect_resolved_at = $now;
|
|
|
|
|
$parent->effect_resolution_reason =
|
|
|
|
|
'Known partial VPC membership batch result';
|
|
|
|
|
$parent->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function blockLocked(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudVpc $vpc, string $code): void
|
|
|
|
|
{
|
|
|
|
|
$batch->state = 'blocked';
|
|
|
|
|
$batch->last_error_code = substr($code, 0, 64);
|
|
|
|
|
$batch->updated_at = time();
|
|
|
|
|
$batch->save();
|
|
|
|
|
$vpc->state = 'degraded';
|
|
|
|
|
$vpc->updated_at = time();
|
|
|
|
|
$vpc->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{pending:int,succeeded:int,failed:int,unknown:int} */
|
|
|
|
|
private static function hostStageOutcomes(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudJob $current, string $currentStatus): array
|
|
|
|
|
{
|
|
|
|
|
$counts = ['pending' => 0, 'succeeded' => 0, 'failed' => 0,
|
|
|
|
|
'unknown' => 0];
|
|
|
|
|
$jobs = CloudJob::query()->where('vpc_batch_id', (int) $batch->id)
|
|
|
|
|
->whereIn('kind', ['vpc_host_apply', 'vpc_host_remove'])
|
|
|
|
|
->orderBy('id')->get();
|
|
|
|
|
if ($jobs->isEmpty()) {
|
|
|
|
|
throw new CloudError('VPC 批次宿主机子任务账本缺失', 409);
|
|
|
|
|
}
|
|
|
|
|
foreach ($jobs as $job) {
|
|
|
|
|
if ((int) $job->id === (int) $current->id) {
|
|
|
|
|
$bucket = match ($currentStatus) {
|
|
|
|
|
'succeeded' => 'succeeded',
|
|
|
|
|
'failed' => 'failed',
|
|
|
|
|
default => 'unknown',
|
|
|
|
|
};
|
|
|
|
|
} elseif ((string) $job->phase !== 'terminal') {
|
|
|
|
|
$bucket = 'pending';
|
|
|
|
|
} elseif ((bool) $job->finalization_incident
|
|
|
|
|
|| (string) $job->effect_resolution === 'unknown') {
|
|
|
|
|
$bucket = 'unknown';
|
|
|
|
|
} elseif ((string) $job->effect_resolution === 'applied') {
|
|
|
|
|
$bucket = 'succeeded';
|
|
|
|
|
} else {
|
|
|
|
|
$bucket = 'failed';
|
|
|
|
|
}
|
|
|
|
|
$counts[$bucket]++;
|
|
|
|
|
}
|
|
|
|
|
return $counts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function newParentJob(int $userId, string $requestKey,
|
|
|
|
|
string $fingerprint, CloudVpc $vpc, string $action,
|
|
|
|
|
array $instanceIds): CloudJob
|
|
|
|
|
{
|
|
|
|
|
$now = time();
|
|
|
|
|
$job = new CloudJob();
|
|
|
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
|
|
|
$job->request_idempotency_key = $requestKey;
|
|
|
|
|
$job->request_fingerprint = $fingerprint;
|
|
|
|
|
$job->vpc_id = (int) $vpc->id;
|
|
|
|
|
$job->user_id = $userId;
|
|
|
|
|
$job->host_id = null;
|
|
|
|
|
$job->kind = self::PARENT_KIND;
|
|
|
|
|
$job->execution_mode = 'local';
|
|
|
|
|
$job->sealed_payload = CloudSecrets::encrypt([
|
|
|
|
|
'local_vpc_membership_batch' => true,
|
|
|
|
|
'vpc_key' => (string) $vpc->vpc_key,
|
|
|
|
|
'action' => $action,
|
|
|
|
|
'instance_ids' => $instanceIds,
|
|
|
|
|
], 'job-payload');
|
|
|
|
|
// This local coordination row is completed only by child finalizers;
|
|
|
|
|
// it never crosses an Agent dispatch deadline.
|
|
|
|
|
$job->dispatch_deadline_at = $now + 315360000;
|
|
|
|
|
$job->available_at = 0;
|
|
|
|
|
$job->created_at = $job->updated_at = $now;
|
|
|
|
|
$job->save();
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function newItem(CloudVpcMembershipBatch $batch,
|
|
|
|
|
CloudInstance $instance, CloudVpcPort $port,
|
|
|
|
|
string $state): CloudVpcMembershipBatchItem
|
|
|
|
|
{
|
|
|
|
|
$now = time();
|
|
|
|
|
$item = new CloudVpcMembershipBatchItem();
|
|
|
|
|
$item->batch_id = (int) $batch->id;
|
|
|
|
|
$item->user_id = (int) $batch->user_id;
|
|
|
|
|
$item->instance_id = (int) $instance->id;
|
|
|
|
|
$item->vpc_port_id = (int) $port->id;
|
|
|
|
|
$item->host_id = (int) $instance->host_id;
|
|
|
|
|
$item->child_job_id = null;
|
|
|
|
|
$item->state = $state;
|
|
|
|
|
$item->last_error_code = '';
|
|
|
|
|
$item->created_at = $item->updated_at = $now;
|
|
|
|
|
$item->save();
|
|
|
|
|
return $item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function itemsLocked(CloudVpcMembershipBatch $batch)
|
|
|
|
|
{
|
|
|
|
|
return CloudVpcMembershipBatchItem::query()
|
|
|
|
|
->where('batch_id', (int) $batch->id)->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function dto(CloudVpcMembershipBatch $batch): array
|
|
|
|
|
{
|
|
|
|
|
$items = CloudVpcMembershipBatchItem::query()
|
|
|
|
|
->where('batch_id', (int) $batch->id)->get();
|
|
|
|
|
$vpc = CloudVpc::find((int) $batch->vpc_id);
|
|
|
|
|
return [
|
|
|
|
|
'batch_key' => (string) $batch->batch_key,
|
|
|
|
|
'vpc_id' => (string) $batch->vpc_id,
|
|
|
|
|
'vpc_key' => $vpc === null ? null : (string) $vpc->vpc_key,
|
|
|
|
|
'action' => (string) $batch->kind,
|
|
|
|
|
'state' => (string) $batch->state,
|
|
|
|
|
'total' => $items->count(),
|
|
|
|
|
'succeeded' => $items->where('state', 'succeeded')->count(),
|
|
|
|
|
'failed' => $items->where('state', 'failed')->count(),
|
|
|
|
|
'unknown' => $items->where('state', 'unknown')->count(),
|
|
|
|
|
'task_id' => (string) $batch->parent_job_id,
|
|
|
|
|
'status_url' => '/api/v1/tasks/' . (int) $batch->parent_job_id,
|
|
|
|
|
'created_at' => (int) $batch->created_at,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return list<int> */
|
|
|
|
|
private static function instanceIds($value): array
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($value) || !array_is_list($value) || $value === []) {
|
|
|
|
|
throw new CloudError('请选择至少一台实例');
|
|
|
|
|
}
|
|
|
|
|
$ids = [];
|
|
|
|
|
foreach ($value as $candidate) {
|
|
|
|
|
if (is_bool($candidate)
|
|
|
|
|
|| (!is_int($candidate)
|
|
|
|
|
&& (!is_string($candidate)
|
|
|
|
|
|| preg_match('/^[1-9][0-9]*$/D', $candidate) !== 1))) {
|
|
|
|
|
throw new CloudError('实例列表包含无效标识');
|
|
|
|
|
}
|
|
|
|
|
$id = (int) $candidate;
|
|
|
|
|
if ($id < 1) {
|
|
|
|
|
throw new CloudError('实例列表包含无效标识');
|
|
|
|
|
}
|
|
|
|
|
$ids[$id] = $id;
|
|
|
|
|
}
|
|
|
|
|
$ids = array_values($ids);
|
|
|
|
|
sort($ids, SORT_NUMERIC);
|
|
|
|
|
return $ids;
|
|
|
|
|
}
|
|
|
|
|
}
|