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.
264 lines
11 KiB
PHP
264 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceMigration;
|
|
use App\Models\CloudInstanceSnapshot;
|
|
use App\Models\CloudPeerMigrationArtifact;
|
|
|
|
/** Path-free artifact catalog for PeerMigration V3 direct host transfers. */
|
|
final class CloudPeerMigrationArtifactService
|
|
{
|
|
private const GIB = 1073741824;
|
|
|
|
/** @return array<int,CloudInstanceSnapshot> */
|
|
public static function freezeSnapshotsLocked(CloudInstance $instance): array
|
|
{
|
|
$rows = CloudInstanceSnapshot::where(
|
|
'instance_id', (int) $instance->id
|
|
)->where('user_id', (int) $instance->user_id)
|
|
->whereIn('state', [
|
|
'creating', 'ready', 'restoring', 'deleting', 'uncertain',
|
|
])->orderBy('id')->lockForUpdate()->get()->all();
|
|
if (count($rows) > CloudSnapshotService::MAX_PER_INSTANCE) {
|
|
throw new CloudError('实例快照目录超过可迁移上限', 409);
|
|
}
|
|
foreach ($rows as $snapshot) {
|
|
if ((string) $snapshot->state !== 'ready'
|
|
|| (int) $snapshot->host_id !== (int) $instance->host_id
|
|
|| (int) ($snapshot->size_bytes ?? 0) < 1
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) $snapshot->snapshot_key) !== 1) {
|
|
throw new CloudError(
|
|
'实例存在未就绪或宿主机不一致的快照,不能维护迁移',
|
|
409
|
|
);
|
|
}
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
/** @param array<int,CloudInstanceSnapshot> $snapshots */
|
|
public static function additionalDiskGib(array $snapshots): int
|
|
{
|
|
$bytes = 0;
|
|
foreach ($snapshots as $snapshot) {
|
|
$bytes += max(0, (int) ($snapshot->size_bytes ?? 0));
|
|
}
|
|
return (int) ceil($bytes / self::GIB);
|
|
}
|
|
|
|
public static function advisoryAdditionalDiskGib(
|
|
CloudInstance $instance
|
|
): int {
|
|
return self::additionalDiskGib(CloudInstanceSnapshot::where(
|
|
'instance_id', (int) $instance->id
|
|
)->where('host_id', (int) $instance->host_id)
|
|
->where('user_id', (int) $instance->user_id)
|
|
->where('state', 'ready')->get()->all());
|
|
}
|
|
|
|
/** Snapshot capacity frozen into one persisted peer session. */
|
|
public static function artifactDiskGib(
|
|
CloudInstanceMigration $migration
|
|
): int {
|
|
$bytes = (int) CloudPeerMigrationArtifact::where(
|
|
'migration_id', (int) $migration->id
|
|
)->where('role', 'snapshot')->sum('virtual_size_bytes');
|
|
return (int) ceil(max(0, $bytes) / self::GIB);
|
|
}
|
|
|
|
/** @param array<int,CloudInstanceSnapshot> $snapshots */
|
|
public static function persistLocked(
|
|
CloudInstanceMigration $migration,
|
|
array $snapshots,
|
|
int $rootVirtualSizeBytes
|
|
): void {
|
|
if ($rootVirtualSizeBytes < 1) {
|
|
throw new \LogicException('Invalid peer migration root capacity');
|
|
}
|
|
$now = time();
|
|
$root = new CloudPeerMigrationArtifact();
|
|
$root->migration_id = (int) $migration->id;
|
|
$root->artifact_id = bin2hex(random_bytes(16));
|
|
$root->role = 'root';
|
|
$root->source_snapshot_id = null;
|
|
$root->snapshot_key = null;
|
|
$root->name = '';
|
|
$root->consistency = 'crash';
|
|
$root->object_size_bytes = 0;
|
|
$root->virtual_size_bytes = $rootVirtualSizeBytes;
|
|
$root->sha256 = '';
|
|
$root->state = 'creating';
|
|
$root->snapshot_created_at = null;
|
|
$root->created_at = $root->updated_at = $now;
|
|
$root->save();
|
|
|
|
foreach ($snapshots as $snapshot) {
|
|
$artifact = new CloudPeerMigrationArtifact();
|
|
$artifact->migration_id = (int) $migration->id;
|
|
$artifact->artifact_id = bin2hex(random_bytes(16));
|
|
$artifact->role = 'snapshot';
|
|
$artifact->source_snapshot_id = (int) $snapshot->id;
|
|
$artifact->snapshot_key = (string) $snapshot->snapshot_key;
|
|
$artifact->name = (string) $snapshot->name;
|
|
$artifact->consistency = (string) $snapshot->consistency;
|
|
$artifact->object_size_bytes = 0;
|
|
$artifact->virtual_size_bytes = (int) $snapshot->size_bytes;
|
|
$artifact->sha256 = '';
|
|
$artifact->state = 'creating';
|
|
$artifact->snapshot_created_at = (int) $snapshot->created_at;
|
|
$artifact->created_at = $artifact->updated_at = $now;
|
|
$artifact->save();
|
|
}
|
|
}
|
|
|
|
/** @return array<int,array<string,string>> */
|
|
public static function sourceRequestArtifacts(
|
|
CloudInstanceMigration $migration
|
|
): array {
|
|
return CloudPeerMigrationArtifact::where(
|
|
'migration_id', (int) $migration->id
|
|
)->orderBy('id')->get()->map(static function ($artifact): array {
|
|
$value = [
|
|
'id' => (string) $artifact->artifact_id,
|
|
'role' => (string) $artifact->role,
|
|
];
|
|
if ((string) $artifact->role === 'snapshot') {
|
|
$value['snapshot_key'] = (string) $artifact->snapshot_key;
|
|
}
|
|
return $value;
|
|
})->all();
|
|
}
|
|
|
|
/** Validate and seal the source Agent's complete path-free manifest. */
|
|
public static function finishPrepareLocked(
|
|
CloudInstanceMigration $migration,
|
|
array $result
|
|
): void {
|
|
if (($result['source_stopped'] ?? null) !== true
|
|
|| ($result['source_autostart_disabled'] ?? null) !== true
|
|
|| !is_array($result['artifacts'] ?? null)) {
|
|
throw new CloudError('源 Agent 未确认 PeerMigration 工件已冻结', 502);
|
|
}
|
|
$rows = CloudPeerMigrationArtifact::where(
|
|
'migration_id', (int) $migration->id
|
|
)->orderBy('id')->lockForUpdate()->get();
|
|
$received = self::artifactMap($result['artifacts']);
|
|
if (count($received) !== count($rows)) {
|
|
throw new CloudError('源 Agent 返回的迁移工件数量不一致', 502);
|
|
}
|
|
$now = time();
|
|
foreach ($rows as $row) {
|
|
$item = $received[(string) $row->artifact_id] ?? null;
|
|
self::assertArtifactMatches($row, $item, false);
|
|
$row->object_size_bytes = (int) $item['size_bytes'];
|
|
$row->virtual_size_bytes = (int) $item['virtual_size_bytes'];
|
|
$row->sha256 = (string) $item['sha256'];
|
|
if ((string) $row->role === 'snapshot') {
|
|
$row->snapshot_created_at = (int) $item['created_at'];
|
|
}
|
|
$row->state = 'ready';
|
|
$row->updated_at = $now;
|
|
$row->save();
|
|
}
|
|
}
|
|
|
|
/** Validate target receipts and atomically rebind the entire snapshot catalog. */
|
|
public static function finishReceiveLocked(
|
|
CloudInstanceMigration $migration,
|
|
array $result
|
|
): void {
|
|
if (($result['staged_powered_off'] ?? null) !== true
|
|
|| (string) ($result['status'] ?? '') !== 'shutoff'
|
|
|| !is_array($result['artifacts'] ?? null)) {
|
|
throw new CloudError('目标 Agent 未确认实例离线且工件完整', 502);
|
|
}
|
|
$rows = CloudPeerMigrationArtifact::where(
|
|
'migration_id', (int) $migration->id
|
|
)->orderBy('id')->lockForUpdate()->get();
|
|
$received = self::artifactMap($result['artifacts']);
|
|
if (count($received) !== count($rows)) {
|
|
throw new CloudError('目标 Agent 返回的迁移工件数量不一致', 502);
|
|
}
|
|
$now = time();
|
|
foreach ($rows as $row) {
|
|
$item = $received[(string) $row->artifact_id] ?? null;
|
|
self::assertArtifactMatches($row, $item, true);
|
|
if ((string) $row->role === 'snapshot') {
|
|
$snapshot = CloudInstanceSnapshot::where(
|
|
'id', (int) $row->source_snapshot_id
|
|
)->lockForUpdate()->first();
|
|
if ($snapshot === null
|
|
|| (int) $snapshot->instance_id !== (int) $migration->instance_id
|
|
|| (int) $snapshot->host_id !== (int) $migration->source_host_id
|
|
|| (string) $snapshot->state !== 'ready'
|
|
|| !hash_equals((string) $row->snapshot_key,
|
|
(string) $snapshot->snapshot_key)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 期间实例快照目录已变化', 409
|
|
);
|
|
}
|
|
$snapshot->host_id = (int) $migration->target_host_id;
|
|
$snapshot->updated_at = $now;
|
|
$snapshot->save();
|
|
}
|
|
$row->state = 'restored';
|
|
$row->updated_at = $now;
|
|
$row->save();
|
|
}
|
|
}
|
|
|
|
/** @return array<string,array<string,mixed>> */
|
|
private static function artifactMap(array $items): array
|
|
{
|
|
$received = [];
|
|
foreach ($items as $item) {
|
|
$id = is_array($item) ? ($item['id'] ?? null) : null;
|
|
if (!is_string($id)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $id) !== 1
|
|
|| isset($received[$id])) {
|
|
throw new CloudError('Agent 迁移工件回执标识无效或重复', 502);
|
|
}
|
|
$received[$id] = $item;
|
|
}
|
|
return $received;
|
|
}
|
|
|
|
private static function assertArtifactMatches(
|
|
CloudPeerMigrationArtifact $row,
|
|
$item,
|
|
bool $sealed
|
|
): void {
|
|
$size = is_array($item) ? filter_var($item['size_bytes'] ?? null,
|
|
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
|
$virtual = is_array($item) ? filter_var(
|
|
$item['virtual_size_bytes'] ?? null,
|
|
FILTER_VALIDATE_INT,
|
|
['options' => ['min_range' => 1]]
|
|
) : false;
|
|
$created = is_array($item) ? filter_var($item['created_at'] ?? null,
|
|
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
|
$digest = is_array($item) ? ($item['sha256'] ?? null) : null;
|
|
if (!is_array($item)
|
|
|| !hash_equals((string) $row->role, (string) ($item['role'] ?? ''))
|
|
|| !hash_equals((string) ($row->snapshot_key ?? ''),
|
|
(string) ($item['snapshot_key'] ?? ''))
|
|
|| !hash_equals((string) $row->name, (string) ($item['name'] ?? ''))
|
|
|| !hash_equals((string) $row->consistency,
|
|
(string) ($item['consistency'] ?? ''))
|
|
|| $size === false || $virtual === false || $created === false
|
|
|| !is_string($digest)
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $digest) !== 1
|
|
|| ($sealed && ((int) $row->object_size_bytes !== (int) $size
|
|
|| (int) $row->virtual_size_bytes !== (int) $virtual
|
|
|| !hash_equals((string) $row->sha256, $digest)
|
|
|| ((string) $row->role === 'snapshot'
|
|
&& (int) $row->snapshot_created_at !== (int) $created)))
|
|
|| (!$sealed && (int) $row->virtual_size_bytes !== (int) $virtual)) {
|
|
throw new CloudError('Agent 迁移工件与冻结目录不一致', 502);
|
|
}
|
|
}
|
|
}
|