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.
710 lines
30 KiB
PHP
710 lines
30 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestore;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceMigration;
|
|
use App\Models\CloudInstanceMigrationSourceDebt;
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudOrder;
|
|
use App\Models\CloudResourceReservation;
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/**
|
|
* Maintenance-migration resource settlement.
|
|
*
|
|
* Reservation and claim identities never move. The target becomes the paid
|
|
* authority in place while the original source becomes a held cleanup debt.
|
|
*/
|
|
final class CloudInstanceMigrationSettlement
|
|
{
|
|
public static function settleCutoverLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudActivePublicNetworkClaimSet $frozenSourceClaimSet
|
|
): CloudInstanceMigrationSourceDebt {
|
|
$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();
|
|
$restore = CloudBackupRestore::where('id', (int) $migration->restore_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
|
|
|| $restore === null || $order === null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 切换缺少资源账本', 409
|
|
);
|
|
}
|
|
$existing = CloudInstanceMigrationSourceDebt::where(
|
|
'migration_id', (int) $migration->id
|
|
)->lockForUpdate()->first();
|
|
if ($existing !== null) {
|
|
self::assertActiveAuthorityLocked(
|
|
$migration, $existing, $instance, $source, $target,
|
|
$order, $restore
|
|
);
|
|
return $existing;
|
|
}
|
|
|
|
$sourceClaimSet = CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $source->id,
|
|
'held'
|
|
);
|
|
$targetClaimSet = CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $target->id,
|
|
'committed',
|
|
(int) $instance->id
|
|
);
|
|
if ($frozenSourceClaimSet->reservationId() !== (int) $source->id
|
|
|| $frozenSourceClaimSet->hostId()
|
|
!== (int) $migration->source_host_id
|
|
|| $sourceClaimSet->reservationId() !== (int) $source->id
|
|
|| $sourceClaimSet->hostId() !== (int) $source->host_id
|
|
|| $sourceClaimSet->networkAttachmentId()
|
|
!== $frozenSourceClaimSet->networkAttachmentId()
|
|
|| !hash_equals(
|
|
$sourceClaimSet->digest(),
|
|
$frozenSourceClaimSet->digest()
|
|
)
|
|
|| !hash_equals(
|
|
$sourceClaimSet->publicNetwork()->fingerprint(),
|
|
$frozenSourceClaimSet->publicNetwork()->fingerprint()
|
|
)
|
|
|| $targetClaimSet->reservationId() !== (int) $target->id
|
|
|| $targetClaimSet->hostId() !== (int) $migration->target_host_id
|
|
|| (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->host_id !== (int) $migration->source_host_id
|
|
|| (int) $target->host_id !== (int) $migration->target_host_id
|
|
|| (string) $source->state !== 'held'
|
|
|| $source->instance_id !== null
|
|
|| (int) $source->order_id !== (int) $order->id
|
|
|| (int) $source->offer_id !== (int) $order->offer_id
|
|
|| $source->backup_restore_id !== null
|
|
|| $source->backup_restore_attempt_key !== null
|
|
|| $source->migration_source_debt_id !== null
|
|
|| (string) $target->state !== 'committed'
|
|
|| (int) $target->instance_id !== (int) $instance->id
|
|
|| (int) $target->backup_restore_id !== (int) $restore->id
|
|
|| $target->order_id !== null || $target->offer_id !== null
|
|
|| $target->backup_restore_attempt_key !== null
|
|
|| $target->migration_source_debt_id !== null
|
|
|| (int) $restore->reservation_id !== (int) $target->id
|
|
|| (int) $order->reservation_id !== (int) $source->id
|
|
|| (int) $order->host_id !== (int) $source->host_id
|
|
|| (int) $order->resource_pool_id
|
|
!== (int) $source->resource_pool_id
|
|
|| (int) $instance->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| (int) $source->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| (int) $source->user_id !== (int) $instance->user_id
|
|
|| (int) $target->user_id !== (int) $instance->user_id) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 切换资源账本不一致', 409
|
|
);
|
|
}
|
|
|
|
$frozen = CloudFrozenPublicNetworkIdentity::fromClaimSet(
|
|
(string) $migration->source_uuid,
|
|
$frozenSourceClaimSet
|
|
);
|
|
$now = time();
|
|
$debt = new CloudInstanceMigrationSourceDebt();
|
|
$debt->debt_key = bin2hex(random_bytes(16));
|
|
$debt->migration_id = (int) $migration->id;
|
|
$debt->source_reservation_id = (int) $source->id;
|
|
$debt->source_host_id = (int) $source->host_id;
|
|
$debt->source_uuid = strtolower((string) $migration->source_uuid);
|
|
$debt->source_network_attachment_id =
|
|
$frozenSourceClaimSet->networkAttachmentId();
|
|
$debt->source_identity_json = $frozen->identityJson();
|
|
$debt->source_identity_sha256 = $frozen->identitySha256();
|
|
$debt->source_claim_set_sha256 = $frozen->claimSetSha256();
|
|
$debt->source_public_network_json = $frozen->publicNetworkJson();
|
|
$debt->source_public_network_sha256 =
|
|
$frozen->publicNetworkSha256();
|
|
$debt->cleanup_operation_id = null;
|
|
$debt->cleanup_job_id = null;
|
|
$debt->state = 'prepared';
|
|
$debt->active_debt = 1;
|
|
$debt->last_error_code = '';
|
|
$debt->resolved_at = null;
|
|
$debt->created_at = $now;
|
|
$debt->updated_at = $now;
|
|
$debt->save();
|
|
|
|
// Free the unique commercial source before assigning it to target.
|
|
$source->order_id = null;
|
|
$source->offer_id = null;
|
|
$source->migration_source_debt_id = (int) $debt->id;
|
|
$source->updated_at = $now;
|
|
$source->save();
|
|
$target->backup_restore_id = null;
|
|
$target->order_id = (int) $order->id;
|
|
$target->offer_id = (int) $order->offer_id;
|
|
$target->updated_at = $now;
|
|
$target->save();
|
|
$restore->reservation_id = null;
|
|
$restore->updated_at = $now;
|
|
$restore->save();
|
|
$order->host_id = (int) $target->host_id;
|
|
$order->resource_pool_id = (int) $target->resource_pool_id;
|
|
$order->reservation_id = (int) $target->id;
|
|
$order->updated_at = $now;
|
|
$order->save();
|
|
return $debt;
|
|
}
|
|
|
|
public static function markRetainedLocked(
|
|
CloudInstanceMigration $migration
|
|
): void {
|
|
[$debt] = self::activeAuthorityLocked($migration);
|
|
if ((string) $debt->state === 'retained') {
|
|
return;
|
|
}
|
|
if ((string) $debt->state !== 'prepared'
|
|
|| $debt->cleanup_operation_id !== null
|
|
|| $debt->cleanup_job_id !== null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本不能转为保留状态', 409
|
|
);
|
|
}
|
|
$debt->state = 'retained';
|
|
$debt->last_error_code = '';
|
|
$debt->updated_at = time();
|
|
$debt->save();
|
|
}
|
|
|
|
public static function bindCleanupLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudJob $job
|
|
): void {
|
|
[$debt] = self::activeAuthorityLocked($migration);
|
|
self::assertCleanupJob($migration, $job);
|
|
if ((string) $debt->state === 'cleanup_running'
|
|
&& (int) $debt->cleanup_job_id === (int) $job->id
|
|
&& hash_equals(
|
|
(string) $debt->cleanup_operation_id,
|
|
(string) $job->operation_id
|
|
)) {
|
|
return;
|
|
}
|
|
if (!in_array((string) $debt->state,
|
|
['prepared', 'retained'], true)
|
|
|| $debt->cleanup_operation_id !== null
|
|
|| $debt->cleanup_job_id !== null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理债务已被其他任务领取', 409
|
|
);
|
|
}
|
|
$debt->cleanup_operation_id = (string) $job->operation_id;
|
|
$debt->cleanup_job_id = (int) $job->id;
|
|
$debt->state = 'cleanup_running';
|
|
$debt->last_error_code = '';
|
|
$debt->updated_at = time();
|
|
$debt->save();
|
|
}
|
|
|
|
public static function markCleanupAttentionLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudJob $job,
|
|
string $errorCode
|
|
): void {
|
|
[$debt] = self::activeAuthorityLocked($migration);
|
|
self::assertBoundCleanup($migration, $debt, $job);
|
|
if (!in_array((string) $debt->state,
|
|
['cleanup_running', 'attention'], true)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理核对状态不一致', 409
|
|
);
|
|
}
|
|
$debt->state = 'attention';
|
|
$debt->last_error_code = $errorCode;
|
|
$debt->updated_at = time();
|
|
$debt->save();
|
|
}
|
|
|
|
public static function markCleanupFailedLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudJob $job,
|
|
string $errorCode
|
|
): void {
|
|
[$debt] = self::activeAuthorityLocked($migration);
|
|
self::assertBoundCleanup($migration, $debt, $job);
|
|
if (!in_array((string) $debt->state,
|
|
['cleanup_running', 'attention'], true)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理失败状态不一致', 409
|
|
);
|
|
}
|
|
$debt->cleanup_operation_id = null;
|
|
$debt->cleanup_job_id = null;
|
|
$debt->state = 'retained';
|
|
$debt->last_error_code = $errorCode;
|
|
$debt->updated_at = time();
|
|
$debt->save();
|
|
}
|
|
|
|
public static function releaseSourceLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudJob $job
|
|
): void {
|
|
$migration->getConnection()->transaction(static function () use (
|
|
$migration,
|
|
$job
|
|
): void {
|
|
// Validate the migration-owned projection before entering the
|
|
// allocator. Do not lock its reservation first: allocator order is
|
|
// host -> network context -> reservation -> claims -> addresses.
|
|
$debt = CloudInstanceMigrationSourceDebt::where(
|
|
'migration_id', (int) $migration->id
|
|
)->first();
|
|
if ($debt === null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本缺少清理债务', 409
|
|
);
|
|
}
|
|
if ((string) $debt->state === 'released') {
|
|
self::assertReleasedAuthority($migration, $debt);
|
|
self::assertBoundCleanup($migration, $debt, $job);
|
|
return;
|
|
}
|
|
$instance = CloudInstance::find((int) $migration->instance_id);
|
|
$source = CloudResourceReservation::find(
|
|
(int) $migration->source_reservation_id
|
|
);
|
|
$target = CloudResourceReservation::find(
|
|
(int) $migration->target_reservation_id
|
|
);
|
|
$order = $instance === null || $instance->order_id === null
|
|
? null : CloudOrder::find((int) $instance->order_id);
|
|
$restore = CloudBackupRestore::find((int) $migration->restore_id);
|
|
if ($instance === null || $source === null || $target === null
|
|
|| $order === null || $restore === null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本债务账本不完整', 409
|
|
);
|
|
}
|
|
self::assertActiveAuthorityLocked(
|
|
$migration, $debt, $instance, $source, $target,
|
|
$order, $restore
|
|
);
|
|
self::assertBoundCleanup($migration, $debt, $job);
|
|
if (!in_array((string) $debt->state,
|
|
['cleanup_running', 'attention'], true)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理完成状态不一致', 409
|
|
);
|
|
}
|
|
CloudPublicNetworkAllocationService::releaseBundle(
|
|
(int) $source->id,
|
|
(string) $source->operation_id,
|
|
'maintenance_migration_source_removed',
|
|
true
|
|
);
|
|
$now = time();
|
|
$debt = CloudInstanceMigrationSourceDebt::where(
|
|
'id', (int) $debt->id
|
|
)->lockForUpdate()->first();
|
|
$source = CloudResourceReservation::where(
|
|
'id', (int) $source->id
|
|
)->lockForUpdate()->first();
|
|
if ($debt === null || $source === null
|
|
|| (string) $source->state !== 'held'
|
|
|| $source->instance_id !== null
|
|
|| (int) $source->migration_source_debt_id
|
|
!== (int) $debt->id
|
|
|| !in_array((string) $debt->state,
|
|
['cleanup_running', 'attention'], true)
|
|
|| (int) $debt->cleanup_job_id !== (int) $job->id
|
|
|| !hash_equals(
|
|
(string) $debt->cleanup_operation_id,
|
|
(string) $job->operation_id
|
|
)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本资源预留已变化', 409
|
|
);
|
|
}
|
|
$source->state = 'released';
|
|
$source->released_at = $now;
|
|
$source->release_reason =
|
|
'maintenance_migration_source_removed';
|
|
$source->expires_at = null;
|
|
$source->updated_at = $now;
|
|
$source->save();
|
|
$debt->state = 'released';
|
|
$debt->active_debt = null;
|
|
$debt->last_error_code = '';
|
|
$debt->resolved_at = $now;
|
|
$debt->updated_at = $now;
|
|
$debt->save();
|
|
}, 3);
|
|
}
|
|
|
|
/**
|
|
* @return array{0:CloudInstanceMigrationSourceDebt,
|
|
* 1:CloudResourceReservation,2:CloudResourceReservation,
|
|
* 3:CloudInstance,4:CloudOrder,5:CloudBackupRestore}
|
|
*/
|
|
private static function activeAuthorityLocked(
|
|
CloudInstanceMigration $migration,
|
|
?CloudInstanceMigrationSourceDebt $knownDebt = null
|
|
): array {
|
|
$debt = $knownDebt ?? CloudInstanceMigrationSourceDebt::where(
|
|
'migration_id', (int) $migration->id
|
|
)->lockForUpdate()->first();
|
|
$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();
|
|
$restore = CloudBackupRestore::where('id', (int) $migration->restore_id)
|
|
->lockForUpdate()->first();
|
|
if ($debt === null || $instance === null || $source === null
|
|
|| $target === null || $order === null || $restore === null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本债务账本不完整', 409
|
|
);
|
|
}
|
|
self::assertActiveAuthorityLocked(
|
|
$migration, $debt, $instance, $source, $target, $order, $restore
|
|
);
|
|
return [$debt, $source, $target, $instance, $order, $restore];
|
|
}
|
|
|
|
private static function assertActiveAuthorityLocked(
|
|
CloudInstanceMigration $migration,
|
|
CloudInstanceMigrationSourceDebt $debt,
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $source,
|
|
CloudResourceReservation $target,
|
|
CloudOrder $order,
|
|
CloudBackupRestore $restore
|
|
): void {
|
|
if ((int) $debt->migration_id !== (int) $migration->id
|
|
|| (int) $debt->source_reservation_id !== (int) $source->id
|
|
|| (int) $debt->source_host_id !== (int) $migration->source_host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $debt->source_uuid),
|
|
strtolower((string) $migration->source_uuid)
|
|
)
|
|
|| (int) ($debt->active_debt ?? 0) !== 1
|
|
|| (int) $source->migration_source_debt_id !== (int) $debt->id
|
|
|| $source->order_id !== null || $source->offer_id !== null
|
|
|| $source->backup_restore_id !== null
|
|
|| $source->backup_restore_attempt_key !== null
|
|
|| (string) $source->state !== 'held'
|
|
|| $source->instance_id !== null
|
|
|| (int) $source->host_id !== (int) $migration->source_host_id
|
|
|| (int) $target->order_id !== (int) $order->id
|
|
|| (int) $target->offer_id !== (int) $order->offer_id
|
|
|| $target->backup_restore_id !== null
|
|
|| $target->backup_restore_attempt_key !== null
|
|
|| $target->migration_source_debt_id !== null
|
|
|| (string) $target->state !== 'committed'
|
|
|| (int) $target->instance_id !== (int) $instance->id
|
|
|| (int) $target->host_id !== (int) $migration->target_host_id
|
|
|| (int) $instance->host_id !== (int) $target->host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $instance->uuid),
|
|
strtolower((string) $migration->target_uuid)
|
|
)
|
|
|| (int) $instance->reservation_id !== (int) $target->id
|
|
|| (int) $instance->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| (int) $order->reservation_id !== (int) $target->id
|
|
|| (int) $order->host_id !== (int) $target->host_id
|
|
|| (int) $order->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| $restore->reservation_id !== null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本债务权威链不一致', 409
|
|
);
|
|
}
|
|
$sourceClaimSet = CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $source->id,
|
|
'held'
|
|
);
|
|
try {
|
|
CloudFrozenPublicNetworkIdentity::fromStored(
|
|
(int) $source->id,
|
|
(int) $source->host_id,
|
|
(string) $migration->source_uuid,
|
|
(int) $debt->source_network_attachment_id,
|
|
(string) $debt->source_identity_json,
|
|
(string) $debt->source_identity_sha256,
|
|
(string) $debt->source_public_network_json,
|
|
(string) $debt->source_public_network_sha256,
|
|
(string) $debt->source_claim_set_sha256,
|
|
$sourceClaimSet
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本冻结双栈身份不一致', 409
|
|
);
|
|
}
|
|
CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $target->id,
|
|
'committed',
|
|
(int) $instance->id
|
|
);
|
|
}
|
|
|
|
private static function assertCleanupJob(
|
|
CloudInstanceMigration $migration,
|
|
CloudJob $job
|
|
): void {
|
|
if ((string) $job->kind !== 'instance_migration_cleanup'
|
|
|| (int) $job->id < 1
|
|
|| (int) $job->instance_id !== (int) $migration->instance_id
|
|
|| (int) $job->host_id !== (int) $migration->source_host_id
|
|
|| (int) $job->backup_restore_id !== (int) $migration->restore_id
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) $job->operation_id) !== 1) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理任务身份不一致', 409
|
|
);
|
|
}
|
|
}
|
|
|
|
private static function assertBoundCleanup(
|
|
CloudInstanceMigration $migration,
|
|
CloudInstanceMigrationSourceDebt $debt,
|
|
CloudJob $job
|
|
): void {
|
|
self::assertCleanupJob($migration, $job);
|
|
if ((int) $debt->cleanup_job_id !== (int) $job->id
|
|
|| !hash_equals(
|
|
(string) $debt->cleanup_operation_id,
|
|
(string) $job->operation_id
|
|
)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源清理债务与任务不一致', 409
|
|
);
|
|
}
|
|
}
|
|
|
|
private static function assertReleasedAuthority(
|
|
CloudInstanceMigration $migration,
|
|
CloudInstanceMigrationSourceDebt $debt
|
|
): void {
|
|
$instance = CloudInstance::find((int) $migration->instance_id);
|
|
$source = CloudResourceReservation::find(
|
|
(int) $migration->source_reservation_id
|
|
);
|
|
$target = CloudResourceReservation::find(
|
|
(int) $migration->target_reservation_id
|
|
);
|
|
$order = $instance === null || $instance->order_id === null
|
|
? null : CloudOrder::find((int) $instance->order_id);
|
|
$restore = CloudBackupRestore::find((int) $migration->restore_id);
|
|
if ((int) $debt->migration_id !== (int) $migration->id
|
|
|| (int) $debt->source_reservation_id
|
|
!== (int) $migration->source_reservation_id
|
|
|| (int) $debt->source_host_id
|
|
!== (int) $migration->source_host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $debt->source_uuid),
|
|
strtolower((string) $migration->source_uuid)
|
|
)
|
|
|| (string) $debt->state !== 'released'
|
|
|| $debt->active_debt !== null
|
|
|| $debt->resolved_at === null
|
|
|| $debt->cleanup_operation_id === null
|
|
|| $debt->cleanup_job_id === null
|
|
|| $source === null || $target === null || $instance === null
|
|
|| $order === null || $restore === null
|
|
|| (int) $source->migration_source_debt_id !== (int) $debt->id
|
|
|| $source->order_id !== null || $source->offer_id !== null
|
|
|| $source->backup_restore_id !== null
|
|
|| $source->backup_restore_attempt_key !== null
|
|
|| (string) $source->state !== 'released'
|
|
|| $source->released_at === null || $source->expires_at !== null
|
|
|| $source->instance_id !== null
|
|
|| (int) $source->host_id !== (int) $migration->source_host_id
|
|
|| (string) $source->release_reason
|
|
!== 'maintenance_migration_source_removed'
|
|
|| (int) $target->order_id !== (int) $order->id
|
|
|| (int) $target->offer_id !== (int) $order->offer_id
|
|
|| $target->backup_restore_id !== null
|
|
|| $target->backup_restore_attempt_key !== null
|
|
|| $target->migration_source_debt_id !== null
|
|
|| (string) $target->state !== 'committed'
|
|
|| (int) $target->instance_id !== (int) $instance->id
|
|
|| (int) $target->host_id !== (int) $migration->target_host_id
|
|
|| (int) $instance->host_id !== (int) $target->host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $instance->uuid),
|
|
strtolower((string) $migration->target_uuid)
|
|
)
|
|
|| (int) $instance->reservation_id !== (int) $target->id
|
|
|| (int) $instance->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| (int) $order->reservation_id !== (int) $target->id
|
|
|| (int) $order->host_id !== (int) $target->host_id
|
|
|| (int) $order->resource_pool_id
|
|
!== (int) $target->resource_pool_id
|
|
|| $restore->reservation_id !== null) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 源副本释放账本不一致', 409
|
|
);
|
|
}
|
|
self::assertReleasedSourceNetworkAuthority(
|
|
$migration,
|
|
$debt,
|
|
$source
|
|
);
|
|
CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $target->id,
|
|
'committed',
|
|
(int) $instance->id
|
|
);
|
|
}
|
|
|
|
private static function assertReleasedSourceNetworkAuthority(
|
|
CloudInstanceMigration $migration,
|
|
CloudInstanceMigrationSourceDebt $debt,
|
|
CloudResourceReservation $source
|
|
): void {
|
|
try {
|
|
$networkValue = json_decode(
|
|
(string) $debt->source_public_network_json,
|
|
true,
|
|
64,
|
|
JSON_THROW_ON_ERROR
|
|
);
|
|
$network = CloudPublicNetworkSnapshot::fromArray($networkValue);
|
|
} catch (\Throwable) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源双栈快照损坏', 409
|
|
);
|
|
}
|
|
$claimRows = DB::table('cloud_resource_reservation_ips')
|
|
->where('reservation_id', (int) $source->id)
|
|
->where('operation_id', (string) $source->operation_id)
|
|
->orderBy('address_family')->orderBy('id')->get()->all();
|
|
if (DB::table('cloud_resource_reservation_ips')
|
|
->where('reservation_id', (int) $source->id)
|
|
->where('active_claim', 1)->exists()) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源仍有活动双栈声明', 409
|
|
);
|
|
}
|
|
$claims = [];
|
|
foreach ($claimRows as $claim) {
|
|
$family = (int) ($claim->address_family ?? 0);
|
|
if (!in_array($family, [4, 6], true)
|
|
|| isset($claims[$family])
|
|
|| !hash_equals(
|
|
(string) $source->operation_id,
|
|
(string) ($claim->operation_id ?? '')
|
|
)
|
|
|| !hash_equals(
|
|
(string) $source->release_reason,
|
|
(string) ($claim->release_reason ?? '')
|
|
)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源双栈声明不一致', 409
|
|
);
|
|
}
|
|
$claims[$family] = $claim;
|
|
}
|
|
try {
|
|
$claimIdentity = CloudPublicNetworkClaimSetIdentity::
|
|
fromReleasedLedger((int) $source->id, $claims, $network);
|
|
} catch (\Throwable) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源双栈声明损坏', 409
|
|
);
|
|
}
|
|
$canonicalIdentity = [
|
|
'contract' => 1,
|
|
'reservation_id' => (int) $source->id,
|
|
'host_id' => (int) $source->host_id,
|
|
'uuid' => strtolower((string) $migration->source_uuid),
|
|
'network_attachment_id' =>
|
|
(int) $debt->source_network_attachment_id,
|
|
'claim_set_sha256' => $claimIdentity->digest(),
|
|
'public_network_sha256' => $network->fingerprint(),
|
|
];
|
|
$canonicalJson = json_encode(
|
|
$canonicalIdentity,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
);
|
|
if ((int) $debt->source_network_attachment_id < 1
|
|
|| (int) $claims[4]->network_attachment_id
|
|
!== (int) $debt->source_network_attachment_id
|
|
|| (int) $claims[6]->network_attachment_id
|
|
!== (int) $debt->source_network_attachment_id
|
|
|| !hash_equals(
|
|
$claimIdentity->digest(),
|
|
(string) $debt->source_claim_set_sha256
|
|
)
|
|
|| !hash_equals(
|
|
$network->fingerprint(),
|
|
(string) $debt->source_public_network_sha256
|
|
)
|
|
|| !hash_equals(
|
|
$canonicalJson,
|
|
(string) $debt->source_identity_json
|
|
)
|
|
|| !hash_equals(
|
|
hash('sha256', $canonicalJson),
|
|
(string) $debt->source_identity_sha256
|
|
)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源冻结双栈身份不一致', 409
|
|
);
|
|
}
|
|
|
|
$addresses = DB::table('cloud_public_ips')->whereIn('id', [
|
|
(int) $claims[4]->public_ip_id,
|
|
(int) $claims[6]->public_ip_id,
|
|
])->orderBy('id')->get()->keyBy('id');
|
|
foreach ([4, 6] as $family) {
|
|
$claim = $claims[$family];
|
|
$address = $addresses->get((int) $claim->public_ip_id);
|
|
if (!is_object($address)
|
|
|| (int) $address->host_id !== (int) $source->host_id
|
|
|| (int) $address->address_family !== $family
|
|
|| (int) $address->public_ip_pool_id
|
|
!== (int) $claim->public_ip_pool_id
|
|
|| !hash_equals(
|
|
(string) $address->address . '/'
|
|
. (int) $address->prefix_length,
|
|
$network->address($family)
|
|
)
|
|
|| (int) ($address->reservation_id ?? 0)
|
|
=== (int) $source->id
|
|
|| (int) ($address->reservation_ip_claim_id ?? 0)
|
|
=== (int) $claim->id
|
|
|| hash_equals(
|
|
(string) $source->operation_id,
|
|
(string) ($address->operation_id ?? '')
|
|
)) {
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
'PeerMigration 已释放源公网地址仍保留旧归属', 409
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|