|
|
|
|
@ -21,6 +21,8 @@ use App\Models\CloudVpcPort;
|
|
|
|
|
*/
|
|
|
|
|
final class CloudVpcPortService
|
|
|
|
|
{
|
|
|
|
|
private const DELETED_CLEANUP_RETRY_DELAY = 300;
|
|
|
|
|
|
|
|
|
|
private const JOB_KINDS = [
|
|
|
|
|
'vpc_host_apply', 'vpc_host_remove',
|
|
|
|
|
'vpc_port_attach', 'vpc_port_detach',
|
|
|
|
|
@ -255,6 +257,186 @@ final class CloudVpcPortService
|
|
|
|
|
}, 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start route/IP cleanup only after the Agent has proved the VM and every
|
|
|
|
|
* managed artifact are absent. The caller already holds the instance row
|
|
|
|
|
* lock inside the delete finalizer transaction. No private address is
|
|
|
|
|
* released until every resulting host projection has converged.
|
|
|
|
|
*
|
|
|
|
|
* @return list<CloudJob>
|
|
|
|
|
*/
|
|
|
|
|
public static function releaseAfterInstanceDeletedLocked(
|
|
|
|
|
CloudInstance $instance
|
|
|
|
|
): array {
|
|
|
|
|
$reference = CloudVpcPort::query()
|
|
|
|
|
->where('instance_id', (int) $instance->id)
|
|
|
|
|
->where('active_claim', 1)->first();
|
|
|
|
|
if ($reference === null) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$vpc = CloudVpc::query()->where('id', (int) $reference->vpc_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$port = CloudVpcPort::query()->where('id', (int) $reference->id)
|
|
|
|
|
->where('active_claim', 1)->lockForUpdate()->first();
|
|
|
|
|
if ($vpc === null || $port === null
|
|
|
|
|
|| (int) $port->instance_id !== (int) $instance->id
|
|
|
|
|
|| (int) $port->user_id !== (int) $instance->user_id
|
|
|
|
|
|| (int) $port->vpc_id !== (int) $vpc->id
|
|
|
|
|
|| (int) $vpc->user_id !== (int) $instance->user_id) {
|
|
|
|
|
throw new CloudError('删除实例的 VPC 端口账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if (self::activePortJobLocked($instance, $port) !== null) {
|
|
|
|
|
throw new CloudError('删除实例仍有 VPC 任务未收敛', 409);
|
|
|
|
|
}
|
|
|
|
|
if ((string) $port->state === 'release_pending') {
|
|
|
|
|
$job = self::retryReleaseLocked($instance, $vpc, $port);
|
|
|
|
|
return $job === null ? [] : [$job];
|
|
|
|
|
}
|
|
|
|
|
if (!in_array((string) $port->state,
|
|
|
|
|
['attached', 'preparing'], true)) {
|
|
|
|
|
throw new CloudError('删除实例的 VPC 端口状态不允许释放', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
$port->state = 'release_pending';
|
|
|
|
|
$port->last_operation_id = null;
|
|
|
|
|
$port->last_error_code = '';
|
|
|
|
|
$port->updated_at = $now;
|
|
|
|
|
$port->save();
|
|
|
|
|
$revision = (int) $vpc->desired_revision + 1;
|
|
|
|
|
$vpc->desired_revision = $revision;
|
|
|
|
|
$vpc->state = 'provisioning';
|
|
|
|
|
$vpc->updated_at = $now;
|
|
|
|
|
$vpc->save();
|
|
|
|
|
$jobs = self::queueProjectionLocked($vpc, $port, $revision);
|
|
|
|
|
if ($jobs === []) {
|
|
|
|
|
self::releasePortLocked($vpc, $port);
|
|
|
|
|
}
|
|
|
|
|
return $jobs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Queue new, audited cleanup generations for deleted instances only. */
|
|
|
|
|
public static function retryDeletedCleanupDue(int $limit = 10): int
|
|
|
|
|
{
|
|
|
|
|
$limit = max(1, min(100, $limit));
|
|
|
|
|
$candidates = CloudVpcPort::query()
|
|
|
|
|
->join('cloud_instances as deleted_instance',
|
|
|
|
|
'deleted_instance.id', '=', 'cloud_vpc_ports.instance_id')
|
|
|
|
|
->where('cloud_vpc_ports.active_claim', 1)
|
|
|
|
|
->where('cloud_vpc_ports.state', 'release_pending')
|
|
|
|
|
->where('deleted_instance.status', 'deleted')
|
|
|
|
|
->orderBy('cloud_vpc_ports.id')
|
|
|
|
|
->limit($limit)
|
|
|
|
|
->get([
|
|
|
|
|
'cloud_vpc_ports.id as port_id',
|
|
|
|
|
'cloud_vpc_ports.instance_id as instance_id',
|
|
|
|
|
]);
|
|
|
|
|
$queued = 0;
|
|
|
|
|
foreach ($candidates as $candidate) {
|
|
|
|
|
try {
|
|
|
|
|
$queued += (new CloudInstance())->getConnection()
|
|
|
|
|
->transaction(static function () use ($candidate): int {
|
|
|
|
|
$instance = CloudInstance::query()
|
|
|
|
|
->where('id', (int) $candidate->instance_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($instance === null
|
|
|
|
|
|| (string) $instance->status !== 'deleted') {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$reference = CloudVpcPort::query()
|
|
|
|
|
->where('id', (int) $candidate->port_id)
|
|
|
|
|
->where('instance_id', (int) $instance->id)
|
|
|
|
|
->where('active_claim', 1)->first();
|
|
|
|
|
if ($reference === null
|
|
|
|
|
|| (string) $reference->state
|
|
|
|
|
!== 'release_pending') {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!self::deletedCleanupRetryReadyLocked(
|
|
|
|
|
$reference, time()
|
|
|
|
|
)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$jobs = self::releaseAfterInstanceDeletedLocked(
|
|
|
|
|
$instance
|
|
|
|
|
);
|
|
|
|
|
return count($jobs);
|
|
|
|
|
}, 3);
|
|
|
|
|
} catch (\Throwable $error) {
|
|
|
|
|
error_log('cloud VPC deleted-instance cleanup unavailable: '
|
|
|
|
|
. get_class($error));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $queued;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function deletedCleanupRetryReadyLocked(
|
|
|
|
|
CloudVpcPort $port,
|
|
|
|
|
int $now
|
|
|
|
|
): bool {
|
|
|
|
|
if (self::activePortJob(
|
|
|
|
|
(int) $port->instance_id,
|
|
|
|
|
(int) $port->id
|
|
|
|
|
) !== null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$states = CloudVpcHostState::query()
|
|
|
|
|
->where('vpc_id', (int) $port->vpc_id)
|
|
|
|
|
->orderBy('host_id')->lockForUpdate()->get();
|
|
|
|
|
if ($states->every(static fn ($state): bool =>
|
|
|
|
|
self::hostReady($state))) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
foreach ($states as $state) {
|
|
|
|
|
if (self::hostReady($state)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (self::knownNotAppliedHostFailureLocked(
|
|
|
|
|
$state, $now - self::DELETED_CLEANUP_RETRY_DELAY
|
|
|
|
|
) === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the exact terminal generation only when it proves that no host
|
|
|
|
|
* mutation was applied. A failed UI state alone is never replay evidence.
|
|
|
|
|
*/
|
|
|
|
|
private static function knownNotAppliedHostFailureLocked(
|
|
|
|
|
CloudVpcHostState $state,
|
|
|
|
|
?int $completedNoLaterThan = null
|
|
|
|
|
): ?CloudJob {
|
|
|
|
|
if ((string) $state->sync_state !== 'failed'
|
|
|
|
|
|| !is_string($state->last_operation_id)
|
|
|
|
|
|| $state->last_operation_id === '') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$job = CloudJob::query()
|
|
|
|
|
->where('operation_id', (string) $state->last_operation_id)
|
|
|
|
|
->where('vpc_id', (int) $state->vpc_id)
|
|
|
|
|
->where('host_id', (int) $state->host_id)
|
|
|
|
|
->whereIn('kind', ['vpc_host_apply', 'vpc_host_remove'])
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($job === null
|
|
|
|
|
|| (string) $job->phase !== 'terminal'
|
|
|
|
|
|| (string) $job->outcome !== 'failed'
|
|
|
|
|
|| (string) $job->status !== 'failed'
|
|
|
|
|
|| (string) $job->effect_resolution !== 'not_applied'
|
|
|
|
|
|| (bool) $job->effect_may_have_applied
|
|
|
|
|
|| !(bool) $job->safe_to_resubmit
|
|
|
|
|
|| (bool) $job->finalization_incident) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if ($completedNoLaterThan !== null
|
|
|
|
|
&& max((int) ($job->terminal_at ?? 0),
|
|
|
|
|
(int) ($job->updated_at ?? 0)) > $completedNoLaterThan) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Trusted worker marker for VPC jobs that must finish after acceptance. */
|
|
|
|
|
public static function isSystemJob(CloudJob $job, array $payload): bool
|
|
|
|
|
{
|
|
|
|
|
@ -612,7 +794,20 @@ final class CloudVpcPortService
|
|
|
|
|
}
|
|
|
|
|
foreach ($states as $state) {
|
|
|
|
|
if (!self::hostReady($state)) {
|
|
|
|
|
throw new CloudError('旧 VPC 宿主机投影尚未收敛,不能清理', 409);
|
|
|
|
|
if (self::knownNotAppliedHostFailureLocked($state) === null) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'旧 VPC 宿主机投影尚未收敛,不能清理', 409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$hasAppliedProjection = (int) $state->applied_revision > 0
|
|
|
|
|
&& preg_match('/^[a-f0-9]{64}$/D',
|
|
|
|
|
(string) $state->applied_digest) === 1;
|
|
|
|
|
if (!$hasAppliedProjection) {
|
|
|
|
|
// A known-not-applied first projection has no host state
|
|
|
|
|
// to remove. Dropping only this failed intent is safe.
|
|
|
|
|
$state->delete();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$state->sync_state = 'pending';
|
|
|
|
|
$state->last_error_code = '';
|
|
|
|
|
@ -671,6 +866,11 @@ final class CloudVpcPortService
|
|
|
|
|
if ((string) $state->sync_state !== 'failed') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (self::knownNotAppliedHostFailureLocked($state) === null) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'VPC 路由失败结果尚未证明未生效,不能重试', 409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$projection = $projections[(int) $state->host_id] ?? null;
|
|
|
|
|
if ($projection !== null
|
|
|
|
|
&& hash_equals((string) $state->desired_digest,
|
|
|
|
|
@ -707,6 +907,11 @@ final class CloudVpcPortService
|
|
|
|
|
if ((string) $state->sync_state !== 'failed') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (self::knownNotAppliedHostFailureLocked($state) === null) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'VPC 清理失败结果尚未证明未生效,不能重试', 409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Rebuild the desired topology. A state absent from the new
|
|
|
|
|
// projection is a remove retry; otherwise queue the exact apply.
|
|
|
|
|
$ports = CloudVpcPort::query()->where('vpc_id', (int) $vpc->id)
|
|
|
|
|
|