|
|
|
|
@ -12,8 +12,6 @@ use App\Models\CloudHostStorageConfiguration;
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Models\CloudNetworkLine;
|
|
|
|
|
use App\Models\CloudOffer;
|
|
|
|
|
use App\Models\CloudPublicIp;
|
|
|
|
|
use App\Models\CloudPublicIpPool;
|
|
|
|
|
use App\Models\CloudRegion;
|
|
|
|
|
use App\Models\CloudResourcePool;
|
|
|
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
|
@ -891,327 +889,6 @@ final class CloudPlacementService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reserve target capacity and a fresh IPv4 for an administrator restore.
|
|
|
|
|
* The restore row and operation ID are created before this method runs.
|
|
|
|
|
*
|
|
|
|
|
* @return array{reservation:CloudResourceReservation,host:CloudHost,
|
|
|
|
|
* public_ip:CloudPublicIp,network:array<string,mixed>}
|
|
|
|
|
*/
|
|
|
|
|
public static function reserveBackupRestore(CloudBackupRestore $restore,
|
|
|
|
|
int $userId, array $resources, int $requiredResourcePoolId,
|
|
|
|
|
int $additionalDiskGib = 0): array
|
|
|
|
|
{
|
|
|
|
|
self::assertBackupRestoreTransitionReady($restore);
|
|
|
|
|
$resources = self::upgradeResources($resources);
|
|
|
|
|
$capacityResources = $resources;
|
|
|
|
|
$capacityResources['disk_gib'] += max(0, $additionalDiskGib);
|
|
|
|
|
$hostId = (int) $restore->target_host_id;
|
|
|
|
|
if ($hostId < 1 || $requiredResourcePoolId < 1) {
|
|
|
|
|
throw new CloudError('备份恢复目标身份无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$poolRef = CloudResourcePool::find($requiredResourcePoolId);
|
|
|
|
|
$lineRef = $poolRef === null ? null
|
|
|
|
|
: CloudNetworkLine::find((int) $poolRef->line_id);
|
|
|
|
|
$region = $lineRef === null ? null
|
|
|
|
|
: CloudRegion::where('id', (int) $lineRef->region_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$line = $lineRef === null ? null
|
|
|
|
|
: CloudNetworkLine::where('id', (int) $lineRef->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$pool = $poolRef === null ? null
|
|
|
|
|
: CloudResourcePool::where('id', (int) $poolRef->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$host = CloudHost::where('id', $hostId)->lockForUpdate()->first();
|
|
|
|
|
$membership = CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id', $hostId
|
|
|
|
|
)->where('resource_pool_id', $requiredResourcePoolId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$policy = CloudHostCapacityPolicy::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$storage = CloudHostStorageConfiguration::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$agent = CloudHostAgent::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($host === null || $membership === null || $pool === null
|
|
|
|
|
|| $line === null || $region === null || $policy === null
|
|
|
|
|
|| $storage === null || !$storage->isApplied()
|
|
|
|
|
|| $agent === null || !$agent->isReady()
|
|
|
|
|
|| !(bool) $host->enabled
|
|
|
|
|
|| (string) $membership->scheduler_state !== 'active'
|
|
|
|
|
|| (int) $membership->resource_pool_id !== $requiredResourcePoolId
|
|
|
|
|
|| (int) $pool->id !== $requiredResourcePoolId
|
|
|
|
|
|| (int) $pool->line_id !== (int) $line->id
|
|
|
|
|
|| (int) $line->region_id !== (int) $region->id
|
|
|
|
|
|| (string) $pool->status !== 'active'
|
|
|
|
|
|| (string) $line->status !== 'active'
|
|
|
|
|
|| (string) $region->status !== 'active') {
|
|
|
|
|
throw new CloudError('所选目标宿主机暂不支持备份恢复', 409);
|
|
|
|
|
}
|
|
|
|
|
$networkCandidates = CloudPlacementNetworkService::lockedCandidates(
|
|
|
|
|
$hostId,
|
|
|
|
|
(int) $line->id,
|
|
|
|
|
$agent
|
|
|
|
|
);
|
|
|
|
|
if ($networkCandidates === []) {
|
|
|
|
|
throw new CloudError('目标宿主机的线路网络接入尚未就绪', 409);
|
|
|
|
|
}
|
|
|
|
|
$reservations = CloudResourceReservation::where('host_id', $hostId)
|
|
|
|
|
->whereIn('state', ['held', 'uncertain', 'committed'])
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
if (!self::hasCapacity(
|
|
|
|
|
$policy,
|
|
|
|
|
CloudPanelUsageService::forHost($hostId, $reservations),
|
|
|
|
|
$capacityResources
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudError('目标宿主机没有足够的可用容量', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$network = CloudPlacementNetworkService::lockAvailableSelection(
|
|
|
|
|
$hostId,
|
|
|
|
|
$networkCandidates
|
|
|
|
|
);
|
|
|
|
|
if ($network === null) {
|
|
|
|
|
throw new CloudError('目标宿主机没有可分配的公网 IP', 409);
|
|
|
|
|
}
|
|
|
|
|
$attachment = $network['attachment'];
|
|
|
|
|
$selectedPool = $network['public_pool'];
|
|
|
|
|
$ip = $network['public_ip'];
|
|
|
|
|
$dns = self::poolDnsServers($selectedPool);
|
|
|
|
|
$reservation = new CloudResourceReservation();
|
|
|
|
|
$reservation->reservation_key = bin2hex(random_bytes(16));
|
|
|
|
|
$reservation->operation_id = (string) $restore->operation_id;
|
|
|
|
|
$reservation->order_id = null;
|
|
|
|
|
$reservation->backup_restore_id = (int) $restore->id;
|
|
|
|
|
$reservation->offer_id = null;
|
|
|
|
|
$reservation->resource_pool_id = (int) $pool->id;
|
|
|
|
|
$reservation->host_id = $hostId;
|
|
|
|
|
$reservation->user_id = $userId;
|
|
|
|
|
$reservation->instance_id = null;
|
|
|
|
|
$reservation->network_attachment_id = (int) $attachment->id;
|
|
|
|
|
$reservation->public_ip_pool_id = (int) $selectedPool->id;
|
|
|
|
|
$reservation->public_ip_id = (int) $ip->id;
|
|
|
|
|
$reservation->cpu_cores = $resources['cpu_cores'];
|
|
|
|
|
$reservation->memory_mib = $resources['memory_mib'];
|
|
|
|
|
// A maintenance restore reserves the root disk plus every transported
|
|
|
|
|
// snapshot volume. The finalizer normalizes this temporary claim back
|
|
|
|
|
// to the root disk once the snapshot catalog is atomically rebound.
|
|
|
|
|
$reservation->disk_gib = $capacityResources['disk_gib'];
|
|
|
|
|
$reservation->state = 'held';
|
|
|
|
|
$reservation->expires_at = null;
|
|
|
|
|
$reservation->released_at = null;
|
|
|
|
|
$reservation->release_reason = '';
|
|
|
|
|
$reservation->created_at = $reservation->updated_at = time();
|
|
|
|
|
$reservation->save();
|
|
|
|
|
|
|
|
|
|
$ip->state = 'reserved';
|
|
|
|
|
$ip->reservation_id = (int) $reservation->id;
|
|
|
|
|
$ip->instance_id = null;
|
|
|
|
|
$ip->operation_id = (string) $restore->operation_id;
|
|
|
|
|
$ip->updated_at = time();
|
|
|
|
|
$ip->save();
|
|
|
|
|
return [
|
|
|
|
|
'reservation' => $reservation,
|
|
|
|
|
'host' => $host,
|
|
|
|
|
'public_ip' => $ip,
|
|
|
|
|
'network' => [
|
|
|
|
|
'network' => (string) $attachment->network_selector,
|
|
|
|
|
'public_pool_key' => (string) $selectedPool->pool_key,
|
|
|
|
|
'public_pool_cidr' => (string) $selectedPool->cidr,
|
|
|
|
|
'public_ip_cidr' => (string) $ip->address . '/'
|
|
|
|
|
. (int) $ip->prefix_length,
|
|
|
|
|
'gateway' => (string) $selectedPool->gateway,
|
|
|
|
|
'dns_servers' => $dns,
|
|
|
|
|
'resource_pool_id' => (int) $pool->id,
|
|
|
|
|
'resource_pool_name' => (string) $pool->name,
|
|
|
|
|
'line_id' => (int) $line->id,
|
|
|
|
|
'line_name' => (string) $line->name,
|
|
|
|
|
'region_id' => (int) $region->id,
|
|
|
|
|
'region_name' => (string) $region->name,
|
|
|
|
|
'country_code' => strtoupper((string) $region->country_code),
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Re-open an already held restore placement without selecting new capacity.
|
|
|
|
|
*
|
|
|
|
|
* Maintenance migration reserves its target before the source is stopped.
|
|
|
|
|
* The backup finalizer must therefore consume that exact host/address
|
|
|
|
|
* reservation instead of racing a second scheduler pass after downtime has
|
|
|
|
|
* begun.
|
|
|
|
|
*
|
|
|
|
|
* @return array{reservation:CloudResourceReservation,network:array<string,mixed>}
|
|
|
|
|
*/
|
|
|
|
|
public static function preparedBackupRestorePlacement(
|
|
|
|
|
CloudBackupRestore $restore,
|
|
|
|
|
int $instanceId
|
|
|
|
|
): array {
|
|
|
|
|
self::assertBackupRestoreTransitionReady($restore);
|
|
|
|
|
$reservation = CloudResourceReservation::where(
|
|
|
|
|
'backup_restore_id',
|
|
|
|
|
(int) $restore->id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$ip = $reservation === null ? null : CloudPublicIp::where(
|
|
|
|
|
'id',
|
|
|
|
|
(int) $reservation->public_ip_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$pool = $reservation === null ? null : CloudPublicIpPool::where(
|
|
|
|
|
'id',
|
|
|
|
|
(int) $reservation->public_ip_pool_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$attachment = $reservation === null ? null
|
|
|
|
|
: CloudHostNetworkAttachment::where(
|
|
|
|
|
'id',
|
|
|
|
|
(int) $reservation->network_attachment_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$resourcePool = $reservation === null ? null
|
|
|
|
|
: CloudResourcePool::find((int) $reservation->resource_pool_id);
|
|
|
|
|
$membership = $reservation === null ? null
|
|
|
|
|
: CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id',
|
|
|
|
|
(int) $reservation->host_id
|
|
|
|
|
)->where(
|
|
|
|
|
'resource_pool_id',
|
|
|
|
|
(int) $reservation->resource_pool_id
|
|
|
|
|
)->first();
|
|
|
|
|
if ($reservation === null || $ip === null || $pool === null
|
|
|
|
|
|| $attachment === null || $resourcePool === null
|
|
|
|
|
|| $membership === null
|
|
|
|
|
|| (int) $restore->reservation_id !== (int) $reservation->id
|
|
|
|
|
|| (int) $restore->target_host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $reservation->backup_restore_id !== (int) $restore->id
|
|
|
|
|
|| (int) $reservation->instance_id !== $instanceId
|
|
|
|
|
|| (string) $reservation->state !== 'held'
|
|
|
|
|
|| (string) $membership->scheduler_state !== 'active'
|
|
|
|
|
|| (int) $attachment->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $attachment->line_id !== (int) $resourcePool->line_id
|
|
|
|
|
|| (int) $pool->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $pool->network_attachment_id !== (int) $attachment->id
|
|
|
|
|
|| (int) $ip->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $ip->public_ip_pool_id !== (int) $pool->id
|
|
|
|
|
|| (string) $ip->state !== 'reserved'
|
|
|
|
|
|| (int) ($ip->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| $ip->instance_id !== null
|
|
|
|
|
|| !hash_equals(
|
|
|
|
|
(string) $restore->operation_id,
|
|
|
|
|
(string) $reservation->operation_id
|
|
|
|
|
)
|
|
|
|
|
|| !hash_equals(
|
|
|
|
|
(string) $reservation->operation_id,
|
|
|
|
|
(string) ($ip->operation_id ?? '')
|
|
|
|
|
)
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $pool->pool_key) !== 1
|
|
|
|
|
|| !CloudNetworkSelector::isValid(
|
|
|
|
|
(string) $attachment->network_selector
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
|
|
|
'维护迁移目标预留在源实例停机后发生变化',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'reservation' => $reservation,
|
|
|
|
|
'network' => [
|
|
|
|
|
'network' => (string) $attachment->network_selector,
|
|
|
|
|
'public_pool_key' => (string) $pool->pool_key,
|
|
|
|
|
'public_pool_cidr' => (string) $pool->cidr,
|
|
|
|
|
'public_ip_cidr' => (string) $ip->address . '/'
|
|
|
|
|
. (int) $ip->prefix_length,
|
|
|
|
|
'gateway' => (string) $pool->gateway,
|
|
|
|
|
'dns_servers' => self::poolDnsServers($pool),
|
|
|
|
|
'resource_pool_id' => (int) $reservation->resource_pool_id,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Commit, release, or fence one restore reservation after exact evidence. */
|
|
|
|
|
public static function finishBackupRestore(CloudBackupRestore $restore,
|
|
|
|
|
int $instanceId, string $publicIpCidr, string $status): void
|
|
|
|
|
{
|
|
|
|
|
self::assertBackupRestoreTransitionReady($restore);
|
|
|
|
|
$host = CloudHost::where('id', (int) $restore->target_host_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$reservation = CloudResourceReservation::where(
|
|
|
|
|
'backup_restore_id', (int) $restore->id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$ip = $reservation === null ? null : CloudPublicIp::where(
|
|
|
|
|
'id', (int) $reservation->public_ip_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
if ($host === null || $reservation === null || $ip === null
|
|
|
|
|
|| (int) $reservation->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $reservation->backup_restore_id !== (int) $restore->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) $restore->operation_id)
|
|
|
|
|
|| !hash_equals((string) $ip->address . '/'
|
|
|
|
|
. (int) $ip->prefix_length, $publicIpCidr)) {
|
|
|
|
|
throw new CloudError('备份恢复的资源预留账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ((string) $reservation->state === 'committed') {
|
|
|
|
|
if ((string) $ip->state !== 'assigned'
|
|
|
|
|
|| (int) $reservation->instance_id !== $instanceId
|
|
|
|
|
|| (int) $ip->instance_id !== $instanceId
|
|
|
|
|
|| (int) $ip->reservation_id !== (int) $reservation->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) $ip->operation_id)) {
|
|
|
|
|
throw new CloudError('已恢复实例的资源账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($status !== 'succeeded' && $status !== 'uncertain') {
|
|
|
|
|
throw new CloudError('已恢复实例不能回滚资源预留', 409);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $reservation->state === 'released') {
|
|
|
|
|
if ((string) $ip->state !== 'available'
|
|
|
|
|
|| $ip->reservation_id !== null
|
|
|
|
|
|| $ip->instance_id !== null
|
|
|
|
|
|| $ip->operation_id !== null) {
|
|
|
|
|
throw new CloudError('已释放的备份恢复资源账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($status !== 'failed') {
|
|
|
|
|
throw new CloudError('已释放的备份恢复不能重新提交资源', 409);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!in_array((string) $reservation->state,
|
|
|
|
|
['held', 'uncertain'], true)
|
|
|
|
|
|| !in_array((string) $ip->state,
|
|
|
|
|
['reserved', 'uncertain'], true)
|
|
|
|
|
|| (int) ($ip->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))
|
|
|
|
|
|| (int) ($reservation->instance_id ?? 0) !== $instanceId) {
|
|
|
|
|
throw new CloudError('备份恢复的资源预留状态无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$now = time();
|
|
|
|
|
if ($status === 'succeeded') {
|
|
|
|
|
$reservation->state = 'committed';
|
|
|
|
|
$reservation->instance_id = $instanceId;
|
|
|
|
|
$reservation->expires_at = null;
|
|
|
|
|
$ip->state = 'assigned';
|
|
|
|
|
$ip->instance_id = $instanceId;
|
|
|
|
|
} elseif ($status === 'failed') {
|
|
|
|
|
$reservation->state = 'released';
|
|
|
|
|
$reservation->released_at = $now;
|
|
|
|
|
$reservation->release_reason = 'backup_restore_not_applied';
|
|
|
|
|
$reservation->expires_at = null;
|
|
|
|
|
$ip->state = 'available';
|
|
|
|
|
$ip->reservation_id = null;
|
|
|
|
|
$ip->instance_id = null;
|
|
|
|
|
$ip->operation_id = null;
|
|
|
|
|
} elseif ($status === 'uncertain') {
|
|
|
|
|
$reservation->state = 'uncertain';
|
|
|
|
|
$reservation->expires_at = null;
|
|
|
|
|
$ip->state = 'uncertain';
|
|
|
|
|
} else {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid backup restore result');
|
|
|
|
|
}
|
|
|
|
|
$reservation->updated_at = $ip->updated_at = $now;
|
|
|
|
|
$reservation->save();
|
|
|
|
|
$ip->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Keep an ambiguous create and both addresses unavailable for reconciliation. */
|
|
|
|
|
public static function markUncertain(CloudOrder $order): void
|
|
|
|
|
{
|
|
|
|
|
@ -1311,144 +988,6 @@ final class CloudPlacementService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Atomically settle the PHP-side address ledger for a network-change job. */
|
|
|
|
|
public static function finishNetworkChange(CloudInstance $instance,
|
|
|
|
|
string $operationId, int $sourceIpId, int $targetIpId,
|
|
|
|
|
int $sourceAttachmentId, int $targetAttachmentId, int $sourcePoolId,
|
|
|
|
|
int $targetPoolId, string $outcome): void
|
|
|
|
|
{
|
|
|
|
|
CloudPublicNetworkTransitionGate::assertWriterReady();
|
|
|
|
|
if (!in_array($outcome, ['succeeded', 'failed', 'uncertain'], true)
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1
|
|
|
|
|
|| min($sourceIpId, $targetIpId, $sourceAttachmentId,
|
|
|
|
|
$targetAttachmentId, $sourcePoolId, $targetPoolId) < 1
|
|
|
|
|
|| $sourceIpId === $targetIpId) {
|
|
|
|
|
throw new CloudError('公网 IP 变更账本参数无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$host = CloudHost::where('id', (int) $instance->host_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
|
|
|
|
|
// Host is the sentinel for every host-scoped writer. Lock attachment
|
|
|
|
|
// and pool rows in numeric order before the reservation/IP ledger so
|
|
|
|
|
// placement and network changes cannot invert their lock hierarchy.
|
|
|
|
|
$attachmentIds = array_values(array_unique([
|
|
|
|
|
$sourceAttachmentId,
|
|
|
|
|
$targetAttachmentId,
|
|
|
|
|
]));
|
|
|
|
|
sort($attachmentIds, SORT_NUMERIC);
|
|
|
|
|
$attachments = CloudHostNetworkAttachment::whereIn(
|
|
|
|
|
'id',
|
|
|
|
|
$attachmentIds
|
|
|
|
|
)->orderBy('id')->lockForUpdate()->get()->keyBy('id');
|
|
|
|
|
$sourceAttachment = $attachments->get($sourceAttachmentId);
|
|
|
|
|
$targetAttachment = $attachments->get($targetAttachmentId);
|
|
|
|
|
$poolIds = array_values(array_unique([$sourcePoolId, $targetPoolId]));
|
|
|
|
|
sort($poolIds, SORT_NUMERIC);
|
|
|
|
|
$pools = CloudPublicIpPool::whereIn('id', $poolIds)->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
|
|
|
$sourcePool = $pools->get($sourcePoolId);
|
|
|
|
|
$targetPool = $pools->get($targetPoolId);
|
|
|
|
|
$reservation = $instance->reservation_id === null ? null
|
|
|
|
|
: CloudResourceReservation::where('id', (int) $instance->reservation_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$reservationPool = $reservation === null ? null
|
|
|
|
|
: CloudResourcePool::find((int) $reservation->resource_pool_id);
|
|
|
|
|
if ($host === null || $sourceAttachment === null
|
|
|
|
|
|| $targetAttachment === null
|
|
|
|
|
|| $sourcePool === null || $targetPool === null
|
|
|
|
|
|| $reservationPool === null
|
|
|
|
|
|| (int) $sourceAttachment->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $targetAttachment->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $reservation->resource_pool_id
|
|
|
|
|
!== (int) ($instance->resource_pool_id ?? 0)
|
|
|
|
|
|| (int) $sourceAttachment->line_id
|
|
|
|
|
!== (int) $reservationPool->line_id
|
|
|
|
|
|| (int) $targetAttachment->line_id
|
|
|
|
|
!== (int) $reservationPool->line_id
|
|
|
|
|
|| (int) $sourcePool->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $targetPool->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $sourcePool->network_attachment_id
|
|
|
|
|
!== $sourceAttachmentId
|
|
|
|
|
|| (int) $targetPool->network_attachment_id
|
|
|
|
|
!== $targetAttachmentId
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D', (string) $targetPool->pool_key) !== 1
|
|
|
|
|
|| $reservation === null
|
|
|
|
|
|| (string) $reservation->state !== 'committed'
|
|
|
|
|
|| (int) $reservation->host_id !== (int) $host->id
|
|
|
|
|
|| (int) ($reservation->instance_id ?? 0) !== (int) $instance->id
|
|
|
|
|
|| (int) ($reservation->network_attachment_id ?? 0)
|
|
|
|
|
!== $sourceAttachmentId
|
|
|
|
|
|| (int) ($reservation->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|
|
|
|| (int) $reservation->public_ip_id !== $sourceIpId) {
|
|
|
|
|
throw new CloudError('虚拟机公网 IP 资源预留不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ipIds = [$sourceIpId, $targetIpId];
|
|
|
|
|
sort($ipIds, SORT_NUMERIC);
|
|
|
|
|
$ips = CloudPublicIp::whereIn('id', $ipIds)->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
|
|
|
$source = $ips->get($sourceIpId);
|
|
|
|
|
$target = $ips->get($targetIpId);
|
|
|
|
|
if ($source === null || $target === null
|
|
|
|
|
|| (int) $source->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $target->host_id !== (int) $host->id
|
|
|
|
|
|| (int) ($source->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|
|
|
|| (int) ($target->public_ip_pool_id ?? 0) !== $targetPoolId
|
|
|
|
|
|| (string) $source->state !== 'assigned'
|
|
|
|
|
|| (int) ($source->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| (int) ($source->instance_id ?? 0) !== (int) $instance->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) ($source->operation_id ?? ''))
|
|
|
|
|
|| !in_array((string) $target->state, ['changing', 'uncertain'], true)
|
|
|
|
|
|| $target->reservation_id !== null
|
|
|
|
|
|| (int) ($target->instance_id ?? 0) !== (int) $instance->id
|
|
|
|
|
|| !hash_equals($operationId, (string) ($target->operation_id ?? ''))) {
|
|
|
|
|
throw new CloudError('公网 IP 变更账本关联不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($outcome === 'succeeded'
|
|
|
|
|
&& !self::publicIpBelongsToPool($target, $targetPool)) {
|
|
|
|
|
throw new CloudError('公网 IP 变更结果与地址池账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
if ($outcome === 'uncertain') {
|
|
|
|
|
$target->state = 'uncertain';
|
|
|
|
|
$target->updated_at = $now;
|
|
|
|
|
$target->save();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($outcome === 'failed') {
|
|
|
|
|
$target->state = 'available';
|
|
|
|
|
$target->reservation_id = null;
|
|
|
|
|
$target->instance_id = null;
|
|
|
|
|
$target->operation_id = null;
|
|
|
|
|
$target->updated_at = $now;
|
|
|
|
|
$target->save();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reservation_id is unique: release the old row before binding the new
|
|
|
|
|
// one, while both rows and the reservation remain locked.
|
|
|
|
|
$source->state = 'available';
|
|
|
|
|
$source->reservation_id = null;
|
|
|
|
|
$source->instance_id = null;
|
|
|
|
|
$source->operation_id = null;
|
|
|
|
|
$source->updated_at = $now;
|
|
|
|
|
$source->save();
|
|
|
|
|
|
|
|
|
|
$reservation->public_ip_id = $targetIpId;
|
|
|
|
|
$reservation->network_attachment_id = $targetAttachmentId;
|
|
|
|
|
$reservation->public_ip_pool_id = $targetPoolId;
|
|
|
|
|
$reservation->save();
|
|
|
|
|
|
|
|
|
|
$target->state = 'assigned';
|
|
|
|
|
$target->reservation_id = (int) $reservation->id;
|
|
|
|
|
$target->instance_id = (int) $instance->id;
|
|
|
|
|
$target->operation_id = (string) $reservation->operation_id;
|
|
|
|
|
$target->updated_at = $now;
|
|
|
|
|
$target->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function tryHost(int $hostId, CloudResourcePool $pool,
|
|
|
|
|
CloudOrder $order, CloudOffer $offer, array $resources,
|
|
|
|
|
array $payload,
|
|
|
|
|
@ -1619,33 +1158,6 @@ final class CloudPlacementService
|
|
|
|
|
return $payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Return the validated DNS snapshot frozen on one public address pool. */
|
|
|
|
|
private static function poolDnsServers(CloudPublicIpPool $pool): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$dnsServers = json_decode(
|
|
|
|
|
(string) $pool->dns_servers_json,
|
|
|
|
|
true,
|
|
|
|
|
16,
|
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable $error) {
|
|
|
|
|
throw new CloudError('公网地址池 DNS 配置损坏', 409);
|
|
|
|
|
}
|
|
|
|
|
if (!is_array($dnsServers) || !array_is_list($dnsServers)
|
|
|
|
|
|| $dnsServers === []
|
|
|
|
|
|| count($dnsServers) > 4
|
|
|
|
|
|| count(array_filter($dnsServers, static fn ($value): bool =>
|
|
|
|
|
is_string($value) && filter_var(
|
|
|
|
|
$value,
|
|
|
|
|
FILTER_VALIDATE_IP,
|
|
|
|
|
FILTER_FLAG_IPV4
|
|
|
|
|
) !== false
|
|
|
|
|
)) !== count($dnsServers)) {
|
|
|
|
|
throw new CloudError('公网地址池 DNS 配置损坏', 409);
|
|
|
|
|
}
|
|
|
|
|
return array_values($dnsServers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertOrderOffer(CloudOrder $order, CloudOffer $offer,
|
|
|
|
|
Shop $plan): void
|
|
|
|
|
@ -1699,15 +1211,6 @@ final class CloudPlacementService
|
|
|
|
|
return substr($reason, 0, 64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertBackupRestoreTransitionReady(
|
|
|
|
|
CloudBackupRestore $restore
|
|
|
|
|
): void {
|
|
|
|
|
if ($restore->backup_id === null) {
|
|
|
|
|
CloudPublicNetworkTransitionGate::
|
|
|
|
|
assertLegacyMigrationWriterRetired();
|
|
|
|
|
}
|
|
|
|
|
CloudPublicNetworkTransitionGate::assertPortableRestoreReady();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function markReservationReleased(
|
|
|
|
|
CloudResourceReservation $reservation,
|
|
|
|
|
@ -2055,87 +1558,5 @@ final class CloudPlacementService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function availablePublicIpQuery(int $hostId, int $publicPoolId,
|
|
|
|
|
int $prefixLength)
|
|
|
|
|
{
|
|
|
|
|
return CloudPublicIp::query()->where('host_id', $hostId)
|
|
|
|
|
->where('public_ip_pool_id', $publicPoolId)
|
|
|
|
|
->where('prefix_length', $prefixLength)
|
|
|
|
|
->where('state', 'available')
|
|
|
|
|
->whereNull('reservation_id')->whereNull('instance_id')
|
|
|
|
|
->whereNull('operation_id')->orderBy('id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function publicIpBelongsToPool(CloudPublicIp $ip,
|
|
|
|
|
CloudPublicIpPool $pool): bool
|
|
|
|
|
{
|
|
|
|
|
if ((int) $ip->prefix_length !== (int) $pool->guest_prefix_length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!self::ipv4BelongsToCidr(
|
|
|
|
|
(string) $ip->address, (string) $pool->cidr
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$blocked = [(string) $pool->gateway];
|
|
|
|
|
$managementAddress = CloudPanelUsageService::managementAddress(
|
|
|
|
|
CloudHost::find((int) $pool->host_id)
|
|
|
|
|
);
|
|
|
|
|
if ($managementAddress !== null) {
|
|
|
|
|
$blocked[] = $managementAddress;
|
|
|
|
|
}
|
|
|
|
|
foreach (['dns_servers_json', 'reserved_ips_json'] as $field) {
|
|
|
|
|
try {
|
|
|
|
|
$values = json_decode((string) $pool->{$field}, true,
|
|
|
|
|
16, JSON_THROW_ON_ERROR);
|
|
|
|
|
} catch (\Throwable $error) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!is_array($values) || !array_is_list($values)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
|
if (!is_string($value)
|
|
|
|
|
|| filter_var($value, FILTER_VALIDATE_IP,
|
|
|
|
|
FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$blocked[] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return !in_array((string) $ip->address, $blocked, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipv4BelongsToCidr(string $addressValue,
|
|
|
|
|
string $cidr): bool
|
|
|
|
|
{
|
|
|
|
|
$parts = explode('/', $cidr, 2);
|
|
|
|
|
if (count($parts) !== 2 || !ctype_digit($parts[1])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$prefix = (int) $parts[1];
|
|
|
|
|
$address = self::ipv4Number($addressValue);
|
|
|
|
|
$network = self::ipv4Number($parts[0]);
|
|
|
|
|
if ($address === null || $network === null || $prefix < 24 || $prefix > 30) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$block = 2 ** (32 - $prefix);
|
|
|
|
|
$base = (int) (floor($network / $block) * $block);
|
|
|
|
|
return $address > $base && $address < $base + $block - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipv4Number(string $value): ?int
|
|
|
|
|
{
|
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$packed = inet_pton($value);
|
|
|
|
|
if ($packed === false || strlen($packed) !== 4) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$parts = unpack('Nnumber', $packed);
|
|
|
|
|
return is_array($parts) && isset($parts['number'])
|
|
|
|
|
? (int) sprintf('%u', $parts['number']) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|