From b96d79ad049f15a7b3287c3c1f54eff7669e4635 Mon Sep 17 00:00:00 2001 From: codex Date: Tue, 3 Nov 2026 14:19:01 +0800 Subject: [PATCH] fix: fall back across dual-stack address pools --- .../CloudPublicNetworkAllocationService.php | 134 +++++++++++----- ...loudPublicNetworkAllocationServiceTest.php | 143 ++++++++++++++++++ 2 files changed, 237 insertions(+), 40 deletions(-) diff --git a/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php b/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php index c8613c53..4ae2b856 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php +++ b/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php @@ -59,7 +59,8 @@ final class CloudPublicNetworkAllocationService foreach (self::FAMILIES as $family) { $pool = $selection['pools'][$family]; $ip = self::reserveAddress($pool, $reservationId, - $operationId, $now); + $operationId, $now, + $selection['address_candidates'][$family] ?? null); $claimId = DB::table('cloud_resource_reservation_ips') ->insertGetId([ 'reservation_id' => $reservationId, @@ -462,27 +463,43 @@ final class CloudPublicNetworkAllocationService ->orderBy('address_family')->orderBy('priority')->orderBy('id') ->lockForUpdate()->get(); $pools = []; + $addressCandidates = []; foreach ($rows as $row) { $family = (int) $row->address_family; - if (!isset($pools[$family])) { - self::validatePool($row, $family); + self::validatePool($row, $family); + if (isset($pools[$family])) { + continue; + } + $candidate = self::lockReservableAddressCandidate($row); + if ($candidate !== null) { $pools[$family] = $row; + $addressCandidates[$family] = $candidate; } } if (isset($pools[4], $pools[6])) { - return ['attachment' => $attachment, 'pools' => $pools]; + return [ + 'attachment' => $attachment, + 'pools' => $pools, + 'address_candidates' => $addressCandidates, + ]; } } throw new CloudError('宿主机没有完整可用的双栈公网地址池', 409); } - private static function reserveAddress(object $pool, int $reservationId, - string $operationId, int $now): object - { + /** + * Returns either an existing available row or one free sparse IPv6 value. + * The caller already owns the candidate pool lock, so a later claim in the + * same transaction observes the same answer. + * + * @return null|array{row:?object,candidate:?CloudIpAddress, + * range_start:?CloudIpAddress,range_end:?CloudIpAddress} + */ + private static function lockReservableAddressCandidate( + object $pool + ): ?array { $family = (int) $pool->address_family; if ($family === 6) { - $materializedCount = DB::table('cloud_public_ips') - ->where('public_ip_pool_id', (int) $pool->id)->count(); $activeCount = DB::table('cloud_public_ips') ->where('public_ip_pool_id', (int) $pool->id) ->whereIn('state', [ @@ -490,7 +507,7 @@ final class CloudPublicNetworkAllocationService ])->count(); if ((int) $pool->max_allocations < 1 || $activeCount >= (int) $pool->max_allocations) { - throw new CloudError('公网 IPv6 地址池已经达到分配上限', 409); + return null; } } $available = DB::table('cloud_public_ips') @@ -501,26 +518,27 @@ final class CloudPublicNetworkAllocationService ->whereNull('operation_id')->orderBy('id')->lockForUpdate()->get(); foreach ($available as $row) { if (self::addressAllowedByPool($row, $pool)) { - return self::claimAddress($row, $reservationId, $operationId, - $now); + return [ + 'row' => $row, + 'candidate' => null, + 'range_start' => null, + 'range_end' => null, + ]; } } if ($family !== 6) { - throw new CloudError('公网 IPv4 地址池已经耗尽', 409); + return null; } - $rangeStart = CloudIpAddress::parse($pool->allocation_start ?? null); $rangeEnd = CloudIpAddress::parse($pool->allocation_end ?? null); - if ($rangeStart->family() !== 6 || $rangeEnd->family() !== 6 - || $rangeStart->compare($rangeEnd) > 0) { - throw new CloudError('公网 IPv6 稀疏分配范围损坏', 409); - } $candidate = $pool->allocation_cursor === null ? $rangeStart : CloudIpAddress::parse($pool->allocation_cursor); if ($candidate->compare($rangeStart) < 0 || $candidate->compare($rangeEnd) > 0) { $candidate = $rangeStart; } + $materializedCount = DB::table('cloud_public_ips') + ->where('public_ip_pool_id', (int) $pool->id)->count(); $attempts = max(1, $materializedCount + (int) $pool->max_allocations + count(self::decodedAddresses( @@ -539,32 +557,68 @@ final class CloudPublicNetworkAllocationService ->lockForUpdate()->first(); if ($existing === null && self::rawAddressAllowedByPool($candidate, $pool)) { - $id = DB::table('cloud_public_ips')->insertGetId([ - 'host_id' => (int) $pool->host_id, - 'public_ip_pool_id' => (int) $pool->id, - 'address_family' => 6, - 'address' => $candidate->text(), - 'address_binary' => $candidate->binary(), - 'prefix_length' => (int) $pool->guest_prefix_length, - 'state' => 'reserved', - 'reservation_id' => $reservationId, - 'instance_id' => null, - 'operation_id' => $operationId, - 'created_at' => $now, - 'updated_at' => $now, - ]); - $next = $candidate->compare($rangeEnd) === 0 - ? $rangeStart : $candidate->next(); - DB::table('cloud_public_ip_pools') - ->where('id', (int) $pool->id)->update([ - 'allocation_cursor' => $next->text(), - ]); - return DB::table('cloud_public_ips')->where('id', $id)->first(); + return [ + 'row' => null, + 'candidate' => $candidate, + 'range_start' => $rangeStart, + 'range_end' => $rangeEnd, + ]; } $candidate = $candidate->compare($rangeEnd) === 0 ? $rangeStart : $candidate->next(); } - throw new CloudError('公网 IPv6 地址池没有可分配地址', 409); + return null; + } + + private static function reserveAddress(object $pool, int $reservationId, + string $operationId, int $now, ?array $selection): object + { + $family = (int) $pool->address_family; + if ($selection === null) { + throw new CloudError( + $family === 6 + ? '公网 IPv6 地址池没有可分配地址' + : '公网 IPv4 地址池已经耗尽', + 409 + ); + } + if ($selection['row'] !== null) { + return self::claimAddress( + $selection['row'], + $reservationId, + $operationId, + $now + ); + } + $candidate = $selection['candidate']; + $rangeStart = $selection['range_start']; + $rangeEnd = $selection['range_end']; + if (!$candidate instanceof CloudIpAddress + || !$rangeStart instanceof CloudIpAddress + || !$rangeEnd instanceof CloudIpAddress) { + throw new CloudError('公网 IPv6 稀疏分配选择损坏', 409); + } + $id = DB::table('cloud_public_ips')->insertGetId([ + 'host_id' => (int) $pool->host_id, + 'public_ip_pool_id' => (int) $pool->id, + 'address_family' => 6, + 'address' => $candidate->text(), + 'address_binary' => $candidate->binary(), + 'prefix_length' => (int) $pool->guest_prefix_length, + 'state' => 'reserved', + 'reservation_id' => $reservationId, + 'instance_id' => null, + 'operation_id' => $operationId, + 'created_at' => $now, + 'updated_at' => $now, + ]); + $next = $candidate->compare($rangeEnd) === 0 + ? $rangeStart : $candidate->next(); + DB::table('cloud_public_ip_pools') + ->where('id', (int) $pool->id)->update([ + 'allocation_cursor' => $next->text(), + ]); + return DB::table('cloud_public_ips')->where('id', $id)->first(); } private static function claimAddress(object $row, int $reservationId, diff --git a/web/tests/CloudPublicNetworkAllocationServiceTest.php b/web/tests/CloudPublicNetworkAllocationServiceTest.php index e40bfabe..23580b4a 100644 --- a/web/tests/CloudPublicNetworkAllocationServiceTest.php +++ b/web/tests/CloudPublicNetworkAllocationServiceTest.php @@ -379,4 +379,147 @@ $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 503)->count() === 0, 'Rejecting a non-public pool retained a partial address claim.'); +$addAttachment = static function (int $id, int $hostId, int $lineId, + int $priority): void { + Capsule::table('cloud_host_network_attachments')->insert([ + 'id' => $id, + 'host_id' => $hostId, + 'line_id' => $lineId, + 'network_selector' => 'bridge:br' . $id, + 'state' => 'active', + 'priority' => $priority, + ]); +}; +$addPool = static function (int $id, int $hostId, int $attachmentId, + int $family, int $priority, string $cidr, string $gateway, + ?string $rangeStart = null, ?string $rangeEnd = null, + int $maximum = 0): void { + Capsule::table('cloud_public_ip_pools')->insert([ + 'id' => $id, + 'pool_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT), + 'host_id' => $hostId, + 'network_attachment_id' => $attachmentId, + 'address_family' => $family, + 'cidr' => $cidr, + 'gateway' => $gateway, + 'gateway_on_link' => $family === 6 ? 1 : 0, + 'guest_prefix_length' => (int) substr(strrchr($cidr, '/'), 1), + 'dns_servers_json' => $family === 6 + ? '["2606:4700:4700::1111"]' : '["1.1.1.1"]', + 'reserved_ips_json' => '[]', + 'allocation_start' => $rangeStart, + 'allocation_end' => $rangeEnd, + 'allocation_cursor' => null, + 'max_allocations' => $maximum, + 'state' => 'active', + 'priority' => $priority, + 'desired_revision' => 1, + 'observed_revision' => 1, + 'sync_state' => 'applied', + ]); +}; +$addIp = static function (int $hostId, int $poolId, int $family, + string $address, int $prefix, string $state = 'available'): void { + Capsule::table('cloud_public_ips')->insert([ + 'host_id' => $hostId, + 'public_ip_pool_id' => $poolId, + 'address_family' => $family, + 'address' => $address, + 'address_binary' => inet_pton($address), + 'prefix_length' => $prefix, + 'state' => $state, + 'reservation_id' => $state === 'assigned' ? 9999 : null, + 'instance_id' => $state === 'assigned' ? 9999 : null, + 'operation_id' => $state === 'assigned' ? str_repeat('7', 32) : null, + 'created_at' => time(), + 'updated_at' => time(), + ]); +}; +$addReservation = static function (int $id, int $hostId, + string $operationId): void { + Capsule::table('cloud_resource_reservations')->insert([ + 'id' => $id, + 'host_id' => $hostId, + 'operation_id' => $operationId, + 'state' => 'held', + ]); +}; + +// A complete attachment may have more than one pool per family. Exhaustion +// of a higher-priority pool must not hide capacity in its next sibling. +Capsule::table('cloud_hosts')->insert(['id' => 2]); +$addAttachment(20, 2, 8, 0); +$addPool(110, 2, 20, 4, 0, '9.9.9.0/24', '9.9.9.1'); +Capsule::table('cloud_public_ip_pools')->where('id', 110)->update([ + 'reserved_ips_json' => '["9.9.9.10"]', +]); +$addIp(2, 110, 4, '9.9.9.10', 24); +$addPool(111, 2, 20, 4, 1, '11.0.0.0/24', '11.0.0.1'); +$addIp(2, 111, 4, '11.0.0.10', 24); +$addPool(210, 2, 20, 6, 0, '2404:c140:156:3b::/64', + '2404:c140:156::1', '2404:c140:156:3b::10', + '2404:c140:156:3b::20', 1); +$addIp(2, 210, 6, '2404:c140:156:3b::10', 64, 'assigned'); +$addPool(211, 2, 20, 6, 1, '2404:c140:156:3c::/64', + '2404:c140:156::1', '2404:c140:156:3c::10', + '2404:c140:156:3c::20', 2); +$fallbackOperation = str_repeat('6', 32); +$addReservation(600, 2, $fallbackOperation); +$fallback = CloudPublicNetworkAllocationService::reserveBundle( + 2, 8, 600, $fallbackOperation +); +$assert($fallback->address(4) === '11.0.0.10/24' + && $fallback->address(6) === '2404:c140:156:3c::10/64', + 'Allocation did not skip exhausted higher-priority family pools.'); + +// The same deterministic search must continue with the next attachment when +// the first complete attachment has no usable pair. +$addAttachment(30, 2, 9, 0); +$addPool(120, 2, 30, 4, 0, '12.0.0.0/24', '12.0.0.1'); +$addIp(2, 120, 4, '12.0.0.10', 24); +$addPool(220, 2, 30, 6, 0, '2404:c140:156:3d::/64', + '2404:c140:156::1', '2404:c140:156:3d::10', + '2404:c140:156:3d::20', 1); +$addIp(2, 220, 6, '2404:c140:156:3d::10', 64, 'assigned'); +$addAttachment(31, 2, 9, 1); +$addPool(121, 2, 31, 4, 0, '13.0.0.0/24', '13.0.0.1'); +$addIp(2, 121, 4, '13.0.0.10', 24); +$addPool(221, 2, 31, 6, 0, '2404:c140:156:3e::/64', + '2404:c140:156::1', '2404:c140:156:3e::10', + '2404:c140:156:3e::20', 2); +$attachmentFallbackOperation = str_repeat('5', 32); +$addReservation(601, 2, $attachmentFallbackOperation); +$attachmentFallback = CloudPublicNetworkAllocationService::reserveBundle( + 2, 9, 601, $attachmentFallbackOperation +); +$attachmentClaims = Capsule::table('cloud_resource_reservation_ips') + ->where('reservation_id', 601)->orderBy('address_family')->get(); +$assert($attachmentFallback->address(4) === '13.0.0.10/24' + && $attachmentFallback->address(6) === '2404:c140:156:3e::10/64' + && $attachmentClaims->count() === 2 + && $attachmentClaims->every( + static fn ($claim): bool => (int) $claim->network_attachment_id === 31 + ), 'Allocation did not continue with the next complete attachment.'); + +// An unavailable IPv6 leg must reject the whole bundle before the available +// IPv4 leg is claimed. +Capsule::table('cloud_hosts')->insert(['id' => 3]); +$addAttachment(40, 3, 10, 0); +$addPool(130, 3, 40, 4, 0, '14.0.0.0/24', '14.0.0.1'); +$addIp(3, 130, 4, '14.0.0.10', 24); +$addPool(230, 3, 40, 6, 0, '2404:c140:156:3f::/64', + '2404:c140:156::1', '2404:c140:156:3f::10', + '2404:c140:156:3f::20', 1); +$addIp(3, 230, 6, '2404:c140:156:3f::10', 64, 'assigned'); +$exhaustedOperation = str_repeat('4', 32); +$addReservation(602, 3, $exhaustedOperation); +$rejects(static fn () => CloudPublicNetworkAllocationService::reserveBundle( + 3, 10, 602, $exhaustedOperation +), 'A host without a complete usable pool pair was accepted.'); +$assert(Capsule::table('cloud_resource_reservation_ips') + ->where('reservation_id', 602)->count() === 0 + && Capsule::table('cloud_public_ips')->where('public_ip_pool_id', 130) + ->where('state', 'available')->whereNull('reservation_id')->count() === 1, + 'A failed pair selection left a partial IPv4 claim.'); + echo "Atomic dual-stack public network allocation tests passed.\n";