*/ function mysqlConcurrencyDsnParts(string $dsn): array { if (!str_starts_with($dsn, 'mysql:')) { throw new RuntimeException('Concurrency DSN must use the mysql driver.'); } $parts = []; $allowed = ['host', 'port', 'dbname', 'charset', 'unix_socket']; foreach (explode(';', substr($dsn, 6)) as $segment) { if ($segment === '') { continue; } [$key, $value] = array_pad(explode('=', $segment, 2), 2, ''); $key = strtolower(trim($key)); if ($key === '' || $value === '') { throw new RuntimeException('Concurrency DSN contains an empty field.'); } if (!in_array($key, $allowed, true) || isset($parts[$key])) { throw new RuntimeException( 'Concurrency DSN contains an unsupported or duplicate field.' ); } $parts[$key] = $value; } $database = $parts['dbname'] ?? ''; if (preg_match('/^virt_concurrency_[a-z0-9_]+$/D', $database) !== 1) { throw new RuntimeException( 'Concurrency database must match virt_concurrency_[a-z0-9_]+.' ); } if (!isset($parts['host']) && !isset($parts['unix_socket'])) { throw new RuntimeException('Concurrency DSN requires host or unix_socket.'); } return $parts; } /** @return array */ function mysqlConcurrencyRequireGate(): array { if ((string) getenv(MYSQL_CONCURRENCY_ALLOW) !== '1') { throw new RuntimeException( 'Set VIRT_ALLOW_DESTRUCTIVE_MYSQL_TEST=1 for the isolated test schema.' ); } $dsn = trim((string) getenv(MYSQL_CONCURRENCY_DSN)); $user = (string) getenv(MYSQL_CONCURRENCY_USER); $password = getenv(MYSQL_CONCURRENCY_PASSWORD); if ($dsn === '' || $user === '' || $password === false) { throw new RuntimeException('MySQL concurrency credentials are incomplete.'); } mysqlConcurrencyGlobalDeadlockAssertionEnabled(); return mysqlConcurrencyDsnParts($dsn); } function mysqlConcurrencyGlobalDeadlockAssertionEnabled(): bool { $value = (string) getenv(MYSQL_CONCURRENCY_ASSERT_GLOBAL_DEADLOCKS); if ($value === '' || $value === '0') { return false; } if ($value !== '1') { throw new RuntimeException( 'VIRT_MYSQL_TEST_ASSERT_GLOBAL_DEADLOCKS must be 0 or 1.' ); } return true; } function mysqlConcurrencyGlobalDeadlocks(PDO $pdo): int { $row = $pdo->query( "SHOW GLOBAL STATUS LIKE 'Innodb_deadlocks'" )->fetch(PDO::FETCH_ASSOC); $value = is_array($row) ? ($row['Value'] ?? null) : null; if (!is_scalar($value) || preg_match('/^[0-9]+$/D', (string) $value) !== 1) { throw new RuntimeException( 'MySQL did not expose a valid Innodb_deadlocks counter.' ); } return (int) $value; } /** @return array */ function mysqlConcurrencyConfig(): array { $parts = mysqlConcurrencyRequireGate(); $config = [ 'driver' => 'mysql', 'database' => $parts['dbname'], 'username' => (string) getenv(MYSQL_CONCURRENCY_USER), 'password' => (string) getenv(MYSQL_CONCURRENCY_PASSWORD), 'charset' => $parts['charset'] ?? 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => 'InnoDB', 'options' => [PDO::ATTR_EMULATE_PREPARES => false], ]; if (isset($parts['unix_socket'])) { $config['unix_socket'] = $parts['unix_socket']; $config['host'] = $parts['host'] ?? 'localhost'; } else { $config['host'] = $parts['host']; } if (isset($parts['port'])) { $port = filter_var($parts['port'], FILTER_VALIDATE_INT, [ 'options' => ['min_range' => 1, 'max_range' => 65535], ]); if ($port === false) { throw new RuntimeException('Concurrency DSN port is invalid.'); } $config['port'] = (int) $port; } return $config; } function mysqlConcurrencyPdo(): PDO { $parts = mysqlConcurrencyRequireGate(); $pdo = new PDO( (string) getenv(MYSQL_CONCURRENCY_DSN), (string) getenv(MYSQL_CONCURRENCY_USER), (string) getenv(MYSQL_CONCURRENCY_PASSWORD), [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ] ); if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'mysql') { throw new RuntimeException('Connected database driver is not mysql.'); } $database = (string) $pdo->query('SELECT DATABASE()')->fetchColumn(); if (preg_match('/^virt_concurrency_[a-z0-9_]+$/D', $database) !== 1 || !hash_equals($parts['dbname'], $database)) { throw new RuntimeException('Connected database failed the safety gate.'); } $pdo->exec("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"); $pdo->exec("SET SESSION innodb_lock_wait_timeout = 3"); return $pdo; } function mysqlConcurrencyCapsule(): Capsule { static $capsule = null; if ($capsule instanceof Capsule) { return $capsule; } $capsule = new Capsule(); $capsule->addConnection(mysqlConcurrencyConfig(), 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); Capsule::statement( 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ' ); Capsule::statement('SET SESSION innodb_lock_wait_timeout = 3'); global $System_Config; $System_Config = is_array($System_Config ?? null) ? $System_Config : []; $System_Config['cloud_data_key_file'] = (string) getenv(MYSQL_CONCURRENCY_KEY_FILE); return $capsule; } function mysqlConcurrencyAssert(bool $condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } } /** @return array */ function mysqlConcurrencyPayload(): array { $encoded = (string) getenv(MYSQL_CONCURRENCY_CHILD_PAYLOAD); $decoded = base64_decode($encoded, true); if ($decoded === false) { throw new RuntimeException('Child payload is not valid base64.'); } $payload = json_decode($decoded, true, 64, JSON_THROW_ON_ERROR); if (!is_array($payload) || array_is_list($payload)) { throw new RuntimeException('Child payload must be an object.'); } return $payload; } /** @return array */ function mysqlConcurrencyChildOperation(string $operation, array $payload): array { mysqlConcurrencyCapsule(); if ($operation === 'admin_begin') { $family = (int) ($payload['family'] ?? 4); $command = CloudPublicNetworkTransitionCommand::adminExact( (int) $payload['instance_id'], (int) $payload['actor_id'], (string) $payload['request_key'], [$family => [ 'pool_key' => (string) $payload['pool_key'], 'address_cidr' => (string) $payload['address_cidr'], ]] ); $dispatch = CloudPublicNetworkTransitionWorkflow::begin($command); return [ 'transition_id' => $dispatch->transitionId(), 'job_id' => $dispatch->jobId(), 'operation_id' => $dispatch->operationId(), 'replayed' => $dispatch->replayed(), ]; } if ($operation === 'reserve_bundle') { $reservationId = (int) $payload['reservation_id']; $snapshot = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, $reservationId, (string) $payload['operation_id'] ); return [ 'reservation_id' => $reservationId, 'public_network' => $snapshot->toArray(), ]; } if ($operation === 'user_begin') { $command = CloudPublicNetworkTransitionCommand::userPaidAutomatic( (int) $payload['instance_id'], (int) $payload['actor_id'], (string) $payload['request_key'], (string) $payload['expected_amount'] ); $dispatch = CloudPublicNetworkTransitionWorkflow::begin($command); return [ 'transition_id' => $dispatch->transitionId(), 'job_id' => $dispatch->jobId(), 'operation_id' => $dispatch->operationId(), 'replayed' => $dispatch->replayed(), ]; } if ($operation === 'batch_begin') { $actorId = (int) $payload['actor_id']; return CloudPublicIpPoolReplacementBatchService::start( 1, (string) $payload['source_pool_key'], [ 'user_id' => $actorId, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, ], [ 'target_pool_key' => (string) $payload['target_pool_key'], 'expected_source_revision' => (int) $payload['source_revision'], 'expected_target_revision' => (int) $payload['target_revision'], 'confirm_replacement' => true, 'request_key' => (string) $payload['request_key'], ] ); } throw new RuntimeException('Unknown child operation.'); } function mysqlConcurrencyAcknowledgeEntered( string $enteredFile, int $connectionId ): void { if ($enteredFile === '' || str_contains($enteredFile, "\0") || $connectionId < 1) { throw new RuntimeException('Child entered barrier is missing.'); } $temporary = $enteredFile . '.tmp-' . getmypid() . '-' . bin2hex(random_bytes(4)); try { if (file_put_contents( $temporary, 'entered:' . $connectionId . "\n", LOCK_EX ) === false || !rename($temporary, $enteredFile)) { throw new RuntimeException( 'Unable to acknowledge the child service-entry barrier.' ); } } finally { if (is_file($temporary)) { unlink($temporary); } } } function mysqlConcurrencyChildMain(array $argv): never { $operation = (string) ($argv[2] ?? ''); $goFile = (string) getenv(MYSQL_CONCURRENCY_CHILD_GO); $enteredFile = (string) getenv(MYSQL_CONCURRENCY_CHILD_ENTERED); if ($goFile === '' || str_contains($goFile, "\0")) { throw new RuntimeException('Child GO barrier is missing.'); } fwrite(STDOUT, json_encode([ 'ready' => true, 'pid' => getmypid(), ], JSON_THROW_ON_ERROR) . "\n"); fflush(STDOUT); $deadline = microtime(true) + 15.0; while (!is_file($goFile)) { if (microtime(true) >= $deadline) { fwrite(STDOUT, json_encode([ 'ok' => false, 'error' => 'go_barrier_timeout', ], JSON_THROW_ON_ERROR) . "\n"); exit(2); } usleep(10000); } mysqlConcurrencyCapsule(); $connectionId = (int) Capsule::connection()->getPdo() ->query('SELECT CONNECTION_ID()')->fetchColumn(); mysqlConcurrencyAcknowledgeEntered($enteredFile, $connectionId); try { $result = mysqlConcurrencyChildOperation( $operation, mysqlConcurrencyPayload() ); fwrite(STDOUT, json_encode([ 'ok' => true, 'result' => $result, ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES) . "\n"); exit(0); } catch (Throwable $error) { fwrite(STDOUT, json_encode([ 'ok' => false, 'class' => $error::class, 'http_status' => $error instanceof CloudError ? $error->httpStatus : null, 'error' => $error->getMessage(), ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES) . "\n"); exit(1); } } if (($argv[1] ?? null) === '--child') { mysqlConcurrencyChildMain($argv); } /** @return list */ function mysqlConcurrencySchemaTables(): array { return [ 'cloud_public_ip_pool_replacement_batch_items', 'cloud_public_ip_pool_replacement_batches', 'cloud_public_network_transition_ips', 'cloud_public_network_transitions', 'cloud_backup_recoveries', 'cloud_peer_migration_artifacts', 'cloud_instance_migrations', 'cloud_jobs', 'cloud_invoices', 'cloud_instances', 'cloud_resource_reservation_ips', 'cloud_resource_reservations', 'cloud_public_ips', 'cloud_public_ip_pools', 'cloud_host_network_attachments', 'cloud_resource_pools', 'cloud_host_agents', 'cloud_hosts', 'configurations', 'user', ]; } function mysqlConcurrencyAssertDestructiveSchemaSafety(PDO $pdo): void { $schemaTables = mysqlConcurrencySchemaTables(); if (in_array(MYSQL_CONCURRENCY_MARKER_TABLE, $schemaTables, true) || count($schemaTables) !== count(array_unique($schemaTables))) { throw new LogicException( 'Concurrency schema allowlist may not contain marker or duplicates.' ); } $objects = $pdo->query(<<<'SQL' SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.tables WHERE table_schema = DATABASE() ORDER BY TABLE_NAME SQL)->fetchAll(PDO::FETCH_ASSOC); if ($objects === []) { $pdo->exec(<<<'SQL' CREATE TABLE virt_concurrency_harness_marker ( marker_key VARCHAR(32) NOT NULL PRIMARY KEY, marker_value VARCHAR(96) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL); $statement = $pdo->prepare( 'INSERT INTO virt_concurrency_harness_marker ' . '(marker_key, marker_value) VALUES (?, ?)' ); $statement->execute(['owner', MYSQL_CONCURRENCY_MARKER_VALUE]); return; } $allowed = array_fill_keys(array_merge( [MYSQL_CONCURRENCY_MARKER_TABLE], $schemaTables ), true); foreach ($objects as $object) { $name = (string) ($object['TABLE_NAME'] ?? ''); $type = (string) ($object['TABLE_TYPE'] ?? ''); if (!isset($allowed[$name]) || $type !== 'BASE TABLE') { throw new RuntimeException( 'Concurrency schema contains an unknown or unsafe object.' ); } } if (!in_array(MYSQL_CONCURRENCY_MARKER_TABLE, array_column( $objects, 'TABLE_NAME' ), true)) { throw new RuntimeException( 'Non-empty concurrency schema has no harness ownership marker.' ); } try { $markers = $pdo->query( 'SELECT marker_key, marker_value ' . 'FROM virt_concurrency_harness_marker ORDER BY marker_key' )->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $error) { throw new RuntimeException( 'Concurrency schema ownership marker is unreadable.', 0, $error ); } if (count($markers) !== 1 || (string) ($markers[0]['marker_key'] ?? '') !== 'owner' || !hash_equals( MYSQL_CONCURRENCY_MARKER_VALUE, (string) ($markers[0]['marker_value'] ?? '') )) { throw new RuntimeException( 'Concurrency schema ownership marker is invalid.' ); } } function mysqlConcurrencyResetSchema(PDO $pdo): void { mysqlConcurrencyAssertDestructiveSchemaSafety($pdo); $tables = mysqlConcurrencySchemaTables(); $pdo->exec('SET FOREIGN_KEY_CHECKS = 0'); foreach ($tables as $table) { $pdo->exec('DROP TABLE IF EXISTS `' . $table . '`'); } $pdo->exec('SET FOREIGN_KEY_CHECKS = 1'); $statements = [ <<<'SQL' CREATE TABLE `user` ( id INT NOT NULL PRIMARY KEY, money DECIMAL(12,2) NOT NULL, enable TINYINT(1) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE configurations ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(191) NOT NULL, value LONGTEXT NOT NULL, UNIQUE KEY configurations_name (name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_hosts ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, name VARCHAR(80) NOT NULL, enabled TINYINT(1) NOT NULL, created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_host_agents ( host_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, sealed_api_secret LONGTEXT NOT NULL, enabled TINYINT(1) NOT NULL, revision BIGINT UNSIGNED NOT NULL, tested_revision BIGINT UNSIGNED NOT NULL, last_test_status VARCHAR(32) NOT NULL, last_error_code VARCHAR(64) NOT NULL, last_agent_version VARCHAR(64) NOT NULL, network_selectors_json LONGTEXT NOT NULL, public_network_capabilities_json LONGTEXT NOT NULL, last_tested_at BIGINT UNSIGNED NOT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_resource_pools ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, line_id BIGINT UNSIGNED NOT NULL, code VARCHAR(32) NOT NULL, name VARCHAR(80) NOT NULL, status VARCHAR(16) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_host_network_attachments ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, attachment_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, host_id BIGINT UNSIGNED NOT NULL, line_id BIGINT UNSIGNED NOT NULL, name VARCHAR(80) NOT NULL, network_selector VARCHAR(128) NOT NULL, state VARCHAR(16) NOT NULL, priority INT NOT NULL, verified_agent_revision BIGINT UNSIGNED NOT NULL, verification_state VARCHAR(16) NOT NULL, last_verified_at BIGINT UNSIGNED NOT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_attachment_key (attachment_key) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_public_ip_pools ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, 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(64) NOT NULL, gateway VARCHAR(45) NOT NULL, gateway_on_link TINYINT(1) NOT NULL, guest_prefix_length TINYINT UNSIGNED NOT NULL, dns_servers_json LONGTEXT NOT NULL, reserved_ips_json LONGTEXT NOT NULL, allocation_start VARCHAR(45) DEFAULT NULL, allocation_end VARCHAR(45) DEFAULT NULL, allocation_cursor VARCHAR(45) DEFAULT NULL, max_allocations BIGINT UNSIGNED NOT NULL, state VARCHAR(16) NOT NULL, priority INT NOT NULL, desired_revision BIGINT UNSIGNED NOT NULL, observed_revision BIGINT UNSIGNED NOT NULL, sync_state VARCHAR(16) NOT NULL, last_error_code VARCHAR(64) NOT NULL DEFAULT '', created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_pool_key (pool_key), KEY cloud_pool_catalog (host_id, network_attachment_id, address_family, state, priority, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' 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 NOT 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) NOT NULL, reservation_id BIGINT UNSIGNED DEFAULT NULL, reservation_ip_claim_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_family, address_binary), UNIQUE KEY cloud_public_ip_claim (reservation_ip_claim_id), KEY cloud_public_ip_pool_state (public_ip_pool_id, state, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_resource_reservations ( id BIGINT UNSIGNED NOT NULL 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 DEFAULT NULL, backup_restore_id BIGINT UNSIGNED DEFAULT NULL, offer_id BIGINT UNSIGNED DEFAULT 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, network_attachment_id BIGINT UNSIGNED DEFAULT NULL, public_ip_pool_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) NOT NULL, expires_at BIGINT UNSIGNED DEFAULT NULL, released_at BIGINT UNSIGNED DEFAULT NULL, release_reason VARCHAR(64) NOT NULL DEFAULT '', created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_reservation_key (reservation_key), UNIQUE KEY cloud_reservation_operation (operation_id), UNIQUE KEY cloud_reservation_order (order_id), KEY cloud_reservation_instance (instance_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' 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, claim_generation BIGINT UNSIGNED NOT NULL, active_claim TINYINT UNSIGNED DEFAULT 1, 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) NOT NULL, released_at BIGINT UNSIGNED DEFAULT NULL, release_reason VARCHAR(64) NOT NULL DEFAULT '', created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_reservation_ip_active (reservation_id, address_family, active_claim), UNIQUE KEY cloud_reservation_ip_generation (reservation_id, claim_generation, address_family), UNIQUE KEY cloud_reservation_ip_operation (operation_id, address_family), KEY cloud_reservation_ip_pool (public_ip_pool_id, state, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_instances ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, host_id BIGINT UNSIGNED NOT NULL, 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 NOT NULL, reservation_id BIGINT UNSIGNED NOT NULL, uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, name VARCHAR(80) NOT NULL, status VARCHAR(16) NOT NULL, service_status VARCHAR(24) NOT NULL, network_status VARCHAR(24) NOT NULL, expires_at BIGINT UNSIGNED DEFAULT NULL, spec_json LONGTEXT NOT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_instance_uuid (uuid), UNIQUE KEY cloud_instance_reservation (reservation_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_public_network_transitions ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, transition_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, kind VARCHAR(32) NOT NULL, instance_id BIGINT UNSIGNED NOT NULL, host_id BIGINT UNSIGNED NOT NULL, reservation_id BIGINT UNSIGNED NOT NULL, invoice_id BIGINT UNSIGNED DEFAULT NULL, pool_replacement_batch_item_id BIGINT UNSIGNED DEFAULT NULL, requested_by_user_id INT NOT NULL, source_network_attachment_id BIGINT UNSIGNED NOT NULL, target_network_attachment_id BIGINT UNSIGNED NOT NULL, source_claim_set_sha256 CHAR(64) NOT NULL, target_claim_set_sha256 CHAR(64) NOT NULL, source_public_network_json LONGTEXT NOT NULL, source_public_network_sha256 CHAR(64) NOT NULL, target_public_network_json LONGTEXT NOT NULL, target_public_network_sha256 CHAR(64) NOT NULL, state VARCHAR(32) NOT NULL, effect_resolution VARCHAR(24) NOT NULL, active_transition TINYINT UNSIGNED DEFAULT 1, last_error_code VARCHAR(64) NOT NULL DEFAULT '', effect_resolved_at BIGINT UNSIGNED DEFAULT NULL, terminal_at BIGINT UNSIGNED DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_transition_key (transition_key), UNIQUE KEY cloud_transition_operation (operation_id), UNIQUE KEY cloud_transition_invoice (invoice_id), UNIQUE KEY cloud_transition_batch_item (pool_replacement_batch_item_id), UNIQUE KEY cloud_transition_active_instance (instance_id, active_transition), UNIQUE KEY cloud_transition_active_reservation (reservation_id, active_transition) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_public_network_transition_ips ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, transition_id BIGINT UNSIGNED NOT NULL, address_family TINYINT UNSIGNED NOT NULL, source_claim_id BIGINT UNSIGNED NOT NULL, target_claim_id BIGINT UNSIGNED DEFAULT NULL, changed TINYINT(1) NOT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_transition_ip_family (transition_id, address_family), UNIQUE KEY cloud_transition_target_claim (target_claim_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_invoices ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, invoice_no VARCHAR(64) NOT NULL, purpose VARCHAR(32) NOT NULL, instance_id BIGINT UNSIGNED DEFAULT NULL, 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) NOT NULL, validity_days INT UNSIGNED NOT NULL, traffic_quota_bytes BIGINT UNSIGNED NOT NULL, status VARCHAR(24) NOT NULL, paid_via VARCHAR(24) DEFAULT NULL, payment_reference VARCHAR(191) DEFAULT NULL, paid_at BIGINT UNSIGNED DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_invoice_no (invoice_no), UNIQUE KEY cloud_invoice_reference (payment_reference) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_jobs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, operation_id CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, request_idempotency_key VARCHAR(128) NOT NULL, request_fingerprint CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, parent_job_id BIGINT UNSIGNED DEFAULT NULL, supersedes_job_id BIGINT UNSIGNED DEFAULT NULL, vpc_repair_root_job_id BIGINT UNSIGNED DEFAULT NULL, order_id BIGINT UNSIGNED DEFAULT NULL, invoice_id BIGINT UNSIGNED DEFAULT NULL, instance_id BIGINT UNSIGNED DEFAULT NULL, snapshot_id BIGINT UNSIGNED DEFAULT NULL, backup_id BIGINT UNSIGNED DEFAULT NULL, backup_restore_id BIGINT UNSIGNED DEFAULT NULL, backup_recovery_id BIGINT UNSIGNED DEFAULT NULL, public_network_transition_id BIGINT UNSIGNED DEFAULT NULL, vpc_id BIGINT UNSIGNED DEFAULT NULL, vpc_port_id BIGINT UNSIGNED DEFAULT NULL, vpc_batch_id BIGINT UNSIGNED DEFAULT NULL, user_id BIGINT UNSIGNED NOT NULL, host_id BIGINT UNSIGNED DEFAULT NULL, kind VARCHAR(64) NOT NULL, execution_mode VARCHAR(16) NOT NULL, phase VARCHAR(32) NOT NULL, outcome VARCHAR(32) DEFAULT NULL, status VARCHAR(32) NOT NULL, sealed_payload LONGTEXT NOT NULL, attempts INT UNSIGNED NOT NULL, dispatch_attempts INT UNSIGNED NOT NULL, dispatch_deadline_at BIGINT UNSIGNED NOT NULL, dispatch_started_at BIGINT UNSIGNED DEFAULT NULL, agent_accepted_at BIGINT UNSIGNED DEFAULT NULL, receipt_deadline_at BIGINT UNSIGNED DEFAULT NULL, terminal_at BIGINT UNSIGNED DEFAULT NULL, effect_may_have_applied TINYINT(1) DEFAULT NULL, safe_to_resubmit TINYINT(1) DEFAULT NULL, effect_resolution VARCHAR(32) NOT NULL, effect_resolved_at BIGINT UNSIGNED DEFAULT NULL, effect_resolved_by BIGINT UNSIGNED DEFAULT NULL, effect_resolution_reason LONGTEXT DEFAULT NULL, finalization_incident TINYINT(1) NOT NULL, finalization_error_code VARCHAR(64) DEFAULT NULL, completion_event_id VARCHAR(128) DEFAULT NULL, read_at BIGINT UNSIGNED DEFAULT NULL, dispatched_at BIGINT UNSIGNED DEFAULT NULL, lease_token CHAR(32) DEFAULT NULL, lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0, available_at BIGINT UNSIGNED NOT NULL, error_code VARCHAR(64) DEFAULT NULL, result_json LONGTEXT DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_job_operation (operation_id), UNIQUE KEY cloud_job_request (user_id, request_idempotency_key), UNIQUE KEY cloud_job_transition (public_network_transition_id), KEY cloud_job_instance (instance_id, id), KEY cloud_job_parent (parent_job_id, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_instance_migrations ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, instance_id BIGINT UNSIGNED NOT NULL, state VARCHAR(32) NOT NULL, KEY cloud_migration_instance (instance_id, state, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_peer_migration_artifacts ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_backup_recoveries ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, instance_id BIGINT UNSIGNED NOT NULL, state VARCHAR(32) NOT NULL, KEY cloud_backup_recovery_instance (instance_id, state, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_public_ip_pool_replacement_batches ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, batch_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, host_id BIGINT UNSIGNED NOT NULL, source_public_ip_pool_id BIGINT UNSIGNED NOT NULL, target_public_ip_pool_id BIGINT UNSIGNED NOT NULL, source_initial_state VARCHAR(16) NOT NULL, parent_job_id BIGINT UNSIGNED NOT NULL, requested_by_user_id INT NOT NULL, state VARCHAR(16) NOT NULL, active_claim TINYINT UNSIGNED DEFAULT 1, last_error_code VARCHAR(64) NOT NULL DEFAULT '', terminal_at BIGINT UNSIGNED DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_batch_key (batch_key), UNIQUE KEY cloud_batch_parent (parent_job_id), UNIQUE KEY cloud_batch_active_source (source_public_ip_pool_id, active_claim), KEY cloud_batch_target (target_public_ip_pool_id, active_claim, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, <<<'SQL' CREATE TABLE cloud_public_ip_pool_replacement_batch_items ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, batch_id BIGINT UNSIGNED NOT NULL, instance_id BIGINT UNSIGNED NOT NULL, address_family TINYINT UNSIGNED NOT NULL, source_reservation_id BIGINT UNSIGNED NOT NULL, source_public_ip_id BIGINT UNSIGNED NOT NULL, target_public_ip_id BIGINT UNSIGNED NOT NULL, source_network_attachment_id BIGINT UNSIGNED NOT NULL, target_network_attachment_id BIGINT UNSIGNED NOT NULL, child_job_id BIGINT UNSIGNED DEFAULT NULL, state VARCHAR(16) NOT NULL, last_error_code VARCHAR(64) NOT NULL DEFAULT '', created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_batch_item_instance (batch_id, instance_id), UNIQUE KEY cloud_batch_item_child (child_job_id), UNIQUE KEY cloud_batch_item_target (batch_id, target_public_ip_id), KEY cloud_batch_item_state (batch_id, state, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci SQL, ]; foreach ($statements as $statement) { $pdo->exec($statement); } } function mysqlConcurrencySeed(): void { mysqlConcurrencyCapsule(); $now = time(); Capsule::table('configurations')->insert([ 'name' => 'cloud_ip_change_price', 'value' => '5.00', ]); Capsule::table('user')->insert([ ['id' => 10, 'money' => '20.00', 'enable' => 1], ['id' => 98, 'money' => '0.00', 'enable' => 1], ['id' => 99, 'money' => '0.00', 'enable' => 1], ]); Capsule::table('cloud_hosts')->insert([ 'id' => 1, 'name' => 'HKG-A', 'enabled' => 1, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_host_agents')->insert([ 'host_id' => 1, 'sealed_api_secret' => 'unused', 'enabled' => 1, 'revision' => 3, 'tested_revision' => 3, 'last_test_status' => 'healthy', 'last_error_code' => '', 'last_agent_version' => 'mysql-concurrency-test', 'network_selectors_json' => '["bridge:br0"]', 'public_network_capabilities_json' => json_encode([ 'contract' => 1, 'dual_stack_required' => true, 'project_owned_filter' => true, 'ndp_guard' => true, ], JSON_THROW_ON_ERROR), 'last_tested_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_resource_pools')->insert([ 'id' => 1, 'line_id' => 7, 'code' => 'hkg', 'name' => 'Hong Kong', 'status' => 'active', ]); Capsule::table('cloud_host_network_attachments')->insert([ 'id' => 1, 'attachment_key' => str_repeat('1', 32), 'host_id' => 1, 'line_id' => 7, 'name' => 'HKG public', 'network_selector' => 'bridge:br0', 'state' => 'active', 'priority' => 0, 'verified_agent_revision' => 3, 'verification_state' => 'verified', 'last_verified_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $pool = static function ( int $id, string $key, int $family, string $cidr, string $gateway, int $revision ) use ($now): array { return [ 'id' => $id, 'pool_key' => str_repeat($key, 32), 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => $family, 'name' => 'pool-' . $id, 'cidr' => $cidr, 'gateway' => $gateway, 'gateway_on_link' => 0, 'guest_prefix_length' => $family === 4 ? 29 : 64, 'dns_servers_json' => $family === 4 ? '["1.1.1.1"]' : '["2606:4700:4700::1111"]', 'reserved_ips_json' => '[]', 'allocation_start' => $family === 6 ? '2404:c140:156:3a::1000' : null, 'allocation_end' => $family === 6 ? '2404:c140:156:3a::10ff' : null, 'allocation_cursor' => $family === 6 ? '2404:c140:156:3a::1002' : null, 'max_allocations' => $family === 6 ? 64 : 0, 'state' => 'active', 'priority' => $id, 'desired_revision' => $revision, 'observed_revision' => $revision, 'sync_state' => 'applied', 'last_error_code' => '', 'created_at' => $now, 'updated_at' => $now, ]; }; Capsule::table('cloud_public_ip_pools')->insert([ $pool(11, 'a', 4, '8.8.8.0/29', '8.8.8.1', 11), $pool(12, 'b', 6, '2404:c140:156:3a::/64', '2404:c140:156:3a::1', 12), $pool(13, 'c', 4, '8.8.9.0/29', '8.8.9.1', 13), $pool(15, 'e', 4, '8.8.10.0/29', '8.8.10.1', 15), ]); $operation = static fn (int $value): string => str_pad( dechex($value), 32, '0', STR_PAD_LEFT ); $ip = static function ( int $id, int $poolId, int $family, string $address, string $state = 'available', ?int $reservationId = null, ?int $claimId = null, ?int $instanceId = null, ?string $operationId = null ) use ($now): array { return [ 'id' => $id, 'host_id' => 1, 'public_ip_pool_id' => $poolId, 'address_family' => $family, 'address' => $address, 'address_binary' => inet_pton($address), 'prefix_length' => $family === 4 ? 29 : 64, 'state' => $state, 'reservation_id' => $reservationId, 'reservation_ip_claim_id' => $claimId, 'instance_id' => $instanceId, 'operation_id' => $operationId, 'created_at' => $now, 'updated_at' => $now, ]; }; Capsule::table('cloud_public_ips')->insert([ $ip(1101, 11, 4, '8.8.8.2', 'assigned', 1001, 10011, 101, $operation(1001)), $ip(1102, 11, 4, '8.8.8.3', 'assigned', 1002, 10021, 102, $operation(1002)), $ip(1103, 11, 4, '8.8.8.4'), $ip(1104, 11, 4, '8.8.8.5'), $ip(1201, 12, 6, '2404:c140:156:3a::1000', 'assigned', 1001, 10012, 101, $operation(1001)), $ip(1202, 12, 6, '2404:c140:156:3a::1001', 'assigned', 1002, 10022, 102, $operation(1002)), $ip(1301, 13, 4, '8.8.9.2'), $ip(1302, 13, 4, '8.8.9.3'), $ip(1303, 13, 4, '8.8.9.4'), $ip(1304, 13, 4, '8.8.9.5'), $ip(1501, 15, 4, '8.8.10.2'), $ip(1502, 15, 4, '8.8.10.3'), $ip(1503, 15, 4, '8.8.10.4'), $ip(1504, 15, 4, '8.8.10.5'), ]); $snapshot = static function (string $ipv4, string $ipv6): array { return [ 'contract' => 1, 'network_selector' => 'bridge:br0', 'ipv4' => [ 'pool_key' => str_repeat('a', 32), 'pool_cidr' => '8.8.8.0/29', 'address_cidr' => $ipv4 . '/29', 'gateway' => '8.8.8.1', 'gateway_on_link' => false, ], 'ipv6' => [ 'pool_key' => str_repeat('b', 32), 'pool_cidr' => '2404:c140:156:3a::/64', 'address_cidr' => $ipv6 . '/64', 'gateway' => '2404:c140:156:3a::1', 'gateway_on_link' => false, ], 'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'], ]; }; foreach ([ [101, 1001, 10, 10011, 10012, 1101, 1201, '8.8.8.2', '2404:c140:156:3a::1000'], [102, 1002, 10, 10021, 10022, 1102, 1202, '8.8.8.3', '2404:c140:156:3a::1001'], ] as $row) { [ $instanceId, $reservationId, $userId, $claim4, $claim6, $ip4, $ip6, $address4, $address6, ] = $row; Capsule::table('cloud_resource_reservations')->insert([ 'id' => $reservationId, 'reservation_key' => str_pad((string) $reservationId, 32, '0'), 'operation_id' => $operation($reservationId), 'order_id' => $reservationId, 'backup_restore_id' => null, 'offer_id' => 1, 'resource_pool_id' => 1, 'host_id' => 1, 'user_id' => $userId, 'instance_id' => $instanceId, 'network_attachment_id' => null, 'public_ip_pool_id' => null, 'public_ip_id' => null, 'cpu_cores' => 1, 'memory_mib' => 1024, 'disk_gib' => 20, 'state' => 'committed', 'expires_at' => null, 'released_at' => null, 'release_reason' => '', 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_resource_reservation_ips')->insert([ [ 'id' => $claim4, 'reservation_id' => $reservationId, 'address_family' => 4, 'claim_generation' => 1, 'active_claim' => 1, 'network_attachment_id' => 1, 'public_ip_pool_id' => 11, 'public_ip_id' => $ip4, 'operation_id' => $operation($reservationId), 'state' => 'committed', 'released_at' => null, 'release_reason' => '', 'created_at' => $now, 'updated_at' => $now, ], [ 'id' => $claim6, 'reservation_id' => $reservationId, 'address_family' => 6, 'claim_generation' => 1, 'active_claim' => 1, 'network_attachment_id' => 1, 'public_ip_pool_id' => 12, 'public_ip_id' => $ip6, 'operation_id' => $operation($reservationId), 'state' => 'committed', 'released_at' => null, 'release_reason' => '', 'created_at' => $now, 'updated_at' => $now, ], ]); Capsule::table('cloud_instances')->insert([ 'id' => $instanceId, 'host_id' => 1, 'user_id' => $userId, 'order_id' => $reservationId, 'plan_id' => 1, 'offer_id' => 1, 'resource_pool_id' => 1, 'reservation_id' => $reservationId, 'uuid' => sprintf( '00000000-0000-4000-8000-%012d', $instanceId ), 'name' => 'vm-' . $instanceId, 'status' => 'active', 'service_status' => 'active', 'network_status' => 'active', 'expires_at' => $now + 86400, 'spec_json' => json_encode([ 'managed' => true, 'os_source' => 'cloud_template', 'public_network' => $snapshot($address4, $address6), ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES), 'created_at' => $now, 'updated_at' => $now, ]); } foreach ([2001, 2002] as $reservationId) { Capsule::table('cloud_resource_reservations')->insert([ 'id' => $reservationId, 'reservation_key' => str_pad((string) $reservationId, 32, '0'), 'operation_id' => $operation($reservationId), 'order_id' => null, 'backup_restore_id' => null, 'offer_id' => 1, 'resource_pool_id' => 1, 'host_id' => 1, 'user_id' => 10, 'instance_id' => null, 'network_attachment_id' => null, 'public_ip_pool_id' => null, 'public_ip_id' => null, 'cpu_cores' => 1, 'memory_mib' => 1024, 'disk_gib' => 20, 'state' => 'held', 'expires_at' => $now + 900, 'released_at' => null, 'release_reason' => '', 'created_at' => $now, 'updated_at' => $now, ]); } } /** @return list> */ function mysqlConcurrencyRows(string $table, array $columns): array { return Capsule::table($table)->orderBy($columns[0])->get($columns)->map( static fn ($row): array => (array) $row )->all(); } /** * Stable database projection used as a sequential oracle. Random operation * identifiers, encrypted payloads and wall-clock fields are deliberately * excluded; every durable business identity and ledger relationship remains. * * @return array>> */ function mysqlConcurrencyCanonicalSnapshot(): array { return [ 'users' => mysqlConcurrencyRows('user', [ 'id', 'money', 'enable', ]), 'hosts' => mysqlConcurrencyRows('cloud_hosts', [ 'id', 'name', 'enabled', ]), 'agents' => mysqlConcurrencyRows('cloud_host_agents', [ 'host_id', 'enabled', 'revision', 'tested_revision', 'last_test_status', 'network_selectors_json', 'public_network_capabilities_json', ]), 'resource_pools' => mysqlConcurrencyRows('cloud_resource_pools', [ 'id', 'line_id', 'code', 'name', 'status', ]), 'attachments' => mysqlConcurrencyRows( 'cloud_host_network_attachments', [ 'id', 'attachment_key', 'host_id', 'line_id', 'name', 'network_selector', 'state', 'priority', 'verified_agent_revision', 'verification_state', ] ), 'pools' => mysqlConcurrencyRows('cloud_public_ip_pools', [ 'id', 'pool_key', 'host_id', 'network_attachment_id', 'address_family', 'cidr', 'gateway', 'gateway_on_link', 'guest_prefix_length', 'state', 'priority', 'desired_revision', 'observed_revision', 'sync_state', ]), 'ips' => mysqlConcurrencyRows('cloud_public_ips', [ 'id', 'host_id', 'public_ip_pool_id', 'address_family', 'address', 'prefix_length', 'state', 'reservation_id', 'reservation_ip_claim_id', 'instance_id', ]), 'reservations' => mysqlConcurrencyRows( 'cloud_resource_reservations', [ 'id', 'resource_pool_id', 'host_id', 'user_id', 'instance_id', 'state', ] ), 'claims' => mysqlConcurrencyRows('cloud_resource_reservation_ips', [ 'id', 'reservation_id', 'address_family', 'claim_generation', 'active_claim', 'network_attachment_id', 'public_ip_pool_id', 'public_ip_id', 'state', 'released_at', 'release_reason', ]), 'instances' => mysqlConcurrencyRows('cloud_instances', [ 'id', 'host_id', 'user_id', 'resource_pool_id', 'reservation_id', 'uuid', 'name', 'status', 'service_status', 'network_status', 'spec_json', ]), 'transitions' => mysqlConcurrencyRows( 'cloud_public_network_transitions', [ 'id', 'transition_key', 'kind', 'instance_id', 'host_id', 'reservation_id', 'invoice_id', 'pool_replacement_batch_item_id', 'requested_by_user_id', 'source_network_attachment_id', 'target_network_attachment_id', 'source_claim_set_sha256', 'source_public_network_json', 'source_public_network_sha256', 'target_public_network_json', 'target_public_network_sha256', 'state', 'effect_resolution', 'active_transition', 'last_error_code', ] ), 'transition_ips' => mysqlConcurrencyRows( 'cloud_public_network_transition_ips', [ 'id', 'transition_id', 'address_family', 'source_claim_id', 'target_claim_id', 'changed', ] ), 'invoices' => mysqlConcurrencyRows('cloud_invoices', [ 'id', 'purpose', 'instance_id', 'user_id', 'amount', 'currency', 'status', 'paid_via', ]), 'jobs' => mysqlConcurrencyRows('cloud_jobs', [ 'id', 'request_idempotency_key', 'request_fingerprint', 'parent_job_id', 'invoice_id', 'instance_id', 'public_network_transition_id', 'user_id', 'host_id', 'kind', 'execution_mode', 'phase', 'outcome', 'status', 'attempts', 'dispatch_attempts', 'effect_may_have_applied', 'safe_to_resubmit', 'effect_resolution', ]), 'batches' => mysqlConcurrencyRows( 'cloud_public_ip_pool_replacement_batches', [ 'id', 'batch_key', 'host_id', 'source_public_ip_pool_id', 'target_public_ip_pool_id', 'source_initial_state', 'parent_job_id', 'requested_by_user_id', 'state', 'active_claim', 'last_error_code', ] ), 'batch_items' => mysqlConcurrencyRows( 'cloud_public_ip_pool_replacement_batch_items', [ 'id', 'batch_id', 'instance_id', 'address_family', 'source_reservation_id', 'source_public_ip_id', 'target_public_ip_id', 'source_network_attachment_id', 'target_network_attachment_id', 'child_job_id', 'state', 'last_error_code', ] ), ]; } /** @param array>> $expected */ function mysqlConcurrencyAssertCanonical( array $expected, string $context ): void { mysqlConcurrencyAssert( mysqlConcurrencyCanonicalSnapshot() === $expected, $context . ': concurrent state differs from the sequential winner.' ); } function mysqlConcurrencyDrainPipes( array $pipes, string &$stdout, string &$stderr ): void { foreach ([1 => &$stdout, 2 => &$stderr] as $index => &$buffer) { if (!isset($pipes[$index]) || !is_resource($pipes[$index])) { continue; } $chunk = stream_get_contents($pipes[$index]); if (is_string($chunk) && $chunk !== '') { $buffer .= $chunk; } } unset($buffer); } /** @return array{0:string,1:string} */ function mysqlConcurrencyTerminateChild($process, array $pipes): array { $stdout = ''; $stderr = ''; if (is_resource($process) && (proc_get_status($process)['running'] ?? false) === true) { proc_terminate($process); } $deadline = microtime(true) + 0.25; while (is_resource($process) && (proc_get_status($process)['running'] ?? false) === true && microtime(true) < $deadline) { mysqlConcurrencyDrainPipes($pipes, $stdout, $stderr); usleep(10000); } if (is_resource($process) && (proc_get_status($process)['running'] ?? false) === true) { proc_terminate($process, 9); } $deadline = microtime(true) + 0.75; while (is_resource($process) && (proc_get_status($process)['running'] ?? false) === true && microtime(true) < $deadline) { mysqlConcurrencyDrainPipes($pipes, $stdout, $stderr); usleep(10000); } mysqlConcurrencyDrainPipes($pipes, $stdout, $stderr); foreach ($pipes as $pipe) { if (is_resource($pipe)) { fclose($pipe); } } if (is_resource($process) && (proc_get_status($process)['running'] ?? false) !== true) { proc_close($process); } return [$stdout, $stderr]; } function mysqlConcurrencyWaitForReady($process, array $pipes): void { $stdout = ''; $stderr = ''; $deadline = microtime(true) + 5.0; while (microtime(true) < $deadline) { mysqlConcurrencyDrainPipes($pipes, $stdout, $stderr); foreach (preg_split('/\R/', trim($stdout)) ?: [] as $line) { $decoded = json_decode($line, true); if (is_array($decoded) && ($decoded['ready'] ?? false) === true) { return; } } if ((proc_get_status($process)['running'] ?? false) !== true) { [, $tail] = mysqlConcurrencyTerminateChild($process, $pipes); throw new RuntimeException( 'Concurrency child exited before ready: ' . trim($stderr . $tail) ); } usleep(10000); } [, $tail] = mysqlConcurrencyTerminateChild($process, $pipes); throw new RuntimeException( 'Concurrency child readiness exceeded 5 seconds: ' . trim($stderr . $tail) ); } /** * @return array{process:resource,pipes:array,go:string, * entered:string} */ function mysqlConcurrencySpawn( string $operation, array $payload, string $barrierDirectory, string $name ): array { $goFile = $barrierDirectory . '/' . $name . '.go'; $enteredFile = $barrierDirectory . '/' . $name . '.entered'; $environment = getenv(); $environment = is_array($environment) ? $environment : []; $environment[MYSQL_CONCURRENCY_CHILD_PAYLOAD] = base64_encode( json_encode($payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES) ); $environment[MYSQL_CONCURRENCY_CHILD_GO] = $goFile; $environment[MYSQL_CONCURRENCY_CHILD_ENTERED] = $enteredFile; $pipes = []; $process = proc_open( [PHP_BINARY, dirname(__DIR__) . '/CloudPublicNetworkMysqlConcurrencyTest.php', '--child', $operation], [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, dirname(__DIR__, 2), $environment, ['bypass_shell' => true] ); if (!is_resource($process)) { throw new RuntimeException('Unable to start concurrency child.'); } fclose($pipes[0]); $stdoutNonblocking = stream_set_blocking($pipes[1], false); $stderrNonblocking = stream_set_blocking($pipes[2], false); if (!$stdoutNonblocking || !$stderrNonblocking) { proc_terminate($process, 9); foreach ([1, 2] as $index) { if (is_resource($pipes[$index])) { fclose($pipes[$index]); } } proc_close($process); throw new RuntimeException( 'Unable to make concurrency child pipes nonblocking.' ); } mysqlConcurrencyWaitForReady($process, $pipes); return [ 'process' => $process, 'pipes' => $pipes, 'go' => $goFile, 'entered' => $enteredFile, ]; } function mysqlConcurrencyReleaseChild(array $child): void { if (file_put_contents($child['go'], "go\n", LOCK_EX) === false) { throw new RuntimeException('Unable to release child barrier.'); } } function mysqlConcurrencyWaitForEntered(array $child): int { $deadline = microtime(true) + 5.0; while (microtime(true) < $deadline) { if (is_file($child['entered'])) { $entered = file_get_contents($child['entered']); if (is_string($entered) && preg_match( '/^entered:([1-9][0-9]*)\n$/D', $entered, $matches ) === 1) { return (int) $matches[1]; } } if ((proc_get_status($child['process'])['running'] ?? false) !== true) { throw new RuntimeException( 'Concurrency child exited before entering the real service.' ); } usleep(10000); } throw new RuntimeException( 'Timed out waiting for the child service-entry acknowledgement.' ); } /** @return array */ function mysqlConcurrencyCollectChild(array $child): array { $stdout = ''; $stderr = ''; $deadline = microtime(true) + 15.0; while (true) { mysqlConcurrencyDrainPipes($child['pipes'], $stdout, $stderr); if ((proc_get_status($child['process'])['running'] ?? false) !== true) { break; } if (microtime(true) >= $deadline) { [$tailOut, $tailErr] = mysqlConcurrencyTerminateChild( $child['process'], $child['pipes'] ); $stdout .= $tailOut; $stderr .= $tailErr; @unlink($child['go']); throw new RuntimeException( 'Concurrency child exceeded 15 seconds: ' . trim($stderr) ); } usleep(10000); } $drainDeadline = microtime(true) + 0.25; do { mysqlConcurrencyDrainPipes($child['pipes'], $stdout, $stderr); if (feof($child['pipes'][1]) && feof($child['pipes'][2])) { break; } usleep(10000); } while (microtime(true) < $drainDeadline); foreach ($child['pipes'] as $pipe) { if (is_resource($pipe)) { fclose($pipe); } } proc_close($child['process']); @unlink($child['go']); @unlink($child['entered']); $lines = array_values(array_filter( preg_split('/\R/', trim($stdout)) ?: [], static fn (string $line): bool => trim($line) !== '' )); $result = $lines === [] ? null : json_decode(end($lines), true); if (!is_array($result)) { throw new RuntimeException( 'Concurrency child returned invalid JSON: ' . trim($stderr) ); } return $result; } function mysqlConcurrencyStopChild(array $child): void { if (isset($child['process']) && is_resource($child['process'])) { mysqlConcurrencyTerminateChild( $child['process'], $child['pipes'] ?? [] ); } if (isset($child['go']) && is_string($child['go'])) { @unlink($child['go']); } if (isset($child['entered']) && is_string($child['entered'])) { @unlink($child['entered']); } } function mysqlConcurrencyRowLocked(string $subject, int $id): bool { $select = match ($subject) { 'instance' => 'SELECT id FROM cloud_instances WHERE id = ?', 'actor' => 'SELECT id FROM `user` WHERE id = ?', default => throw new InvalidArgumentException( 'Unknown concurrency lock subject.' ), }; $probe = mysqlConcurrencyPdo(); $probe->exec('SET SESSION innodb_lock_wait_timeout = 1'); $probe->beginTransaction(); try { $statement = $probe->prepare($select . ' FOR UPDATE NOWAIT'); try { $statement->execute([$id]); } catch (PDOException $error) { if ((int) ($error->errorInfo[1] ?? 0) !== 1064) { throw $error; } $statement = $probe->prepare($select . ' FOR UPDATE'); $statement->execute([$id]); } $probe->rollBack(); return false; } catch (PDOException $error) { if ($probe->inTransaction()) { $probe->rollBack(); } if (in_array((int) ($error->errorInfo[1] ?? 0), [1205, 1213, 3572], true)) { return true; } throw $error; } } function mysqlConcurrencyInstanceLocked(int $instanceId): bool { return mysqlConcurrencyRowLocked('instance', $instanceId); } function mysqlConcurrencyActorLocked(int $actorId): bool { return mysqlConcurrencyRowLocked('actor', $actorId); } function mysqlConcurrencyWaitForInstanceLock( int $instanceId, array $child ): void { $deadline = microtime(true) + 6.0; while (microtime(true) < $deadline) { if (mysqlConcurrencyInstanceLocked($instanceId)) { return; } if ((proc_get_status($child['process'])['running'] ?? false) !== true) { throw new RuntimeException( 'Winner exited before acquiring the instance lock.' ); } usleep(20000); } throw new RuntimeException('Timed out waiting for the instance lock.'); } function mysqlConcurrencyWaitForActorLock(int $actorId, array $child): void { $deadline = microtime(true) + 6.0; while (microtime(true) < $deadline) { if (mysqlConcurrencyActorLocked($actorId)) { return; } if ((proc_get_status($child['process'])['running'] ?? false) !== true) { throw new RuntimeException( 'Loser exited before acquiring its actor lock.' ); } usleep(20000); } throw new RuntimeException('Timed out waiting for the loser actor lock.'); } /** @return array{0:array,1:array} */ function mysqlConcurrencyRace( PDO $sentinel, string $barrierDirectory, string $operationA, array $payloadA, string $operationB, array $payloadB ): array { $childA = null; $childB = null; try { $actorA = (int) ($payloadA['actor_id'] ?? 0); $actorB = (int) ($payloadB['actor_id'] ?? 0); if ($actorA < 1 || $actorB < 1) { throw new RuntimeException( 'Concurrency payloads require positive actor identities.' ); } $sameActor = $actorA === $actorB; $sentinel->beginTransaction(); if ($sameActor) { $actorLock = $sentinel->prepare( 'SELECT id FROM `user` WHERE id = ? FOR UPDATE' ); $actorLock->execute([$actorA]); if ($actorLock->fetchColumn() === false) { throw new RuntimeException( 'Same-actor concurrency sentinel does not exist.' ); } $childA = mysqlConcurrencySpawn( $operationA, $payloadA, $barrierDirectory, 'a-' . bin2hex(random_bytes(4)) ); $childB = mysqlConcurrencySpawn( $operationB, $payloadB, $barrierDirectory, 'b-' . bin2hex(random_bytes(4)) ); mysqlConcurrencyReleaseChild($childA); mysqlConcurrencyReleaseChild($childB); mysqlConcurrencyWaitForEntered($childA); mysqlConcurrencyWaitForEntered($childB); } else { $sentinel->query( 'SELECT id FROM cloud_hosts WHERE id = 1 FOR UPDATE' )->fetchColumn(); $childA = mysqlConcurrencySpawn( $operationA, $payloadA, $barrierDirectory, 'a-' . bin2hex(random_bytes(4)) ); mysqlConcurrencyReleaseChild($childA); mysqlConcurrencyWaitForInstanceLock(101, $childA); $childB = mysqlConcurrencySpawn( $operationB, $payloadB, $barrierDirectory, 'b-' . bin2hex(random_bytes(4)) ); mysqlConcurrencyReleaseChild($childB); mysqlConcurrencyWaitForActorLock($actorB, $childB); } $sentinel->commit(); $resultA = mysqlConcurrencyCollectChild($childA); $resultB = mysqlConcurrencyCollectChild($childB); return [$resultA, $resultB]; } finally { if ($sentinel->inTransaction()) { $sentinel->rollBack(); } if (is_array($childA)) { mysqlConcurrencyStopChild($childA); } if (is_array($childB)) { mysqlConcurrencyStopChild($childB); } } } function mysqlConcurrencyWaitForHostLock( PDO $observer, int $connectionId, array $child ): void { if ($connectionId < 1) { throw new RuntimeException('Child MySQL connection identity is invalid.'); } $deadline = microtime(true) + 2.0; while (microtime(true) < $deadline) { $processes = $observer->query('SHOW FULL PROCESSLIST') ->fetchAll(PDO::FETCH_ASSOC); foreach ($processes as $process) { $id = (int) ($process['Id'] ?? $process['id'] ?? 0); $info = (string) ($process['Info'] ?? $process['info'] ?? ''); if ($id === $connectionId && stripos($info, 'cloud_hosts') !== false && stripos($info, 'for update') !== false) { return; } } if ((proc_get_status($child['process'])['running'] ?? false) !== true) { throw new RuntimeException( 'Host contender exited before waiting on the host row.' ); } usleep(10000); } throw new RuntimeException( 'Timed out observing the child host-row lock waiter.' ); } /** * Race operations whose shared serialization boundary is the host row. * * Allocators do not own an instance or actor row, so the ordinary race helper * cannot prove that both contenders have entered the production lock chain. * This coordinator holds the canonical host sentinel, releases both children, * waits for transition contenders to own their distinct instance rows, and * verifies every child is still blocked before releasing the host. * * @return array{0:array,1:array} */ function mysqlConcurrencyHostBoundaryRace( PDO $sentinel, string $barrierDirectory, string $operationA, array $payloadA, string $operationB, array $payloadB ): array { $childA = null; $childB = null; try { $sentinel->beginTransaction(); $hostId = $sentinel->query( 'SELECT id FROM cloud_hosts WHERE id = 1 FOR UPDATE' )->fetchColumn(); if ((int) $hostId !== 1) { throw new RuntimeException( 'Host-boundary concurrency sentinel does not exist.' ); } $childA = mysqlConcurrencySpawn( $operationA, $payloadA, $barrierDirectory, 'host-a-' . bin2hex(random_bytes(4)) ); $childB = mysqlConcurrencySpawn( $operationB, $payloadB, $barrierDirectory, 'host-b-' . bin2hex(random_bytes(4)) ); mysqlConcurrencyReleaseChild($childA); mysqlConcurrencyReleaseChild($childB); $connectionA = mysqlConcurrencyWaitForEntered($childA); $connectionB = mysqlConcurrencyWaitForEntered($childB); foreach ([ [$payloadA, $childA], [$payloadB, $childB], ] as [$payload, $child]) { $instanceId = (int) ($payload['instance_id'] ?? 0); if ($instanceId > 0) { mysqlConcurrencyWaitForInstanceLock($instanceId, $child); } } mysqlConcurrencyWaitForHostLock( $sentinel, $connectionA, $childA ); mysqlConcurrencyWaitForHostLock( $sentinel, $connectionB, $childB ); $sentinel->commit(); return [ mysqlConcurrencyCollectChild($childA), mysqlConcurrencyCollectChild($childB), ]; } finally { if ($sentinel->inTransaction()) { $sentinel->rollBack(); } if (is_array($childA)) { mysqlConcurrencyStopChild($childA); } if (is_array($childB)) { mysqlConcurrencyStopChild($childB); } } } function mysqlConcurrencyAssertTransitionResponseOperation( array $response, string $context ): string { $result = $response['result'] ?? null; $transitionId = is_array($result) ? (int) ($result['transition_id'] ?? 0) : 0; $jobId = is_array($result) ? (int) ($result['job_id'] ?? 0) : 0; $operationId = is_array($result) ? (string) ($result['operation_id'] ?? '') : ''; mysqlConcurrencyAssert( ($response['ok'] ?? false) === true && $transitionId > 0 && $jobId > 0 && preg_match('/^[a-f0-9]{32}$/D', $operationId) === 1, $context . ': successful response operation identity is invalid.' ); $stored = Capsule::table( 'cloud_public_network_transitions as transition' )->join( 'cloud_jobs as job', 'job.public_network_transition_id', '=', 'transition.id' )->where('transition.id', $transitionId) ->where('job.id', $jobId)->first([ 'transition.operation_id as transition_operation_id', 'job.operation_id as job_operation_id', 'job.kind as job_kind', 'job.execution_mode as job_execution_mode', ]); mysqlConcurrencyAssert( $stored !== null && (string) $stored->job_kind === 'network_change' && (string) $stored->job_execution_mode === 'agent' && hash_equals( $operationId, (string) $stored->transition_operation_id ) && hash_equals($operationId, (string) $stored->job_operation_id), $context . ': response, transition, and Agent job operations differ.' ); $targets = Capsule::table( 'cloud_public_network_transition_ips as leg' )->join( 'cloud_resource_reservation_ips as target_claim', 'target_claim.id', '=', 'leg.target_claim_id' )->join( 'cloud_public_ips as target_ip', 'target_ip.id', '=', 'target_claim.public_ip_id' )->where('leg.transition_id', $transitionId) ->where('leg.changed', 1)->get([ 'target_claim.operation_id as claim_operation_id', 'target_ip.operation_id as ip_operation_id', ]); mysqlConcurrencyAssert( $targets->isNotEmpty(), $context . ': successful transition has no changed target.' ); foreach ($targets as $target) { mysqlConcurrencyAssert( hash_equals( $operationId, (string) $target->claim_operation_id ) && hash_equals( $operationId, (string) $target->ip_operation_id ), $context . ': response and changed target operations differ.' ); } return $operationId; } function mysqlConcurrencyAssertBatchResponseOperation( array $response, string $context ): string { $result = $response['result'] ?? null; $job = is_array($result) && is_array($result['job'] ?? null) ? $result['job'] : null; $batch = is_array($result) && is_array($result['batch'] ?? null) ? $result['batch'] : null; $jobId = is_array($job) ? (int) ($job['id'] ?? 0) : 0; $operationId = is_array($job) ? (string) ($job['operation_id'] ?? '') : ''; $batchKey = is_array($batch) ? (string) ($batch['batch_key'] ?? '') : ''; mysqlConcurrencyAssert( ($response['ok'] ?? false) === true && $jobId > 0 && preg_match('/^[a-f0-9]{32}$/D', $operationId) === 1 && preg_match('/^[a-f0-9]{32}$/D', $batchKey) === 1, $context . ': successful batch response operation is invalid.' ); $stored = Capsule::table('cloud_jobs as job')->join( 'cloud_public_ip_pool_replacement_batches as batch', 'batch.parent_job_id', '=', 'job.id' )->where('job.id', $jobId)->where('batch.batch_key', $batchKey) ->first([ 'job.operation_id', 'job.kind', 'job.execution_mode', 'batch.parent_job_id', ]); mysqlConcurrencyAssert( $stored !== null && (string) $stored->kind === 'public_ip_pool_replacement_batch' && (string) $stored->execution_mode === 'local' && (int) $stored->parent_job_id === $jobId && hash_equals($operationId, (string) $stored->operation_id), $context . ': response and durable batch-parent operations differ.' ); return $operationId; } function mysqlConcurrencyAssertTransitionClaimSetDigests(): void { $transitions = Capsule::table('cloud_public_network_transitions') ->orderBy('id')->get(); foreach ($transitions as $transition) { $sourceSnapshot = CloudPublicNetworkSnapshot::fromArray( json_decode( (string) $transition->source_public_network_json, true, 64, JSON_THROW_ON_ERROR ) ); $targetSnapshot = CloudPublicNetworkSnapshot::fromArray( json_decode( (string) $transition->target_public_network_json, true, 64, JSON_THROW_ON_ERROR ) ); mysqlConcurrencyAssert( hash_equals( $sourceSnapshot->fingerprint(), (string) $transition->source_public_network_sha256 ) && hash_equals( $targetSnapshot->fingerprint(), (string) $transition->target_public_network_sha256 ), 'Transition public-network snapshot digest is invalid.' ); $legs = Capsule::table('cloud_public_network_transition_ips') ->where('transition_id', (int) $transition->id) ->orderBy('address_family')->get()->keyBy('address_family'); mysqlConcurrencyAssert( $legs->keys()->map( static fn ($family): int => (int) $family )->all() === [4, 6], 'Transition claim-set digest has an incomplete family shape.' ); $claimIds = []; foreach ([4, 6] as $family) { $leg = $legs->get($family); $claimIds[] = (int) $leg->source_claim_id; if ($leg->target_claim_id !== null) { $claimIds[] = (int) $leg->target_claim_id; } } $claimIds = array_values(array_unique($claimIds)); $claims = Capsule::table('cloud_resource_reservation_ips') ->whereIn('id', $claimIds)->get()->keyBy('id'); mysqlConcurrencyAssert( $claims->count() === count($claimIds), 'Transition claim-set digest references a missing claim.' ); $ipIds = $claims->map( static fn ($claim): int => (int) $claim->public_ip_id )->unique()->values()->all(); $ips = Capsule::table('cloud_public_ips')->whereIn('id', $ipIds) ->get()->keyBy('id'); mysqlConcurrencyAssert( $ips->count() === count($ipIds), 'Transition claim-set digest references a missing address.' ); $sourceClaims = []; $sourceAddresses = []; $targetClaims = []; $targetAddresses = []; foreach ([4, 6] as $family) { $leg = $legs->get($family); $sourceClaim = $claims->get((int) $leg->source_claim_id); mysqlConcurrencyAssert( is_object($sourceClaim), 'Transition source claim-set field shape is invalid.' ); $sourceIp = $ips->get((int) $sourceClaim->public_ip_id); mysqlConcurrencyAssert( is_object($sourceIp), 'Transition source address field shape is invalid.' ); $sourceClaims[$family] = $sourceClaim; $sourceAddresses[(int) $sourceIp->id] = $sourceIp; $targetClaim = (bool) $leg->changed ? $claims->get((int) $leg->target_claim_id) : $sourceClaim; mysqlConcurrencyAssert( is_object($targetClaim), 'Transition target claim-set field shape is invalid.' ); $targetIp = $ips->get((int) $targetClaim->public_ip_id); mysqlConcurrencyAssert( is_object($targetIp), 'Transition target address field shape is invalid.' ); $targetClaims[$family] = $targetClaim; $targetAddresses[(int) $targetIp->id] = $targetIp; } $sourceIdentity = CloudPublicNetworkClaimSetIdentity::fromLedger( (int) $transition->reservation_id, $sourceClaims, $sourceAddresses, $sourceSnapshot ); $targetIdentity = CloudPublicNetworkClaimSetIdentity::fromLedger( (int) $transition->reservation_id, $targetClaims, $targetAddresses, $targetSnapshot ); mysqlConcurrencyAssert( hash_equals( $sourceIdentity->digest(), (string) $transition->source_claim_set_sha256 ), 'Transition source claim-set digest does not match its ledger.' ); mysqlConcurrencyAssert( hash_equals( $targetIdentity->digest(), (string) $transition->target_claim_set_sha256 ), 'Transition target claim-set digest does not match its ledger.' ); } } function mysqlConcurrencyAssertHeldAllocationShape(): void { foreach ([2001, 2002] as $reservationId) { $operationId = str_pad( dechex($reservationId), 32, '0', STR_PAD_LEFT ); $reservation = Capsule::table('cloud_resource_reservations') ->where('id', $reservationId)->first(); mysqlConcurrencyAssert( $reservation !== null && (string) $reservation->state === 'held' && $reservation->instance_id === null && (int) $reservation->host_id === 1 && (int) $reservation->resource_pool_id === 1 && hash_equals( $operationId, (string) $reservation->operation_id ), 'Held allocator reservation identity drifted.' ); $claims = Capsule::table('cloud_resource_reservation_ips as claim') ->leftJoin( 'cloud_public_ips as ip', 'ip.id', '=', 'claim.public_ip_id' )->where('claim.reservation_id', $reservationId) ->orderBy('claim.address_family')->get([ 'claim.id', 'claim.address_family', 'claim.claim_generation', 'claim.active_claim', 'claim.network_attachment_id', 'claim.public_ip_pool_id', 'claim.public_ip_id', 'claim.operation_id as claim_operation_id', 'claim.state as claim_state', 'ip.id as ip_id', 'ip.public_ip_pool_id as ip_pool_id', 'ip.address_family as ip_family', 'ip.state as ip_state', 'ip.reservation_id as ip_reservation_id', 'ip.reservation_ip_claim_id as ip_claim_id', 'ip.instance_id as ip_instance_id', 'ip.operation_id as ip_operation_id', ])->all(); mysqlConcurrencyAssert( count($claims) === 0 || count($claims) === 2, 'Held allocator exposed a partial dual-stack claim set.' ); if ($claims === []) { continue; } mysqlConcurrencyAssert( array_map( static fn (object $claim): int => (int) $claim->address_family, $claims ) === [4, 6], 'Held allocator claim set is not exactly dual-stack.' ); foreach ($claims as $claim) { $family = (int) $claim->address_family; $expectedPoolId = $family === 4 ? 11 : 12; mysqlConcurrencyAssert( (int) $claim->claim_generation === 1 && (int) $claim->active_claim === 1 && (int) $claim->network_attachment_id === 1 && (int) $claim->public_ip_pool_id === $expectedPoolId && (int) $claim->public_ip_id === (int) $claim->ip_id && (int) $claim->ip_id > 0 && (string) $claim->claim_state === 'reserved' && (int) $claim->ip_pool_id === $expectedPoolId && (int) $claim->ip_family === $family && (string) $claim->ip_state === 'reserved' && (int) $claim->ip_reservation_id === $reservationId && (int) $claim->ip_claim_id === (int) $claim->id && $claim->ip_instance_id === null && hash_equals( $operationId, (string) $claim->claim_operation_id ) && hash_equals( $operationId, (string) $claim->ip_operation_id ), 'Held allocator claim and reserved IP ownership diverged.' ); } } $duplicateAddress = Capsule::table('cloud_public_ips') ->selectRaw('address_family, address_binary, COUNT(*) AS aggregate') ->groupBy('address_family', 'address_binary') ->havingRaw('COUNT(*) > 1')->exists(); mysqlConcurrencyAssert( !$duplicateAddress, 'Global public-address identity was duplicated.' ); } function mysqlConcurrencyAssertLedger(): void { $rows = Capsule::table('cloud_resource_reservation_ips as claim') ->join( 'cloud_resource_reservations as reservation', 'reservation.id', '=', 'claim.reservation_id' )->join( 'cloud_public_ips as ip', 'ip.id', '=', 'claim.public_ip_id' )->where('claim.active_claim', 1) ->where('claim.state', 'committed') ->orderBy('claim.reservation_id') ->orderBy('claim.address_family')->get([ 'claim.id', 'claim.reservation_id', 'claim.address_family', 'claim.public_ip_pool_id', 'claim.public_ip_id', 'claim.operation_id as claim_operation_id', 'reservation.instance_id as owner_instance_id', 'reservation.operation_id as reservation_operation_id', 'ip.id as ip_id', 'ip.public_ip_pool_id as ip_pool_id', 'ip.address_family as ip_family', 'ip.state as ip_state', 'ip.reservation_id as ip_reservation_id', 'ip.reservation_ip_claim_id as ip_claim_id', 'ip.instance_id as ip_instance_id', 'ip.operation_id as ip_operation_id', ]); $families = []; $seedSourceOperations = [ 1001 => '000000000000000000000000000003e9', 1002 => '000000000000000000000000000003ea', ]; foreach ($rows as $row) { $families[(int) $row->reservation_id][] = (int) $row->address_family; mysqlConcurrencyAssert( (int) $row->public_ip_id === (int) $row->ip_id && (int) $row->public_ip_pool_id === (int) $row->ip_pool_id && (int) $row->address_family === (int) $row->ip_family && (string) $row->ip_state === 'assigned' && (int) $row->reservation_id === (int) $row->ip_reservation_id && (int) $row->id === (int) $row->ip_claim_id && (int) $row->owner_instance_id === (int) $row->ip_instance_id && hash_equals( (string) $row->claim_operation_id, (string) $row->ip_operation_id ) && isset($seedSourceOperations[(int) $row->reservation_id]) && hash_equals( $seedSourceOperations[(int) $row->reservation_id], (string) $row->claim_operation_id ) && hash_equals( (string) $row->reservation_operation_id, (string) $row->claim_operation_id ), 'Seed source claim or IP operation identity changed.' ); } foreach ([1001, 1002] as $reservationId) { mysqlConcurrencyAssert( ($families[$reservationId] ?? []) === [4, 6], 'Reservation lost its active dual-stack claim pair.' ); } mysqlConcurrencyAssertHeldAllocationShape(); $prepared = Capsule::table('cloud_resource_reservation_ips as claim') ->join( 'cloud_public_ips as ip', 'ip.id', '=', 'claim.public_ip_id' )->whereNull('claim.active_claim')->where('claim.state', 'reserved') ->get([ 'claim.id', 'claim.reservation_id', 'claim.address_family', 'claim.public_ip_pool_id', 'ip.public_ip_pool_id as ip_pool_id', 'claim.operation_id as claim_operation_id', 'ip.address_family as ip_family', 'ip.state as ip_state', 'ip.reservation_id as ip_reservation_id', 'ip.reservation_ip_claim_id as ip_claim_id', 'ip.instance_id as ip_instance_id', 'ip.operation_id as ip_operation_id', ]); foreach ($prepared as $row) { mysqlConcurrencyAssert( (int) $row->public_ip_pool_id === (int) $row->ip_pool_id && (int) $row->address_family === (int) $row->ip_family && (string) $row->ip_state === 'reserved' && (int) $row->reservation_id === (int) $row->ip_reservation_id && (int) $row->id === (int) $row->ip_claim_id && $row->ip_instance_id === null && hash_equals( (string) $row->claim_operation_id, (string) $row->ip_operation_id ), 'A prepared claim no longer owns its reserved target IP.' ); } $orphanBoundIp = Capsule::table('cloud_public_ips as ip')->leftJoin( 'cloud_resource_reservation_ips as claim', 'claim.id', '=', 'ip.reservation_ip_claim_id' )->whereNotNull('ip.reservation_ip_claim_id')->whereNull('claim.id') ->exists(); $dirtyAvailableIp = Capsule::table('cloud_public_ips') ->where('state', 'available')->where(static function ($query): void { $query->whereNotNull('reservation_id') ->orWhereNotNull('reservation_ip_claim_id') ->orWhereNotNull('instance_id') ->orWhereNotNull('operation_id'); })->exists(); mysqlConcurrencyAssert(!$orphanBoundIp && !$dirtyAvailableIp, 'IP ownership columns no longer agree with claim state.'); $duplicate = Capsule::table('cloud_public_network_transitions') ->where('active_transition', 1) ->selectRaw('instance_id, COUNT(*) AS aggregate') ->groupBy('instance_id')->havingRaw('COUNT(*) > 1')->exists(); mysqlConcurrencyAssert(!$duplicate, 'More than one active transition exists for one instance.'); $orphanJob = Capsule::table('cloud_jobs as job')->leftJoin( 'cloud_public_network_transitions as transition', 'transition.id', '=', 'job.public_network_transition_id' )->where('job.kind', 'network_change')->whereNull('transition.id') ->exists(); $duplicateJob = Capsule::table('cloud_jobs') ->where('kind', 'network_change') ->selectRaw('public_network_transition_id, COUNT(*) AS aggregate') ->groupBy('public_network_transition_id') ->havingRaw('COUNT(*) > 1')->exists(); $transitionCount = Capsule::table('cloud_public_network_transitions') ->count(); $agentJobCount = Capsule::table('cloud_jobs') ->where('kind', 'network_change') ->where('execution_mode', 'agent')->count(); mysqlConcurrencyAssert( !$orphanJob && !$duplicateJob && $agentJobCount === $transitionCount, 'A transition lost or duplicated its Agent dispatch job.' ); mysqlConcurrencyAssertTransitionClaimSetDigests(); $networkJobs = Capsule::table('cloud_jobs as job')->join( 'cloud_public_network_transitions as transition', 'transition.id', '=', 'job.public_network_transition_id' )->where('job.kind', 'network_change')->get([ 'job.operation_id as job_operation_id', 'job.public_network_transition_id as job_transition_id', 'job.instance_id as job_instance_id', 'job.host_id as job_host_id', 'job.user_id as job_user_id', 'transition.id as transition_id', 'transition.operation_id as transition_operation_id', 'transition.instance_id as transition_instance_id', 'transition.host_id as transition_host_id', 'transition.requested_by_user_id as transition_user_id', ]); foreach ($networkJobs as $row) { mysqlConcurrencyAssert( hash_equals( (string) $row->transition_operation_id, (string) $row->job_operation_id ) && (int) $row->job_transition_id === (int) $row->transition_id && (int) $row->job_instance_id === (int) $row->transition_instance_id && (int) $row->job_host_id === (int) $row->transition_host_id && (int) $row->job_user_id === (int) $row->transition_user_id, 'Agent job operation identity diverged from its transition.' ); } $legs = Capsule::table('cloud_public_network_transition_ips') ->orderBy('transition_id')->orderBy('address_family')->get(); $legsByTransition = []; foreach ($legs as $leg) { $legsByTransition[(int) $leg->transition_id][] = [ (int) $leg->address_family, (int) $leg->changed, $leg->target_claim_id === null ? null : (int) $leg->target_claim_id, ]; } foreach (Capsule::table('cloud_public_network_transitions')->pluck('id') as $transitionId) { $transitionLegs = $legsByTransition[(int) $transitionId] ?? []; mysqlConcurrencyAssert( count($transitionLegs) === 2 && array_column($transitionLegs, 0) === [4, 6] && count(array_filter( $transitionLegs, static fn (array $leg): bool => $leg[1] === 1 && $leg[2] !== null )) === 1, 'Transition legs are not one atomic dual-stack generation.' ); } $changedTargets = Capsule::table( 'cloud_public_network_transition_ips as leg' )->join( 'cloud_public_network_transitions as transition', 'transition.id', '=', 'leg.transition_id' )->join( 'cloud_resource_reservation_ips as target_claim', 'target_claim.id', '=', 'leg.target_claim_id' )->join( 'cloud_public_ips as target_ip', 'target_ip.id', '=', 'target_claim.public_ip_id' )->join( 'cloud_resource_reservations as reservation', 'reservation.id', '=', 'transition.reservation_id' )->join( 'cloud_instances as instance', 'instance.id', '=', 'transition.instance_id' )->where('leg.changed', 1)->get([ 'leg.address_family as leg_family', 'leg.target_claim_id as leg_target_claim_id', 'transition.operation_id as transition_operation_id', 'transition.instance_id as transition_instance_id', 'transition.host_id as transition_host_id', 'transition.reservation_id as transition_reservation_id', 'transition.target_network_attachment_id ' . 'as transition_attachment_id', 'target_claim.id as target_claim_id', 'target_claim.reservation_id as target_claim_reservation_id', 'target_claim.address_family as target_claim_family', 'target_claim.active_claim as target_claim_active', 'target_claim.network_attachment_id as target_claim_attachment_id', 'target_claim.public_ip_pool_id as target_claim_pool_id', 'target_claim.public_ip_id as target_claim_ip_id', 'target_claim.operation_id as target_claim_operation_id', 'target_claim.state as target_claim_state', 'target_ip.id as target_ip_id', 'target_ip.public_ip_pool_id as target_ip_pool_id', 'target_ip.address_family as target_ip_family', 'target_ip.state as target_ip_state', 'target_ip.reservation_id as target_ip_reservation_id', 'target_ip.reservation_ip_claim_id as target_ip_claim_id', 'target_ip.instance_id as target_ip_instance_id', 'target_ip.operation_id as target_ip_operation_id', 'reservation.id as reservation_id', 'reservation.host_id as reservation_host_id', 'reservation.instance_id as reservation_instance_id', 'instance.id as instance_id', 'instance.host_id as instance_host_id', 'instance.reservation_id as instance_reservation_id', ]); foreach ($changedTargets as $row) { mysqlConcurrencyAssert( preg_match( '/^[a-f0-9]{32}$/D', (string) $row->transition_operation_id ) === 1 && hash_equals( (string) $row->transition_operation_id, (string) $row->target_claim_operation_id ) && hash_equals( (string) $row->transition_operation_id, (string) $row->target_ip_operation_id ) && (int) $row->leg_target_claim_id === (int) $row->target_claim_id && (int) $row->target_claim_reservation_id === (int) $row->transition_reservation_id && (int) $row->target_claim_family === (int) $row->leg_family && $row->target_claim_active === null && (string) $row->target_claim_state === 'reserved' && (int) $row->target_claim_attachment_id === (int) $row->transition_attachment_id && (int) $row->target_claim_ip_id === (int) $row->target_ip_id && (int) $row->target_claim_pool_id === (int) $row->target_ip_pool_id && (int) $row->target_claim_family === (int) $row->target_ip_family && (string) $row->target_ip_state === 'reserved' && (int) $row->target_ip_reservation_id === (int) $row->transition_reservation_id && (int) $row->target_ip_claim_id === (int) $row->target_claim_id && $row->target_ip_instance_id === null && (int) $row->reservation_id === (int) $row->transition_reservation_id && (int) $row->reservation_instance_id === (int) $row->transition_instance_id && (int) $row->reservation_host_id === (int) $row->transition_host_id && (int) $row->instance_id === (int) $row->transition_instance_id && (int) $row->instance_host_id === (int) $row->transition_host_id && (int) $row->instance_reservation_id === (int) $row->transition_reservation_id, 'Changed target operation or ownership identity diverged.' ); } mysqlConcurrencyAssert( $prepared->count() === $transitionCount && $changedTargets->count() === $transitionCount, 'Prepared target claims do not match transition generations.' ); $paidTransitions = Capsule::table('cloud_public_network_transitions') ->where('kind', 'user_paid')->count(); $invoiceCount = Capsule::table('cloud_invoices') ->where('purpose', 'ip_change')->where('status', 'paid') ->where('paid_via', 'balance')->count(); $unlinkedInvoice = Capsule::table('cloud_invoices as invoice')->leftJoin( 'cloud_public_network_transitions as transition', 'transition.invoice_id', '=', 'invoice.id' )->where('invoice.purpose', 'ip_change')->whereNull('transition.id') ->exists(); $expectedBalance = number_format( 20.00 - (5.00 * $paidTransitions), 2, '.', '' ); mysqlConcurrencyAssert( $invoiceCount === $paidTransitions && !$unlinkedInvoice && (string) Capsule::table('user')->where('id', 10) ->value('money') === $expectedBalance, 'Paid transition and wallet accounting diverged.' ); $attachment = Capsule::table('cloud_host_network_attachments')->first(); $poolProjection = Capsule::table('cloud_public_ip_pools') ->orderBy('id')->get([ 'id', 'host_id', 'network_attachment_id', 'address_family', 'desired_revision', 'observed_revision', 'sync_state', ])->map(static fn ($row): array => [ 'id' => (int) $row->id, 'host_id' => (int) $row->host_id, 'network_attachment_id' => (int) $row->network_attachment_id, 'address_family' => (int) $row->address_family, 'desired_revision' => (int) $row->desired_revision, 'observed_revision' => (int) $row->observed_revision, 'sync_state' => (string) $row->sync_state, ])->all(); mysqlConcurrencyAssert( Capsule::table('cloud_hosts')->count() === 1 && Capsule::table('cloud_resource_pools')->count() === 1 && Capsule::table('cloud_host_network_attachments')->count() === 1 && $attachment !== null && (int) $attachment->host_id === 1 && (int) $attachment->line_id === 7 && (string) $attachment->network_selector === 'bridge:br0' && (string) $attachment->verification_state === 'verified' && $poolProjection === [ ['id' => 11, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 4, 'desired_revision' => 11, 'observed_revision' => 11, 'sync_state' => 'applied'], ['id' => 12, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 6, 'desired_revision' => 12, 'observed_revision' => 12, 'sync_state' => 'applied'], ['id' => 13, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 4, 'desired_revision' => 13, 'observed_revision' => 13, 'sync_state' => 'applied'], ['id' => 15, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 4, 'desired_revision' => 15, 'observed_revision' => 15, 'sync_state' => 'applied'], ], 'Frozen host, line, attachment, or pool topology drifted.' ); }