feat: add multi-host resource data model

dev
admin 2 months ago
parent 5c8d95bd9a
commit a07e11c681

@ -53,6 +53,17 @@ final class CloudSetup
$table->unsignedBigInteger('traffic_quota_bytes')->default(0);
});
}
if (!$shopSchema->hasColumn('shop', 'created_at')) {
$shopSchema->table('shop', static function ($table) {
$table->unsignedBigInteger('created_at')->default(0);
});
}
if (!$shopSchema->hasColumn('shop', 'updated_at')) {
$shopSchema->table('shop', static function ($table) {
$table->unsignedBigInteger('updated_at')->default(0);
});
}
self::ensureMultiHostDefaults($connection);
self::upgradeCloudInstance($connection);
self::upgradeInvoicePayments($connection);
User::getConnectionResolver()->connection('log')->unprepared(self::schema('cloud_log.sql'));
@ -61,6 +72,22 @@ final class CloudSetup
return true;
}
/**
* Seed feature switches only when absent.
*
* Regions, default lines, pools, capacity policies, offers and IP rows are
* operator-owned records and are never invented by cloudInit. Keeping the
* line dimension disabled by default changes presentation only; the data
* model remains available before differentiated network products are sold.
*/
private static function ensureMultiHostDefaults($connection): void
{
$connection->table('configurations')->insertOrIgnore([
'name' => 'cloud_network_lines_enabled',
'value' => 'false',
]);
}
/**
* Apply the referral-code migration as part of the online-update path.
*

@ -4,5 +4,13 @@ final class CloudHost extends Model
{
protected $connection = 'default';
protected $table = 'cloud_hosts';
protected $casts = ['enabled' => 'boolean'];
protected $casts = [
'id' => 'int',
'enabled' => 'boolean',
'resource_pool_id' => 'int',
'last_seen_at' => 'int',
'health_checked_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,24 @@
<?php
namespace App\Models;
final class CloudHostCapacityPolicy extends Model
{
protected $connection = 'default';
protected $table = 'cloud_host_capacity_policies';
protected $primaryKey = 'host_id';
public $incrementing = false;
protected $casts = [
'host_id' => 'int',
'configured_cpu_cores' => 'int',
'configured_memory_mib' => 'int',
'configured_disk_gib' => 'int',
'safety_cpu_cores' => 'int',
'safety_memory_mib' => 'int',
'safety_disk_gib' => 'int',
'max_instances' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,36 @@
<?php
namespace App\Models;
final class CloudHostCapacitySnapshot extends Model
{
protected $connection = 'default';
protected $table = 'cloud_host_capacity_snapshots';
protected $primaryKey = 'host_id';
public $incrementing = false;
protected $casts = [
'host_id' => 'int',
'cpu_total_cores' => 'int',
'cpu_allocated_cores' => 'int',
'cpu_available_cores' => 'int',
'memory_total_mib' => 'int',
'memory_allocated_mib' => 'int',
'memory_available_mib' => 'int',
'storage_total_bytes' => 'int',
'storage_allocated_bytes' => 'int',
'storage_available_bytes' => 'int',
'instance_total_count' => 'int',
'instance_running_count' => 'int',
'public_ip_configured' => 'boolean',
'public_ip_total_count' => 'int',
'public_ip_reserved_count' => 'int',
'public_ip_allocated_count' => 'int',
'public_ip_available_count' => 'int',
'observed_at' => 'int',
'last_attempt_at' => 'int',
'revision' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -4,6 +4,24 @@ final class CloudInstance extends Model
{
protected $connection = 'default';
protected $table = 'cloud_instances';
protected $casts = [
'id' => 'int',
'host_id' => 'int',
'user_id' => 'int',
'order_id' => 'int',
'plan_id' => 'int',
'offer_id' => 'int',
'resource_pool_id' => 'int',
'reservation_id' => 'int',
'current_period_id' => 'int',
'expires_at' => 'int',
'renewal_price' => 'decimal:2',
'renewal_days' => 'int',
'renewal_traffic_bytes' => 'int',
'traffic_quota_bytes' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
protected $hidden = ['credential_cipher'];
public function serviceAvailable(?int $now = null): bool

@ -5,6 +5,22 @@ final class CloudInvoice extends Model
{
protected $connection = 'default';
protected $table = 'cloud_invoices';
protected $casts = ['amount' => 'decimal:2'];
protected $casts = [
'id' => 'int',
'instance_id' => 'int',
'order_id' => 'int',
'user_id' => 'int',
'plan_id' => 'int',
'offer_id' => 'int',
'renewal_id' => 'int',
'renews_expires_at' => 'int',
'due_at' => 'int',
'amount' => 'decimal:2',
'validity_days' => 'int',
'traffic_quota_bytes' => 'int',
'paid_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
protected $hidden = ['invoice_no', 'payment_reference'];
}

@ -0,0 +1,18 @@
<?php
namespace App\Models;
final class CloudNetworkLine extends Model
{
protected $connection = 'default';
protected $table = 'cloud_network_lines';
protected $casts = [
'id' => 'int',
'region_id' => 'int',
'is_default' => 'boolean',
'sort' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,22 @@
<?php
namespace App\Models;
final class CloudOffer extends Model
{
protected $connection = 'default';
protected $table = 'cloud_offers';
protected $casts = [
'id' => 'int',
'plan_id' => 'int',
'resource_pool_id' => 'int',
'price' => 'decimal:2',
'price_version' => 'int',
'status' => 'boolean',
'stock' => 'int',
'sort' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -4,6 +4,21 @@ final class CloudOrder extends Model
{
protected $connection = 'default';
protected $table = 'cloud_orders';
protected $casts = ['amount' => 'decimal:2'];
protected $casts = [
'id' => 'int',
'user_id' => 'int',
'plan_id' => 'int',
'offer_id' => 'int',
'resource_pool_id' => 'int',
'host_id' => 'int',
'reservation_id' => 'int',
'amount' => 'decimal:2',
'instance_id' => 'int',
'refunded_at' => 'int',
'stock_reserved_at' => 'int',
'stock_released_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
protected $hidden = ['request_hash', 'payload_cipher'];
}

@ -0,0 +1,19 @@
<?php
namespace App\Models;
final class CloudPublicIp extends Model
{
protected $connection = 'default';
protected $table = 'cloud_public_ips';
protected $casts = [
'id' => 'int',
'host_id' => 'int',
'prefix_length' => 'int',
'reservation_id' => 'int',
'instance_id' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,16 @@
<?php
namespace App\Models;
final class CloudRegion extends Model
{
protected $connection = 'default';
protected $table = 'cloud_regions';
protected $casts = [
'id' => 'int',
'sort' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,17 @@
<?php
namespace App\Models;
final class CloudResourcePool extends Model
{
protected $connection = 'default';
protected $table = 'cloud_resource_pools';
protected $casts = [
'id' => 'int',
'line_id' => 'int',
'sort' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,27 @@
<?php
namespace App\Models;
final class CloudResourceReservation extends Model
{
protected $connection = 'default';
protected $table = 'cloud_resource_reservations';
protected $casts = [
'id' => 'int',
'order_id' => 'int',
'offer_id' => 'int',
'resource_pool_id' => 'int',
'host_id' => 'int',
'user_id' => 'int',
'instance_id' => 'int',
'public_ip_id' => 'int',
'cpu_cores' => 'int',
'memory_mib' => 'int',
'disk_gib' => 'int',
'expires_at' => 'int',
'released_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -16,6 +16,7 @@ class Shop extends Model
'disk_gib' => 'int', 'validity_days' => 'int', 'traffic_quota_bytes' => 'int',
'status' => 'int', 'stock' => 'int', 'sort' => 'int',
'price' => 'decimal:2',
'created_at' => 'int', 'updated_at' => 'int',
];
public function content()

@ -0,0 +1,48 @@
<?php
namespace App\Services\Cloud;
use App\Models\Configurations;
/**
* Presentation switch for differentiated network lines.
*
* The scheduler schema always retains a default line for every region. A
* disabled switch therefore hides the choice without changing placement or
* requiring a future production schema migration.
*/
final class CloudNetworkLineFeature
{
public const CONFIG_KEY = 'cloud_network_lines_enabled';
private static $cached = null;
public static function enabled(): bool
{
if (is_bool(self::$cached)) {
return self::$cached;
}
try {
$value = Configurations::where('name', self::CONFIG_KEY)
->value('value');
} catch (\Throwable $error) {
error_log('Unable to read the network-line feature switch: '
. get_class($error));
return self::$cached = false;
}
return self::$cached = in_array(strtolower(trim((string) $value)),
['1', 'true', 'yes', 'on'], true);
}
public static function save(bool $enabled): void
{
$row = Configurations::where('name', self::CONFIG_KEY)->first();
if ($row === null) {
$row = new Configurations();
$row->name = self::CONFIG_KEY;
}
$row->value = $enabled ? 'true' : 'false';
$row->save();
self::$cached = $enabled;
}
}

