getConnection() ->transaction(function () use ($hostId): array { $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } $rows = CloudHostPoolMembershipService::lockedRows($host); return [$host, $rows]; }, 3); $policy = CloudHostCapacityPolicy::find($hostId); $pools = self::poolOptions(); $poolsById = []; foreach ($pools as $pool) { $poolsById[(int) $pool['id']] = $pool; } $usage = CloudPanelUsageService::forHost($hostId); $publicCapacity = CloudAdminPublicNetworkCapacityProjection::forHosts([ $hostId, ])[$hostId] ?? [ 'aggregate' => CloudAdminPublicNetworkCapacityProjection::empty(), 'lines' => [], ]; $capacity = self::capacityDto( $policy, $usage, $publicCapacity['aggregate'] ); $membershipDtos = []; foreach ($memberships as $membership) { $membershipDtos[] = self::membershipDto( $membership, $poolsById[(int) $membership->resource_pool_id] ?? null, $host, $policy, $usage, $capacity, $publicCapacity['lines'] ); } $primary = CloudHostPoolMembershipService::primary($memberships); $primaryDto = null; if ($primary !== null) { foreach ($membershipDtos as $membershipDto) { if ((int) $membershipDto['resource_pool_id'] === (int) $primary->resource_pool_id) { $primaryDto = $membershipDto; break; } } } $warnings = self::warnings($membershipDtos, $capacity); return [ 'host' => [ 'id' => (int) $host->id, 'name' => self::safeText((string) $host->name, 80), 'enabled' => (bool) $host->enabled, // These two compact fields remain a deterministic summary for // callers that do not yet render the complete relation list. // They are not scheduling authority. 'resource_pool_id' => $primary === null ? null : (int) $primary->resource_pool_id, 'scheduler_state' => $primary === null ? 'disabled' : (string) $primary->scheduler_state, 'active_memberships' => count(array_filter( $membershipDtos, static fn (array $row): bool => $row['scheduler_state'] === 'active' )), 'draining_memberships' => count(array_filter( $membershipDtos, static fn (array $row): bool => $row['scheduler_state'] === 'draining' )), ], 'policy' => self::policyDto($policy), 'capacity' => $capacity, 'placement' => $primaryDto, 'memberships' => $membershipDtos, 'pools' => $pools, 'network_lines_enabled' => CloudNetworkLineFeature::enabled(), 'warnings' => $warnings, 'generated_at' => time(), ]; } /** Save only host-global capacity. Memberships are independent relations. */ public static function saveCapacity(int $hostId, array $body): array { $values = self::capacityValues($body); (new CloudHost())->getConnection()->transaction( function () use ($hostId, $values): void { $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } $memberships = CloudHostPoolMembershipService::lockedRows($host); $policy = CloudHostCapacityPolicy::where('host_id', $hostId) ->lockForUpdate()->first(); if ($policy === null) { $policy = new CloudHostCapacityPolicy(); $policy->host_id = $hostId; $policy->created_at = time(); } if (CloudHostPoolMembershipService::activeRows($memberships) !== []) { self::validateCapacity($values); } $now = time(); $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); } /** Insert or update exactly one independently revisioned membership. */ public static function saveMembership(int $hostId, int $poolId, array $body): array { if ($poolId < 1) { throw new CloudError('资源池 ID 无效', 422); } $state = $body['scheduler_state'] ?? null; if (!is_string($state) || !CloudHostPoolMembershipService::isState($state)) { throw new CloudError('调度状态无效', 422); } $priority = self::integer( $body, 'priority', CloudHostPoolMembershipService::MIN_PRIORITY, CloudHostPoolMembershipService::MAX_PRIORITY, '资源池成员优先级' ); $weight = self::integer( $body, 'weight', CloudHostPoolMembershipService::MIN_WEIGHT, CloudHostPoolMembershipService::MAX_WEIGHT, '资源池成员权重' ); $expectedRevision = self::integer( $body, 'expected_revision', 0, PHP_INT_MAX, '资源池成员关系版本' ); (new CloudHost())->getConnection()->transaction(function () use ( $hostId, $poolId, $state, $priority, $weight, $expectedRevision ): void { $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } $memberships = CloudHostPoolMembershipService::lockedRows($host); $existingMembership = CloudHostPoolMembershipService::findForPool( $memberships, $poolId ); $actualRevision = $existingMembership === null ? 0 : max(1, (int) $existingMembership->revision); if ($actualRevision !== $expectedRevision) { throw new CloudError( '资源池成员关系已被修改,请刷新后重试', 409 ); } $policy = CloudHostCapacityPolicy::where('host_id', $hostId) ->lockForUpdate()->first(); $pool = CloudResourcePool::find($poolId); $line = $pool === null ? null : CloudNetworkLine::find((int) $pool->line_id); $region = $line === null ? null : CloudRegion::find((int) $line->region_id); if ($pool === null || $line === null || $region === null) { throw new CloudError('所选资源池、线路或地区不存在', 409); } if ($state !== 'disabled') { self::assertMembershipRegion( $memberships, $poolId, (int) $region->id ); } else { self::assertNoReservationsForPool($hostId, $poolId); } if ($state === 'active') { if ($policy === null) { throw new CloudError( '启用调度前必须先配置宿主机容量', 409 ); } $usage = CloudPanelUsageService::forHost($hostId); if ((int) ($usage['unknown'] ?? 0) > 0) { throw new CloudError( '宿主机存在缺少 CPU、内存或磁盘规格的旧实例,补齐规格后才能启用调度', 409 ); } self::validateActive( $host, $pool, $line, $region, self::policyValues($policy) ); } $now = time(); CloudHostPoolMembershipService::upsertLocked( $host, $memberships, $poolId, $state, $priority, $weight, $expectedRevision, $now ); $host->updated_at = $now; $host->save(); }, 3); return self::read($hostId); } /** Remove exactly one empty membership without disturbing its siblings. */ public static function deleteMembership(int $hostId, int $poolId, int $expectedRevision): array { if ($hostId < 1 || $poolId < 1 || $expectedRevision < 1) { throw new CloudError('资源池成员关系版本无效,请刷新后重试', 422); } (new CloudHost())->getConnection()->transaction(function () use ( $hostId, $poolId, $expectedRevision ): void { $host = CloudHost::where('id', $hostId)->lockForUpdate()->first(); if ($host === null) { throw new CloudError('宿主机不存在', 404); } $memberships = CloudHostPoolMembershipService::lockedRows($host); // Keep the host -> memberships -> policy -> reservations lock // order aligned with the capacity and placement writers. CloudHostCapacityPolicy::where('host_id', $hostId) ->lockForUpdate()->first(); $membership = CloudHostPoolMembershipService::findForPool( $memberships, $poolId ); if ($membership === null) { throw new CloudError('宿主机资源池成员关系不存在', 404); } if ((int) $membership->revision !== $expectedRevision) { throw new CloudError( '资源池成员关系已被修改,请刷新后重试', 409 ); } self::assertNoReservationsForPool($hostId, $poolId); CloudHostPoolMembershipService::removeLocked( $memberships, $poolId, $expectedRevision ); $host->updated_at = time(); $host->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); } self::validateCapacity($values); } private static function validateCapacity(array $values): void { 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); } } } /** Every non-disabled membership on one physical host stays in one region. */ private static function assertMembershipRegion($memberships, int $targetPoolId, int $targetRegionId): void { $poolIds = []; foreach ($memberships as $membership) { if ((int) $membership->resource_pool_id === $targetPoolId || (string) $membership->scheduler_state === 'disabled') { continue; } $poolIds[] = (int) $membership->resource_pool_id; } $poolIds = array_values(array_unique($poolIds)); if ($poolIds === []) { return; } $pools = CloudResourcePool::whereIn('id', $poolIds) ->orderBy('id')->get(['id', 'line_id'])->keyBy('id'); if ($pools->count() !== count($poolIds)) { throw new CloudError('宿主机现有资源池成员关系不完整', 409); } $lineIds = $pools->pluck('line_id')->map( static fn ($id): int => (int) $id )->unique()->values()->all(); $lines = CloudNetworkLine::whereIn('id', $lineIds) ->orderBy('id')->get(['id', 'region_id'])->keyBy('id'); if ($lines->count() !== count($lineIds)) { throw new CloudError('宿主机现有资源池线路关系不完整', 409); } foreach ($poolIds as $poolId) { $line = $lines->get((int) $pools->get($poolId)->line_id); if ($line === null || (int) $line->region_id !== $targetRegionId) { throw new CloudError( '同一宿主机的启用或排空资源池成员必须属于同一地区', 409 ); } } } /** A disabled or deleted relation must not orphan an exact pool claim. */ private static function assertNoReservationsForPool(int $hostId, int $poolId): void { $reservations = CloudResourceReservation::where('host_id', $hostId) ->where('resource_pool_id', $poolId) ->whereIn('state', ['held', 'uncertain', 'committed']) ->orderBy('id')->lockForUpdate()->get(['id']); if ($reservations->isNotEmpty()) { 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 capacityDto($policy, array $usage, array $publicCapacity): array { $result = [ 'instances' => ['allocated' => 0, 'reserved' => 0], 'ip' => $publicCapacity, '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)), ]; return $result; } private static function membershipDto($membership, ?array $pool, CloudHost $host, $policy, array $usage, array $capacity, array $ipCapacityByLine): array { $warnings = []; $state = (string) $membership->scheduler_state; $lineId = (int) ($pool['line_id'] ?? 0); $ipCapacity = $ipCapacityByLine[$lineId] ?? CloudAdminPublicNetworkCapacityProjection::empty(); $instanceCount = (int) ($capacity['instances']['allocated'] ?? 0) + (int) ($capacity['instances']['reserved'] ?? 0); $maxInstances = (int) ($capacity['instances']['max'] ?? 0); $capacityReady = self::capacityReady($policy) && (int) ($usage['unknown'] ?? 0) === 0 && (int) ($capacity['cpu']['free'] ?? 0) > 0 && (int) ($capacity['memory']['free'] ?? 0) > 0 && (int) ($capacity['disk']['free'] ?? 0) > 0 && ($maxInstances < 1 || $instanceCount < $maxInstances); $pathReady = $pool !== null && (string) $pool['status'] === 'active' && (string) $pool['line_status'] === 'active' && (string) $pool['region_status'] === 'active' && (CloudNetworkLineFeature::enabled() || (bool) $pool['line_is_default']); if ($pool === null) { $warnings[] = '资源池、线路或地区关系已缺失。'; } elseif ($state === 'active' && !$pathReady) { $warnings[] = '资源池、线路或地区当前不满足调度条件。'; } if ($state === 'active' && !$capacityReady) { $warnings[] = '宿主机容量尚未正确配置或存在规格未知的实例。'; } if ($state === 'active' && (int) $ipCapacity['bundles']['available'] < 1) { $warnings[] = '该线路没有可调度的公网双栈容量。'; } return [ 'id' => (int) $membership->id, 'resource_pool_id' => (int) $membership->resource_pool_id, 'scheduler_state' => in_array( $state, self::SCHEDULER_STATES, true ) ? $state : 'disabled', 'priority' => (int) $membership->priority, 'weight' => (int) $membership->weight, 'revision' => max(1, (int) ($membership->revision ?? 1)), 'pool_name' => $pool === null ? '资源池已缺失' : $pool['name'], 'pool_status' => $pool === null ? 'missing' : $pool['status'], 'scheduling_policy' => $pool === null ? '' : $pool['scheduling_policy'], 'line_id' => $pool['line_id'] ?? null, 'line_name' => $pool === null ? '线路已缺失' : $pool['line_name'], 'line_status' => $pool === null ? 'missing' : $pool['line_status'], 'line_is_default' => $pool !== null && (bool) $pool['line_is_default'], 'region_id' => $pool['region_id'] ?? null, 'region_name' => $pool === null ? '地区已缺失' : $pool['region_name'], 'region_status' => $pool === null ? 'missing' : $pool['region_status'], 'ip_capacity' => $ipCapacity, 'placement_ready' => (bool) $host->enabled && $state === 'active' && $pathReady && $capacityReady && (int) $ipCapacity['bundles']['available'] > 0, 'warnings' => $warnings, ]; } private static function warnings(array $memberships, array $capacity): array { $warnings = []; foreach ($memberships as $membership) { foreach ($membership['warnings'] as $warning) { $warnings[] = $membership['pool_name'] . ':' . $warning; } } if ((int) ($capacity['unknown_instances'] ?? 0) > 0) { $warnings[] = '存在缺少 CPU、内存或磁盘规格的旧实例;补齐规格前不会接收新实例。'; } return array_values(array_unique($warnings)); } private static function capacityReady($policy): bool { if ($policy === null) { return false; } $values = self::policyValues($policy); foreach (['cpu_cores', 'memory_mib', 'disk_gib'] as $suffix) { if ((int) $values['configured_' . $suffix] < 1 || (int) $values['safety_' . $suffix] >= (int) $values['configured_' . $suffix]) { return false; } } return true; } private static function policyValues($policy): array { return [ 'configured_cpu_cores' => max( 0, (int) ($policy?->configured_cpu_cores ?? 0) ), 'configured_memory_mib' => max( 0, (int) ($policy?->configured_memory_mib ?? 0) ), 'configured_disk_gib' => max( 0, (int) ($policy?->configured_disk_gib ?? 0) ), 'safety_cpu_cores' => max( 0, (int) ($policy?->safety_cpu_cores ?? 0) ), 'safety_memory_mib' => max( 0, (int) ($policy?->safety_memory_mib ?? 0) ), 'safety_disk_gib' => max( 0, (int) ($policy?->safety_disk_gib ?? 0) ), 'max_instances' => max(0, (int) ($policy?->max_instances ?? 0)), ]; } private static function capacityValues(array $body): array { return [ '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, '最大实例数' ), ]; } 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 safeText(string $value, int $maximum): string { $value = preg_replace('/[\x00-\x1f\x7f]/u', '', $value) ?? ''; return mb_substr(trim($value), 0, $maximum); } }