feat: add family-aware sparse public address ledger

go
codex 3 weeks ago
parent 276f8efacb
commit 854e87e277

@ -11,6 +11,7 @@ final class CloudPublicIp extends Model
'id' => 'int',
'host_id' => 'int',
'public_ip_pool_id' => 'int',
'address_family' => 'int',
'prefix_length' => 'int',
'reservation_id' => 'int',
'instance_id' => 'int',

@ -11,7 +11,10 @@ final class CloudPublicIpPool extends Model
'id' => 'int',
'host_id' => 'int',
'network_attachment_id' => 'int',
'address_family' => 'int',
'gateway_on_link' => 'bool',
'guest_prefix_length' => 'int',
'max_allocations' => 'int',
'priority' => 'int',
'desired_revision' => 'int',
'observed_revision' => 'int',

@ -0,0 +1,21 @@
<?php
namespace App\Models;
final class CloudResourceReservationIp extends Model
{
protected $connection = 'default';
protected $table = 'cloud_resource_reservation_ips';
protected $casts = [
'id' => 'int',
'reservation_id' => 'int',
'address_family' => 'int',
'network_attachment_id' => 'int',
'public_ip_pool_id' => 'int',
'public_ip_id' => 'int',
'released_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -259,12 +259,18 @@ CREATE TABLE cloud_public_ip_pools (
pool_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
network_attachment_id BIGINT UNSIGNED NOT NULL,
address_family TINYINT UNSIGNED NOT NULL,
name VARCHAR(80) NOT NULL,
cidr VARCHAR(18) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
gateway VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
cidr VARCHAR(49) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
gateway VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
gateway_on_link TINYINT UNSIGNED NOT NULL DEFAULT 0,
guest_prefix_length TINYINT UNSIGNED NOT NULL,
dns_servers_json TEXT NOT NULL,
reserved_ips_json TEXT NOT NULL,
allocation_start VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
allocation_end VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
allocation_cursor VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
max_allocations INT UNSIGNED NOT NULL DEFAULT 0,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'active',
priority INT NOT NULL DEFAULT 0,
desired_revision BIGINT UNSIGNED NOT NULL DEFAULT 1,
@ -274,9 +280,30 @@ CREATE TABLE cloud_public_ip_pools (
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_public_ip_pool_key (host_id, pool_key),
UNIQUE KEY cloud_public_ip_pool_cidr (host_id, cidr),
KEY cloud_public_ip_pool_attachment (network_attachment_id, state, priority, id),
KEY cloud_public_ip_pool_sync (host_id, sync_state, updated_at, id)
UNIQUE KEY cloud_public_ip_pool_cidr (host_id, address_family, cidr),
KEY cloud_public_ip_pool_attachment (network_attachment_id, address_family, state, priority, id),
KEY cloud_public_ip_pool_sync (host_id, sync_state, updated_at, id),
CONSTRAINT chk_cloud_public_ip_pool_family CHECK (
address_family IN (4,6)
),
CONSTRAINT chk_cloud_public_ip_pool_prefix CHECK (
(address_family = 4 AND guest_prefix_length BETWEEN 1 AND 30)
OR (address_family = 6 AND guest_prefix_length BETWEEN 1 AND 120)
),
CONSTRAINT chk_cloud_public_ip_pool_gateway CHECK (
gateway_on_link IN (0,1)
),
CONSTRAINT chk_cloud_public_ip_pool_allocation CHECK (
(address_family = 4
AND allocation_start IS NULL
AND allocation_end IS NULL
AND allocation_cursor IS NULL
AND max_allocations = 0)
OR (address_family = 6
AND allocation_start IS NOT NULL
AND allocation_end IS NOT NULL
AND max_allocations >= 1)
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Operator limits and safety reserves are authoritative scheduling policy.
@ -302,7 +329,9 @@ CREATE TABLE cloud_public_ips (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
host_id BIGINT UNSIGNED NOT NULL,
public_ip_pool_id BIGINT UNSIGNED DEFAULT NULL,
address_family TINYINT UNSIGNED NOT NULL,
address VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
address_binary VARBINARY(16) NOT NULL,
prefix_length TINYINT UNSIGNED NOT NULL,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'available',
reservation_id BIGINT UNSIGNED DEFAULT NULL,
@ -310,12 +339,19 @@ CREATE TABLE cloud_public_ips (
operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_public_ip_address (address),
KEY cloud_public_ip_available (host_id, state, id),
UNIQUE KEY cloud_public_ip_address (address_family, address_binary),
KEY cloud_public_ip_available (host_id, address_family, state, id),
KEY cloud_public_ip_pool_available (public_ip_pool_id, state, id),
UNIQUE KEY cloud_public_ip_reservation (reservation_id),
UNIQUE KEY cloud_public_ip_reservation (reservation_id, address_family),
KEY cloud_public_ip_instance (instance_id),
UNIQUE KEY cloud_public_ip_operation (operation_id)
UNIQUE KEY cloud_public_ip_operation (operation_id, address_family),
CONSTRAINT chk_cloud_public_ip_family CHECK (
address_family IN (4,6)
),
CONSTRAINT chk_cloud_public_ip_prefix CHECK (
(address_family = 4 AND prefix_length BETWEEN 1 AND 30)
OR (address_family = 6 AND prefix_length BETWEEN 1 AND 120)
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- An administrator public-pool replacement is one durable orchestration
@ -1380,6 +1416,39 @@ CREATE TABLE cloud_resource_reservations (
OR (order_id IS NULL AND backup_restore_id IS NOT NULL AND offer_id IS NULL)
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- One resource reservation owns one address in each required family. Claims
-- are retained after release so reconciliation can prove which exact bundle
-- an operation held without overloading nullable columns on the reservation.
CREATE TABLE cloud_resource_reservation_ips (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
reservation_id BIGINT UNSIGNED NOT NULL,
address_family TINYINT UNSIGNED NOT NULL,
network_attachment_id BIGINT UNSIGNED NOT NULL,
public_ip_pool_id BIGINT UNSIGNED NOT NULL,
public_ip_id BIGINT UNSIGNED NOT NULL,
operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'reserved',
released_at BIGINT UNSIGNED DEFAULT NULL,
release_reason VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_reservation_ip_family (reservation_id, address_family),
UNIQUE KEY cloud_reservation_ip_operation (operation_id, address_family),
KEY cloud_reservation_ip_address (public_ip_id, state, id),
KEY cloud_reservation_ip_pool (public_ip_pool_id, state, id),
KEY cloud_reservation_ip_attachment (network_attachment_id, state, id),
CONSTRAINT chk_cloud_reservation_ip_family CHECK (
address_family IN (4,6)
),
CONSTRAINT chk_cloud_reservation_ip_state CHECK (
state IN ('reserved','committed','uncertain','released')
),
CONSTRAINT chk_cloud_reservation_ip_release CHECK (
(state = 'released' AND released_at IS NOT NULL AND release_reason <> '')
OR (state <> 'released' AND released_at IS NULL AND release_reason = '')
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE cloud_service_periods (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
instance_id BIGINT UNSIGNED NOT NULL,

@ -0,0 +1,102 @@
<?php
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
if (!is_string($schema)) {
throw new RuntimeException('Cloud schema is unreadable.');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$table = static function (string $name) use ($schema): string {
$start = strpos($schema, 'CREATE TABLE ' . $name . ' (');
if ($start === false) {
return '';
}
$end = strpos($schema,
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;',
$start
);
return $end === false ? '' : substr($schema, $start, $end - $start);
};
$pools = $table('cloud_public_ip_pools');
$ips = $table('cloud_public_ips');
$claims = $table('cloud_resource_reservation_ips');
$assert($pools !== ''
&& str_contains($pools, 'address_family TINYINT UNSIGNED NOT NULL')
&& str_contains($pools,
'cidr VARCHAR(49) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
&& str_contains($pools,
'gateway VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
&& str_contains($pools,
'gateway_on_link TINYINT UNSIGNED NOT NULL DEFAULT 0')
&& str_contains($pools,
'allocation_start VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
&& str_contains($pools,
'allocation_end VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
&& str_contains($pools,
'allocation_cursor VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
&& str_contains($pools,
'max_allocations INT UNSIGNED NOT NULL DEFAULT 0'),
'Public pool storage cannot describe bounded sparse IPv6 allocation.');
$assert(str_contains($pools,
'UNIQUE KEY cloud_public_ip_pool_cidr (host_id, address_family, cidr)')
&& str_contains($pools,
'KEY cloud_public_ip_pool_attachment (network_attachment_id, address_family, state, priority, id)')
&& str_contains($pools, 'CONSTRAINT chk_cloud_public_ip_pool_family')
&& str_contains($pools, 'CONSTRAINT chk_cloud_public_ip_pool_allocation'),
'Public pool family identity or sparse-allocation invariants are missing.');
$assert($ips !== ''
&& str_contains($ips, 'address_family TINYINT UNSIGNED NOT NULL')
&& str_contains($ips, 'address_binary VARBINARY(16) NOT NULL')
&& str_contains($ips,
'UNIQUE KEY cloud_public_ip_address (address_family, address_binary)')
&& str_contains($ips,
'UNIQUE KEY cloud_public_ip_reservation (reservation_id, address_family)')
&& str_contains($ips,
'UNIQUE KEY cloud_public_ip_operation (operation_id, address_family)')
&& str_contains($ips, 'CONSTRAINT chk_cloud_public_ip_family'),
'Concrete public-address rows are not family-aware and dual-stack safe.');
$assert($claims !== ''
&& str_contains($claims, 'reservation_id BIGINT UNSIGNED NOT NULL')
&& str_contains($claims, 'address_family TINYINT UNSIGNED NOT NULL')
&& str_contains($claims, 'network_attachment_id BIGINT UNSIGNED NOT NULL')
&& str_contains($claims, 'public_ip_pool_id BIGINT UNSIGNED NOT NULL')
&& str_contains($claims, 'public_ip_id BIGINT UNSIGNED NOT NULL')
&& str_contains($claims,
'UNIQUE KEY cloud_reservation_ip_family (reservation_id, address_family)')
&& str_contains($claims,
'UNIQUE KEY cloud_reservation_ip_operation (operation_id, address_family)')
&& str_contains($claims,
'KEY cloud_reservation_ip_address (public_ip_id, state, id)')
&& str_contains($claims, 'CONSTRAINT chk_cloud_reservation_ip_family')
&& str_contains($claims, 'CONSTRAINT chk_cloud_reservation_ip_state'),
'Reservation-to-address ownership cannot retain two atomic family claims.');
$poolModel = file_get_contents($root . '/app/Models/CloudPublicIpPool.php');
$ipModel = file_get_contents($root . '/app/Models/CloudPublicIp.php');
$claimModelPath = $root . '/app/Models/CloudResourceReservationIp.php';
$claimModel = is_file($claimModelPath)
? file_get_contents($claimModelPath) : false;
$assert(is_string($poolModel)
&& str_contains($poolModel, "'address_family' => 'int'")
&& str_contains($poolModel, "'gateway_on_link' => 'bool'")
&& str_contains($poolModel, "'max_allocations' => 'int'"),
'Public pool model does not expose canonical dual-stack casts.');
$assert(is_string($ipModel)
&& str_contains($ipModel, "'address_family' => 'int'"),
'Public IP model does not expose its address family.');
$assert(is_string($claimModel)
&& str_contains($claimModel,
"protected \$table = 'cloud_resource_reservation_ips';")
&& str_contains($claimModel, "'address_family' => 'int'"),
'Reservation address-claim model is missing.');
echo "Dual-stack public-network schema tests passed.\n";

@ -65,13 +65,17 @@ $assert(str_contains($schema, 'configured_cpu_cores')
&& str_contains($schema, "state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'held'"),
'Panel scheduling policy and reservation-ledger capacity fields are incomplete.');
$assert(str_contains($schema, 'UNIQUE KEY cloud_public_ip_address (address)')
&& str_contains($schema, 'cloud_public_ip_available (host_id, state, id)')
$assert(str_contains($schema,
'UNIQUE KEY cloud_public_ip_address (address_family, address_binary)')
&& str_contains($schema,
'cloud_public_ip_available (host_id, address_family, state, id)')
&& str_contains($schema, 'operation_id CHAR(32)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_reservation (reservation_id)')
'UNIQUE KEY cloud_public_ip_reservation (reservation_id, address_family)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_operation (operation_id, address_family)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_operation (operation_id)')
'UNIQUE KEY cloud_reservation_ip_family (reservation_id, address_family)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_reservation_operation (operation_id)')
&& str_contains($schema, 'public_ip_id BIGINT UNSIGNED DEFAULT NULL'),

Loading…
Cancel
Save