@ -26,15 +26,179 @@ CREATE TABLE IF NOT EXISTS ticket_attachments (
KEY ticket_attachment_user (user_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Public sales geography is independent from the engine host profile. A
-- stable region code is used in URLs and snapshots; the display name may be
-- changed without changing an existing order's identity.
CREATE TABLE IF NOT EXISTS cloud_regions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
name VARCHAR(80) NOT NULL,
country_code CHAR(2) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
status VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'active',
sort INT NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_region_code (code),
KEY cloud_region_catalog (status, sort, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Lines always exist in the data model. When the feature switch is off, the
-- panel uses the one default line created for each region and hides the line
-- choice from ordinary users. This avoids a production schema migration when
-- differentiated network products are enabled later.
CREATE TABLE IF NOT EXISTS cloud_network_lines (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
region_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
name VARCHAR(80) NOT NULL,
is_default TINYINT(1) NOT NULL DEFAULT 0,
status VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'active',
sort INT NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_network_line_code (region_id, code),
KEY cloud_network_line_catalog (region_id, status, sort, id),
KEY cloud_network_line_default (region_id, is_default, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_resource_pools (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
line_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
name VARCHAR(80) NOT NULL,
status VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'active',
scheduling_policy VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'balanced',
sort INT NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_resource_pool_code (line_id, code),
KEY cloud_resource_pool_catalog (line_id, status, sort, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_hosts (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80) NOT NULL,
region VARCHAR(64) NOT NULL DEFAULT '',
country_code CHAR(2) NOT NULL DEFAULT '',
enabled TINYINT(1) NOT NULL DEFAULT 0,
resource_pool_id BIGINT UNSIGNED DEFAULT NULL,
scheduler_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'disabled',
health_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown',
last_seen_at BIGINT UNSIGNED DEFAULT NULL,
health_checked_at BIGINT UNSIGNED DEFAULT NULL,
health_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
default_network VARCHAR(128) NOT NULL DEFAULT 'default',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL
updated_at BIGINT NOT NULL,
KEY cloud_host_name (name),
KEY cloud_host_scheduler (resource_pool_id, scheduler_state, health_state, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
resource_pool_id BIGINT UNSIGNED NULL AFTER enabled;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
scheduler_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'disabled' AFTER resource_pool_id;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
health_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown' AFTER scheduler_state;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
last_seen_at BIGINT UNSIGNED NULL AFTER health_state;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
health_checked_at BIGINT UNSIGNED NULL AFTER last_seen_at;
ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS
health_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '' AFTER health_checked_at;
ALTER TABLE cloud_hosts ADD INDEX IF NOT EXISTS
cloud_host_name (name);
ALTER TABLE cloud_hosts ADD INDEX IF NOT EXISTS
cloud_host_scheduler (resource_pool_id, scheduler_state, health_state, id);
-- Operator limits and safety reserves are authoritative scheduling policy.
-- Zero configured capacity keeps a new host unschedulable until explicitly
-- configured; it never means unlimited capacity.
CREATE TABLE IF NOT EXISTS cloud_host_capacity_policies (
host_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
configured_cpu_cores SMALLINT UNSIGNED NOT NULL DEFAULT 0,
configured_memory_mib BIGINT UNSIGNED NOT NULL DEFAULT 0,
configured_disk_gib BIGINT UNSIGNED NOT NULL DEFAULT 0,
safety_cpu_cores SMALLINT UNSIGNED NOT NULL DEFAULT 0,
safety_memory_mib BIGINT UNSIGNED NOT NULL DEFAULT 0,
safety_disk_gib BIGINT UNSIGNED NOT NULL DEFAULT 0,
max_instances INT UNSIGNED NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- One last-known engine observation per host. The allocated columns reflect
-- libvirt allocations already visible on the host. Pending holds remain in
-- cloud_resource_reservations and must be added separately by the scheduler;
-- they are never folded into this observation cache.
CREATE TABLE IF NOT EXISTS cloud_host_capacity_snapshots (
host_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
cpu_total_cores SMALLINT UNSIGNED NOT NULL DEFAULT 0,
cpu_allocated_cores INT UNSIGNED NOT NULL DEFAULT 0,
cpu_available_cores INT UNSIGNED NOT NULL DEFAULT 0,
memory_total_mib BIGINT UNSIGNED NOT NULL DEFAULT 0,
memory_allocated_mib BIGINT UNSIGNED NOT NULL DEFAULT 0,
memory_available_mib BIGINT UNSIGNED NOT NULL DEFAULT 0,
storage_total_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
storage_allocated_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
storage_available_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
instance_total_count INT UNSIGNED NOT NULL DEFAULT 0,
instance_running_count INT UNSIGNED NOT NULL DEFAULT 0,
public_ip_configured TINYINT(1) NOT NULL DEFAULT 0,
public_ip_total_count INT UNSIGNED NOT NULL DEFAULT 0,
public_ip_reserved_count INT UNSIGNED NOT NULL DEFAULT 0,
public_ip_allocated_count INT UNSIGNED NOT NULL DEFAULT 0,
public_ip_available_count INT UNSIGNED NOT NULL DEFAULT 0,
sync_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'never',
observed_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
last_attempt_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
revision BIGINT UNSIGNED NOT NULL DEFAULT 0,
last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
KEY cloud_host_capacity_sync (sync_state, observed_at, host_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- PHP reserves one concrete address before a paid provisioning job is queued.
-- The private engine still validates the claim on the selected host. An
-- uncertain operation keeps the address unavailable until reconciliation.
CREATE TABLE IF NOT EXISTS cloud_public_ips (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
host_id BIGINT UNSIGNED NOT NULL,
address VARCHAR(45) CHARACTER SET ascii COLLATE ascii_bin 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,
instance_id BIGINT UNSIGNED DEFAULT NULL,
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_reservation (reservation_id),
KEY cloud_public_ip_instance (instance_id),
UNIQUE KEY cloud_public_ip_operation (operation_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- A base plan describes resources. An offer places and prices that plan in
-- one resource pool. Region and line are therefore reached through the pool
-- without duplicating mutable labels on this row.
CREATE TABLE IF NOT EXISTS cloud_offers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
plan_id BIGINT UNSIGNED NOT NULL,
resource_pool_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
name VARCHAR(128) NOT NULL,
price DECIMAL(12,2) NOT NULL DEFAULT 0.00,
currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD',
price_version INT UNSIGNED NOT NULL DEFAULT 1,
status TINYINT(1) NOT NULL DEFAULT 1,
stock INT NOT NULL DEFAULT -1,
sort INT NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_offer_code (code),
UNIQUE KEY cloud_offer_plan_pool (plan_id, resource_pool_id),
KEY cloud_offer_catalog (resource_pool_id, status, sort, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_template_retirements (
template_id VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
@ -69,6 +233,9 @@ CREATE TABLE IF NOT EXISTS cloud_instances (
user_id BIGINT UNSIGNED NOT NULL,
order_id BIGINT UNSIGNED DEFAULT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
offer_id BIGINT UNSIGNED DEFAULT NULL,
resource_pool_id BIGINT UNSIGNED DEFAULT NULL,
reservation_id BIGINT UNSIGNED DEFAULT NULL,
current_period_id BIGINT UNSIGNED DEFAULT NULL,
name VARCHAR(63) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
@ -92,9 +259,21 @@ CREATE TABLE IF NOT EXISTS cloud_instances (
updated_at BIGINT NOT NULL,
UNIQUE KEY uniq_cloud_host_vm (host_id, uuid),
UNIQUE KEY uniq_cloud_instance_order (order_id),
UNIQUE KEY uniq_cloud_instance_reservation (reservation_id),
KEY cloud_instance_owner (user_id, status),
KEY cloud_instance_placement (resource_pool_id, host_id, status),
KEY cloud_instance_lifecycle (service_status, expires_at, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE cloud_instances ADD COLUMN IF NOT EXISTS
offer_id BIGINT UNSIGNED NULL AFTER plan_id;
ALTER TABLE cloud_instances ADD COLUMN IF NOT EXISTS
resource_pool_id BIGINT UNSIGNED NULL AFTER offer_id;
ALTER TABLE cloud_instances ADD COLUMN IF NOT EXISTS
reservation_id BIGINT UNSIGNED NULL AFTER resource_pool_id;
ALTER TABLE cloud_instances ADD UNIQUE INDEX IF NOT EXISTS
uniq_cloud_instance_reservation (reservation_id);
ALTER TABLE cloud_instances ADD INDEX IF NOT EXISTS
cloud_instance_placement (resource_pool_id, host_id, status);
-- Last-known engine power state for fast instance-list first paint. This is
-- an observation cache only; cloud_instances remains the lifecycle source of
-- truth and the engine remains authoritative for the current power state.
@ -140,10 +319,14 @@ CREATE TABLE IF NOT EXISTS cloud_orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
plan_id BIGINT UNSIGNED NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
offer_id BIGINT UNSIGNED DEFAULT NULL,
resource_pool_id BIGINT UNSIGNED DEFAULT NULL,
host_id BIGINT UNSIGNED DEFAULT NULL,
reservation_id BIGINT UNSIGNED DEFAULT NULL,
idempotency_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
amount DECIMAL(12,2) NOT NULL,
currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD',
status VARCHAR(32) NOT NULL DEFAULT 'queued',
spec_json LONGTEXT NOT NULL,
payload_cipher LONGTEXT DEFAULT NULL,
@ -154,7 +337,57 @@ CREATE TABLE IF NOT EXISTS cloud_orders (
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE KEY uniq_cloud_order_request (user_id, idempotency_key),
KEY cloud_order_owner (user_id, id)
UNIQUE KEY uniq_cloud_order_reservation (reservation_id),
KEY cloud_order_owner (user_id, id),
KEY cloud_order_offer_state (offer_id, status, id),
KEY cloud_order_placement (resource_pool_id, host_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE cloud_orders MODIFY host_id BIGINT UNSIGNED NULL;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS
offer_id BIGINT UNSIGNED NULL AFTER plan_id;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS
resource_pool_id BIGINT UNSIGNED NULL AFTER offer_id;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS
reservation_id BIGINT UNSIGNED NULL AFTER host_id;
ALTER TABLE cloud_orders ADD COLUMN IF NOT EXISTS
currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD' AFTER amount;
ALTER TABLE cloud_orders ADD UNIQUE INDEX IF NOT EXISTS
uniq_cloud_order_reservation (reservation_id);
ALTER TABLE cloud_orders ADD INDEX IF NOT EXISTS
cloud_order_offer_state (offer_id, status, id);
ALTER TABLE cloud_orders ADD INDEX IF NOT EXISTS
cloud_order_placement (resource_pool_id, host_id, status);
-- Reservations are the auditable resource ledger. The per-host capacity
-- policy row is locked before sums over held/committed/uncertain rows are
-- evaluated, serializing concurrent schedulers without an engine round trip.
CREATE TABLE IF NOT EXISTS cloud_resource_reservations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
reservation_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
order_id BIGINT UNSIGNED NOT NULL,
offer_id BIGINT UNSIGNED NOT NULL,
resource_pool_id BIGINT UNSIGNED NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
instance_id BIGINT UNSIGNED DEFAULT NULL,
public_ip_id BIGINT UNSIGNED DEFAULT NULL,
cpu_cores SMALLINT UNSIGNED NOT NULL,
memory_mib BIGINT UNSIGNED NOT NULL,
disk_gib BIGINT UNSIGNED NOT NULL,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'held',
expires_at BIGINT UNSIGNED DEFAULT NULL,
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_resource_reservation_key (reservation_key),
UNIQUE KEY cloud_resource_reservation_operation (operation_id),
UNIQUE KEY cloud_resource_reservation_order (order_id),
KEY cloud_resource_reservation_host (host_id, state, expires_at, id),
KEY cloud_resource_reservation_pool (resource_pool_id, state, id),
KEY cloud_resource_reservation_instance (instance_id),
KEY cloud_resource_reservation_ip (public_ip_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cloud_service_periods (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
@ -204,10 +437,12 @@ CREATE TABLE IF NOT EXISTS cloud_invoices (
order_id BIGINT UNSIGNED DEFAULT NULL,
user_id BIGINT UNSIGNED NOT NULL,
plan_id BIGINT UNSIGNED DEFAULT NULL,
offer_id BIGINT UNSIGNED DEFAULT NULL,
renewal_id BIGINT UNSIGNED DEFAULT NULL,
renews_expires_at BIGINT UNSIGNED DEFAULT NULL,
due_at BIGINT UNSIGNED NOT NULL,
amount DECIMAL(12,2) NOT NULL,
currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD',
validity_days INT UNSIGNED NOT NULL,
traffic_quota_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
status VARCHAR(16) NOT NULL DEFAULT 'open',
@ -224,6 +459,10 @@ CREATE TABLE IF NOT EXISTS cloud_invoices (
KEY cloud_invoice_due (status, due_at, id),
KEY cloud_invoice_overview_paid (status, paid_at, purpose, plan_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE cloud_invoices ADD COLUMN IF NOT EXISTS
offer_id BIGINT UNSIGNED NULL AFTER plan_id;
ALTER TABLE cloud_invoices ADD COLUMN IF NOT EXISTS
currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD' AFTER amount;
-- Releases created before void invoices became replaceable used a unique
-- period key. Keep void rows as an audit trail while permitting one corrected
-- replacement bill for the same expiry.

@ -229,7 +229,9 @@ CREATE TABLE `shop` (
`traffic_quota_bytes` bigint(20) unsigned NOT NULL DEFAULT 0,
`status` tinyint(1) NOT NULL DEFAULT 1,
`stock` int(11) NOT NULL DEFAULT -1,
`sort` int(11) NOT NULL DEFAULT 0
`sort` int(11) NOT NULL DEFAULT 0,
`created_at` bigint(20) unsigned NOT NULL DEFAULT 0,
`updated_at` bigint(20) unsigned NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `ss_invite_code` (

@ -0,0 +1,141 @@
<?php
// Static multi-host data-boundary checks. This test needs no database,
// engine, network or application bootstrap.
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$baseSchema = file_get_contents($root . '/sql/ss.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$feature = file_get_contents(
$root . '/app/Services/Cloud/CloudNetworkLineFeature.php'
);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach ([
'cloud_regions',
'cloud_network_lines',
'cloud_resource_pools',
'cloud_host_capacity_policies',
'cloud_host_capacity_snapshots',
'cloud_public_ips',
'cloud_offers',
'cloud_resource_reservations',
] as $table) {
$assert(str_contains($schema, 'CREATE TABLE IF NOT EXISTS ' . $table . ' ('),
'Missing multi-host table: ' . $table);
}
$assert(str_contains($schema, 'UNIQUE KEY cloud_region_code (code)')
&& str_contains($schema,
'UNIQUE KEY cloud_network_line_code (region_id, code)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_pool_code (line_id, code)')
&& str_contains($schema,
'UNIQUE KEY cloud_offer_plan_pool (plan_id, resource_pool_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_reservation_order (order_id)'),
'Region, line, pool, offer or reservation identities are not protected.');
$assert(str_contains($schema,
"scheduler_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'disabled'")
&& str_contains($schema,
"health_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown'")
&& str_contains($schema,
'cloud_host_scheduler (resource_pool_id, scheduler_state, health_state, id)'),
'Host intent and observed health are not stored independently.');
$assert(str_contains($schema, 'configured_cpu_cores')
&& str_contains($schema, 'safety_memory_mib')
&& str_contains($schema, 'cpu_allocated_cores')
&& str_contains($schema, 'cpu_available_cores')
&& str_contains($schema, 'memory_allocated_mib')
&& str_contains($schema, 'memory_available_mib')
&& str_contains($schema, 'storage_total_bytes')
&& str_contains($schema, 'storage_allocated_bytes')
&& str_contains($schema, 'storage_available_bytes')
&& str_contains($schema, 'instance_total_count')
&& str_contains($schema, 'instance_running_count')
&& str_contains($schema, 'public_ip_available_count')
&& str_contains($schema, 'cloud_host_capacity_sync'),
'Scheduling policy and last-known capacity observation are not separated.');
$assert(str_contains($schema, 'UNIQUE KEY cloud_public_ip_address (address)')
&& str_contains($schema, 'cloud_public_ip_available (host_id, state, id)')
&& str_contains($schema, 'operation_id CHAR(32)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_reservation (reservation_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_operation (operation_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_reservation_operation (operation_id)')
&& str_contains($schema, 'public_ip_id BIGINT UNSIGNED DEFAULT NULL'),
'Concrete public IP reservation and uncertain-operation fields are incomplete.');
$assert(str_contains($schema, 'offer_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'resource_pool_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'reservation_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema,
"currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD'")
&& str_contains($schema, 'ALTER TABLE cloud_orders MODIFY host_id BIGINT UNSIGNED NULL'),
'Orders, invoices or instances cannot retain offer and placement results.');
$shopStart = strpos($baseSchema, 'CREATE TABLE `shop` (');
$shopEnd = strpos($baseSchema, ') ENGINE=InnoDB', $shopStart);
$shop = $shopStart === false || $shopEnd === false ? ''
: substr($baseSchema, $shopStart, $shopEnd - $shopStart);
$assert(str_contains($shop, '`created_at` bigint(20) unsigned')
&& str_contains($shop, '`updated_at` bigint(20) unsigned'),
'Fresh base-plan rows lack stable audit timestamps.');
$seedStart = strpos($setup, 'private static function ensureMultiHostDefaults');
$seedEnd = strpos($setup, 'private static function upgradeReferralSchema', $seedStart);
$seed = $seedStart === false || $seedEnd === false ? ''
: substr($setup, $seedStart, $seedEnd - $seedStart);
$assert(str_contains($setup, 'self::ensureMultiHostDefaults($connection);')
&& str_contains($seed, "'name' => 'cloud_network_lines_enabled'")
&& str_contains($seed, "'value' => 'false'")
&& str_contains($seed, 'insertOrIgnore')
&& stripos($seed, 'truncate') === false
&& stripos($seed, 'drop(') === false
&& stripos($seed, 'delete(') === false,
'cloudInit must seed a disabled line switch without deleting operator data.');
$assert(str_contains($feature,
"public const CONFIG_KEY = 'cloud_network_lines_enabled'")
&& str_contains($feature, 'public static function enabled(): bool')
&& str_contains($feature, 'return self::$cached = false;')
&& str_contains($feature, 'public static function save(bool $enabled): void'),
'The fail-closed network-line feature switch is incomplete.');
$models = [
'CloudRegion' => 'cloud_regions',
'CloudNetworkLine' => 'cloud_network_lines',
'CloudResourcePool' => 'cloud_resource_pools',
'CloudHostCapacityPolicy' => 'cloud_host_capacity_policies',
'CloudHostCapacitySnapshot' => 'cloud_host_capacity_snapshots',
'CloudPublicIp' => 'cloud_public_ips',
'CloudOffer' => 'cloud_offers',
'CloudResourceReservation' => 'cloud_resource_reservations',
];
foreach ($models as $class => $table) {
$source = file_get_contents($root . '/app/Models/' . $class . '.php');
$assert($source !== false
&& str_contains($source, 'final class ' . $class . ' extends Model')
&& str_contains($source, "protected \$table = '" . $table . "';")
&& str_contains($source, 'protected $casts = ['),
'Missing or incomplete Eloquent boundary: ' . $class);
}
foreach (['CloudHost', 'Shop', 'CloudOrder', 'CloudInvoice', 'CloudInstance']
as $class) {
$source = file_get_contents($root . '/app/Models/' . $class . '.php');
$assert($source !== false && str_contains($source, 'protected $casts = ['),
'Existing model lacks canonical multi-host casts: ' . $class);
}
echo "Multi-host schema, model and feature-switch contracts passed.\n";
Loading…
Cancel
Save