|
|
|
|
@ -57,6 +57,157 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
}, 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Freeze one child of a pool-replacement batch inside the caller's outer
|
|
|
|
|
* transaction. The batch coordinator already owns the canonical actor ->
|
|
|
|
|
* parent -> instances -> host/agent -> transitions -> resource pools ->
|
|
|
|
|
* attachments/pools -> reservations -> claims/IPs lock chain. This seam
|
|
|
|
|
* therefore performs no lock acquisition and never opens a transaction.
|
|
|
|
|
*/
|
|
|
|
|
public static function beginPoolReplacementBatch(
|
|
|
|
|
CloudPublicNetworkTransitionCommand $command,
|
|
|
|
|
CloudPublicNetworkTransitionBatchPlan $plan
|
|
|
|
|
): CloudPublicNetworkTransitionDispatch {
|
|
|
|
|
$actorUser = $plan->actor;
|
|
|
|
|
$parent = $plan->parent;
|
|
|
|
|
$item = $plan->item;
|
|
|
|
|
$batch = $plan->batch;
|
|
|
|
|
$instance = $plan->instance;
|
|
|
|
|
$host = $plan->host;
|
|
|
|
|
$agent = $plan->agent;
|
|
|
|
|
$resourcePool = $plan->resourcePool;
|
|
|
|
|
$reservation = $plan->reservation;
|
|
|
|
|
$sourceAttachment = $plan->attachment;
|
|
|
|
|
$allClaims = $plan->allClaimsSorted;
|
|
|
|
|
$activeClaims = $plan->activeClaimsByFamily;
|
|
|
|
|
$sourceIps = $plan->sourceIpsByFamily;
|
|
|
|
|
$pools = $plan->poolsById;
|
|
|
|
|
$ips = $plan->ipsById;
|
|
|
|
|
$targetIp = $plan->exactTargetIp;
|
|
|
|
|
$family = (int) $item->address_family;
|
|
|
|
|
$targets = $command->targets();
|
|
|
|
|
if ($command->kind() !== 'pool_replacement'
|
|
|
|
|
|| DB::connection()->transactionLevel() < 1
|
|
|
|
|
|| (int) $actorUser->id !== $command->actorUserId()
|
|
|
|
|
|| (int) $parent->id !== (int) $command->parentJobId()
|
|
|
|
|
|| (string) $parent->kind
|
|
|
|
|
!== 'public_ip_pool_replacement_batch'
|
|
|
|
|
|| (string) $parent->execution_mode !== 'local'
|
|
|
|
|
|| $parent->parent_job_id !== null
|
|
|
|
|
|| $parent->host_id !== null
|
|
|
|
|
|| $parent->public_network_transition_id !== null
|
|
|
|
|
|| (int) $parent->user_id !== $command->actorUserId()
|
|
|
|
|
|| (int) $item->id !== (int) $command->batchItemId()
|
|
|
|
|
|| (int) $item->batch_id !== (int) $batch->id
|
|
|
|
|
|| (int) $item->instance_id !== $command->instanceId()
|
|
|
|
|
|| (string) $item->state !== 'preparing'
|
|
|
|
|
|| $item->child_job_id !== null
|
|
|
|
|
|| (int) $batch->parent_job_id !== (int) $parent->id
|
|
|
|
|
|| (int) $batch->requested_by_user_id
|
|
|
|
|
!== $command->actorUserId()
|
|
|
|
|
|| (int) $batch->host_id !== (int) $host->id
|
|
|
|
|
|| (string) $batch->state !== 'running'
|
|
|
|
|
|| (int) ($batch->active_claim ?? 0) !== 1
|
|
|
|
|
|| (int) $instance->id !== $command->instanceId()
|
|
|
|
|
|| (int) $instance->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $agent->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $resourcePool->id !== (int) $instance->resource_pool_id
|
|
|
|
|
|| (int) $reservation->id !== (int) $instance->reservation_id
|
|
|
|
|
|| (int) $reservation->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $reservation->instance_id !== (int) $instance->id
|
|
|
|
|
|| (int) $reservation->resource_pool_id
|
|
|
|
|
!== (int) $resourcePool->id
|
|
|
|
|
|| (string) $reservation->state !== 'committed'
|
|
|
|
|
|| (int) $item->source_reservation_id !== (int) $reservation->id
|
|
|
|
|
|| !in_array($family, self::FAMILIES, true)
|
|
|
|
|
|| array_keys($targets) !== [$family]
|
|
|
|
|
|| count($activeClaims) !== 2
|
|
|
|
|
|| !isset($activeClaims[4], $activeClaims[6])
|
|
|
|
|
|| !$sourceIps[4] instanceof CloudPublicIp
|
|
|
|
|
|| !$sourceIps[6] instanceof CloudPublicIp
|
|
|
|
|
|| (int) $activeClaims[$family]->public_ip_id
|
|
|
|
|
!== (int) $item->source_public_ip_id
|
|
|
|
|
|| (int) $targetIp->id !== (int) $item->target_public_ip_id) {
|
|
|
|
|
throw new CloudError('公网地址池迁移子任务身份无效', 409);
|
|
|
|
|
}
|
|
|
|
|
if (!$host instanceof CloudHost || !(bool) $host->enabled
|
|
|
|
|
|| !$agent instanceof CloudHostAgent || !$agent->isReady()
|
|
|
|
|
|| !$resourcePool instanceof CloudResourcePool
|
|
|
|
|
|| !$reservation instanceof CloudResourceReservation
|
|
|
|
|
|| (string) $reservation->state !== 'committed'
|
|
|
|
|
|| (int) $reservation->instance_id !== (int) $instance->id
|
|
|
|
|
|| (int) $reservation->host_id !== (int) $instance->host_id
|
|
|
|
|
|| (int) $reservation->resource_pool_id
|
|
|
|
|
!== (int) $instance->resource_pool_id
|
|
|
|
|
|| (int) $item->source_reservation_id !== (int) $reservation->id) {
|
|
|
|
|
throw new CloudError('公网地址池迁移资源账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$sourcePool = $pools->get(
|
|
|
|
|
(int) $activeClaims[$family]->public_ip_pool_id
|
|
|
|
|
);
|
|
|
|
|
$targetPool = $pools->get((int) $targetIp->public_ip_pool_id);
|
|
|
|
|
$attachmentId = (int) $activeClaims[4]->network_attachment_id;
|
|
|
|
|
$target = $targets[$family];
|
|
|
|
|
if (!$sourcePool instanceof CloudPublicIpPool
|
|
|
|
|
|| !$targetPool instanceof CloudPublicIpPool
|
|
|
|
|
|| !$sourceAttachment instanceof CloudHostNetworkAttachment
|
|
|
|
|
|| (int) $activeClaims[6]->network_attachment_id !== $attachmentId
|
|
|
|
|
|| (int) $item->source_network_attachment_id !== $attachmentId
|
|
|
|
|
|| (int) $item->target_network_attachment_id !== $attachmentId
|
|
|
|
|
|| (int) $sourcePool->id
|
|
|
|
|
!== (int) $batch->source_public_ip_pool_id
|
|
|
|
|
|| (int) $targetPool->id
|
|
|
|
|
!== (int) $batch->target_public_ip_pool_id
|
|
|
|
|
|| (int) $sourcePool->address_family !== $family
|
|
|
|
|
|| (int) $targetPool->address_family !== $family
|
|
|
|
|
|| (int) $sourcePool->host_id !== (int) $instance->host_id
|
|
|
|
|
|| (int) $targetPool->host_id !== (int) $instance->host_id
|
|
|
|
|
|| (int) $sourcePool->network_attachment_id !== $attachmentId
|
|
|
|
|
|| (int) $targetPool->network_attachment_id !== $attachmentId
|
|
|
|
|
|| (int) $sourceAttachment->line_id
|
|
|
|
|
!== (int) $resourcePool->line_id
|
|
|
|
|
|| !hash_equals((string) $targetPool->pool_key,
|
|
|
|
|
(string) $target['pool_key'])
|
|
|
|
|
|| !hash_equals(
|
|
|
|
|
(string) $targetIp->address . '/'
|
|
|
|
|
. (int) $targetIp->prefix_length,
|
|
|
|
|
(string) $target['address_cidr']
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudError('公网地址池迁移冻结拓扑不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (self::FAMILIES as $sourceFamily) {
|
|
|
|
|
self::assertCommittedSourceLeg(
|
|
|
|
|
$activeClaims[$sourceFamily],
|
|
|
|
|
$sourceIps[$sourceFamily],
|
|
|
|
|
(int) $reservation->id,
|
|
|
|
|
(int) $instance->id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$dispatch = self::freezeTransitionLocked(
|
|
|
|
|
$command,
|
|
|
|
|
$actorUser,
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
$resourcePool,
|
|
|
|
|
$reservation,
|
|
|
|
|
$allClaims,
|
|
|
|
|
$activeClaims,
|
|
|
|
|
$sourceIps,
|
|
|
|
|
$sourceAttachment,
|
|
|
|
|
collect([(int) $sourceAttachment->id => $sourceAttachment]),
|
|
|
|
|
$pools,
|
|
|
|
|
$ips,
|
|
|
|
|
$targetIp,
|
|
|
|
|
$host
|
|
|
|
|
);
|
|
|
|
|
$item->child_job_id = $dispatch->jobId();
|
|
|
|
|
$item->state = 'queued';
|
|
|
|
|
$item->updated_at = time();
|
|
|
|
|
$item->save();
|
|
|
|
|
return $dispatch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reserve a transition while the caller owns the actor row and the outer
|
|
|
|
|
* transaction. This internal seam must never create a nested transaction:
|
|
|
|
|
@ -317,7 +468,41 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
$sourceIps[$family] = $sourceIp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sourceAttachment = $attachments->get($sourceAttachmentId);
|
|
|
|
|
return self::freezeTransitionLocked(
|
|
|
|
|
$command,
|
|
|
|
|
$actorUser,
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
$resourcePool,
|
|
|
|
|
$reservation,
|
|
|
|
|
$allClaims,
|
|
|
|
|
$activeClaims,
|
|
|
|
|
$sourceIps,
|
|
|
|
|
$attachments->get($sourceAttachmentId),
|
|
|
|
|
$attachments,
|
|
|
|
|
$pools,
|
|
|
|
|
$ips
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function freezeTransitionLocked(
|
|
|
|
|
CloudPublicNetworkTransitionCommand $command,
|
|
|
|
|
User $actorUser,
|
|
|
|
|
CloudInstance $instance,
|
|
|
|
|
CloudHostAgent $agent,
|
|
|
|
|
CloudResourcePool $resourcePool,
|
|
|
|
|
CloudResourceReservation $reservation,
|
|
|
|
|
array $allClaims,
|
|
|
|
|
array $activeClaims,
|
|
|
|
|
array $sourceIps,
|
|
|
|
|
CloudHostNetworkAttachment $sourceAttachment,
|
|
|
|
|
$attachments,
|
|
|
|
|
$pools,
|
|
|
|
|
$ips,
|
|
|
|
|
?CloudPublicIp $exactTargetIp = null,
|
|
|
|
|
?CloudHost $lockedHost = null
|
|
|
|
|
): CloudPublicNetworkTransitionDispatch {
|
|
|
|
|
$reservationId = (int) $reservation->id;
|
|
|
|
|
$sourceSnapshot = self::snapshot(
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
@ -339,19 +524,57 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
);
|
|
|
|
|
self::assertInstanceSnapshot($instance, $sourceSnapshot);
|
|
|
|
|
|
|
|
|
|
[$targetAttachment, $targetClaims, $targetIps, $changed] =
|
|
|
|
|
self::selectTargetLocked(
|
|
|
|
|
$command,
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
$sourceAttachment,
|
|
|
|
|
$activeClaims,
|
|
|
|
|
$sourceIps,
|
|
|
|
|
$attachments,
|
|
|
|
|
$pools,
|
|
|
|
|
$ips,
|
|
|
|
|
(int) $resourcePool->line_id
|
|
|
|
|
if ($exactTargetIp instanceof CloudPublicIp) {
|
|
|
|
|
$targetFamilies = array_keys($command->targets());
|
|
|
|
|
$family = count($targetFamilies) === 1
|
|
|
|
|
? (int) $targetFamilies[0] : 0;
|
|
|
|
|
$targetPool = $pools->get(
|
|
|
|
|
(int) $exactTargetIp->public_ip_pool_id
|
|
|
|
|
);
|
|
|
|
|
if (!in_array($family, self::FAMILIES, true)
|
|
|
|
|
|| !$targetPool instanceof CloudPublicIpPool
|
|
|
|
|
|| (int) $targetPool->address_family !== $family
|
|
|
|
|
|| (int) $targetPool->network_attachment_id
|
|
|
|
|
!== (int) $sourceAttachment->id
|
|
|
|
|
|| !self::poolUsable(
|
|
|
|
|
$targetPool,
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
$attachments,
|
|
|
|
|
(int) $resourcePool->line_id
|
|
|
|
|
)
|
|
|
|
|
|| !self::ipAvailableForPool(
|
|
|
|
|
$exactTargetIp,
|
|
|
|
|
$targetPool,
|
|
|
|
|
$lockedHost
|
|
|
|
|
)
|
|
|
|
|
|| (int) $exactTargetIp->id === (int) $sourceIps[$family]->id) {
|
|
|
|
|
throw new CloudError('公网地址池迁移精确目标已不可用', 409);
|
|
|
|
|
}
|
|
|
|
|
$targetAttachment = $sourceAttachment;
|
|
|
|
|
$targetClaims = $activeClaims;
|
|
|
|
|
$targetIps = $sourceIps;
|
|
|
|
|
$targetClaims[$family] = (object) [
|
|
|
|
|
'network_attachment_id' => (int) $sourceAttachment->id,
|
|
|
|
|
'public_ip_pool_id' => (int) $targetPool->id,
|
|
|
|
|
];
|
|
|
|
|
$targetIps[$family] = $exactTargetIp;
|
|
|
|
|
$changed = [$family => true];
|
|
|
|
|
} else {
|
|
|
|
|
[$targetAttachment, $targetClaims, $targetIps, $changed] =
|
|
|
|
|
self::selectTargetLocked(
|
|
|
|
|
$command,
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
$sourceAttachment,
|
|
|
|
|
$activeClaims,
|
|
|
|
|
$sourceIps,
|
|
|
|
|
$attachments,
|
|
|
|
|
$pools,
|
|
|
|
|
$ips,
|
|
|
|
|
(int) $resourcePool->line_id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$targetSnapshot = self::snapshot(
|
|
|
|
|
$instance,
|
|
|
|
|
$agent,
|
|
|
|
|
@ -465,10 +688,30 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
if ($updated !== 1) {
|
|
|
|
|
throw new CloudError('目标公网地址声明绑定失败', 409);
|
|
|
|
|
}
|
|
|
|
|
$targetClaim = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', $claimId)->first();
|
|
|
|
|
$boundIp = CloudPublicIp::where('id', (int) $targetIp->id)
|
|
|
|
|
->first();
|
|
|
|
|
$targetClaim = (object) [
|
|
|
|
|
'id' => $claimId,
|
|
|
|
|
'reservation_id' => $reservationId,
|
|
|
|
|
'address_family' => $family,
|
|
|
|
|
'claim_generation' => $generation,
|
|
|
|
|
'active_claim' => null,
|
|
|
|
|
'network_attachment_id' =>
|
|
|
|
|
(int) $targetPool->network_attachment_id,
|
|
|
|
|
'public_ip_pool_id' => (int) $targetPool->id,
|
|
|
|
|
'public_ip_id' => (int) $targetIp->id,
|
|
|
|
|
'operation_id' => $operationId,
|
|
|
|
|
'state' => 'reserved',
|
|
|
|
|
'released_at' => null,
|
|
|
|
|
'release_reason' => '',
|
|
|
|
|
'created_at' => $now,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
];
|
|
|
|
|
$targetIp->state = 'reserved';
|
|
|
|
|
$targetIp->reservation_id = $reservationId;
|
|
|
|
|
$targetIp->reservation_ip_claim_id = $claimId;
|
|
|
|
|
$targetIp->instance_id = null;
|
|
|
|
|
$targetIp->operation_id = $operationId;
|
|
|
|
|
$targetIp->updated_at = $now;
|
|
|
|
|
$boundIp = $targetIp;
|
|
|
|
|
$effectiveClaims[$family] = $targetClaim;
|
|
|
|
|
$effectiveIps[$family] = $boundIp;
|
|
|
|
|
self::insertTransitionLeg(
|
|
|
|
|
@ -496,7 +739,7 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
$job->operation_id = $operationId;
|
|
|
|
|
$job->request_idempotency_key = $command->requestKey();
|
|
|
|
|
$job->request_fingerprint = $command->fingerprint();
|
|
|
|
|
$job->parent_job_id = null;
|
|
|
|
|
$job->parent_job_id = $command->parentJobId();
|
|
|
|
|
$job->invoice_id = $invoice?->id;
|
|
|
|
|
$job->instance_id = (int) $instance->id;
|
|
|
|
|
$job->public_network_transition_id = (int) $transition->id;
|
|
|
|
|
@ -2568,7 +2811,7 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipAvailableForPool(CloudPublicIp $ip,
|
|
|
|
|
CloudPublicIpPool $pool): bool
|
|
|
|
|
CloudPublicIpPool $pool, ?CloudHost $lockedHost = null): bool
|
|
|
|
|
{
|
|
|
|
|
if ((string) $ip->state !== 'available'
|
|
|
|
|
|| $ip->reservation_id !== null
|
|
|
|
|
@ -2577,7 +2820,7 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
self::assertAddressInPool($ip, $pool);
|
|
|
|
|
self::assertAddressInPool($ip, $pool, $lockedHost);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
return false;
|
|
|
|
|
@ -2661,7 +2904,7 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertAddressInPool(CloudPublicIp $ip,
|
|
|
|
|
CloudPublicIpPool $pool): void
|
|
|
|
|
CloudPublicIpPool $pool, ?CloudHost $lockedHost = null): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$contract = CloudPublicIpPoolContract::fromRow($pool);
|
|
|
|
|
@ -2678,7 +2921,9 @@ final class CloudPublicNetworkTransitionWorkflow
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
|
}
|
|
|
|
|
$management = CloudPanelUsageService::managementAddress(
|
|
|
|
|
CloudHost::find((int) $pool->host_id)
|
|
|
|
|
$lockedHost instanceof CloudHost
|
|
|
|
|
? $lockedHost
|
|
|
|
|
: CloudHost::find((int) $pool->host_id)
|
|
|
|
|
);
|
|
|
|
|
if (is_string($management) && $management !== ''
|
|
|
|
|
&& $address->equals(CloudIpAddress::parse($management))) {
|
|
|
|
|
|