|
|
|
|
@ -948,16 +948,12 @@ final class CloudVpcService
|
|
|
|
|
if ($locked === null) {
|
|
|
|
|
throw new CloudError('VPC 不存在', 404);
|
|
|
|
|
}
|
|
|
|
|
$runtimeRetryableSync = in_array(
|
|
|
|
|
$configurationRetryableSync = in_array(
|
|
|
|
|
(string) $locked->sync_state, ['waiting', 'uncertain'], true
|
|
|
|
|
) || ((string) $locked->sync_state === 'applied'
|
|
|
|
|
&& in_array((string) ($locked->last_error_code ?? ''), [
|
|
|
|
|
'vpc_private_guest_shutdown_pending',
|
|
|
|
|
'vpc_private_guest_stopped',
|
|
|
|
|
], true));
|
|
|
|
|
$runtimeObservationPending = $allowDeferredRetry
|
|
|
|
|
);
|
|
|
|
|
$configurationObservationPending = $allowDeferredRetry
|
|
|
|
|
&& (string) $locked->state === 'degraded'
|
|
|
|
|
&& $runtimeRetryableSync
|
|
|
|
|
&& $configurationRetryableSync
|
|
|
|
|
&& (int) $locked->desired_revision === (int) $locked->observed_revision
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) $locked->desired_digest,
|
|
|
|
|
@ -965,7 +961,8 @@ final class CloudVpcService
|
|
|
|
|
);
|
|
|
|
|
$operation = CloudVpcOperation::where('vpc_id', $locked->id)
|
|
|
|
|
->where(function ($query) use (
|
|
|
|
|
$allowDeferredRetry, $allowFailedRetry, $runtimeObservationPending, $locked
|
|
|
|
|
$allowDeferredRetry, $allowFailedRetry,
|
|
|
|
|
$configurationObservationPending, $locked
|
|
|
|
|
): void {
|
|
|
|
|
$query->whereIn('status', $allowDeferredRetry
|
|
|
|
|
? ['queued', 'running', 'uncertain', 'waiting_observed']
|
|
|
|
|
@ -994,13 +991,13 @@ final class CloudVpcService
|
|
|
|
|
->where('desired_revision',
|
|
|
|
|
(int) $locked->desired_revision);
|
|
|
|
|
});
|
|
|
|
|
if ($runtimeObservationPending) {
|
|
|
|
|
// Member removal may already be durably committed
|
|
|
|
|
// while a surviving VM still lacks exact guest
|
|
|
|
|
// evidence. Re-run only this root's current,
|
|
|
|
|
// idempotent target until runtime proof catches up.
|
|
|
|
|
$query->orWhere(function ($runtime) use ($locked): void {
|
|
|
|
|
$runtime->where('status', 'succeeded')
|
|
|
|
|
if ($configurationObservationPending) {
|
|
|
|
|
// A succeeded operation may need another exact host
|
|
|
|
|
// configuration readback after a later observation
|
|
|
|
|
// became uncertain. Guest runtime health alone never
|
|
|
|
|
// reopens the immutable apply operation.
|
|
|
|
|
$query->orWhere(function ($configuration) use ($locked): void {
|
|
|
|
|
$configuration->where('status', 'succeeded')
|
|
|
|
|
->where('kind', '!=', 'vpc_delete')
|
|
|
|
|
->where('evidence_version', '>=',
|
|
|
|
|
self::OBSERVATION_EVIDENCE_VERSION)
|
|
|
|
|
@ -1042,10 +1039,10 @@ final class CloudVpcService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$now = time();
|
|
|
|
|
// Runtime-only refreshes intentionally keep a completed operation
|
|
|
|
|
// terminal while borrowing its lease. The lease, rather than the
|
|
|
|
|
// status string, is therefore the concurrency boundary for both
|
|
|
|
|
// ordinary applies and background observation refreshes.
|
|
|
|
|
// Configuration readback refreshes intentionally keep a completed
|
|
|
|
|
// operation terminal while borrowing its lease. The lease, rather
|
|
|
|
|
// than the status string, is the concurrency boundary for both
|
|
|
|
|
// ordinary applies and exact observation refreshes.
|
|
|
|
|
if ((int) $operation->lease_until > $now) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
@ -1079,10 +1076,10 @@ final class CloudVpcService
|
|
|
|
|
$locked->save();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$runtimeProofRefresh = (string) $operation->status === 'succeeded'
|
|
|
|
|
$configurationProofRefresh = (string) $operation->status === 'succeeded'
|
|
|
|
|
&& (int) ($operation->evidence_version ?? 0)
|
|
|
|
|
>= self::OBSERVATION_EVIDENCE_VERSION;
|
|
|
|
|
if (!$runtimeProofRefresh) {
|
|
|
|
|
if (!$configurationProofRefresh) {
|
|
|
|
|
$operation->status = 'running';
|
|
|
|
|
$operation->finished_at = null;
|
|
|
|
|
}
|
|
|
|
|
@ -1091,12 +1088,12 @@ final class CloudVpcService
|
|
|
|
|
$operation->attempts = (int) $operation->attempts + 1;
|
|
|
|
|
$operation->updated_at = $now;
|
|
|
|
|
$operation->save();
|
|
|
|
|
return [$operation, $payload, $runtimeProofRefresh, $previousOutcomePending];
|
|
|
|
|
return [$operation, $payload, $configurationProofRefresh, $previousOutcomePending];
|
|
|
|
|
}, 3);
|
|
|
|
|
if ($claim === null) {
|
|
|
|
|
return self::show($vpcKey, $actor);
|
|
|
|
|
}
|
|
|
|
|
[$operation, $payload, $runtimeProofRefresh, $previousOutcomePending] = $claim;
|
|
|
|
|
[$operation, $payload, $configurationProofRefresh, $previousOutcomePending] = $claim;
|
|
|
|
|
$leaseToken = (string) $operation->lease_token;
|
|
|
|
|
// Only the explicit user reconcile route may transfer an exact,
|
|
|
|
|
// childless retained Engine root for this immutable target. Keep the
|
|
|
|
|
@ -1171,7 +1168,7 @@ final class CloudVpcService
|
|
|
|
|
$connection = (new CloudVpc())->getConnection();
|
|
|
|
|
$connection->transaction(function () use (
|
|
|
|
|
$operation, $leaseToken, $vpc, $status, $error, $now,
|
|
|
|
|
$runtimeProofRefresh
|
|
|
|
|
$configurationProofRefresh
|
|
|
|
|
): void {
|
|
|
|
|
// Keep the lock order identical to every mutator: VPC root
|
|
|
|
|
// first, then its operation row. This also prevents an old
|
|
|
|
|
@ -1181,14 +1178,14 @@ final class CloudVpcService
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$row = CloudVpcOperation::where('operation_id', (string) $operation->operation_id)
|
|
|
|
|
->where('status', $runtimeProofRefresh ? 'succeeded' : 'running')
|
|
|
|
|
->where('status', $configurationProofRefresh ? 'succeeded' : 'running')
|
|
|
|
|
->where('lease_token', $leaseToken)->lockForUpdate()->first();
|
|
|
|
|
if ($row === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($runtimeProofRefresh) {
|
|
|
|
|
// A runtime-only poll must not erase the last successful
|
|
|
|
|
// configuration proof when transport is temporarily
|
|
|
|
|
if ($configurationProofRefresh) {
|
|
|
|
|
// A configuration readback refresh must not erase the last
|
|
|
|
|
// successful proof when transport is temporarily
|
|
|
|
|
// unavailable. Release the lease and retry later.
|
|
|
|
|
$row->lease_token = null;
|
|
|
|
|
$row->lease_until = 0;
|
|
|
|
|
@ -1681,6 +1678,37 @@ final class CloudVpcService
|
|
|
|
|
$expectedVpcCidr = null;
|
|
|
|
|
$desiredRoutes = [];
|
|
|
|
|
$desiredContractValid = true;
|
|
|
|
|
$desiredHostClosure = null;
|
|
|
|
|
$normalizeHostClosure = static function ($rows): ?array {
|
|
|
|
|
if (!is_array($rows) || !array_is_list($rows)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$closure = [];
|
|
|
|
|
foreach ($rows as $host) {
|
|
|
|
|
if (!is_array($host)
|
|
|
|
|
|| !is_string($host['host_id'] ?? null)
|
|
|
|
|
|| preg_match('/^[1-9][0-9]*$/D', $host['host_id']) !== 1
|
|
|
|
|
|| isset($closure[$host['host_id']])
|
|
|
|
|
|| !is_array($host['members'] ?? null)
|
|
|
|
|
|| !array_is_list($host['members'])) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$members = [];
|
|
|
|
|
foreach ($host['members'] as $memberId) {
|
|
|
|
|
if (!is_string($memberId)
|
|
|
|
|
|| preg_match(self::KEY_PATTERN, $memberId) !== 1
|
|
|
|
|
|| isset($members[$memberId])) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$members[$memberId] = true;
|
|
|
|
|
}
|
|
|
|
|
$memberIds = array_keys($members);
|
|
|
|
|
sort($memberIds, SORT_STRING);
|
|
|
|
|
$closure[$host['host_id']] = $memberIds;
|
|
|
|
|
}
|
|
|
|
|
ksort($closure, SORT_STRING);
|
|
|
|
|
return $closure;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
$desired = self::desiredDocument($vpc);
|
|
|
|
|
$expectedVpcCidr = self::cidr(
|
|
|
|
|
@ -1709,6 +1737,7 @@ final class CloudVpcService
|
|
|
|
|
$expectedDestinations[] = (string) $member['private_ip_cidr'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$desiredHostClosure = $normalizeHostClosure($desired['hosts'] ?? null);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
// A malformed desired ledger is itself a closed gate.
|
|
|
|
|
$desiredContractValid = false;
|
|
|
|
|
@ -1717,6 +1746,7 @@ final class CloudVpcService
|
|
|
|
|
$expectedDestinations = [];
|
|
|
|
|
$expectedVpcCidr = null;
|
|
|
|
|
$desiredRoutes = [];
|
|
|
|
|
$desiredHostClosure = null;
|
|
|
|
|
}
|
|
|
|
|
$expectedDestinations = array_values(array_unique(array_filter(
|
|
|
|
|
$expectedDestinations,
|
|
|
|
|
@ -1997,6 +2027,40 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
$liveRequired = $required->filter(static fn ($row): bool =>
|
|
|
|
|
(string) $row->state !== 'deleting');
|
|
|
|
|
$expectedLiveHostIds = array_map(
|
|
|
|
|
'strval', array_keys($expectedMembersByHost)
|
|
|
|
|
);
|
|
|
|
|
$liveRequiredHostIds = $liveRequired->keys()->map(
|
|
|
|
|
static fn ($hostId): string => (string) $hostId
|
|
|
|
|
)->values()->all();
|
|
|
|
|
$requiredHostIds = $required->keys()->map(
|
|
|
|
|
static fn ($hostId): string => (string) $hostId
|
|
|
|
|
)->values()->all();
|
|
|
|
|
sort($expectedLiveHostIds, SORT_STRING);
|
|
|
|
|
sort($liveRequiredHostIds, SORT_STRING);
|
|
|
|
|
sort($requiredHostIds, SORT_STRING);
|
|
|
|
|
$desiredHostIds = is_array($desiredHostClosure)
|
|
|
|
|
? array_map('strval', array_keys($desiredHostClosure)) : [];
|
|
|
|
|
sort($desiredHostIds, SORT_STRING);
|
|
|
|
|
$expectedMemberClosure = [];
|
|
|
|
|
foreach ($expectedMembersByHost as $hostId => $members) {
|
|
|
|
|
$memberIds = array_keys($members);
|
|
|
|
|
sort($memberIds, SORT_STRING);
|
|
|
|
|
$expectedMemberClosure[(string) $hostId] = $memberIds;
|
|
|
|
|
}
|
|
|
|
|
ksort($expectedMemberClosure, SORT_STRING);
|
|
|
|
|
$hostClosureExact = $desiredContractValid
|
|
|
|
|
&& is_array($desiredHostClosure)
|
|
|
|
|
&& $desiredHostIds === $requiredHostIds
|
|
|
|
|
&& $expectedLiveHostIds === $liveRequiredHostIds;
|
|
|
|
|
if ($hostClosureExact) {
|
|
|
|
|
foreach ($expectedMemberClosure as $hostId => $memberIds) {
|
|
|
|
|
if (($desiredHostClosure[$hostId] ?? null) !== $memberIds) {
|
|
|
|
|
$hostClosureExact = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// A Fabric or host control-plane digest cannot prove that a
|
|
|
|
|
// deleting private interface/address is gone. The engine reaches
|
|
|
|
|
// an exact host observation only after consuming every durable
|
|
|
|
|
@ -2026,6 +2090,10 @@ final class CloudVpcService
|
|
|
|
|
// inexact merely because both appear in the same operation.
|
|
|
|
|
$allRequiredHostsExact = $required->count() > 0;
|
|
|
|
|
$allRequiredHostsConfigExact = $required->count() > 0;
|
|
|
|
|
if (!$hostClosureExact) {
|
|
|
|
|
$allRequiredHostsExact = false;
|
|
|
|
|
$allRequiredHostsConfigExact = false;
|
|
|
|
|
}
|
|
|
|
|
$allRemovalProof = true;
|
|
|
|
|
$hasRemovalTarget = false;
|
|
|
|
|
$currentConfigExactHostIds = [];
|
|
|
|
|
@ -2175,8 +2243,8 @@ final class CloudVpcService
|
|
|
|
|
} elseif (!$deleting) {
|
|
|
|
|
$hostError = is_string($evidence['last_error_code'] ?? null)
|
|
|
|
|
? (string) $evidence['last_error_code'] : null;
|
|
|
|
|
$deployment->state = $active ? 'active' : 'degraded';
|
|
|
|
|
$deployment->last_error_code = $active ? null
|
|
|
|
|
$deployment->state = $configActive ? 'active' : 'degraded';
|
|
|
|
|
$deployment->last_error_code = $configActive ? null
|
|
|
|
|
: ($hostError ?? 'vpc_host_observation_mismatch');
|
|
|
|
|
} else {
|
|
|
|
|
$deployment->last_error_code = $removalActive
|
|
|
|
|
@ -2184,7 +2252,7 @@ final class CloudVpcService
|
|
|
|
|
: ($evidence['last_error_code'] ?? 'vpc_host_removal_unverified');
|
|
|
|
|
}
|
|
|
|
|
$deployment->updated_at = time();
|
|
|
|
|
if ($active || $removalActive) {
|
|
|
|
|
if ($configActive || $removalActive) {
|
|
|
|
|
$deployment->last_success_at = time();
|
|
|
|
|
} else {
|
|
|
|
|
$deployment->last_failure_at = time();
|
|
|
|
|
@ -2373,6 +2441,7 @@ final class CloudVpcService
|
|
|
|
|
&& !$pendingChildTombstone;
|
|
|
|
|
$configurationComplete = $allRequiredHostsConfigExact
|
|
|
|
|
&& $allRemovalProof && $liveRequired->count() > 0
|
|
|
|
|
&& $allExpectedMembers !== []
|
|
|
|
|
&& !$pendingChildTombstone;
|
|
|
|
|
$removalOnlyComplete = $hasRemovalTarget && $allRemovalProof
|
|
|
|
|
&& $allRequiredHostsExact && $liveRequired->count() === 0
|
|
|
|
|
@ -2389,7 +2458,7 @@ final class CloudVpcService
|
|
|
|
|
&& $allRequiredHostsConfigExact && $allRemovalProof
|
|
|
|
|
&& !$pendingChildTombstone;
|
|
|
|
|
// Once the detach mutation has advanced the root to this exact
|
|
|
|
|
// revision, later runtime-only retries must not reopen or repeat
|
|
|
|
|
// revision, later readback retries must not reopen or repeat
|
|
|
|
|
// the already released member/IP tombstone. They keep the same
|
|
|
|
|
// operation terminal while retrying guest evidence for siblings.
|
|
|
|
|
$memberDetachPreviouslyCommitted =
|
|
|
|
|
@ -2414,36 +2483,6 @@ final class CloudVpcService
|
|
|
|
|
// Derive it solely from the per-host closed set; mixed reasons
|
|
|
|
|
// intentionally collapse to the generic control-plane code.
|
|
|
|
|
$deferredError = self::singleDeferredEvidenceError($hostEvidence);
|
|
|
|
|
$hasUnresolvedMemberEvidence = false;
|
|
|
|
|
$hasSuspendedMemberEvidence = false;
|
|
|
|
|
$hasStoppingMemberEvidence = false;
|
|
|
|
|
$hasStoppedMemberEvidence = false;
|
|
|
|
|
foreach ($allExpectedMembers as $memberId => $_expectedMember) {
|
|
|
|
|
$runtimeEvidence = $memberGuestEvidence[(string) $memberId] ?? null;
|
|
|
|
|
$runtimeState = is_array($runtimeEvidence)
|
|
|
|
|
? (string) ($runtimeEvidence['state'] ?? '') : '';
|
|
|
|
|
if (!in_array(
|
|
|
|
|
$runtimeState, ['active', 'suspended', 'stopping', 'stopped'], true
|
|
|
|
|
)) {
|
|
|
|
|
$hasUnresolvedMemberEvidence = true;
|
|
|
|
|
} elseif ($runtimeState === 'suspended') {
|
|
|
|
|
$hasSuspendedMemberEvidence = true;
|
|
|
|
|
} elseif ($runtimeState === 'stopping') {
|
|
|
|
|
$hasStoppingMemberEvidence = true;
|
|
|
|
|
} elseif ($runtimeState === 'stopped') {
|
|
|
|
|
$hasStoppedMemberEvidence = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$runtimeObservationPending = $operationSucceeded
|
|
|
|
|
&& $configurationComplete
|
|
|
|
|
&& $liveRequired->count() > 0 && !$allActive
|
|
|
|
|
&& ($hasUnresolvedMemberEvidence || $hasStoppingMemberEvidence
|
|
|
|
|
|| $hasStoppedMemberEvidence);
|
|
|
|
|
$intentionalRuntimeSuspended = $operationSucceeded
|
|
|
|
|
&& $configurationComplete && !$allActive
|
|
|
|
|
&& !$hasUnresolvedMemberEvidence && !$hasStoppingMemberEvidence
|
|
|
|
|
&& !$hasStoppedMemberEvidence
|
|
|
|
|
&& $hasSuspendedMemberEvidence;
|
|
|
|
|
$configurationObservationUncertain = $operationPreviouslySucceeded
|
|
|
|
|
&& !$configurationComplete && $liveRequired->count() > 0;
|
|
|
|
|
// A no-members Fabric gate is terminal only after the database has
|
|
|
|
|
@ -2551,16 +2590,14 @@ final class CloudVpcService
|
|
|
|
|
// a tenant data plane active. It may advance the exact desired
|
|
|
|
|
// revision and finish the operation, but stays degraded/unusable
|
|
|
|
|
// until a live member host passes the Fabric gate.
|
|
|
|
|
$vpc->state = $operationSucceeded && $allActive ? 'active' : 'degraded';
|
|
|
|
|
$vpc->state = $operationSucceeded && $configurationComplete
|
|
|
|
|
? 'active' : 'degraded';
|
|
|
|
|
$vpc->sync_state = $operationSucceeded
|
|
|
|
|
? ($configurationObservationUncertain ? 'uncertain'
|
|
|
|
|
: ($runtimeObservationPending ? 'waiting' : 'applied'))
|
|
|
|
|
? ($configurationObservationUncertain ? 'uncertain' : 'applied')
|
|
|
|
|
: ($dataPlaneDeferred ? 'waiting' : ($terminalFailure ? 'failed' : 'uncertain'));
|
|
|
|
|
$vpc->last_error_code = $operationSucceeded
|
|
|
|
|
? (($runtimeObservationPending || $configurationObservationUncertain)
|
|
|
|
|
? ($deferredError ?? 'vpc_data_plane_deferred')
|
|
|
|
|
: ($intentionalRuntimeSuspended
|
|
|
|
|
? 'vpc_private_network_suspended' : null))
|
|
|
|
|
? ($configurationObservationUncertain
|
|
|
|
|
? ($deferredError ?? 'vpc_data_plane_deferred') : null)
|
|
|
|
|
: ($dataPlaneDeferred ? ($deferredError ?? 'vpc_data_plane_deferred')
|
|
|
|
|
: ($terminalFailure ? $gateError
|
|
|
|
|
: ($deferredError ?? 'vpc_observation_incomplete')));
|
|
|
|
|
@ -3504,7 +3541,7 @@ final class CloudVpcService
|
|
|
|
|
'error_code' => $ok ? null : $state['reason_code']];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Persist the latest VM-side observation without advancing a VPC root. */
|
|
|
|
|
/** Persist VM health; only current persistent-config drift invalidates the root proof. */
|
|
|
|
|
public static function recordPrivateObservation(
|
|
|
|
|
int $instanceId, array $validation, string $operationId
|
|
|
|
|
): bool {
|
|
|
|
|
@ -3518,8 +3555,9 @@ final class CloudVpcService
|
|
|
|
|
|| !$schema->hasTable('cloud_vpcs')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$hasHostDeployments = $schema->hasTable('cloud_vpc_host_deployments');
|
|
|
|
|
return (bool) $connection->transaction(function () use (
|
|
|
|
|
$instanceId, $validation, $operationId
|
|
|
|
|
$instanceId, $validation, $operationId, $hasHostDeployments
|
|
|
|
|
): bool {
|
|
|
|
|
// Match the VPC -> member lock order used by control-plane
|
|
|
|
|
// mutations. A lifecycle readback from an older desired revision
|
|
|
|
|
@ -3534,6 +3572,14 @@ final class CloudVpcService
|
|
|
|
|
if ($vpc === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Keep the same root -> host -> member -> lease lock order used by
|
|
|
|
|
// recordObserved. A lifecycle receipt may invalidate one exact
|
|
|
|
|
// host projection, but must not deadlock with a VPC readback.
|
|
|
|
|
$deployment = $hasHostDeployments
|
|
|
|
|
? CloudVpcHostDeployment::where('vpc_id', (int) $vpc->id)
|
|
|
|
|
->where('host_id', (int) $probe->host_id)
|
|
|
|
|
->lockForUpdate()->first()
|
|
|
|
|
: null;
|
|
|
|
|
$member = CloudVpcMember::where('id', (int) $probe->id)
|
|
|
|
|
->whereNotIn('state', ['deleted'])->lockForUpdate()->first();
|
|
|
|
|
if ($member === null) {
|
|
|
|
|
@ -3563,7 +3609,12 @@ final class CloudVpcService
|
|
|
|
|
$expectedDigest = self::safeDigest($state['desired_digest'] ?? null);
|
|
|
|
|
$memberDigest = self::safeDigest($member->desired_digest);
|
|
|
|
|
if ((int) $member->desired_revision !== $expectedRevision
|
|
|
|
|
|| $memberDigest !== $expectedDigest) {
|
|
|
|
|
|| $memberDigest !== $expectedDigest
|
|
|
|
|
|| (int) $vpc->desired_revision !== $expectedRevision
|
|
|
|
|
|| !self::digestMatches(
|
|
|
|
|
(string) $vpc->desired_digest,
|
|
|
|
|
(string) ($expectedDigest ?? '')
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $vpc->state === 'deleting') {
|
|
|
|
|
@ -3611,6 +3662,38 @@ final class CloudVpcService
|
|
|
|
|
&& (string) ($state['state'] ?? '') === 'stopped';
|
|
|
|
|
$runtimeStopping = $ok && $desired !== 'removed'
|
|
|
|
|
&& (string) ($state['state'] ?? '') === 'stopping';
|
|
|
|
|
// A structured, identity-bound lifecycle readback can disprove
|
|
|
|
|
// the durable host configuration independently of guest/QGA
|
|
|
|
|
// health. Missing observations do not revoke a prior receipt;
|
|
|
|
|
// current XML/config/seed or revision/digest drift does.
|
|
|
|
|
$structuredPersistentEvidence = $desired !== 'removed'
|
|
|
|
|
&& ($state['required'] ?? false) === true
|
|
|
|
|
&& is_string($state['vm_uuid'] ?? null)
|
|
|
|
|
&& preg_match(
|
|
|
|
|
'/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/Di',
|
|
|
|
|
(string) $state['vm_uuid']
|
|
|
|
|
) === 1;
|
|
|
|
|
$expectedMac = self::privateMacForMember(
|
|
|
|
|
(string) $vpc->vpc_key,
|
|
|
|
|
(string) $member->member_key
|
|
|
|
|
);
|
|
|
|
|
$persistentConfigExact = $structuredPersistentEvidence
|
|
|
|
|
&& ($state['interface'] ?? null) === self::PRIVATE_INTERFACE
|
|
|
|
|
&& is_string($expectedMac)
|
|
|
|
|
&& is_string($state['private_mac'] ?? null)
|
|
|
|
|
&& hash_equals($expectedMac, strtolower((string) $state['private_mac']))
|
|
|
|
|
&& (string) ($state['private_ip_cidr'] ?? '')
|
|
|
|
|
=== (string) $member->private_ip . '/32'
|
|
|
|
|
&& ($state['xml_verified'] ?? false) === true
|
|
|
|
|
&& ($state['config_verified'] ?? false) === true
|
|
|
|
|
&& ($state['seed_verified'] ?? false) === true
|
|
|
|
|
&& (int) ($state['observed_revision'] ?? 0) === $expectedRevision
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) ($expectedDigest ?? ''),
|
|
|
|
|
(string) ($state['observed_digest'] ?? '')
|
|
|
|
|
);
|
|
|
|
|
$persistentConfigMismatch = !$ok
|
|
|
|
|
&& $structuredPersistentEvidence && !$persistentConfigExact;
|
|
|
|
|
// Mirror the member outcome into its lease row. A private-IP
|
|
|
|
|
// state left at `deleting` or `active` after readback would make
|
|
|
|
|
// the next expectation disagree with the member ledger.
|
|
|
|
|
@ -3650,103 +3733,42 @@ final class CloudVpcService
|
|
|
|
|
$private->last_error_code = $member->last_error_code;
|
|
|
|
|
$private->updated_at = time();
|
|
|
|
|
$private->save();
|
|
|
|
|
self::refreshVpcRuntimeAggregate($vpc, (int) $member->host_id);
|
|
|
|
|
return true;
|
|
|
|
|
}, 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Refresh host/root runtime state without changing exact config proof. */
|
|
|
|
|
private static function refreshVpcRuntimeAggregate(CloudVpc $vpc, int $hostId): void
|
|
|
|
|
{
|
|
|
|
|
$now = time();
|
|
|
|
|
$host = CloudVpcHostDeployment::where('vpc_id', $vpc->id)
|
|
|
|
|
->where('host_id', $hostId)->lockForUpdate()->first();
|
|
|
|
|
if ($host !== null && (string) $host->state !== 'deleting'
|
|
|
|
|
&& (string) $host->state !== 'deleted') {
|
|
|
|
|
$hostMembers = CloudVpcMember::where('vpc_id', $vpc->id)
|
|
|
|
|
->where('host_id', $hostId)
|
|
|
|
|
->whereNotIn('state', ['deleting', 'deleted'])->get();
|
|
|
|
|
$hostReady = !$hostMembers->isEmpty()
|
|
|
|
|
&& $hostMembers->every(static fn ($row): bool =>
|
|
|
|
|
(string) $row->state === 'active');
|
|
|
|
|
$hostError = null;
|
|
|
|
|
foreach ($hostMembers as $hostMember) {
|
|
|
|
|
if ((string) $hostMember->state === 'active') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$hostError = self::safeErrorCode(
|
|
|
|
|
(string) ($hostMember->last_error_code ?? '')
|
|
|
|
|
) ?? 'vpc_private_observation_missing';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$host->state = $hostReady ? 'active' : 'degraded';
|
|
|
|
|
$host->last_error_code = $hostReady ? null : $hostError;
|
|
|
|
|
$host->updated_at = $now;
|
|
|
|
|
if ($hostReady) {
|
|
|
|
|
$host->last_success_at = $now;
|
|
|
|
|
} else {
|
|
|
|
|
$host->last_failure_at = $now;
|
|
|
|
|
}
|
|
|
|
|
$host->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$members = CloudVpcMember::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereNotIn('state', ['deleting', 'deleted'])->get();
|
|
|
|
|
$memberIds = $members->pluck('id')->map(
|
|
|
|
|
static fn ($id): int => (int) $id
|
|
|
|
|
)->values()->all();
|
|
|
|
|
$leases = CloudVpcPrivateIp::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereIn('member_id', $memberIds)->get()->keyBy('member_id');
|
|
|
|
|
$runtimeReady = !$members->isEmpty();
|
|
|
|
|
$rootError = null;
|
|
|
|
|
foreach ($members as $member) {
|
|
|
|
|
$lease = $leases->get((int) $member->id);
|
|
|
|
|
if ((string) $member->state === 'active'
|
|
|
|
|
&& $lease !== null && (string) $lease->state === 'active') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$runtimeReady = false;
|
|
|
|
|
$runtimeError = self::safeErrorCode(
|
|
|
|
|
(string) ($member->last_error_code ?? '')
|
|
|
|
|
);
|
|
|
|
|
if ($runtimeError === null && $lease !== null) {
|
|
|
|
|
$runtimeError = self::safeErrorCode(
|
|
|
|
|
(string) ($lease->last_error_code ?? '')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$rootError ??= self::safeErrorCode(
|
|
|
|
|
(string) ($runtimeError ?? '')
|
|
|
|
|
) ?? 'vpc_private_observation_missing';
|
|
|
|
|
}
|
|
|
|
|
$hostsReady = !CloudVpcHostDeployment::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereNotIn('state', ['active', 'deleted'])->exists();
|
|
|
|
|
$runtimeReady = $runtimeReady && $hostsReady;
|
|
|
|
|
$configExact = (int) $vpc->desired_revision === (int) $vpc->observed_revision
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) $vpc->desired_digest,
|
|
|
|
|
(string) $vpc->observed_digest
|
|
|
|
|
);
|
|
|
|
|
if ($configExact && (string) $vpc->state !== 'deleting'
|
|
|
|
|
&& (string) $vpc->state !== 'deleted') {
|
|
|
|
|
$vpc->state = $runtimeReady ? 'active' : 'degraded';
|
|
|
|
|
if ($runtimeReady) {
|
|
|
|
|
$vpc->sync_state = 'applied';
|
|
|
|
|
$vpc->last_error_code = null;
|
|
|
|
|
$vpc->last_success_at = $now;
|
|
|
|
|
} else {
|
|
|
|
|
// An intentional lifecycle link-down does not invalidate the
|
|
|
|
|
// shared host configuration receipt. Preserve an applied
|
|
|
|
|
// config state; other runtime gaps retain their waiting lane.
|
|
|
|
|
if ((string) $vpc->sync_state === 'applied') {
|
|
|
|
|
$vpc->sync_state = 'applied';
|
|
|
|
|
}
|
|
|
|
|
$vpc->last_error_code = $rootError
|
|
|
|
|
?? 'vpc_private_observation_missing';
|
|
|
|
|
if ($persistentConfigMismatch && $deployment !== null
|
|
|
|
|
&& (string) $deployment->state === 'active'
|
|
|
|
|
&& (int) $deployment->desired_revision === $expectedRevision
|
|
|
|
|
&& (int) $deployment->observed_revision === $expectedRevision
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) ($expectedDigest ?? ''),
|
|
|
|
|
(string) $deployment->desired_digest
|
|
|
|
|
)
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) ($expectedDigest ?? ''),
|
|
|
|
|
(string) $deployment->observed_digest
|
|
|
|
|
)
|
|
|
|
|
&& (string) $deployment->last_operation_id
|
|
|
|
|
=== (string) $vpc->last_operation_id
|
|
|
|
|
&& (string) $vpc->state === 'active'
|
|
|
|
|
&& (string) $vpc->sync_state === 'applied'
|
|
|
|
|
&& (int) $vpc->observed_revision === $expectedRevision
|
|
|
|
|
&& self::digestMatches(
|
|
|
|
|
(string) ($expectedDigest ?? ''),
|
|
|
|
|
(string) $vpc->observed_digest
|
|
|
|
|
)) {
|
|
|
|
|
$now = time();
|
|
|
|
|
$deployment->state = 'degraded';
|
|
|
|
|
$deployment->last_error_code = 'vpc_private_observation_mismatch';
|
|
|
|
|
$deployment->last_failure_at = $now;
|
|
|
|
|
$deployment->updated_at = $now;
|
|
|
|
|
$deployment->save();
|
|
|
|
|
$vpc->state = 'degraded';
|
|
|
|
|
$vpc->sync_state = 'uncertain';
|
|
|
|
|
$vpc->last_error_code = 'vpc_private_observation_mismatch';
|
|
|
|
|
$vpc->last_failure_at = $now;
|
|
|
|
|
$vpc->updated_at = $now;
|
|
|
|
|
$vpc->save();
|
|
|
|
|
}
|
|
|
|
|
$vpc->updated_at = $now;
|
|
|
|
|
$vpc->save();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}, 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function safeDigest($value): ?string
|
|
|
|
|
@ -5037,7 +5059,8 @@ final class CloudVpcService
|
|
|
|
|
$observedDigest = (string) ($vpc->observed_digest ?? '');
|
|
|
|
|
$hostRows = CloudVpcHostDeployment::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereNotIn('state', ['deleted'])->get();
|
|
|
|
|
$hostsReady = $hostRows->count() > 0;
|
|
|
|
|
$configurationHostsReady = $hostRows->count() > 0;
|
|
|
|
|
$runtimeReady = $configurationHostsReady;
|
|
|
|
|
$pendingTombstone = CloudVpcHostDeployment::where('vpc_id', $vpc->id)
|
|
|
|
|
->where('state', 'deleting')->exists();
|
|
|
|
|
foreach ([
|
|
|
|
|
@ -5053,7 +5076,8 @@ final class CloudVpcService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($pendingTombstone) {
|
|
|
|
|
$hostsReady = false;
|
|
|
|
|
$configurationHostsReady = false;
|
|
|
|
|
$runtimeReady = false;
|
|
|
|
|
}
|
|
|
|
|
// A VPC projection is not usable while any live member points at an
|
|
|
|
|
// instance that has left the active lifecycle state. This closes the
|
|
|
|
|
@ -5061,8 +5085,19 @@ final class CloudVpcService
|
|
|
|
|
// matching root digest looking healthy to the tenant UI.
|
|
|
|
|
$runtimeMembers = CloudVpcMember::where('vpc_id', $vpc->id)
|
|
|
|
|
->whereNotIn('state', ['deleted'])->get([
|
|
|
|
|
'id', 'instance_id', 'private_ip_id', 'state',
|
|
|
|
|
'id', 'instance_id', 'host_id', 'private_ip_id', 'state',
|
|
|
|
|
]);
|
|
|
|
|
$memberHostIds = $runtimeMembers->pluck('host_id')->map(
|
|
|
|
|
static fn ($hostId): string => (string) $hostId
|
|
|
|
|
)->unique()->values()->all();
|
|
|
|
|
$deploymentHostIds = $hostRows->pluck('host_id')->map(
|
|
|
|
|
static fn ($hostId): string => (string) $hostId
|
|
|
|
|
)->unique()->values()->all();
|
|
|
|
|
sort($memberHostIds, SORT_STRING);
|
|
|
|
|
sort($deploymentHostIds, SORT_STRING);
|
|
|
|
|
if ($runtimeMembers->isEmpty() || $memberHostIds !== $deploymentHostIds) {
|
|
|
|
|
$configurationHostsReady = false;
|
|
|
|
|
}
|
|
|
|
|
$memberInstances = $runtimeMembers->pluck('instance_id')
|
|
|
|
|
->map(static fn ($id): int => (int) $id)
|
|
|
|
|
->unique()->values()->all();
|
|
|
|
|
@ -5091,7 +5126,7 @@ final class CloudVpcService
|
|
|
|
|
|| (string) ($instanceStates->get(
|
|
|
|
|
(int) $runtimeMember->instance_id
|
|
|
|
|
)?->network_status ?? '') !== 'active') {
|
|
|
|
|
$hostsReady = false;
|
|
|
|
|
$runtimeReady = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Only a fresh, identity-bound running observation can support
|
|
|
|
|
@ -5101,7 +5136,7 @@ final class CloudVpcService
|
|
|
|
|
if (!is_array($runtime)
|
|
|
|
|
|| ($runtime['state_fresh'] ?? false) !== true
|
|
|
|
|
|| (string) ($runtime['power_state'] ?? '') !== 'running') {
|
|
|
|
|
$hostsReady = false;
|
|
|
|
|
$runtimeReady = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -5112,13 +5147,15 @@ final class CloudVpcService
|
|
|
|
|
|| (int) $host->observed_revision !== (int) $vpc->desired_revision
|
|
|
|
|
|| !self::digestMatches((string) $host->desired_digest, $desiredDigest)
|
|
|
|
|
|| !self::digestMatches((string) $host->observed_digest, $desiredDigest)) {
|
|
|
|
|
$hostsReady = false;
|
|
|
|
|
$configurationHostsReady = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$runtimeReady = $runtimeReady && $configurationHostsReady;
|
|
|
|
|
$rootReady = self::digestMatches($desiredDigest, $observedDigest);
|
|
|
|
|
$currentOperation = null;
|
|
|
|
|
$currentOperationReady = false;
|
|
|
|
|
$currentOperationConfigurationReady = false;
|
|
|
|
|
$lastOperationId = (string) ($vpc->last_operation_id ?? '');
|
|
|
|
|
if (preg_match(self::KEY_PATTERN, $lastOperationId) === 1) {
|
|
|
|
|
$currentOperation = CloudVpcOperation::where('vpc_id', $vpc->id)
|
|
|
|
|
@ -5129,11 +5166,20 @@ final class CloudVpcService
|
|
|
|
|
&& (string) $currentOperation->status === 'succeeded'
|
|
|
|
|
&& (int) $currentOperation->evidence_version
|
|
|
|
|
>= self::OBSERVATION_EVIDENCE_VERSION;
|
|
|
|
|
$workflow = $currentOperation === null
|
|
|
|
|
? null : CloudVpcPowerWorkflow::state($currentOperation);
|
|
|
|
|
$currentOperationConfigurationReady = $currentOperation !== null
|
|
|
|
|
&& (int) $currentOperation->evidence_version
|
|
|
|
|
>= self::OBSERVATION_EVIDENCE_VERSION
|
|
|
|
|
&& ($currentOperationReady
|
|
|
|
|
|| ($workflow['config_confirmed'] ?? false) === true);
|
|
|
|
|
}
|
|
|
|
|
$usable = (string) $vpc->state === 'active'
|
|
|
|
|
$configurationReady = (string) $vpc->state === 'active'
|
|
|
|
|
&& (string) $vpc->sync_state === 'applied'
|
|
|
|
|
&& (int) $vpc->desired_revision === (int) $vpc->observed_revision
|
|
|
|
|
&& $rootReady && $hostsReady && $currentOperationReady;
|
|
|
|
|
&& $rootReady && $configurationHostsReady
|
|
|
|
|
&& $currentOperationConfigurationReady;
|
|
|
|
|
$usable = $configurationReady && $runtimeReady;
|
|
|
|
|
$rawErrorCode = trim((string) ($vpc->last_error_code ?? ''));
|
|
|
|
|
// setup_state is a read-only tenant projection. It also repairs the
|
|
|
|
|
// presentation of legacy empty roots that were persisted as failed
|
|
|
|
|
@ -5221,6 +5267,10 @@ final class CloudVpcService
|
|
|
|
|
'sync_state' => (string) $vpc->sync_state,
|
|
|
|
|
'desired_revision' => (int) $vpc->desired_revision,
|
|
|
|
|
'observed_revision' => (int) $vpc->observed_revision,
|
|
|
|
|
// Configuration and aggregate guest runtime are separate public
|
|
|
|
|
// facts. A stopped or unverified guest may make `usable` false
|
|
|
|
|
// without reopening this exact host-configuration receipt.
|
|
|
|
|
'configured' => $configurationReady,
|
|
|
|
|
'usable' => $usable,
|
|
|
|
|
'member_count' => $runtimeMembers->count(),
|
|
|
|
|
'setup_state' => $awaitingMembers ? 'awaiting_members' : null,
|
|
|
|
|
|