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.
249 lines
10 KiB
PHP
249 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceMigration;
|
|
use App\Models\CloudOrder;
|
|
use App\Models\CloudPublicIp;
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
/**
|
|
* Resource-ledger settlement for maintenance migration.
|
|
*
|
|
* Stage execution decides when a result is proven. This module alone moves
|
|
* order ownership, reservations and public IP assignments after that proof.
|
|
*/
|
|
final class CloudInstanceMigrationSettlement
|
|
{
|
|
/**
|
|
* Make the order-linked reservation authoritative on the target while a
|
|
* separate restore reservation accounts for the stopped source duplicate.
|
|
*/
|
|
public static function retainSourceLocked(
|
|
CloudInstanceMigration $migration
|
|
): void {
|
|
if (self::isSettled($migration)) {
|
|
return;
|
|
}
|
|
$instance = CloudInstance::where(
|
|
'id', (int) $migration->instance_id
|
|
)->lockForUpdate()->first();
|
|
$source = CloudResourceReservation::where(
|
|
'id', (int) $migration->source_reservation_id
|
|
)->lockForUpdate()->first();
|
|
$target = CloudResourceReservation::where(
|
|
'id', (int) $migration->target_reservation_id
|
|
)->lockForUpdate()->first();
|
|
$order = $instance === null || $instance->order_id === null ? null
|
|
: CloudOrder::where('id', (int) $instance->order_id)
|
|
->lockForUpdate()->first();
|
|
$sourceIp = $source === null ? null : CloudPublicIp::where(
|
|
'id', (int) $source->public_ip_id
|
|
)->lockForUpdate()->first();
|
|
$targetIp = $target === null ? null : CloudPublicIp::where(
|
|
'id', (int) $target->public_ip_id
|
|
)->lockForUpdate()->first();
|
|
if ($instance === null || $source === null || $target === null
|
|
|| $order === null || $sourceIp === null || $targetIp === null
|
|
|| (int) $instance->host_id !== (int) $migration->target_host_id
|
|
|| !hash_equals(strtolower((string) $instance->uuid),
|
|
strtolower((string) $migration->target_uuid))
|
|
|| (int) $instance->reservation_id !== (int) $target->id
|
|
|| (int) $source->order_id !== (int) $order->id
|
|
|| $source->backup_restore_id !== null
|
|
|| (int) $target->backup_restore_id !== (int) $migration->restore_id
|
|
|| $target->order_id !== null
|
|
|| (string) $source->state !== 'committed'
|
|
|| (string) $target->state !== 'committed'
|
|
|| (int) $sourceIp->reservation_id !== (int) $source->id
|
|
|| (int) $targetIp->reservation_id !== (int) $target->id) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 保留源副本时资源账本不一致', 409
|
|
);
|
|
}
|
|
$now = time();
|
|
$sourceFields = [];
|
|
$targetFields = [];
|
|
foreach (['resource_pool_id', 'host_id', 'network_attachment_id',
|
|
'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib',
|
|
'disk_gib'] as $field) {
|
|
$sourceFields[$field] = $source->{$field};
|
|
$targetFields[$field] = $target->{$field};
|
|
}
|
|
foreach ($targetFields as $field => $value) {
|
|
$source->{$field} = $value;
|
|
}
|
|
$source->instance_id = (int) $instance->id;
|
|
$source->updated_at = $now;
|
|
$source->save();
|
|
foreach ($sourceFields as $field => $value) {
|
|
$target->{$field} = $value;
|
|
}
|
|
$target->instance_id = null;
|
|
$target->updated_at = $now;
|
|
$target->save();
|
|
|
|
$targetIp->reservation_id = (int) $source->id;
|
|
$targetIp->instance_id = (int) $instance->id;
|
|
$targetIp->operation_id = (string) $source->operation_id;
|
|
$targetIp->state = 'assigned';
|
|
$targetIp->updated_at = $now;
|
|
$targetIp->save();
|
|
$sourceIp->reservation_id = (int) $target->id;
|
|
$sourceIp->instance_id = null;
|
|
$sourceIp->operation_id = (string) $target->operation_id;
|
|
$sourceIp->state = 'assigned';
|
|
$sourceIp->updated_at = $now;
|
|
$sourceIp->save();
|
|
|
|
$instance->reservation_id = (int) $source->id;
|
|
$instance->resource_pool_id = (int) $source->resource_pool_id;
|
|
$instance->updated_at = $now;
|
|
$instance->save();
|
|
$order->host_id = (int) $source->host_id;
|
|
$order->resource_pool_id = (int) $source->resource_pool_id;
|
|
$order->reservation_id = (int) $source->id;
|
|
$order->updated_at = $now;
|
|
$order->save();
|
|
$oldSourceId = (int) $migration->source_reservation_id;
|
|
$migration->source_reservation_id = (int) $target->id;
|
|
$migration->target_reservation_id = $oldSourceId;
|
|
$migration->updated_at = $now;
|
|
$migration->save();
|
|
}
|
|
|
|
public static function isSettled(
|
|
CloudInstanceMigration $migration
|
|
): bool {
|
|
$instance = CloudInstance::find((int) $migration->instance_id);
|
|
$target = CloudResourceReservation::find(
|
|
(int) $migration->target_reservation_id
|
|
);
|
|
return $instance !== null && $target !== null
|
|
&& (int) $instance->reservation_id === (int) $target->id
|
|
&& $target->order_id !== null
|
|
&& $target->backup_restore_id === null
|
|
&& (int) $target->host_id === (int) $migration->target_host_id;
|
|
}
|
|
|
|
/** Source deletion is proven; transfer its order-linked reservation. */
|
|
public static function finalizeSourceDeletionLocked(
|
|
CloudInstanceMigration $migration
|
|
): void {
|
|
$instance = CloudInstance::where(
|
|
'id', (int) $migration->instance_id
|
|
)->lockForUpdate()->first();
|
|
$source = CloudResourceReservation::where(
|
|
'id', (int) $migration->source_reservation_id
|
|
)->lockForUpdate()->first();
|
|
$target = CloudResourceReservation::where(
|
|
'id', (int) $migration->target_reservation_id
|
|
)->lockForUpdate()->first();
|
|
$sourceIp = $source === null ? null : CloudPublicIp::where(
|
|
'id', (int) $source->public_ip_id
|
|
)->lockForUpdate()->first();
|
|
$targetIp = $target === null ? null : CloudPublicIp::where(
|
|
'id', (int) $target->public_ip_id
|
|
)->lockForUpdate()->first();
|
|
$order = $instance === null || $instance->order_id === null ? null
|
|
: CloudOrder::where('id', (int) $instance->order_id)
|
|
->lockForUpdate()->first();
|
|
if ($instance === null || $source === null || $target === null
|
|
|| $sourceIp === null || $targetIp === null || $order === null
|
|
|| (int) $instance->host_id !== (int) $migration->target_host_id
|
|
|| !hash_equals(strtolower((string) $instance->uuid),
|
|
strtolower((string) $migration->target_uuid))
|
|
|| (int) $instance->reservation_id !== (int) $target->id
|
|
|| (int) $source->order_id !== (int) $order->id
|
|
|| (string) $source->state !== 'committed'
|
|
|| (int) $target->backup_restore_id !== (int) $migration->restore_id
|
|
|| (string) $target->state !== 'committed'
|
|
|| (int) $sourceIp->reservation_id !== (int) $source->id
|
|
|| (int) $targetIp->reservation_id !== (int) $target->id) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 清理后的资源预留账本不一致', 409
|
|
);
|
|
}
|
|
$now = time();
|
|
$sourceIp->state = 'available';
|
|
$sourceIp->reservation_id = null;
|
|
$sourceIp->instance_id = null;
|
|
$sourceIp->operation_id = null;
|
|
$sourceIp->updated_at = $now;
|
|
$sourceIp->save();
|
|
foreach (['resource_pool_id', 'host_id', 'network_attachment_id',
|
|
'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib',
|
|
'disk_gib'] as $field) {
|
|
$source->{$field} = $target->{$field};
|
|
}
|
|
$source->instance_id = (int) $instance->id;
|
|
$source->updated_at = $now;
|
|
$source->save();
|
|
$targetIp->reservation_id = (int) $source->id;
|
|
$targetIp->instance_id = (int) $instance->id;
|
|
$targetIp->operation_id = (string) $source->operation_id;
|
|
$targetIp->state = 'assigned';
|
|
$targetIp->updated_at = $now;
|
|
$targetIp->save();
|
|
$target->instance_id = null;
|
|
$target->network_attachment_id = null;
|
|
$target->public_ip_pool_id = null;
|
|
$target->public_ip_id = null;
|
|
$target->state = 'released';
|
|
$target->released_at = $now;
|
|
$target->release_reason = 'maintenance_migration_transferred';
|
|
$target->updated_at = $now;
|
|
$target->save();
|
|
$instance->reservation_id = (int) $source->id;
|
|
$instance->resource_pool_id = (int) $source->resource_pool_id;
|
|
$instance->updated_at = $now;
|
|
$instance->save();
|
|
$order->host_id = (int) $source->host_id;
|
|
$order->resource_pool_id = (int) $source->resource_pool_id;
|
|
$order->reservation_id = (int) $source->id;
|
|
$order->updated_at = $now;
|
|
$order->save();
|
|
$migration->source_reservation_id = (int) $target->id;
|
|
$migration->target_reservation_id = (int) $source->id;
|
|
$migration->updated_at = $now;
|
|
$migration->save();
|
|
}
|
|
|
|
public static function releaseRetainedSourceLocked(
|
|
CloudInstanceMigration $migration
|
|
): void {
|
|
$debt = CloudResourceReservation::where(
|
|
'id', (int) $migration->source_reservation_id
|
|
)->lockForUpdate()->first();
|
|
$ip = $debt === null ? null : CloudPublicIp::where(
|
|
'id', (int) $debt->public_ip_id
|
|
)->lockForUpdate()->first();
|
|
if ($debt === null || $ip === null
|
|
|| (int) $debt->host_id !== (int) $migration->source_host_id
|
|
|| (int) $debt->backup_restore_id !== (int) $migration->restore_id
|
|
|| (string) $debt->state !== 'committed'
|
|
|| (int) $ip->reservation_id !== (int) $debt->id
|
|
|| !hash_equals((string) $debt->operation_id,
|
|
(string) $ip->operation_id)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本债务账本不一致', 409
|
|
);
|
|
}
|
|
$now = time();
|
|
$ip->state = 'available';
|
|
$ip->reservation_id = null;
|
|
$ip->instance_id = null;
|
|
$ip->operation_id = null;
|
|
$ip->updated_at = $now;
|
|
$ip->save();
|
|
$debt->state = 'released';
|
|
$debt->released_at = $now;
|
|
$debt->release_reason = 'maintenance_migration_source_removed';
|
|
$debt->expires_at = null;
|
|
$debt->instance_id = null;
|
|
$debt->updated_at = $now;
|
|
$debt->save();
|
|
}
|
|
}
|