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.
534 lines
21 KiB
PHP
534 lines
21 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudPublicNetworkTransition;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/**
|
|
* Persists authenticated Agent evidence without settling network ownership.
|
|
*
|
|
* This boundary may quarantine prepared target claims as uncertain, but it
|
|
* never activates a target, releases a source, refunds an invoice or publishes
|
|
* a terminal transition. Those effects remain in the settlement workflow.
|
|
*/
|
|
final class CloudPublicNetworkTransitionEvidence
|
|
{
|
|
public const RECOVERY_PREPARE_PENDING = 'recovery_prepare_pending';
|
|
|
|
/** Publish the dispatch boundary without consulting mutable catalogs. */
|
|
public static function markDispatched(CloudJob $claimed): void
|
|
{
|
|
if ($claimed->public_network_transition_id === null) {
|
|
return;
|
|
}
|
|
CloudDispatchLedger::assertValid($claimed);
|
|
if ((string) $claimed->phase !== 'dispatching'
|
|
|| (int) $claimed->attempts !== 1
|
|
|| (int) $claimed->dispatch_attempts !== 1
|
|
|| $claimed->dispatched_at === null
|
|
|| $claimed->receipt_deadline_at === null) {
|
|
throw new CloudError('公网网络变更尚未跨越派发边界', 409);
|
|
}
|
|
$updated = CloudPublicNetworkTransition::where(
|
|
'id',
|
|
(int) $claimed->public_network_transition_id
|
|
)->where('operation_id', (string) $claimed->operation_id)
|
|
->where('instance_id', (int) $claimed->instance_id)
|
|
->where('host_id', (int) $claimed->host_id)
|
|
->where('state', 'queued')
|
|
->where('effect_resolution', 'not_applicable')
|
|
->where('active_transition', 1)
|
|
->update([
|
|
'state' => 'awaiting_receipt',
|
|
'updated_at' => time(),
|
|
]);
|
|
if ($updated === 1) {
|
|
return;
|
|
}
|
|
$transition = CloudPublicNetworkTransition::find(
|
|
(int) $claimed->public_network_transition_id
|
|
);
|
|
if ($transition instanceof CloudPublicNetworkTransition
|
|
&& (string) $transition->state === 'awaiting_receipt'
|
|
&& (string) $transition->effect_resolution === 'not_applicable'
|
|
&& (int) $transition->active_transition === 1
|
|
&& hash_equals(
|
|
(string) $transition->operation_id,
|
|
(string) $claimed->operation_id
|
|
)) {
|
|
return;
|
|
}
|
|
throw new CloudError('公网网络变更派发账本不一致', 409);
|
|
}
|
|
|
|
/** Fail closed when Worker can no longer classify the Agent effect. */
|
|
public static function markJobUncertain(CloudJob $claimed): void
|
|
{
|
|
if ($claimed->public_network_transition_id === null) {
|
|
return;
|
|
}
|
|
$payload = self::recoveryPayload($claimed);
|
|
$parentId = (int) ($claimed->parent_job_id ?? 0);
|
|
DB::connection()->transaction(static function () use (
|
|
$claimed,
|
|
$payload,
|
|
$parentId
|
|
): void {
|
|
$actor = User::where('id', (int) $claimed->user_id)
|
|
->lockForUpdate()->first();
|
|
$parent = $parentId > 0
|
|
? CloudJob::where('id', $parentId)
|
|
->lockForUpdate()->first()
|
|
: null;
|
|
$job = CloudJob::where('id', (int) $claimed->id)
|
|
->lockForUpdate()->first();
|
|
if (!$actor instanceof User || !$job instanceof CloudJob) {
|
|
throw new CloudError('公网网络变更未知效果账户缺失', 409);
|
|
}
|
|
if ((int) $job->user_id !== (int) $claimed->user_id
|
|
|| ($parentId > 0 && !$parent instanceof CloudJob)
|
|
|| (int) ($job->parent_job_id ?? 0) !== $parentId) {
|
|
throw new CloudError('公网网络变更父任务关联不一致', 409);
|
|
}
|
|
$instance = CloudInstance::where('id', (int) $job->instance_id)
|
|
->lockForUpdate()->first();
|
|
CloudPublicNetworkTransitionContext::lockHostBoundary(
|
|
(int) ($job->host_id ?? 0)
|
|
);
|
|
$transition = CloudPublicNetworkTransition::where(
|
|
'id',
|
|
(int) $job->public_network_transition_id
|
|
)->lockForUpdate()->first();
|
|
if (!$instance instanceof CloudInstance
|
|
|| !$transition instanceof CloudPublicNetworkTransition) {
|
|
throw new CloudError('公网网络变更未知效果账本缺失', 409);
|
|
}
|
|
[$source, $target] =
|
|
CloudPublicNetworkTransitionContext::assertAssociation(
|
|
$job,
|
|
$instance,
|
|
$transition,
|
|
$payload,
|
|
true
|
|
);
|
|
if ((string) $transition->state === 'finalization_pending'
|
|
|| in_array((string) $transition->state,
|
|
['succeeded', 'failed'], true)) {
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
$resolution = (string) $transition->effect_resolution;
|
|
$allowed = (string) $transition->state
|
|
=== 'finalization_pending'
|
|
? ['prepared', $resolution]
|
|
: [$resolution];
|
|
if (!in_array($ledger['projection'], $allowed, true)) {
|
|
throw new CloudError(
|
|
'公网网络变更持久证据与资源投影不一致',
|
|
409
|
|
);
|
|
}
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked(
|
|
$parent,
|
|
$job,
|
|
$transition
|
|
);
|
|
return;
|
|
}
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
if ($ledger['projection'] !== 'prepared') {
|
|
throw new CloudError('公网网络变更未知效果投影不一致', 409);
|
|
}
|
|
$now = time();
|
|
foreach ($ledger['legs'] as $family => $leg) {
|
|
if (!(bool) $leg->changed) {
|
|
continue;
|
|
}
|
|
$claim = $ledger['target_claims'][$family];
|
|
$ip = $ledger['target_ips'][$family];
|
|
DB::table('cloud_resource_reservation_ips')
|
|
->where('id', (int) $claim->id)->update([
|
|
'state' => 'uncertain',
|
|
'updated_at' => $now,
|
|
]);
|
|
$ip->state = 'uncertain';
|
|
$ip->updated_at = $now;
|
|
$ip->save();
|
|
}
|
|
CloudPublicNetworkTransitionContext::assertInvoiceAssociation(
|
|
$transition,
|
|
$job,
|
|
$actor,
|
|
$payload,
|
|
false
|
|
);
|
|
$transition->state = 'effect_unknown';
|
|
$transition->effect_resolution = 'unknown';
|
|
$transition->effect_resolved_at = null;
|
|
$transition->last_error_code = 'local_finalization_failed';
|
|
$transition->updated_at = $now;
|
|
$transition->save();
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked($parent, $job, $transition);
|
|
}, 3);
|
|
}
|
|
|
|
/**
|
|
* Persist authenticated Agent evidence before local ledger finalization.
|
|
*/
|
|
public static function record(
|
|
CloudJob $claimed,
|
|
string $catalogStatus,
|
|
mixed $result,
|
|
array $payload,
|
|
array $operation,
|
|
bool $recoveryPrepare = false
|
|
): void {
|
|
if ($claimed->public_network_transition_id === null) {
|
|
return;
|
|
}
|
|
$resolution = match ($catalogStatus) {
|
|
'succeeded' => 'applied',
|
|
'failed' => 'not_applied',
|
|
'uncertain' => 'unknown',
|
|
default => throw new CloudError(
|
|
'公网网络变更回执状态无效',
|
|
409
|
|
),
|
|
};
|
|
$parentId = (int) ($claimed->parent_job_id ?? 0);
|
|
DB::connection()->transaction(static function () use (
|
|
$claimed,
|
|
$resolution,
|
|
$result,
|
|
$payload,
|
|
$operation,
|
|
$recoveryPrepare,
|
|
$parentId
|
|
): void {
|
|
$actor = User::where('id', (int) $claimed->user_id)
|
|
->lockForUpdate()->first();
|
|
$parent = $parentId > 0
|
|
? CloudJob::where('id', $parentId)
|
|
->lockForUpdate()->first()
|
|
: null;
|
|
$job = CloudJob::where('id', (int) $claimed->id)
|
|
->lockForUpdate()->first();
|
|
if (!$actor instanceof User || !$job instanceof CloudJob) {
|
|
throw new CloudError('公网网络变更任务账户不存在', 409);
|
|
}
|
|
if ((int) $job->user_id !== (int) $claimed->user_id
|
|
|| ($parentId > 0 && !$parent instanceof CloudJob)
|
|
|| (int) ($job->parent_job_id ?? 0) !== $parentId) {
|
|
throw new CloudError('公网网络变更父任务关联不一致', 409);
|
|
}
|
|
$sameLease = is_string($claimed->lease_token)
|
|
&& is_string($job->lease_token)
|
|
&& hash_equals(
|
|
(string) $claimed->lease_token,
|
|
(string) $job->lease_token
|
|
);
|
|
$recoveryTerminal = $recoveryPrepare
|
|
&& (string) $job->phase === 'terminal'
|
|
&& (string) $job->effect_resolution === 'unknown';
|
|
if (!hash_equals(
|
|
(string) $claimed->operation_id,
|
|
(string) $job->operation_id
|
|
)
|
|
|| (!$sameLease && !$recoveryTerminal)
|
|
|| (!$recoveryTerminal && (string) $job->phase === 'terminal')) {
|
|
throw new CloudError('公网网络变更回执租约已失效', 409);
|
|
}
|
|
$instance = CloudInstance::where('id', (int) $job->instance_id)
|
|
->lockForUpdate()->first();
|
|
CloudPublicNetworkTransitionContext::lockHostBoundary(
|
|
(int) ($job->host_id ?? 0)
|
|
);
|
|
$transition = CloudPublicNetworkTransition::where(
|
|
'id',
|
|
(int) $job->public_network_transition_id
|
|
)->lockForUpdate()->first();
|
|
if (!$instance instanceof CloudInstance
|
|
|| !$transition instanceof CloudPublicNetworkTransition) {
|
|
throw new CloudError('公网网络变更冻结账本缺失', 409);
|
|
}
|
|
[$source, $target] =
|
|
CloudPublicNetworkTransitionContext::assertAssociation(
|
|
$job,
|
|
$instance,
|
|
$transition,
|
|
$payload,
|
|
true
|
|
);
|
|
if (in_array((string) $transition->state,
|
|
['succeeded', 'failed'], true)) {
|
|
if (!hash_equals(
|
|
(string) $transition->effect_resolution,
|
|
$resolution
|
|
)) {
|
|
throw new CloudError(
|
|
'公网网络变更已有冲突的终态证据',
|
|
409
|
|
);
|
|
}
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
if ($ledger['projection'] !== $resolution) {
|
|
throw new CloudError(
|
|
'公网网络变更终态资源投影不一致',
|
|
409
|
|
);
|
|
}
|
|
CloudPublicNetworkTransitionContext::assertInvoiceAssociation(
|
|
$transition,
|
|
$job,
|
|
$actor,
|
|
$payload,
|
|
$resolution === 'not_applied'
|
|
);
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked(
|
|
$parent,
|
|
$job,
|
|
$transition
|
|
);
|
|
return;
|
|
}
|
|
if ((string) $transition->state === 'finalization_pending') {
|
|
if ($resolution === 'unknown'
|
|
|| !hash_equals(
|
|
(string) $transition->effect_resolution,
|
|
$resolution
|
|
)) {
|
|
throw new CloudError(
|
|
'公网网络变更已有冲突的最终化证据',
|
|
409
|
|
);
|
|
}
|
|
if ($resolution === 'applied') {
|
|
self::assertAppliedResult($instance, $target, $result);
|
|
}
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
if (!in_array($ledger['projection'],
|
|
['prepared', $resolution], true)) {
|
|
throw new CloudError(
|
|
'公网网络变更待最终化投影不一致',
|
|
409
|
|
);
|
|
}
|
|
CloudPublicNetworkTransitionContext::assertInvoiceAssociation(
|
|
$transition,
|
|
$job,
|
|
$actor,
|
|
$payload,
|
|
false
|
|
);
|
|
if ($recoveryPrepare
|
|
&& !hash_equals(
|
|
(string) $transition->last_error_code,
|
|
self::RECOVERY_PREPARE_PENDING
|
|
)) {
|
|
$transition->last_error_code =
|
|
self::RECOVERY_PREPARE_PENDING;
|
|
$transition->updated_at = time();
|
|
$transition->save();
|
|
}
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked(
|
|
$parent,
|
|
$job,
|
|
$transition
|
|
);
|
|
return;
|
|
}
|
|
if (!in_array((string) $transition->state, [
|
|
'reserved', 'queued', 'awaiting_receipt', 'effect_unknown',
|
|
], true)) {
|
|
throw new CloudError('公网网络变更状态不允许记录回执', 409);
|
|
}
|
|
if ((string) $transition->state === 'effect_unknown'
|
|
&& $resolution === 'unknown') {
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
if ($ledger['projection'] !== 'prepared') {
|
|
throw new CloudError(
|
|
'公网网络变更未知效果资源投影不一致',
|
|
409
|
|
);
|
|
}
|
|
foreach ($ledger['legs'] as $family => $leg) {
|
|
if ((bool) $leg->changed
|
|
&& ((string) $ledger['target_claims'][$family]->state
|
|
!== 'uncertain'
|
|
|| (string) $ledger['target_ips'][$family]->state
|
|
!== 'uncertain')) {
|
|
throw new CloudError(
|
|
'公网网络变更未知效果目标未锁定',
|
|
409
|
|
);
|
|
}
|
|
}
|
|
CloudPublicNetworkTransitionContext::assertInvoiceAssociation(
|
|
$transition,
|
|
$job,
|
|
$actor,
|
|
$payload,
|
|
false
|
|
);
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked(
|
|
$parent,
|
|
$job,
|
|
$transition
|
|
);
|
|
return;
|
|
}
|
|
|
|
$ledger = CloudPublicNetworkTransitionContext::lockLedger(
|
|
$instance,
|
|
$transition,
|
|
$source,
|
|
$target
|
|
);
|
|
if ($ledger['projection'] !== 'prepared') {
|
|
throw new CloudError(
|
|
'公网网络变更回执前资源账本已发生变化',
|
|
409
|
|
);
|
|
}
|
|
CloudPublicNetworkTransitionContext::assertInvoiceAssociation(
|
|
$transition,
|
|
$job,
|
|
$actor,
|
|
$payload,
|
|
false
|
|
);
|
|
$now = time();
|
|
if ($resolution === 'unknown') {
|
|
foreach ($ledger['legs'] as $family => $leg) {
|
|
if (!(bool) $leg->changed) {
|
|
continue;
|
|
}
|
|
$claim = $ledger['target_claims'][$family];
|
|
$ip = $ledger['target_ips'][$family];
|
|
DB::table('cloud_resource_reservation_ips')
|
|
->where('id', (int) $claim->id)->update([
|
|
'state' => 'uncertain',
|
|
'updated_at' => $now,
|
|
]);
|
|
$ip->state = 'uncertain';
|
|
$ip->updated_at = $now;
|
|
$ip->save();
|
|
}
|
|
$transition->state = 'effect_unknown';
|
|
$transition->effect_resolution = 'unknown';
|
|
$transition->effect_resolved_at = null;
|
|
} else {
|
|
if ($resolution === 'applied') {
|
|
self::assertAppliedResult($instance, $target, $result);
|
|
}
|
|
$transition->state = 'finalization_pending';
|
|
$transition->effect_resolution = $resolution;
|
|
$transition->effect_resolved_at = $now;
|
|
}
|
|
$transition->last_error_code = $recoveryPrepare
|
|
? self::RECOVERY_PREPARE_PENDING
|
|
: self::errorCode($operation);
|
|
$transition->updated_at = $now;
|
|
$transition->save();
|
|
CloudPublicIpPoolReplacementBatchService::
|
|
projectTransitionEvidenceLocked($parent, $job, $transition);
|
|
}, 3);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public static function recoveryPayload(CloudJob $job): array
|
|
{
|
|
try {
|
|
$payload = CloudSecrets::decrypt(
|
|
(string) $job->sealed_payload,
|
|
'job-payload'
|
|
);
|
|
} catch (\Throwable) {
|
|
throw new CloudRecoveryPayloadUnavailable(
|
|
'公网网络变更恢复载荷无法解密',
|
|
409
|
|
);
|
|
}
|
|
if (!is_array($payload) || array_is_list($payload)) {
|
|
throw new CloudRecoveryPayloadUnavailable(
|
|
'公网网络变更恢复载荷无效',
|
|
409
|
|
);
|
|
}
|
|
return $payload;
|
|
}
|
|
|
|
private static function assertAppliedResult(
|
|
CloudInstance $instance,
|
|
CloudPublicNetworkSnapshot $target,
|
|
mixed $result
|
|
): void {
|
|
$vm = is_array($result) && is_array($result['vm'] ?? null)
|
|
? $result['vm'] : $result;
|
|
try {
|
|
$confirmed = is_array($vm)
|
|
? CloudPublicNetworkSnapshot::fromArray(
|
|
$vm['public_network'] ?? null
|
|
) : null;
|
|
} catch (\Throwable) {
|
|
$confirmed = null;
|
|
}
|
|
if (!is_array($vm) || ($vm['network_changed'] ?? null) !== true
|
|
|| !is_string($vm['uuid'] ?? null)
|
|
|| !hash_equals(
|
|
strtolower((string) $instance->uuid),
|
|
strtolower((string) $vm['uuid'])
|
|
)
|
|
|| !$confirmed instanceof CloudPublicNetworkSnapshot
|
|
|| !hash_equals(
|
|
$target->fingerprint(),
|
|
$confirmed->fingerprint()
|
|
)) {
|
|
throw new CloudError(
|
|
'Agent 成功结果未确认精确的双栈公网目标',
|
|
502
|
|
);
|
|
}
|
|
}
|
|
|
|
private static function errorCode(array $operation): string
|
|
{
|
|
$code = is_string($operation['error_code'] ?? null)
|
|
? strtolower(trim($operation['error_code'])) : '';
|
|
return preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $code) === 1
|
|
? $code : '';
|
|
}
|
|
}
|