You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudVpcMembershipBatchServ...

1152 lines
45 KiB
PHP

<?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 INSTANCE_FENCE_KIND = 'vpc_batch_instance_fence';
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);
$existing = CloudJob::query()->where('user_id', $userId)
->where('request_idempotency_key', $requestKey)->first();
if ($existing !== null) {
return self::existingResult(
$existing, $fingerprint, $vpcKey, $userId
);
}
return (new CloudVpcMembershipBatch())->getConnection()->transaction(
static function () use (
$actor, $userId, $requestKey, $fingerprint, $batchKey,
$vpcKey, $action, $instanceIds
): array {
$instances = CloudVpcLockOrder::instances($instanceIds);
if ($instances->count() !== count($instanceIds)) {
throw new CloudError('部分实例不存在或状态已变化', 409);
}
$vpc = CloudVpcLockOrder::vpcByKey($vpcKey);
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) {
return self::existingResult(
$prior, $fingerprint, $vpcKey, $userId
);
}
if (CloudVpcMembershipBatch::query()
->where('vpc_id', (int) $vpc->id)
->where('active_claim', 1)->lockForUpdate()->exists()) {
throw new CloudError('该 VPC 已有批量操作正在执行', 409);
}
CloudVpcPortService::assertBatchVpcSettledLocked($vpc);
foreach ($instanceIds as $instanceId) {
$instance = $instances->get($instanceId);
CloudVpcPortService::assertBatchInstanceLocked(
$instance,
$actor,
$action === 'attach'
);
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
);
}
private static function existingResult(
CloudJob $prior,
string $fingerprint,
string $vpcKey,
int $userId
): array {
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();
$vpc = CloudVpc::query()->where('vpc_key', $vpcKey)->first();
if ($batch === null || $vpc === null
|| (int) $batch->vpc_id !== (int) $vpc->id
|| (int) $batch->user_id !== $userId
|| (int) $prior->user_id !== $userId) {
throw new CloudError('VPC 批次幂等账本不完整', 409);
}
return ['job' => $prior, 'batch' => self::dto($batch),
'unchanged' => true];
}
/** @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();
}
/**
* Continue only an attach rollback whose previous cleanup attempts are
* all proven not applied. This never replays an unknown Agent mutation.
*/
public static function cancelAttach(array $actor, string $vpcKey,
string $batchKey): array
{
if (preg_match('/^[a-f0-9]{32}$/D', $vpcKey) !== 1
|| preg_match('/^[a-f0-9]{32}$/D', $batchKey) !== 1) {
throw new CloudError('VPC 批次标识无效', 404);
}
$userId = (int) ($actor['user_id'] ?? 0);
return (new CloudVpcMembershipBatch())->getConnection()->transaction(
static function () use ($userId, $vpcKey, $batchKey): array {
$vpc = CloudVpc::query()->where('vpc_key', $vpcKey)
->lockForUpdate()->first();
if ($vpc === null || (int) $vpc->user_id !== $userId) {
throw new CloudError('VPC 不存在或当前不可用', 404);
}
$batch = CloudVpcMembershipBatch::query()
->where('batch_key', $batchKey)
->where('vpc_id', (int) $vpc->id)
->where('user_id', $userId)
->lockForUpdate()->first();
if ($batch === null || (string) $batch->kind !== 'attach') {
throw new CloudError('VPC 加入批次不存在', 404);
}
$parent = CloudJob::query()
->where('id', (int) $batch->parent_job_id)
->lockForUpdate()->first();
if ($parent === null
|| (string) $parent->kind !== self::PARENT_KIND) {
throw new CloudError('VPC 批次父任务账本不一致', 409);
}
if (in_array((string) $batch->state,
self::TERMINAL_STATES, true)) {
return ['job' => $parent, 'batch' => self::dto($batch),
'unchanged' => true];
}
if ((string) $batch->state === 'cleanup') {
return ['job' => $parent, 'batch' => self::dto($batch),
'unchanged' => true];
}
$items = self::itemsLocked($batch);
if ((string) $batch->state !== 'blocked'
|| !self::canRetryAttachCleanup($batch, $items, true)) {
throw new CloudError(
'撤销结果尚未确认,不能重复清理;请等待任务核对',
409
);
}
$focus = CloudVpcPort::query()
->where('id', (int) $items->first()->vpc_port_id)
->where('active_claim', 1)->lockForUpdate()->first();
if ($focus === null) {
throw new CloudError('VPC 撤销端口账本缺失', 409);
}
$revision = (int) $vpc->desired_revision + 1;
CloudVpcPort::query()
->whereIn('id', $items->pluck('vpc_port_id')->all())
->where('active_claim', 1)->update([
'desired_revision' => $revision,
'updated_at' => time(),
]);
$batch->state = 'cleanup';
$batch->desired_revision = $revision;
$batch->last_error_code = '';
$batch->updated_at = time();
$batch->save();
$vpc->desired_revision = $revision;
$vpc->state = 'provisioning';
$vpc->updated_at = time();
$vpc->save();
$jobs = CloudVpcPortService::queueBatchProjectionLocked(
$vpc,
$focus,
$revision,
(int) $batch->id
);
if ($jobs === []) {
self::finishAttachCleanupLocked($batch, $parent, $vpc);
}
return ['job' => $parent, 'batch' => self::dto($batch),
'unchanged' => false];
},
3
);
}
/** 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();
}
/**
* Treat one exact host-removal child as converged only after the physical
* host-retirement service has proved the disabled node owns no resources.
*/
public static function finishRetiredHostRemovalLocked(
CloudJob $job,
CloudVpc $vpc
): void {
if ((string) $job->kind !== 'vpc_host_remove'
|| $job->vpc_batch_id === null
|| (string) $job->effect_resolution !== 'applied') {
throw new CloudError('VPC 批次退役子任务无效', 409);
}
$batch = CloudVpcMembershipBatch::query()
->where('id', (int) $job->vpc_batch_id)
->lockForUpdate()->first();
if ($batch === null || (int) $batch->vpc_id !== (int) $vpc->id
|| (string) $batch->state !== 'blocked'
|| in_array((string) $batch->state,
self::TERMINAL_STATES, true)) {
throw new CloudError('VPC 批次退役账本不一致', 409);
}
$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);
}
self::advanceHostStageLocked(
$batch, $parent, $job, 'succeeded', '', $vpc
);
}
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');
self::newInstanceFenceJob($batch, $instance, $port);
$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'
&& (string) $batch->state === 'cleanup') {
self::blockLocked($batch, $vpc,
$code !== '' ? $code : 'vpc_attach_cleanup_failed');
return;
}
if ((string) $batch->kind === 'attach') {
self::beginAttachCleanupLocked(
$batch,
$parent,
$vpc,
$code !== '' ? $code : 'vpc_host_apply_failed',
(string) $current->operation_id
);
return;
}
$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') {
if ((string) $batch->state === 'cleanup') {
self::finishAttachCleanupLocked($batch, $parent, $vpc);
return;
}
self::releaseAttachPortJobsLocked($batch, $vpc);
return;
}
self::finishDetachCleanupLocked($batch, $parent, $vpc);
}
private static function beginAttachCleanupLocked(
CloudVpcMembershipBatch $batch,
CloudJob $parent,
CloudVpc $vpc,
string $code,
string $provenNotAppliedOperationId
): void {
$items = self::itemsLocked($batch);
if ($items->isEmpty()
|| $items->contains(static fn ($item): bool =>
(string) $item->state !== 'waiting_route')) {
throw new CloudError('VPC 加入撤销明细状态不一致', 409);
}
$now = time();
$revision = (int) $vpc->desired_revision + 1;
foreach ($items as $item) {
$port = CloudVpcPort::query()
->where('id', (int) $item->vpc_port_id)
->where('active_claim', 1)->lockForUpdate()->first();
if ($port === null || (string) $port->state !== 'preparing') {
throw new CloudError('VPC 加入撤销端口状态不一致', 409);
}
$port->state = 'release_pending';
$port->desired_revision = $revision;
$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();
}
$vpc->desired_revision = $revision;
$vpc->state = 'provisioning';
$vpc->updated_at = $now;
$vpc->save();
$batch->state = 'cleanup';
$batch->desired_revision = $revision;
$batch->last_error_code = substr($code, 0, 64);
$batch->updated_at = $now;
$batch->save();
$focus = CloudVpcPort::query()
->where('id', (int) $items->first()->vpc_port_id)
->where('active_claim', 1)->lockForUpdate()->first();
if ($focus === null) {
throw new CloudError('VPC 加入撤销缺少焦点端口', 409);
}
$jobs = CloudVpcPortService::queueBatchProjectionLocked(
$vpc,
$focus,
$revision,
(int) $batch->id,
$provenNotAppliedOperationId
);
if ($jobs === []) {
self::finishAttachCleanupLocked($batch, $parent, $vpc);
}
}
private static function finishAttachCleanupLocked(
CloudVpcMembershipBatch $batch,
CloudJob $parent,
CloudVpc $vpc
): void {
$items = self::itemsLocked($batch);
$now = time();
$failureCode = 'vpc_batch_attach_route_failed';
foreach ($items as $item) {
if ((string) $item->last_error_code !== '') {
$failureCode = (string) $item->last_error_code;
break;
}
}
foreach ($items 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();
}
$vpc->state = 'active';
$vpc->updated_at = $now;
$vpc->save();
self::finishLocked(
$batch,
$parent,
false,
false,
true,
$failureCode
);
}
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
);
self::completeInstanceFenceLocked(
$batch,
(int) $instance->id,
(int) $port->id,
true,
false,
''
);
$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();
if ((string) $batch->kind === 'attach') {
foreach ($items as $item) {
self::completeInstanceFenceLocked(
$batch,
(int) $item->instance_id,
(int) $item->vpc_port_id,
$success,
$safe,
$success ? '' : $code
);
}
}
$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'])
->where('request_idempotency_key', 'like',
CloudVpcPortService::batchProjectionRequestPrefix(
(int) $batch->id,
(int) $batch->desired_revision
) . '%')
->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 canRetryAttachCleanup(
CloudVpcMembershipBatch $batch,
$items,
bool $lock
): bool {
if ((string) $batch->kind !== 'attach' || $items->isEmpty()) {
return false;
}
$portQuery = CloudVpcPort::query()
->whereIn('id', $items->pluck('vpc_port_id')->all())
->where('active_claim', 1)->orderBy('id');
$ports = $lock ? $portQuery->lockForUpdate()->get()
: $portQuery->get();
if ($ports->count() !== $items->count()
|| $ports->contains(static fn ($port): bool =>
(string) $port->state !== 'release_pending')) {
return false;
}
$stateQuery = CloudVpcHostState::query()
->where('vpc_id', (int) $batch->vpc_id)->orderBy('host_id');
$states = $lock ? $stateQuery->lockForUpdate()->get()
: $stateQuery->get();
foreach ($states as $state) {
if ((string) $state->sync_state === 'ready') {
continue;
}
if ((string) $state->sync_state !== 'failed'
|| !is_string($state->last_operation_id)) {
return false;
}
$jobQuery = CloudJob::query()
->where('operation_id', (string) $state->last_operation_id)
->where('vpc_batch_id', (int) $batch->id);
$job = $lock ? $jobQuery->lockForUpdate()->first()
: $jobQuery->first();
if ($job === null || (string) $job->phase !== 'terminal'
|| (string) $job->outcome !== 'failed'
|| (string) $job->effect_resolution !== 'not_applied'
|| (int) $job->effect_may_have_applied !== 0) {
return false;
}
}
return true;
}
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 newInstanceFenceJob(
CloudVpcMembershipBatch $batch,
CloudInstance $instance,
CloudVpcPort $port
): CloudJob {
$now = time();
$job = new CloudJob();
$job->operation_id = bin2hex(random_bytes(16));
$job->request_idempotency_key = substr(
'vpc-batch-fence-' . (int) $batch->id . '-'
. (int) $instance->id,
0,
128
);
$job->request_fingerprint = hash('sha256', implode('|', [
self::INSTANCE_FENCE_KIND,
(int) $batch->id,
(int) $instance->id,
(int) $port->id,
]));
$job->parent_job_id = (int) $batch->parent_job_id;
$job->instance_id = (int) $instance->id;
$job->vpc_id = (int) $batch->vpc_id;
$job->vpc_port_id = (int) $port->id;
$job->vpc_batch_id = (int) $batch->id;
$job->user_id = (int) $batch->user_id;
$job->host_id = null;
$job->kind = self::INSTANCE_FENCE_KIND;
$job->execution_mode = 'local';
$job->sealed_payload = CloudSecrets::encrypt([
'local_vpc_batch_instance_fence' => true,
'batch_key' => (string) $batch->batch_key,
'instance_id' => (int) $instance->id,
'vpc_port_id' => (int) $port->id,
], 'job-payload');
// Only the batch coordinator may hand this fence to a real Agent job
// or close it. It never crosses a transport 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 completeInstanceFenceLocked(
CloudVpcMembershipBatch $batch,
int $instanceId,
int $portId,
bool $success,
bool $safeToResubmit,
string $errorCode
): void {
$fences = CloudJob::query()
->where('kind', self::INSTANCE_FENCE_KIND)
->where('vpc_batch_id', (int) $batch->id)
->where('parent_job_id', (int) $batch->parent_job_id)
->where('instance_id', $instanceId)
->where('vpc_port_id', $portId)
->lockForUpdate()->get();
if ($fences->count() !== 1) {
throw new CloudError('VPC 批次实例围栏账本不完整', 409);
}
$fence = $fences->first();
if ((string) $fence->phase === 'terminal') {
return;
}
CloudJobService::completeAggregateLocked(
$fence,
$success ? 'succeeded' : 'failed',
false,
!$success && $safeToResubmit,
$success ? '' : ($errorCode !== ''
? $errorCode : 'vpc_batch_instance_fence_released'),
[
'batch_key' => (string) $batch->batch_key,
'instance_id' => $instanceId,
'vpc_port_id' => $portId,
]
);
}
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)->orderBy('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(),
'items' => $items->map(static fn ($item): array => [
'instance_id' => (string) $item->instance_id,
'state' => (string) $item->state,
])->values()->all(),
'can_cancel' => (string) $batch->state === 'blocked'
&& self::canRetryAttachCleanup($batch, $items, false),
'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;
}
}