fix(vpc): exclude occupied instances from member choices
parent
5783ae618f
commit
09801dc6e8
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026101202' scope='global'}
|
||||
{assign var=venix_asset_version value='2026101203' scope='global'}
|
||||
|
||||
@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud {
|
||||
// Reconciliation in this suite must settle from durable local evidence;
|
||||
// fail immediately if it unexpectedly reaches the private HTTP boundary.
|
||||
final class CloudEngineClient
|
||||
{
|
||||
public static int $requests = 0;
|
||||
|
||||
public function __construct(...$arguments)
|
||||
{
|
||||
}
|
||||
|
||||
public function vpc(...$arguments): array
|
||||
{
|
||||
self::$requests++;
|
||||
throw new \RuntimeException('Candidate recovery must not contact the engine');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
use App\Services\Cloud\CloudEngineClient;
|
||||
use App\Services\Cloud\CloudVpcPageService;
|
||||
use App\Services\Cloud\CloudVpcService;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
require __DIR__ . '/CloudVpcMemberReuseTest.php';
|
||||
if (!isset($capsule)) {
|
||||
return;
|
||||
}
|
||||
$schema->table('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->text('spec_json')->default('{}');
|
||||
});
|
||||
$seedRecovery = static function (int $id, string $memberState = 'deleted') use (
|
||||
$seedVpc, $seedInstance, $seedPair
|
||||
): array {
|
||||
$octet = $id - 900;
|
||||
$vpc = $seedVpc($id, '10.98.' . $octet . '.0/24',
|
||||
'10.98.' . $octet . '.0/24', 10, 'deleted', 'active');
|
||||
$instanceId = $id * 100;
|
||||
$memberId = $id * 1000;
|
||||
$privateId = $memberId + 1;
|
||||
$seedInstance($instanceId, 10);
|
||||
Capsule::table('cloud_instances')->where('id', $instanceId)->update([
|
||||
'spec_json' => json_encode(['region_id' => 1, 'region' => 'Test Region']),
|
||||
]);
|
||||
$seedPair($memberId, $privateId, $vpc, $instanceId, 10,
|
||||
'10.98.' . $octet . '.1', $memberState);
|
||||
return $vpc + [
|
||||
'instance_id' => $instanceId, 'member_id' => $memberId,
|
||||
'private_id' => $privateId, 'private_ip' => '10.98.' . $octet . '.1',
|
||||
];
|
||||
};
|
||||
$attachment = static function (array $vpc) use ($actor, $assert): array {
|
||||
$rows = array_column(CloudVpcPageService::bootstrap($actor)['instances'], null, 'id');
|
||||
$assert(isset($rows[$vpc['instance_id']]), 'Recovery instance is missing from the page bootstrap.');
|
||||
return $rows[$vpc['instance_id']]['vpc_attachment'];
|
||||
};
|
||||
$assertRetained = static function (array $vpc) use ($assert, $attachment): void {
|
||||
$assert(Capsule::table('cloud_vpc_members')->where('id', $vpc['member_id'])->count() === 1
|
||||
&& Capsule::table('cloud_vpc_private_ips')->where('id', $vpc['private_id'])->count() === 1
|
||||
&& $attachment($vpc)['state'] === 'cleanup_pending',
|
||||
'An unproven or in-flight identity was released or became selectable.');
|
||||
};
|
||||
|
||||
$recoverable = $seedRecovery(980);
|
||||
$beforeRoot = (array) Capsule::table('cloud_vpcs')->where('id', 980)->first();
|
||||
$beforeOperation = (array) Capsule::table('cloud_vpc_operations')
|
||||
->where('operation_id', $recoverable['operation_id'])->first();
|
||||
CloudVpcService::show($recoverable['vpc_key'], $actor);
|
||||
$assertRetained($recoverable);
|
||||
foreach ([false, true] as $allowDeferredRetry) {
|
||||
$expectCloudError(static fn () => CloudVpcService::reconcile(
|
||||
$recoverable['vpc_key'], $actor, $allowDeferredRetry
|
||||
), 409);
|
||||
$assertRetained($recoverable);
|
||||
}
|
||||
CloudVpcService::reconcile($recoverable['vpc_key'], $actor, true, true);
|
||||
$assert(Capsule::table('cloud_vpc_members')->where('id', $recoverable['member_id'])->count() === 0
|
||||
&& Capsule::table('cloud_vpc_private_ips')->where('id', $recoverable['private_id'])->count() === 0
|
||||
&& $attachment($recoverable) === ['state' => 'available', 'vpc_key' => null, 'vpc_name' => null]
|
||||
&& (array) Capsule::table('cloud_vpcs')->where('id', 980)->first() === $beforeRoot
|
||||
&& (array) Capsule::table('cloud_vpc_operations')
|
||||
->where('operation_id', $recoverable['operation_id'])->first() === $beforeOperation
|
||||
&& Capsule::table('cloud_vpc_operations')->where('vpc_id', 980)->count() === 1,
|
||||
'Explicit proof-based recovery must release only the old pair and leave root/revision/operation audit unchanged.');
|
||||
$expectCloudError(static fn () => CloudVpcService::reconcile(
|
||||
$recoverable['vpc_key'], $actor, true, true
|
||||
), 409);
|
||||
|
||||
// A later root marker does not erase exact historical absence evidence when
|
||||
// there is no operation to claim; the existing proof verifier remains owner.
|
||||
$advanced = $seedRecovery(981);
|
||||
Capsule::table('cloud_vpcs')->where('id', 981)->update([
|
||||
'desired_revision' => 2, 'desired_digest' => hash('sha256', 'recovery-newer-root'),
|
||||
'state' => 'degraded', 'sync_state' => 'uncertain',
|
||||
'last_operation_id' => $keyFor('recovery-newer-root', 981),
|
||||
]);
|
||||
CloudVpcService::reconcile($advanced['vpc_key'], $actor, true, true);
|
||||
$assert($attachment($advanced)['state'] === 'available'
|
||||
&& (int) Capsule::table('cloud_vpcs')->where('id', 981)->value('desired_revision') === 2
|
||||
&& Capsule::table('cloud_vpc_operations')->where('vpc_id', 981)->count() === 1,
|
||||
'Recovery either lost valid historical proof or created a replacement operation.');
|
||||
|
||||
$missingProof = $seedRecovery(982);
|
||||
Capsule::table('cloud_vpc_operations')->where('operation_id', $missingProof['operation_id'])
|
||||
->update(['observed_json' => null]);
|
||||
$deleting = $seedRecovery(983, 'deleting');
|
||||
$referenced = $seedRecovery(984);
|
||||
$seedRoute(984000, $referenced, $referenced['member_id'], '10.99.0.1/32', $referenced['private_ip']);
|
||||
$rootDeleting = $seedRecovery(985);
|
||||
Capsule::table('cloud_vpcs')->where('id', 985)->update(['state' => 'deleting']);
|
||||
foreach ([$missingProof, $deleting, $referenced, $rootDeleting] as $blocked) {
|
||||
$expectCloudError(static fn () => CloudVpcService::reconcile(
|
||||
$blocked['vpc_key'], $actor, true, true
|
||||
), 409);
|
||||
$assertRetained($blocked);
|
||||
$assert(Capsule::table('cloud_vpc_operations')->where('vpc_id', $blocked['id'])->count() === 1,
|
||||
'A refused cleanup must not create an operation.');
|
||||
}
|
||||
|
||||
// A real pending target takes precedence even with a valid older removal
|
||||
// proof. Its live lease returns the ordinary document without GC or replay.
|
||||
$inFlight = $seedRecovery(986);
|
||||
$historical = (array) Capsule::table('cloud_vpc_operations')
|
||||
->where('operation_id', $inFlight['operation_id'])->first();
|
||||
$pendingDocument = json_decode($historical['payload_json'], true);
|
||||
unset($pendingDocument['operation_id'], $pendingDocument['desired_digest']);
|
||||
$pendingDocument['desired_revision'] = 2;
|
||||
$pendingDigest = hash('sha256', $canonicalJson($pendingDocument));
|
||||
$pendingId = $keyFor('candidate-recovery-pending', 986);
|
||||
$pendingDocument['operation_id'] = $pendingId;
|
||||
$pendingDocument['desired_digest'] = $pendingDigest;
|
||||
$pending = $historical;
|
||||
unset($pending['id']);
|
||||
$pending = array_replace($pending, [
|
||||
'operation_id' => $pendingId, 'desired_revision' => 2,
|
||||
'idempotency_key' => 'candidate-recovery-pending-986',
|
||||
'request_hash' => hash('sha256', 'candidate-recovery-pending-986'),
|
||||
'status' => 'running', 'payload_json' => json_encode($pendingDocument),
|
||||
'observed_json' => null, 'finished_at' => null,
|
||||
'lease_token' => str_repeat('a', 32), 'lease_until' => time() + 300,
|
||||
]);
|
||||
Capsule::table('cloud_vpc_operations')->insert($pending);
|
||||
Capsule::table('cloud_vpcs')->where('id', 986)->update([
|
||||
'desired_revision' => 2, 'desired_digest' => $pendingDigest,
|
||||
'state' => 'degraded', 'sync_state' => 'uncertain', 'last_operation_id' => $pendingId,
|
||||
]);
|
||||
foreach (['queued', 'running', 'uncertain', 'waiting_observed'] as $status) {
|
||||
Capsule::table('cloud_vpc_operations')->where('operation_id', $pendingId)->update(['status' => $status]);
|
||||
CloudVpcService::reconcile($inFlight['vpc_key'], $actor, true, true);
|
||||
$assertRetained($inFlight);
|
||||
$assert(Capsule::table('cloud_vpc_operations')->where('operation_id', $pendingId)->value('status') === $status
|
||||
&& Capsule::table('cloud_vpc_operations')->where('vpc_id', 986)->count() === 2,
|
||||
'A live operation was bypassed, replayed or replaced by candidate cleanup.');
|
||||
}
|
||||
$assert(CloudEngineClient::$requests === 0, 'Candidate cleanup unexpectedly contacted the engine.');
|
||||
|
||||
echo "VPC member candidate recovery runtime passed.\n";
|
||||
}
|
||||
@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
use App\Services\Cloud\CloudVpcPageService;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
if (!is_file($root . '/vendor/autoload.php')) {
|
||||
echo "VPC page membership runtime skipped (vendor missing).\n";
|
||||
return;
|
||||
}
|
||||
require $root . '/vendor/autoload.php';
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schema = $capsule->schema('default');
|
||||
$schema->create('cloud_regions', static function (Blueprint $table): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->string('name');
|
||||
$table->string('country_code');
|
||||
$table->string('status');
|
||||
$table->integer('sort');
|
||||
});
|
||||
$schema->create('cloud_region_fabrics', static function (Blueprint $table): void {
|
||||
$table->integer('region_id')->primary();
|
||||
$table->integer('fabric_revision');
|
||||
$table->string('state');
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->integer('user_id');
|
||||
$table->string('name');
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
$table->string('service_status');
|
||||
$table->integer('expires_at')->nullable();
|
||||
});
|
||||
$schema->create('cloud_vpcs', static function (Blueprint $table): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->integer('user_id');
|
||||
$table->integer('region_id');
|
||||
$table->string('vpc_key');
|
||||
$table->string('name');
|
||||
$table->string('state');
|
||||
});
|
||||
$schema->create('cloud_vpc_members', static function (Blueprint $table): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->integer('instance_id');
|
||||
$table->integer('vpc_id');
|
||||
$table->string('state');
|
||||
});
|
||||
$schema->create('cloud_vpc_private_ips', static function (Blueprint $table): void {
|
||||
$table->integer('instance_id');
|
||||
$table->integer('member_id')->nullable();
|
||||
$table->integer('vpc_id');
|
||||
});
|
||||
Capsule::table('cloud_regions')->insert([
|
||||
['id' => 1, 'name' => 'Region A', 'country_code' => 'hk', 'status' => 'active', 'sort' => 1],
|
||||
['id' => 2, 'name' => 'Region B', 'country_code' => 'jp', 'status' => 'active', 'sort' => 0],
|
||||
]);
|
||||
Capsule::table('cloud_region_fabrics')->insert([
|
||||
['region_id' => 1, 'fabric_revision' => 1, 'state' => 'active'],
|
||||
['region_id' => 2, 'fabric_revision' => 0, 'state' => 'active'],
|
||||
]);
|
||||
$key = static fn (int $id): string => str_pad(dechex($id), 32, '0', STR_PAD_LEFT);
|
||||
foreach ([1 => 'active', 2 => 'active', 3 => 'deleting', 4 => 'deleted', 5 => 'active'] as $id => $state) {
|
||||
Capsule::table('cloud_vpcs')->insert([
|
||||
'id' => $id, 'user_id' => $id === 5 ? 8 : 7, 'region_id' => 1,
|
||||
'vpc_key' => $key($id), 'name' => $id === 5 ? 'foreign-secret-name' : 'VPC ' . $id,
|
||||
'state' => $state,
|
||||
]);
|
||||
}
|
||||
$seedInstance = static function (int $id, array $overrides = []): void {
|
||||
Capsule::table('cloud_instances')->insert(array_replace([
|
||||
'id' => $id, 'user_id' => 7, 'name' => sprintf('VM %04d', $id), 'status' => 'active',
|
||||
'spec_json' => json_encode(['region_id' => 1, 'region' => 'Region A']),
|
||||
'service_status' => 'active', 'expires_at' => time() + 3600,
|
||||
], $overrides));
|
||||
};
|
||||
$seedMember = static function (int $instanceId, int $vpcId, string $state): void {
|
||||
Capsule::table('cloud_vpc_members')->insert([
|
||||
'id' => $instanceId, 'instance_id' => $instanceId, 'vpc_id' => $vpcId, 'state' => $state,
|
||||
]);
|
||||
Capsule::table('cloud_vpc_private_ips')->insert([
|
||||
'instance_id' => $instanceId, 'member_id' => $instanceId, 'vpc_id' => $vpcId,
|
||||
]);
|
||||
};
|
||||
for ($id = 1; $id <= 25; $id++) {
|
||||
$seedInstance($id);
|
||||
}
|
||||
$seedInstance(90, ['user_id' => 8, 'name' => 'foreign-instance-name']);
|
||||
$seedMember(2, 1, 'active');
|
||||
$seedMember(3, 2, 'active');
|
||||
foreach ([4 => 'applying', 5 => 'uncertain', 6 => 'degraded', 7 => 'failed', 8 => 'pending'] as $id => $state) {
|
||||
$seedMember($id, 1, $state);
|
||||
}
|
||||
$seedMember(11, 1, 'deleting');
|
||||
$seedMember(12, 1, 'deleted');
|
||||
$seedMember(13, 3, 'active');
|
||||
$seedMember(14, 4, 'active');
|
||||
Capsule::table('cloud_vpc_private_ips')->insert(['instance_id' => 15, 'member_id' => null, 'vpc_id' => 1]);
|
||||
$seedMember(16, 5, 'active');
|
||||
$seedMember(17, 999, 'active');
|
||||
$seedMember(18, 1, 'active');
|
||||
Capsule::table('cloud_vpc_private_ips')->where('instance_id', 18)->update(['vpc_id' => 5]);
|
||||
$seedMember(19, 1, 'active');
|
||||
// Model a pre-existing corrupt database without relaxing the production unique key.
|
||||
Capsule::table('cloud_vpc_members')->insert(['id' => 190, 'instance_id' => 19, 'vpc_id' => 2, 'state' => 'active']);
|
||||
Capsule::table('cloud_instances')->where('id', 20)->update(['expires_at' => time() - 1]);
|
||||
Capsule::table('cloud_instances')->where('id', 21)->update(['service_status' => 'suspended']);
|
||||
Capsule::table('cloud_instances')->where('id', 22)->update(['service_status' => 'exempt', 'expires_at' => null]);
|
||||
Capsule::table('cloud_instances')->where('id', 23)->update(['status' => 'deleted']);
|
||||
Capsule::table('cloud_instances')->where('id', 24)->update([
|
||||
'spec_json' => json_encode(['region_id' => 2, 'region' => 'Region B']),
|
||||
]);
|
||||
Capsule::table('cloud_instances')->where('id', 25)->update(['spec_json' => 'invalid json']);
|
||||
|
||||
$actor = ['user_id' => 7, 'is_global' => true];
|
||||
$read = static function () use ($actor): array {
|
||||
$data = CloudVpcPageService::bootstrap($actor);
|
||||
return array_column($data['instances'], null, 'id');
|
||||
};
|
||||
$connection = $capsule->getConnection('default');
|
||||
$connection->enableQueryLog();
|
||||
$rows = $read();
|
||||
$queryCount = count($connection->getQueryLog());
|
||||
$assert($rows[1]['vpc_attachment'] === ['state' => 'available', 'vpc_key' => null, 'vpc_name' => null],
|
||||
'An unoccupied instance must remain selectable.');
|
||||
foreach ([2 => 1, 3 => 2, 4 => 1, 5 => 1, 6 => 1, 7 => 1, 8 => 1] as $id => $vpcId) {
|
||||
$assert($rows[$id]['vpc_attachment'] === [
|
||||
'state' => 'attached', 'vpc_key' => $key($vpcId), 'vpc_name' => 'VPC ' . $vpcId,
|
||||
], 'Every occupied member state must expose its owner-scoped VPC attachment.');
|
||||
}
|
||||
$assert($rows[2]['region_id'] === $rows[3]['region_id'],
|
||||
'The cross-VPC fixture must exercise two VPCs in the same region.');
|
||||
foreach ([11, 12, 13, 14] as $id) {
|
||||
$assert($rows[$id]['vpc_attachment']['state'] === 'cleanup_pending',
|
||||
'Member or root deletion must retain the instance reservation.');
|
||||
}
|
||||
foreach ([15, 16, 17, 18, 19] as $id) {
|
||||
$assert($rows[$id]['vpc_attachment'] === ['state' => 'unavailable', 'vpc_key' => null, 'vpc_name' => null],
|
||||
'Orphan, conflicting, duplicate or foreign ledgers must be unavailable without disclosing a VPC.');
|
||||
}
|
||||
$assert(!isset($rows[90]) && !isset($rows[23]) && !str_contains(json_encode($rows), 'foreign-'),
|
||||
'The tenant projection must not reveal other accounts or deleted instances.');
|
||||
$assert(!$rows[20]['service_available'] && !$rows[21]['service_available'] && $rows[22]['service_available']
|
||||
&& $rows[24]['region_id'] === 2 && $rows[25]['region_id'] === null,
|
||||
'Membership projection must preserve service lifecycle and region eligibility fields.');
|
||||
$bootstrap = CloudVpcPageService::bootstrap($actor);
|
||||
$assert(count($bootstrap['regions']) === 1 && $bootstrap['regions'][0]['country_code'] === 'HK',
|
||||
'The existing active Fabric region projection must remain unchanged.');
|
||||
$assert(CloudVpcPageService::bootstrap(['user_id' => 0])['instances'] === [],
|
||||
'An actor without an account must not receive instance candidates.');
|
||||
|
||||
// A reload must observe joins immediately; only normal physical cleanup frees
|
||||
// occupancy, never a terminal-looking member state or deletion of half a pair.
|
||||
$seedMember(1, 2, 'applying');
|
||||
$assert($read()[1]['vpc_attachment']['state'] === 'attached', 'A refresh missed a newly attached instance.');
|
||||
Capsule::table('cloud_vpc_members')->where('instance_id', 1)->update(['state' => 'deleted']);
|
||||
$assert($read()[1]['vpc_attachment']['state'] === 'cleanup_pending', 'Deleted membership was prematurely released.');
|
||||
Capsule::table('cloud_vpc_members')->where('instance_id', 1)->delete();
|
||||
$assert($read()[1]['vpc_attachment']['state'] === 'unavailable', 'An orphan lease was prematurely released.');
|
||||
Capsule::table('cloud_vpc_private_ips')->where('instance_id', 1)->delete();
|
||||
$assert($read()[1]['vpc_attachment']['state'] === 'available', 'Completed cleanup did not restore candidacy on refresh.');
|
||||
|
||||
$assertAllUnavailable = static function (string $reason) use ($read, $assert): void {
|
||||
$observed = $read();
|
||||
$assert(count($observed) === 24, 'Read failure must keep the bounded instance display available.');
|
||||
foreach ($observed as $row) {
|
||||
$assert($row['vpc_attachment'] === ['state' => 'unavailable', 'vpc_key' => null, 'vpc_name' => null], $reason);
|
||||
}
|
||||
};
|
||||
foreach (['cloud_vpc_members', 'cloud_vpc_private_ips', 'cloud_vpcs'] as $table) {
|
||||
$schema->rename($table, $table . '_hidden');
|
||||
$assertAllUnavailable('A missing ledger table was mistaken for no membership.');
|
||||
$schema->rename($table . '_hidden', $table);
|
||||
}
|
||||
// Inject a read failure at the actual database execution boundary while all
|
||||
// table checks pass. An unavailable ledger must not reopen free candidates.
|
||||
$failMembershipRead = true;
|
||||
$connection->beforeExecuting(static function (string $query) use (&$failMembershipRead): void {
|
||||
if ($failMembershipRead && str_contains($query, 'from "cloud_vpc_members"')) {
|
||||
throw new RuntimeException('Simulated ledger read outage');
|
||||
}
|
||||
});
|
||||
$assertAllUnavailable('A failed ledger query was mistaken for no membership.');
|
||||
$failMembershipRead = false;
|
||||
$assert($read()[1]['vpc_attachment']['state'] === 'available', 'Recovered reads must refresh candidacy.');
|
||||
|
||||
for ($id = 100; $id < 305; $id++) {
|
||||
$seedInstance($id);
|
||||
}
|
||||
$connection->flushQueryLog();
|
||||
$bounded = $read();
|
||||
$assert(count($bounded) === 200, 'The page lost its 200-instance bound.');
|
||||
$assert(count($connection->getQueryLog()) === $queryCount,
|
||||
'Membership projection introduced per-instance database queries.');
|
||||
$assert(array_slice(array_keys($bounded), 0, 24) === array_keys($rows),
|
||||
'Membership projection changed stable instance ordering.');
|
||||
|
||||
echo "VPC page membership runtime passed.\n";
|
||||
Loading…
Reference in New Issue