getConnection() ->transaction(function () use ($hostId): array { $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } $rows = CloudHostPoolMembershipService::lockedRows($host, time()); $selected = CloudHostPoolMembershipService::selected($host, $rows); $poolId = $host->resource_pool_id; $state = (string) $host->scheduler_state; CloudHostPoolMembershipService::mirrorLocked($host, $selected); if ($poolId !== $host->resource_pool_id || $state !== (string) $host->scheduler_state) { $host->updated_at = time(); $host->save(); } return [$host, $selected, $rows]; }, 3); $policy = CloudHostCapacityPolicy::find($hostId); $pools = self::poolOptions(); $selected = null; foreach ($pools as $pool) { if ((int) $pool['id'] === (int) ($membership?->resource_pool_id ?? 0)) { $selected = $pool; break; } } $usage = self::hostUsage($hostId, (int) ($selected['line_id'] ?? 0)); $capacity = self::capacityDto($policy, $usage); return [ 'host' => [ 'id' => (int) $host->id, 'name' => self::safeText((string) $host->name, 80), 'enabled' => (bool) $host->enabled, 'resource_pool_id' => $membership === null ? null : (int) $membership->resource_pool_id, 'scheduler_state' => in_array((string) ($membership?->scheduler_state ?? 'disabled'), self::SCHEDULER_STATES, true) ? (string) ($membership?->scheduler_state ?? 'disabled') : 'disabled', ], 'policy' => self::policyDto($policy), 'capacity' => $capacity, 'placement' => $selected, 'memberships' => $memberships->map(static fn ($row): array => [ 'id' => (int) $row->id, 'resource_pool_id' => (int) $row->resource_pool_id, 'scheduler_state' => (string) $row->scheduler_state, 'priority' => (int) $row->priority, 'weight' => (int) $row->weight, ])->values()->all(), 'pools' => $pools, 'network_lines_enabled' => CloudNetworkLineFeature::enabled(), 'warnings' => self::warnings($host, $selected, $capacity), 'generated_at' => time(), ]; } public static function save(int $hostId, array $body): array { $poolId = self::nullablePositiveId($body['resource_pool_id'] ?? null); $schedulerState = $body['scheduler_state'] ?? null; if (!is_string($schedulerState) || !in_array($schedulerState, self::SCHEDULER_STATES, true)) { throw new CloudError('调度状态无效'); } $values = [ 'configured_cpu_cores' => self::integer($body, 'configured_cpu_cores', 0, 65535, '配置 CPU'), 'configured_memory_mib' => self::integer($body, 'configured_memory_mib', 0, 2147483647, '配置内存'), 'configured_disk_gib' => self::integer($body, 'configured_disk_gib', 0, 2147483647, '配置磁盘'), 'safety_cpu_cores' => self::integer($body, 'safety_cpu_cores', 0, 65535, 'CPU 安全预留'), 'safety_memory_mib' => self::integer($body, 'safety_memory_mib', 0, 2147483647, '内存安全预留'), 'safety_disk_gib' => self::integer($body, 'safety_disk_gib', 0, 2147483647, '磁盘安全预留'), 'max_instances' => self::integer($body, 'max_instances', 0, 2147483647, '最大实例数'), ]; (new CloudHost())->getConnection()->transaction( function () use ($hostId, $poolId, $schedulerState, $values): void { // All capacity writers use host -> policy order. Keep this // order aligned with placement and reservation reconciliation. $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } // Membership is the scheduling authority. Lock it directly // after the host sentinel and before host-global capacity. CloudHostPoolMembershipService::lockedRows($host, time()); $policy = CloudHostCapacityPolicy::where('host_id', $hostId) ->lockForUpdate()->first(); if ($policy === null) { $policy = new CloudHostCapacityPolicy(); $policy->host_id = $hostId; $policy->created_at = time(); } $pool = $poolId === null ? null : CloudResourcePool::find($poolId); $line = $pool === null ? null : CloudNetworkLine::find((int) $pool->line_id); $region = $line === null ? null : CloudRegion::find((int) $line->region_id); if ($poolId !== null && $pool === null) { throw new CloudError('所选资源池不存在', 409); } if ($schedulerState === 'active') { $usage = self::hostUsage($hostId); if ((int) ($usage['unknown'] ?? 0) > 0) { throw new CloudError( '宿主机存在缺少 CPU、内存或磁盘规格的旧实例,补齐规格后才能启用调度', 409 ); } self::validateActive($host, $pool, $line, $region, $values); } self::assertReservationLine($host, $pool, $line); self::assertNetworkLine($host, $line); $now = time(); CloudHostPoolMembershipService::replaceLocked( $host, $poolId, $schedulerState, $now ); $host->updated_at = $now; $host->save(); foreach ($values as $field => $value) { $policy->{$field} = $value; } $policy->updated_at = $now; $policy->save(); }, 3 ); return self::read($hostId); } private static function validateActive(CloudHost $host, $pool, $line, $region, array $values): void { if (!(bool) $host->enabled) { throw new CloudError('宿主机连接未启用,不能启用自动调度', 409); } if ($pool === null || (string) $pool->status !== 'active') { throw new CloudError('启用调度前必须选择启用的资源池', 409); } if ($line === null || (string) $line->status !== 'active' || $region === null || (string) $region->status !== 'active') { throw new CloudError('资源池所属地区或线路未启用', 409); } if (!CloudNetworkLineFeature::enabled() && !(bool) $line->is_default) { throw new CloudError('线路功能关闭时只能使用地区默认线路', 409); } foreach (['cpu_cores' => 'CPU', 'memory_mib' => '内存', 'disk_gib' => '磁盘'] as $suffix => $label) { $configured = (int) $values['configured_' . $suffix]; $safety = (int) $values['safety_' . $suffix]; if ($configured <= 0) { throw new CloudError('启用调度前' . $label . '配置容量必须大于 0', 409); } if ($safety >= $configured) { throw new CloudError($label . '安全预留必须小于配置容量', 409); } } } /** Keep existing instances on the line captured by their sales pool. */ private static function assertReservationLine(CloudHost $host, $pool, $line): void { $reservations = CloudResourceReservation::where('host_id', (int) $host->id) ->whereIn('state', ['held', 'uncertain', 'committed']) ->orderBy('id')->lockForUpdate()->get(['id', 'resource_pool_id']); if ($reservations->isEmpty()) { return; } if ($pool === null || $line === null) { throw new CloudError('宿主机仍有实例或资源预留,不能移除所属资源池', 409); } $poolIds = $reservations->pluck('resource_pool_id')->map( static fn ($id): int => (int) $id )->unique()->values()->all(); $reservationPools = CloudResourcePool::whereIn('id', $poolIds) ->orderBy('id')->get(['id', 'line_id'])->keyBy('id'); if ($reservationPools->count() !== count($poolIds)) { throw new CloudError('宿主机现有资源预留的线路关系不完整', 409); } foreach ($poolIds as $poolId) { if ((int) $reservationPools->get($poolId)->line_id !== (int) $line->id) { throw new CloudError('宿主机仍有其他线路的实例或资源预留,不能跨线路更换资源池', 409); } } } /** Do not relabel a configured network as another commercial line. */ private static function assertNetworkLine(CloudHost $host, $line): void { $attachments = CloudHostNetworkAttachment::where( 'host_id', (int) $host->id )->orderBy('id')->lockForUpdate()->get(['id', 'line_id']); $publicPools = CloudPublicIpPool::where('host_id', (int) $host->id) ->orderBy('id')->lockForUpdate()->get(['id', 'network_attachment_id']); if ($attachments->isEmpty() && $publicPools->isEmpty()) { return; } if ($attachments->isEmpty() || $line === null) { throw new CloudError( '宿主机已有公网网络接入,不能移除所属线路', 409 ); } $attachmentIds = $attachments->pluck('id')->map( static fn ($id): int => (int) $id )->all(); foreach ($publicPools as $publicPool) { if (!in_array((int) $publicPool->network_attachment_id, $attachmentIds, true)) { throw new CloudError('宿主机公网地址池的线路关系不完整', 409); } } foreach ($attachments as $attachment) { if ((int) $attachment->line_id !== (int) $line->id) { throw new CloudError( '宿主机已有其他线路的公网网络接入,不能静默跨线路更换资源池', 409 ); } } } private static function poolOptions(): array { $lines = []; foreach (CloudNetworkLine::orderBy('sort')->orderBy('id')->get() as $line) { $lines[(int) $line->id] = $line; } $regions = []; foreach (CloudRegion::orderBy('sort')->orderBy('id')->get() as $region) { $regions[(int) $region->id] = $region; } $result = []; foreach (CloudResourcePool::orderBy('sort')->orderBy('id')->get() as $pool) { $line = $lines[(int) $pool->line_id] ?? null; $region = $line === null ? null : ($regions[(int) $line->region_id] ?? null); $result[] = [ 'id' => (int) $pool->id, 'name' => self::safeText((string) $pool->name, 80), 'status' => self::safeText((string) $pool->status, 16), 'scheduling_policy' => self::safeText((string) $pool->scheduling_policy, 32), 'line_id' => $line === null ? null : (int) $line->id, 'line_name' => $line === null ? '线路已缺失' : self::safeText((string) $line->name, 80), 'line_status' => $line === null ? 'missing' : self::safeText((string) $line->status, 16), 'line_is_default' => $line !== null && (bool) $line->is_default, 'region_id' => $region === null ? null : (int) $region->id, 'region_name' => $region === null ? '地区已缺失' : self::safeText((string) $region->name, 80), 'region_status' => $region === null ? 'missing' : self::safeText((string) $region->status, 16), ]; } return $result; } private static function policyDto($policy): array { $fields = ['configured_cpu_cores', 'configured_memory_mib', 'configured_disk_gib', 'safety_cpu_cores', 'safety_memory_mib', 'safety_disk_gib', 'max_instances']; $result = []; foreach ($fields as $field) { $result[$field] = $policy === null ? 0 : max(0, (int) $policy->{$field}); } return $result; } private static function hostUsage(int $hostId, ?int $lineId = null): array { $usage = CloudPanelUsageService::forHost($hostId); $usage['ip'] = []; $lineId = $lineId !== null && $lineId > 0 ? $lineId : null; $host = $lineId === null ? null : CloudHost::find($hostId); $ipQuery = CloudPublicIp::where('cloud_public_ips.host_id', $hostId); if ($lineId !== null && $host !== null) { $ipQuery->join( 'cloud_public_ip_pools as public_pool', 'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id' )->join( 'cloud_host_network_attachments as network_attachment', 'network_attachment.id', '=', 'public_pool.network_attachment_id' )->where('network_attachment.line_id', $lineId) ->whereColumn('public_pool.host_id', 'cloud_public_ips.host_id') ->whereColumn('network_attachment.host_id', 'cloud_public_ips.host_id') ->where('network_attachment.network_selector', (string) $host->default_network) ->select([ 'cloud_public_ips.state', 'cloud_public_ips.reservation_id', 'cloud_public_ips.instance_id', 'cloud_public_ips.operation_id', 'network_attachment.state as attachment_state', 'network_attachment.sync_state as attachment_sync_state', 'network_attachment.observed_revision as attachment_observed_revision', 'network_attachment.desired_revision as attachment_desired_revision', 'public_pool.state as pool_state', 'public_pool.sync_state as pool_sync_state', 'public_pool.observed_revision as pool_observed_revision', 'public_pool.desired_revision as pool_desired_revision', ]); } foreach ($ipQuery->get() as $row) { if ($lineId !== null && (string) $row->state === 'available' && ((int) ($row->reservation_id ?? 0) > 0 || (int) ($row->instance_id ?? 0) > 0 || (string) ($row->operation_id ?? '') !== '' || (string) ($row->attachment_state ?? '') !== 'active' || (string) ($row->attachment_sync_state ?? '') !== 'applied' || (int) ($row->attachment_observed_revision ?? 0) < (int) ($row->attachment_desired_revision ?? 0) || (string) ($row->pool_state ?? '') !== 'active' || (string) ($row->pool_sync_state ?? '') !== 'applied' || (int) ($row->pool_observed_revision ?? 0) < (int) ($row->pool_desired_revision ?? 0))) { continue; } $key = (string) $row->state; $usage['ip'][$key] = ($usage['ip'][$key] ?? 0) + 1; } return $usage; } private static function capacityDto($policy, array $usage): array { $result = ['instances' => ['allocated' => 0, 'reserved' => 0], 'ip' => [ 'total' => 0, 'allocated' => 0, 'reserved' => 0, 'available' => 0, 'disabled' => 0, ], 'unknown_instances' => (int) ($usage['unknown'] ?? 0)]; foreach (['cpu_cores' => 'cpu', 'memory_mib' => 'memory', 'disk_gib' => 'disk'] as $field => $key) { $configuredField = match ($field) { 'cpu_cores' => 'configured_cpu_cores', 'memory_mib' => 'configured_memory_mib', default => 'configured_disk_gib', }; $safetyField = match ($field) { 'cpu_cores' => 'safety_cpu_cores', 'memory_mib' => 'safety_memory_mib', default => 'safety_disk_gib', }; $configured = max(0, (int) ($policy?->{$configuredField} ?? 0)); $safety = max(0, (int) ($policy?->{$safetyField} ?? 0)); $allocated = (int) ($usage[$key]['allocated'] ?? 0); $reserved = (int) ($usage[$key]['reserved'] ?? 0); $result[$key] = compact('configured', 'safety', 'allocated', 'reserved') + [ 'allocatable' => max(0, $configured - $safety), 'free' => max(0, $configured - $safety - $allocated - $reserved), ]; } $result['instances'] = [ 'allocated' => (int) ($usage['instances']['allocated'] ?? 0), 'reserved' => (int) ($usage['instances']['reserved'] ?? 0), 'max' => max(0, (int) ($policy?->max_instances ?? 0)), ]; $result['ip']['total'] = array_sum($usage['ip'] ?? []); $result['ip']['allocated'] = (int) ($usage['ip']['assigned'] ?? 0); $result['ip']['reserved'] = (int) ($usage['ip']['reserved'] ?? 0) + (int) ($usage['ip']['changing'] ?? 0) + (int) ($usage['ip']['uncertain'] ?? 0); $result['ip']['available'] = (int) ($usage['ip']['available'] ?? 0); $result['ip']['disabled'] = (int) ($usage['ip']['disabled'] ?? 0); return $result; } private static function warnings(CloudHost $host, ?array $placement, array $capacity): array { $warnings = []; if ($placement === null && (string) $host->scheduler_state === 'active') { $warnings[] = '当前资源池已缺失,调度器不会选择此宿主机。'; } elseif ($placement !== null && (string) $host->scheduler_state === 'active' && ((string) $placement['status'] !== 'active' || (string) $placement['line_status'] !== 'active' || (string) $placement['region_status'] !== 'active' || (!CloudNetworkLineFeature::enabled() && !$placement['line_is_default']))) { $warnings[] = '当前资源池、线路或地区已不满足调度条件;保存的配置会保留,但不会接收新实例。'; } if ((int) ($capacity['unknown_instances'] ?? 0) > 0) { $warnings[] = '存在缺少 CPU、内存或磁盘规格的旧实例;补齐规格前不会接收新实例。'; } return $warnings; } private static function integer(array $body, string $key, int $minimum, int $maximum, string $label): int { if (!array_key_exists($key, $body)) { throw new CloudError('缺少必填字段:' . $key); } $value = filter_var($body[$key], FILTER_VALIDATE_INT); if ($value === false || $value < $minimum || $value > $maximum) { throw new CloudError($label . '必须为 ' . $minimum . ' 至 ' . $maximum . ' 的整数'); } return (int) $value; } private static function nullablePositiveId($value): ?int { if ($value === null || $value === '') { return null; } $id = filter_var($value, FILTER_VALIDATE_INT); if ($id === false || $id < 1) { throw new CloudError('资源池 ID 无效'); } return (int) $id; } private static function safeText(string $value, int $maximum): string { $value = preg_replace('/[\x00-\x1f\x7f]/u', '', $value) ?? ''; return mb_substr(trim($value), 0, $maximum); } }