diff --git a/web/app/Controllers/CloudApiController.php b/web/app/Controllers/CloudApiController.php index 9798948a..f932e130 100644 --- a/web/app/Controllers/CloudApiController.php +++ b/web/app/Controllers/CloudApiController.php @@ -831,6 +831,17 @@ final class CloudApiController } $ids = array_values(array_unique(array_map('intval', explode(',', (string) $raw)))); $snapshots = CloudInstancePageService::forActorIds($actor, $ids); + // Normal list refreshes are cache-only and therefore return without + // waiting on any host. An explicit live=1 request retains the + // previous synchronous engine probe for diagnostics/manual refresh. + $liveRequested = ($request->getQueryParams()['live'] ?? '') === '1'; + if (!$liveRequested) { + $statuses = []; + foreach ($snapshots as $snapshot) { + $statuses[] = CloudInstancePageService::powerStatus($snapshot); + } + return $this->json($response, ['vms' => $statuses]); + } $live = []; $engine = new CloudEngineClient(); foreach ($this->vmSnapshotsByHost($snapshots) as $hostId => $group) { diff --git a/web/app/Controllers/CloudPageController.php b/web/app/Controllers/CloudPageController.php index 6e89d961..6b6b8735 100644 --- a/web/app/Controllers/CloudPageController.php +++ b/web/app/Controllers/CloudPageController.php @@ -628,7 +628,8 @@ final class CloudPageController private function vmListRow(array $vm): array { - [$stateLabel, $stateClass] = $this->vmState((string) ($vm['state'] ?? 'unknown')); + [$stateLabel, $stateClass, $stateSyncLabel, $stateSyncClass] + = $this->vmListState($vm); [$systemLabel, $systemValue] = $this->vmSystem($vm); return [ 'api_id' => (string) ($vm['api_id'] ?? $vm['id'] ?? ''), @@ -636,6 +637,8 @@ final class CloudPageController 'name' => (string) ($vm['name'] ?? '未命名虚拟机'), 'state_label' => $stateLabel, 'state_class' => $stateClass, + 'state_sync_label' => $stateSyncLabel, + 'state_sync_class' => $stateSyncClass, 'region' => trim((string) ($vm['region'] ?? '')) ?: '未设置', 'system_label' => $systemLabel, 'system_value' => $systemValue, @@ -649,6 +652,40 @@ final class CloudPageController ]; } + /** Presentation for the cached list state; operation controls stay live. */ + private function vmListState(array $vm): array + { + $state = strtolower((string) ($vm['state'] ?? 'unknown')); + $sync = strtolower(trim((string) ($vm['state_sync_status'] ?? 'pending'))); + $hasState = $state !== '' && $state !== 'unknown' && $state !== 'nostate'; + if (in_array($sync, ['offline', 'host_offline', 'unavailable', 'host_unreachable'], true)) { + return ['暂时无法同步', 'other', '', 'offline']; + } + if (in_array($sync, ['pending', 'never'], true)) { + $hint = trim((string) ($vm['state_label_hint'] ?? '')); + if ($hint === '') { + $hint = in_array((string) ($vm['instance_status'] ?? ''), + ['creating', 'provisioning', 'deploying'], true) + ? '部署中' : '等待宿主机同步'; + } + return [$hint, 'other', '', '']; + } + if (!$hasState) { + $hint = trim((string) ($vm['state_label_hint'] ?? '')); + if ($hint === '') { + $hint = in_array((string) ($vm['instance_status'] ?? ''), + ['creating', 'provisioning', 'deploying'], true) + ? '部署中' : '等待宿主机同步'; + } + return [$hint, 'other', '', '']; + } + [$label, $class] = $this->vmState($state); + if ((bool) ($vm['state_fresh'] ?? false)) { + return [$label, $class, '', '']; + } + return [$label, $class, '状态同步中', 'stale']; + } + private function vmDetailView(array $vm): array { [$stateLabel, $stateClass] = $this->vmState((string) ($vm['state'] ?? 'unknown')); @@ -717,7 +754,7 @@ final class CloudPageController 'crashed' => ['已崩溃', 'stopped'], 'paused' => ['已暂停', 'other'], 'pmsuspended' => ['已挂起', 'other'], - default => ['未知', 'other'], + default => ['等待宿主机同步', 'other'], }; } diff --git a/web/app/Middleware/CloudApi.php b/web/app/Middleware/CloudApi.php index ca861577..f21e33ae 100644 --- a/web/app/Middleware/CloudApi.php +++ b/web/app/Middleware/CloudApi.php @@ -22,7 +22,11 @@ final class CloudApi || preg_match('#^/api/v1/ssh-keys(?:/|$)#D', $path) === 1 || preg_match('#^/api/v1/vms/[0-9]+/operations$#D', $path) === 1 || $path === '/api/v1/announcements' - || $path === '/api/v1/vms/page'; + || $path === '/api/v1/vms/page' + // The normal list status poll reads only cloud_instance_runtime. + // Keep the explicit live=1 diagnostic path session-backed. + || ($path === '/api/v1/vms/statuses' + && (($request->getQueryParams()['live'] ?? '') !== '1')); $actor = CloudAccess::actor(!$databaseOnly); if (AdminCsrf::requiresProtection($request->getMethod())) { if ($request->getHeaderLine('X-CSRF-Token') !== '') { diff --git a/web/app/Models/CloudInstanceRuntime.php b/web/app/Models/CloudInstanceRuntime.php new file mode 100644 index 00000000..79a5af93 --- /dev/null +++ b/web/app/Models/CloudInstanceRuntime.php @@ -0,0 +1,21 @@ + 'integer', + 'host_id' => 'integer', + 'last_success_at' => 'integer', + 'last_attempt_at' => 'integer', + 'revision' => 'integer', + 'created_at' => 'integer', + 'updated_at' => 'integer', + ]; +} diff --git a/web/app/Services/Cloud/CloudInstancePageService.php b/web/app/Services/Cloud/CloudInstancePageService.php index 36e81710..c2b0b3a0 100644 --- a/web/app/Services/Cloud/CloudInstancePageService.php +++ b/web/app/Services/Cloud/CloudInstancePageService.php @@ -10,10 +10,10 @@ use App\Services\Config; /** * Database-only, actor-scoped instance snapshots for user-facing pages. * - * This service must never contact the private engine. It deliberately gives - * the browser a useful first paint even when one or more libvirt hosts are - * slow or unavailable; live power state is layered on through the dedicated - * status endpoints after the HTML is visible. + * This service must never contact the private engine. It gives the browser a + * useful first paint from the last known runtime observation even when one or + * more libvirt hosts are slow or unavailable; the dedicated status endpoint + * can still layer a live observation after the HTML is visible. */ final class CloudInstancePageService { @@ -32,11 +32,10 @@ final class CloudInstancePageService $snapshots = self::listSnapshots($records); $counts = ['running' => 0, 'stopped' => 0, 'other' => 0]; foreach ($snapshots as $vm) { - $counts[self::stateCategory((string) ($vm['state'] ?? 'unknown'))]++; + $counts[self::listStateCategory($vm)]++; } - // Power state is not persisted in cloud_instances. Any records outside - // the visible page are therefore unknown until the lightweight live - // status request completes; never invent a running/stopped count. + // Runtime observations are scoped to the visible page. Never invent a + // running/stopped count for rows that have no successful observation. $counts['other'] += max(0, $total - count($snapshots)); $vms = $snapshots; @@ -139,16 +138,36 @@ final class CloudInstancePageService $records = self::visibleQuery($actor) ->whereIn('id', array_keys($ordered)) ->get(['id', 'uuid', 'host_id', 'name']); + $runtime = []; + try { + // Resolve the cache against the current authorized DB identity. + // This prevents a reused instance ID from inheriting a previous + // host/UUID's last-known power state. + $runtime = CloudInstanceRuntimeService::forInstances($records); + } catch (\Throwable $error) { + // The cache is optional during a rolling schema upgrade. Keep + // authorization and the actor-visible identity independent of it. + $runtime = []; + } $vms = []; foreach ($records as $instance) { + $cached = $runtime[(string) $instance->id] ?? [ + 'state' => 'unknown', + 'state_fresh' => false, + 'state_observed_at' => null, + 'state_sync_status' => 'pending', + 'state_label_hint' => '等待宿主机同步', + ]; $vms[] = [ 'id' => (string) $instance->id, 'api_id' => (string) $instance->id, 'uuid' => (string) $instance->uuid, 'host_id' => (int) $instance->host_id, 'name' => (string) $instance->name, - 'state' => 'unknown', - ]; + ] + array_intersect_key($cached, array_flip([ + 'state', 'state_fresh', 'state_observed_at', + 'state_sync_status', 'state_label_hint', + ])); } usort($vms, static fn (array $left, array $right): int => ($ordered[(int) $left['id']] ?? PHP_INT_MAX) @@ -205,14 +224,25 @@ final class CloudInstancePageService $matches = $liveUuid !== '' && hash_equals( strtolower((string) $snapshot['uuid']), $liveUuid ); - $available = $matches && CloudInstanceMetadata::state($live['state'] ?? null) !== 'unknown'; + $cachedState = CloudInstanceMetadata::state($snapshot['state'] ?? null); + $liveAvailable = $matches + && CloudInstanceMetadata::state($live['state'] ?? null) !== 'unknown'; + $available = $liveAvailable || (($snapshot['state_fresh'] ?? false) === true); + $state = $liveAvailable + ? CloudInstanceMetadata::state($live['state']) : $cachedState; return [ 'id' => (string) $snapshot['id'], 'api_id' => (string) $snapshot['api_id'], 'uuid' => (string) $snapshot['uuid'], 'name' => (string) $snapshot['name'], - 'state' => $available ? CloudInstanceMetadata::state($live['state']) : 'unknown', + 'state' => $state, 'live_state_available' => $available, + 'state_fresh' => $liveAvailable || (bool) ($snapshot['state_fresh'] ?? false), + 'state_observed_at' => $liveAvailable + ? time() : ($snapshot['state_observed_at'] ?? null), + 'state_sync_status' => $liveAvailable ? 'fresh' + : (string) ($snapshot['state_sync_status'] ?? 'pending'), + 'state_label_hint' => (string) ($snapshot['state_label_hint'] ?? ''), ]; } @@ -221,6 +251,8 @@ final class CloudInstancePageService { $keys = [ 'id', 'api_id', 'uuid', 'name', 'state', 'region', 'location', + 'state_fresh', 'state_observed_at', 'state_sync_status', + 'state_label_hint', 'instance_status', 'os_source', 'os_name', 'install_iso', 'public_ip_cidr', 'public_ip', 'login_name', 'engine_metadata_version', 'vcpu', 'cpu_cores', 'memory_mib', 'ram_mib', 'disks', 'disk_gib', @@ -234,6 +266,10 @@ final class CloudInstancePageService return []; } $hosts = self::hostMap($instances); + // The runtime table is the only source for a persisted power state. + // Keep this optional so older installations can still render their + // database-backed list while the runtime table is being provisioned. + $runtime = self::runtimeMap($instances); $result = []; foreach ($instances as $instance) { $decoded = json_decode((string) $instance->spec_json, true); @@ -245,14 +281,86 @@ final class CloudInstancePageService if ($region === '') { $region = trim((string) ($spec['region'] ?? $spec['location'] ?? '')); } + $cached = $runtime[(int) $instance->id] ?? [ + 'state' => 'unknown', + 'state_fresh' => false, + 'state_observed_at' => null, + 'state_sync_status' => 'pending', + 'state_label_hint' => '等待宿主机同步', + ]; $result[] = self::listItem(array_merge($spec, [ 'id' => (string) $instance->id, 'api_id' => (string) $instance->id, 'uuid' => (string) $instance->uuid, 'name' => (string) $instance->name, - 'state' => 'unknown', + 'instance_status' => (string) $instance->status, 'region' => $region, - ])); + ], $cached)); + } + return $result; + } + + /** + * Return safe, actor-independent cached power state keyed by instance id. + * The runtime service owns freshness and host-health decisions; this + * adapter only limits the fields exposed to the user page. + */ + private static function runtimeMap($instances): array + { + if ($instances->isEmpty() + || !class_exists(CloudInstanceRuntimeService::class) + || !method_exists(CloudInstanceRuntimeService::class, 'forInstances')) { + return []; + } + try { + $rows = CloudInstanceRuntimeService::forInstances($instances); + } catch (\Throwable $error) { + // A missing table or a transient read failure must not hide the + // database-backed instance list. The UI will show a sync hint. + return []; + } + if (!is_array($rows)) { + return []; + } + $result = []; + foreach ($rows as $key => $row) { + if (is_object($row) && method_exists($row, 'toArray')) { + $row = $row->toArray(); + } + if (!is_array($row)) { + continue; + } + $id = filter_var($row['instance_id'] ?? $row['id'] ?? $key, + FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); + if ($id === false) { + continue; + } + $state = CloudInstanceMetadata::state($row['state'] ?? 'unknown'); + $fresh = filter_var($row['state_fresh'] ?? $row['fresh'] ?? false, + FILTER_VALIDATE_BOOLEAN); + $observed = $row['state_observed_at'] ?? $row['last_success_at'] ?? null; + if (is_string($observed) && $observed !== '' && !is_numeric($observed)) { + $observed = strtotime($observed) ?: null; + } + $observed = is_numeric($observed) ? max(0, (int) $observed) : null; + $syncStatus = strtolower(trim((string) ($row['state_sync_status'] + ?? $row['sync_status'] ?? 'pending'))); + if (!preg_match('/^[a-z0-9_-]{1,32}$/D', $syncStatus)) { + $syncStatus = 'pending'; + } + $hint = trim((string) ($row['state_label_hint'] ?? '')); + if ($hint === '' || (function_exists('mb_strlen') + ? mb_strlen($hint) : strlen($hint)) > 32) { + $hint = $syncStatus === 'unavailable' || $syncStatus === 'offline' + ? '暂时无法同步' : '等待宿主机同步'; + } + $result[(int) $id] = [ + 'state' => $state, + 'state_fresh' => $fresh, + 'state_observed_at' => $observed, + 'state_sync_status' => $syncStatus, + 'state_label_hint' => $hint, + ]; } return $result; } @@ -428,7 +536,7 @@ final class CloudInstancePageService private static function listColumns(): array { - return ['id', 'uuid', 'host_id', 'name', 'spec_json']; + return ['id', 'uuid', 'host_id', 'name', 'status', 'spec_json']; } private static function pagination(int $page, int $perPage, int $total, int $count): array @@ -458,4 +566,14 @@ final class CloudInstancePageService } return 'other'; } + + /** Do not count a cached state as running/stopped while its host is offline. */ + private static function listStateCategory(array $vm): string + { + $sync = strtolower(trim((string) ($vm['state_sync_status'] ?? 'pending'))); + if (in_array($sync, ['offline', 'host_offline', 'unavailable', 'host_unreachable', 'pending'], true)) { + return 'other'; + } + return self::stateCategory((string) ($vm['state'] ?? 'unknown')); + } } diff --git a/web/app/Services/Cloud/CloudInstanceRuntimeService.php b/web/app/Services/Cloud/CloudInstanceRuntimeService.php new file mode 100644 index 00000000..32be68ff --- /dev/null +++ b/web/app/Services/Cloud/CloudInstanceRuntimeService.php @@ -0,0 +1,441 @@ +first(); + if ($runtime === null) { + $runtime = new CloudInstanceRuntime(); + $runtime->instance_id = $instanceId; + $runtime->host_id = $hostId; + $runtime->uuid = $uuid; + $runtime->power_state = 'unknown'; + $runtime->sync_state = 'never'; + $runtime->last_success_at = 0; + $runtime->last_attempt_at = 0; + $runtime->revision = 0; + $runtime->last_error_code = ''; + $runtime->created_at = $now; + $runtime->updated_at = $now; + $runtime->save(); + return $runtime; + } + + if (!self::sameIdentity($runtime, $hostId, $uuid)) { + self::reset($runtime, $hostId, $uuid, $now); + $runtime->save(); + return $runtime; + } + return $runtime; + } + + /** + * Save one trusted engine observation. Unknown engine states are never + * promoted to running or stopped; they leave the cached state unchanged. + */ + public static function markObserved(int $instanceId, int $hostId, string $uuid, + $powerState, ?int $now = null, ?int $revision = null): CloudInstanceRuntime + { + $now = self::now($now); + [$instanceId, $hostId, $uuid] = self::identity($instanceId, $hostId, $uuid); + $state = self::normalizeState($powerState); + // Resolve the identity before writing, but apply the write through a + // conditional revision update. A worker write (which omits the + // token) advances the revision and therefore invalidates any in-flight + // background poll carrying an older token. + // A tokenized background response must never recreate a row that a + // completed delete already removed. Immediate worker writes may seed + // a legacy/missing row, but only when no batch token is present. + self::identityRow($instanceId, $hostId, $uuid, $now, $revision === null); + $values = [ + 'last_attempt_at' => $now, + 'updated_at' => $now, + ]; + if ($state !== 'unknown') { + $values += [ + 'power_state' => $state, + 'last_success_at' => $now, + 'sync_state' => 'ok', + 'last_error_code' => '', + ]; + } else { + // A malformed or unsupported engine item is a failed observation; + // retaining the previous state avoids falsely reporting shutdown. + $values += [ + 'sync_state' => 'unavailable', + 'last_error_code' => 'invalid_response', + ]; + } + return self::writeObservation( + $instanceId, $hostId, $uuid, $values, $revision + ); + } + + /** + * Claim one host-batch item before issuing vm-states. The returned token + * is valid only until another writer advances the row revision. + */ + public static function beginObservation(int $instanceId, int $hostId, string $uuid, + ?int $now = null): int + { + $now = self::now($now); + [$instanceId, $hostId, $uuid] = self::identity($instanceId, $hostId, $uuid); + for ($attempt = 0; $attempt < 3; $attempt++) { + // The sync pass seeds rows before claiming them. Do not recreate + // one that a concurrent delete has already forgotten. + $runtime = self::identityRow($instanceId, $hostId, $uuid, $now, false); + $current = max(0, (int) $runtime->revision); + $next = $current + 1; + $updated = CloudInstanceRuntime::where('instance_id', $instanceId) + ->where('host_id', $hostId) + ->where('uuid', $uuid) + ->where('revision', $current) + ->increment('revision', 1, [ + 'last_attempt_at' => $now, + 'updated_at' => $now, + ]); + if ((int) $updated === 1) { + return $next; + } + } + throw new \RuntimeException('Runtime observation token unavailable'); + } + + /** Mark a host batch unavailable while preserving its last good state. */ + public static function markUnavailable(array $instanceIds, ?int $hostId = null, + ?int $now = null, string $errorCode = 'host_unavailable', array $revisions = []): int + { + $now = self::now($now); + $ids = self::ids($instanceIds); + if ($ids === []) { + return 0; + } + $errorCode = self::errorCode($errorCode); + if ($hostId !== null) { + if ($hostId < 1) { + throw new \InvalidArgumentException('Host ID must be positive'); + } + } + $values = [ + 'sync_state' => 'unavailable', + 'last_attempt_at' => $now, + 'last_error_code' => $errorCode, + 'updated_at' => $now, + ]; + // Tokenized writes are intentionally per-row: each row may have a + // different claim revision. A stale token simply affects zero rows. + if ($revisions !== []) { + $updated = 0; + foreach ($ids as $id) { + if (!array_key_exists($id, $revisions)) { + continue; + } + $query = CloudInstanceRuntime::where('instance_id', $id); + if ($hostId !== null) { + $query->where('host_id', $hostId); + } + $query->where('revision', (int) $revisions[$id]); + $updated += (int) $query->increment('revision', 1, $values); + } + return $updated; + } + $query = CloudInstanceRuntime::whereIn('instance_id', $ids); + if ($hostId !== null) { + $query->where('host_id', $hostId); + } + return (int) $query->increment('revision', 1, $values); + } + + /** Remove an observation when its panel instance is irreversibly deleted. */ + public static function forget(int $instanceId): int + { + if ($instanceId < 1) { + return 0; + } + return (int) CloudInstanceRuntime::where('instance_id', $instanceId)->delete(); + } + + /** + * Return rows keyed by instance ID. Missing rows are represented as a + * pending observation, allowing callers to render deployment/syncing + * without inventing a power state. + */ + public static function forIds(array $instanceIds, ?int $now = null): array + { + $now = self::now($now); + $ids = self::ids($instanceIds); + if ($ids === []) { + return []; + } + $rows = []; + foreach (CloudInstanceRuntime::whereIn('instance_id', $ids)->get() as $runtime) { + $rows[(int) $runtime->instance_id] = self::view($runtime, $now); + } + foreach ($ids as $id) { + if (!isset($rows[$id])) { + $rows[$id] = self::pendingView($id, $now); + } + } + // Preserve the caller's de-duplicated ID order for predictable JSON. + $ordered = []; + foreach ($ids as $id) { + $ordered[(string) $id] = $rows[$id]; + } + return $ordered; + } + + /** Return observations for an iterable of CloudInstance-like records. */ + public static function forInstances(iterable $instances, ?int $now = null): array + { + $items = []; + foreach ($instances as $instance) { + $id = self::value($instance, 'id'); + if ($id === null) { + $id = self::value($instance, 'instance_id'); + } + $id = filter_var($id, FILTER_VALIDATE_INT, + ['options' => ['min_range' => 1]]); + if ($id === false) { + continue; + } + $items[(int) $id] = [ + 'host_id' => self::value($instance, 'host_id'), + 'uuid' => self::value($instance, 'uuid'), + ]; + } + $views = self::forIds(array_keys($items), $now); + foreach ($items as $id => $identity) { + $currentHost = max(0, (int) $identity['host_id']); + $currentUuid = is_string($identity['uuid']) + ? strtolower(trim($identity['uuid'])) : ''; + $cachedHost = (int) ($views[(string) $id]['host_id'] ?? 0); + $cachedUuid = strtolower(trim((string) ($views[(string) $id]['uuid'] ?? ''))); + // A reused instance row must never inherit the previous VM's + // power state. Treat a partial or mismatched identity as a fresh + // pending observation and expose only the current DB identity. + $identityMismatch = ($cachedHost !== 0 || $cachedUuid !== '') + && ($cachedHost !== $currentHost || $cachedUuid !== $currentUuid); + if ($identityMismatch || $currentHost < 1 || !self::isUuid($currentUuid)) { + $views[(string) $id] = self::pendingView((int) $id, self::now($now)); + } + $views[(string) $id]['host_id'] = $currentHost; + $views[(string) $id]['uuid'] = $currentUuid; + } + return $views; + } + + /** Normalize engine values to the panel's bounded state vocabulary. */ + public static function normalizeState($value): string + { + $state = CloudInstanceMetadata::state($value); + return match ($state) { + 'shutoff', 'shut_off', 'stopped' => 'stopped', + 'running' => 'running', + 'paused' => 'paused', + 'crashed' => 'crashed', + 'pmsuspended' => 'pmsuspended', + 'blocked' => 'blocked', + default => 'unknown', + }; + } + + /** + * Apply a cache write while atomically advancing its revision. When a + * token is supplied, the row must still carry exactly that revision; + * otherwise this is a worker/immediate write that supersedes any poll. + */ + private static function writeObservation(int $instanceId, int $hostId, string $uuid, + array $values, ?int $revision): CloudInstanceRuntime + { + $query = CloudInstanceRuntime::where('instance_id', $instanceId) + ->where('host_id', $hostId) + ->where('uuid', $uuid); + if ($revision !== null) { + $query->where('revision', max(0, $revision)); + } + $updated = $query->increment('revision', 1, $values); + $runtime = CloudInstanceRuntime::where('instance_id', $instanceId)->first(); + if ($runtime !== null) { + // A zero-row conditional update means a newer writer won the + // race. Return that current row without overwriting it. + return $runtime; + } + // The row can disappear during an irreversible delete race. Do not + // recreate a cache row for a VM that was just forgotten; return an + // ephemeral identity-only value to preserve the historical contract. + $runtime = new CloudInstanceRuntime(); + $runtime->instance_id = $instanceId; + $runtime->host_id = $hostId; + $runtime->uuid = $uuid; + return $runtime; + } + + private static function identityRow(int $instanceId, int $hostId, string $uuid, + int $now, bool $create = true): CloudInstanceRuntime + { + $runtime = CloudInstanceRuntime::where('instance_id', $instanceId)->first(); + if ($runtime === null) { + if (!$create) { + throw new \RuntimeException('Runtime observation was removed'); + } + return self::seed($instanceId, $hostId, $uuid, $now); + } + if (!self::sameIdentity($runtime, $hostId, $uuid)) { + throw new \RuntimeException('Runtime observation identity mismatch'); + } + return $runtime; + } + + private static function reset(CloudInstanceRuntime $runtime, int $hostId, + string $uuid, int $now): void + { + $runtime->host_id = $hostId; + $runtime->uuid = $uuid; + $runtime->power_state = 'unknown'; + $runtime->sync_state = 'never'; + $runtime->last_success_at = 0; + $runtime->last_attempt_at = 0; + // Bump the generation when an instance ID is reused, invalidating any + // response still in flight for the previous host/UUID identity. + $runtime->revision = max(0, (int) $runtime->revision) + 1; + $runtime->last_error_code = ''; + $runtime->updated_at = $now; + } + + private static function view(CloudInstanceRuntime $runtime, int $now): array + { + $success = max(0, (int) $runtime->last_success_at); + $age = $success > 0 ? max(0, $now - $success) : null; + $future = $success > $now; + $fresh = !$future && $success > 0 && $age !== null && $age <= self::FRESHNESS_SECONDS + && (string) $runtime->sync_state === 'ok'; + $sync = match ((string) $runtime->sync_state) { + 'unavailable' => 'unavailable', + 'never' => 'pending', + default => $success === 0 || $future ? 'pending' : ($fresh ? 'fresh' : 'syncing'), + }; + return [ + 'instance_id' => (string) $runtime->instance_id, + 'host_id' => (int) $runtime->host_id, + 'uuid' => (string) $runtime->uuid, + 'state' => self::normalizeState($runtime->power_state), + 'power_state' => self::normalizeState($runtime->power_state), + 'fresh' => $fresh, + 'state_fresh' => $fresh, + 'freshness' => $success === 0 || $future ? 'never' : ($fresh ? 'fresh' : 'stale'), + 'sync_status' => $sync, + 'state_sync_status' => $sync === 'unavailable' ? 'offline' : $sync, + 'last_success_at' => $success > 0 ? $success : null, + 'state_observed_at' => $success > 0 && !$future ? $success : null, + 'last_attempt_at' => max(0, (int) $runtime->last_attempt_at) ?: null, + 'age_seconds' => $age, + 'state_label_hint' => $sync === 'unavailable' + ? '暂时无法同步' : ($success === 0 || $future ? '等待宿主机同步' : ''), + ]; + } + + private static function pendingView(int $instanceId, int $now): array + { + return [ + 'instance_id' => (string) $instanceId, + 'host_id' => 0, + 'uuid' => '', + 'state' => 'unknown', + 'power_state' => 'unknown', + 'fresh' => false, + 'state_fresh' => false, + 'freshness' => 'never', + 'sync_status' => 'pending', + 'state_sync_status' => 'pending', + 'last_success_at' => null, + 'state_observed_at' => null, + 'last_attempt_at' => null, + 'age_seconds' => null, + 'state_label_hint' => '等待宿主机同步', + ]; + } + + private static function sameIdentity(CloudInstanceRuntime $runtime, int $hostId, + string $uuid): bool + { + return (int) $runtime->host_id === $hostId + && hash_equals(strtolower((string) $runtime->uuid), $uuid); + } + + private static function identity(int $instanceId, int $hostId, string $uuid): array + { + if ($instanceId < 1 || $hostId < 1) { + throw new \InvalidArgumentException('Instance and host IDs must be positive'); + } + $uuid = strtolower(trim($uuid)); + // Libvirt accepts operator-supplied UUIDs whose version/variant + // nibbles are not RFC 4122 values. Match the existing panel identity + // boundary and require only the canonical five-group shape. + if (!self::isUuid($uuid)) { + throw new \InvalidArgumentException('Invalid runtime UUID'); + } + return [$instanceId, $hostId, $uuid]; + } + + private static function isUuid(string $uuid): bool + { + return preg_match( + '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D', + strtolower(trim($uuid)) + ) === 1; + } + + private static function ids(array $values): array + { + $ids = []; + foreach ($values as $value) { + $id = filter_var($value, FILTER_VALIDATE_INT, + ['options' => ['min_range' => 1]]); + if ($id !== false) { + $ids[(int) $id] = true; + } + } + return array_keys($ids); + } + + private static function value($instance, string $key) + { + if (is_array($instance)) { + return $instance[$key] ?? null; + } + return is_object($instance) ? ($instance->{$key} ?? null) : null; + } + + private static function errorCode(string $value): string + { + return in_array($value, self::ERROR_CODES, true) ? $value : 'host_unavailable'; + } + + private static function now(?int $now): int + { + return max(1, $now ?? time()); + } +} diff --git a/web/app/Services/Cloud/CloudInstanceRuntimeSyncService.php b/web/app/Services/Cloud/CloudInstanceRuntimeSyncService.php new file mode 100644 index 00000000..eb758504 --- /dev/null +++ b/web/app/Services/Cloud/CloudInstanceRuntimeSyncService.php @@ -0,0 +1,259 @@ +engine ??= new CloudEngineClient(null, self::ENGINE_TIMEOUT_SECONDS); + } + + /** + * Poll active instances in host batches. The returned counters contain + * no engine-controlled text and are suitable for heartbeat diagnostics. + */ + public function sync(int $limit = 500): array + { + $result = ['observed' => 0, 'unavailable' => 0, 'skipped' => 0, 'errors' => []]; + if (!$this->runtimeTableAvailable()) { + // Rolling upgrades may start the daemon before cloudInit has run. + // Keep billing/lifecycle work healthy until the table is present. + $result['skipped'] = 1; + return $result; + } + + $actor = $this->systemActor(); + if ($actor === null) { + $result['errors'][] = 'runtime_status_sync_unconfigured'; + return $result; + } + + try { + // Select the least recently attempted observations first. This + // gives a bounded daemon pass a rotating cursor without storing a + // second global cursor: every successful or failed host batch + // advances last_attempt_at, so high instance IDs cannot starve + // behind a permanently busy low-ID prefix. + $instanceQuery = CloudInstance::leftJoin( + 'cloud_instance_runtime as runtime', + 'runtime.instance_id', '=', 'cloud_instances.id' + )->where('cloud_instances.status', '<>', 'deleted') + ->orderByRaw('COALESCE(runtime.last_attempt_at, runtime.last_success_at, 0) ASC') + ->orderBy('cloud_instances.id') + ->limit(max(1, min(2000, $limit))); + // Production uses MariaDB, where filtering malformed legacy UUIDs + // at the database boundary prevents them from occupying every + // bounded pass. Keep the query usable in SQLite-based contract + // tests and local diagnostics; the application-level validation + // below remains the final guard in either driver. + if ((new CloudInstance())->getConnection()->getDriverName() === 'mysql') { + $instanceQuery->whereRaw( + "cloud_instances.uuid REGEXP ?", + ['^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$'] + ); + } + $instances = $instanceQuery->get([ + 'cloud_instances.id', 'cloud_instances.host_id', 'cloud_instances.uuid', + ]); + } catch (\Throwable $error) { + $result['errors'][] = 'runtime_status_sync_failed'; + return $result; + } + if ($instances->isEmpty()) { + return $result; + } + + $groups = []; + foreach ($instances as $instance) { + $hostId = (int) $instance->host_id; + $uuid = strtolower(trim((string) $instance->uuid)); + if ($hostId < 1 || preg_match( + '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D', + $uuid + ) !== 1) { + $result['skipped']++; + continue; + } + $groups[$hostId][] = [ + 'id' => (int) $instance->id, + 'host_id' => $hostId, + 'uuid' => $uuid, + ]; + // Legacy instances may predate the independent cache. Seeding a + // row does not claim a power state; the batch response still must + // provide the first successful observation. + try { + CloudInstanceRuntimeService::seed( + (int) $instance->id, $hostId, $uuid + ); + } catch (\Throwable $error) { + $result['skipped']++; + // Do not leave a row that failed identity seeding in the + // host group; otherwise syncHost() would claim it again and + // count the same failure twice in one pass. + array_pop($groups[$hostId]); + } + } + + foreach ($groups as $hostId => $group) { + $this->syncHost((int) $hostId, $group, $actor, $result); + } + return $result; + } + + private function syncHost(int $hostId, array $group, array $actor, array &$result): void + { + // Claim every row immediately before the host request. A successful + // worker operation advances the same revision, making this batch + // stale without allowing its eventual response to overwrite the + // worker's fresher state. + $active = []; + $revisions = []; + foreach ($group as $row) { + try { + $revisions[(int) $row['id']] = CloudInstanceRuntimeService::beginObservation( + (int) $row['id'], $hostId, (string) $row['uuid'] + ); + $active[] = $row; + } catch (\Throwable $error) { + $result['skipped']++; + } + } + if ($active === []) { + return; + } + $group = $active; + $ids = array_map(static fn (array $row): int => (int) $row['id'], $group); + $allowed = []; + foreach ($group as $row) { + $allowed[(string) $row['uuid']] = $row; + } + try { + $payload = $this->engine->host($hostId, 'GET', 'vm-states', $actor); + } catch (CloudError $error) { + CloudInstanceRuntimeService::markUnavailable( + $ids, $hostId, time(), self::errorCode($error), $revisions + ); + $result['unavailable'] += count($ids); + $result['errors'][] = 'runtime_status_sync_failed'; + return; + } catch (\Throwable $error) { + CloudInstanceRuntimeService::markUnavailable( + $ids, $hostId, time(), 'host_unavailable', $revisions + ); + $result['unavailable'] += count($ids); + $result['errors'][] = 'runtime_status_sync_failed'; + return; + } + + $seen = []; + $items = is_array($payload['items'] ?? null) ? $payload['items'] : []; + foreach ($items as $item) { + if (!is_array($item) || !is_string($item['uuid'] ?? null)) { + continue; + } + $uuid = strtolower(trim($item['uuid'])); + if (!isset($allowed[$uuid])) { + continue; + } + $row = $allowed[$uuid]; + $state = $item['state'] ?? ($item['power_state'] ?? null); + $seen[$uuid] = true; + try { + CloudInstanceRuntimeService::markObserved( + (int) $row['id'], $hostId, $uuid, $state, null, + $revisions[(int) $row['id']] ?? null + ); + if (CloudInstanceRuntimeService::normalizeState($state) !== 'unknown') { + $result['observed']++; + } else { + $result['unavailable']++; + } + } catch (\Throwable $error) { + $result['skipped']++; + } + } + + $missing = []; + foreach ($allowed as $uuid => $row) { + if (!isset($seen[$uuid])) { + $missing[] = (int) $row['id']; + } + } + if ($missing !== []) { + $missingRevisions = []; + foreach ($missing as $missingId) { + if (array_key_exists($missingId, $revisions)) { + $missingRevisions[$missingId] = $revisions[$missingId]; + } + } + CloudInstanceRuntimeService::markUnavailable( + $missing, $hostId, time(), 'invalid_response', $missingRevisions + ); + $result['unavailable'] += count($missing); + $result['errors'][] = 'runtime_status_sync_failed'; + } + } + + private function systemActor(): ?array + { + $ids = []; + foreach (explode(',', (string) Config::get('super_admin', '')) as $value) { + $id = filter_var(trim($value), FILTER_VALIDATE_INT, + ['options' => ['min_range' => 1]]); + if ($id !== false) { + $ids[(int) $id] = true; + } + } + if ($ids === []) { + return null; + } + try { + $user = User::whereIn('id', array_keys($ids)) + ->where('enable', 1)->where('is_admin', 1)->first(['id']); + } catch (\Throwable $error) { + return null; + } + if ($user === null || (int) $user->id < 1) { + return null; + } + return [ + 'user_id' => (int) $user->id, + 'is_admin' => true, + 'is_super_admin' => true, + 'can_manage' => true, + 'session_id' => 'cloud-runtime-sync', + ]; + } + + private function runtimeTableAvailable(): bool + { + try { + return (new CloudInstanceRuntime())->getConnection() + ->getSchemaBuilder()->hasTable('cloud_instance_runtime'); + } catch (\Throwable $error) { + return false; + } + } + + private static function errorCode(CloudError $error): string + { + return in_array($error->httpStatus, [408, 429, 502, 503, 504], true) + ? 'host_unavailable' : 'engine_error'; + } +} diff --git a/web/app/Services/Cloud/CloudLifecycleService.php b/web/app/Services/Cloud/CloudLifecycleService.php index 2f93799f..3fbbfc78 100644 --- a/web/app/Services/Cloud/CloudLifecycleService.php +++ b/web/app/Services/Cloud/CloudLifecycleService.php @@ -58,6 +58,20 @@ final class CloudLifecycleService // Unknown traffic is fail-open. Time expiry remains enforceable. error_log('cloud traffic sync unavailable: ' . get_class($error)); } + // Power state is an observation cache, not a billing decision. Keep + // it on the lifecycle cadence so list pages can render immediately + // from the last successful host batch without waiting on libvirt. + $this->heartbeat?->busy('runtime_status_sync'); + try { + $runtime = (new CloudInstanceRuntimeSyncService())->sync($batchSize); + foreach (array_unique($runtime['errors'] ?? []) as $code) { + $this->observeFailure((string) $code); + } + } catch (\Throwable $error) { + $this->observeFailure('runtime_status_sync_failed'); + // A status poll must never prevent invoice or expiry policy work. + error_log('cloud runtime status sync unavailable: ' . get_class($error)); + } $queued = 0; $this->heartbeat?->busy('instance_reconcile'); CloudInstance::where('status', '<>', 'deleted') diff --git a/web/app/Services/Cloud/CloudRuntimeHeartbeat.php b/web/app/Services/Cloud/CloudRuntimeHeartbeat.php index 53ac9581..39cbb37d 100644 --- a/web/app/Services/Cloud/CloudRuntimeHeartbeat.php +++ b/web/app/Services/Cloud/CloudRuntimeHeartbeat.php @@ -10,11 +10,13 @@ final class CloudRuntimeHeartbeat private const PHASES = ['startup' => '启动', 'worker_tick' => '任务消费或核对', 'notification' => '邮件通知', 'lifecycle_tick' => '生命周期巡检', 'provision_expiry' => '开通账单过期处理', 'invoice_generation' => '续费账单生成', - 'traffic_sync' => '流量同步', 'instance_reconcile' => '实例策略核对', + 'traffic_sync' => '流量同步', 'runtime_status_sync' => '实例状态同步', + 'instance_reconcile' => '实例策略核对', 'idle' => '等待下一轮', 'stopped' => '已退出']; private const ERRORS = ['worker_tick_failed', 'notification_failed', 'lifecycle_tick_failed', 'provision_expiry_failed', 'invoice_generation_failed', - 'traffic_sync_failed', 'instance_reconcile_failed', 'task_failed', + 'traffic_sync_failed', 'runtime_status_sync_failed', + 'runtime_status_sync_unconfigured', 'instance_reconcile_failed', 'task_failed', 'task_uncertain', 'runtime_error']; private const SCAN_LIMIT = 512; private const FILE_LIMIT = 4096; diff --git a/web/app/Services/Cloud/CloudWorkerService.php b/web/app/Services/Cloud/CloudWorkerService.php index 45efb3be..5fc39caf 100644 --- a/web/app/Services/Cloud/CloudWorkerService.php +++ b/web/app/Services/Cloud/CloudWorkerService.php @@ -401,6 +401,11 @@ final class CloudWorkerService } $instance->updated_at = time(); $instance->save(); + // Seed the independent runtime observation immediately after + // provisioning. If the engine returned a trusted power + // state, promote it now; otherwise the background host batch + // will provide the first observation without showing "未知". + self::recordRuntime($instance, $vm, 'create', []); if ($billingSnapshot) { $period = CloudServicePeriod::where('order_id', $order->id)->first() ?? new CloudServicePeriod(); @@ -606,6 +611,12 @@ final class CloudWorkerService $instance->network_status = 'active'; $instance->network_suspended_at = null; } + // Successful power/reinstall/policy operations often + // include the resulting VM state. Persist that bounded + // observation so the next page render is immediate; + // missing state is deliberately left for vm-states. + self::recordRuntime($instance, is_array($result) ? $result : [], + (string) $job->kind, $payload); $instance->updated_at = time(); $instance->save(); } @@ -685,6 +696,54 @@ final class CloudWorkerService return CloudOperationCatalog::isPowerAction($action ?? '') ? $action : null; } + /** + * Persist only a successful, allow-listed power observation. Runtime + * cache failures are isolated from the durable job outcome: the engine + * result and billing state remain authoritative even during a rolling + * schema upgrade where the cache table may not exist yet. + */ + private static function recordRuntime(CloudInstance $instance, array $result, + string $kind, array $payload): void + { + try { + if (in_array($kind, ['delete', 'expiry_delete'], true)) { + CloudInstanceRuntimeService::forget((int) $instance->id); + return; + } + $source = is_array($result['vm'] ?? null) ? $result['vm'] : $result; + $state = $source['state'] ?? ($source['power_state'] ?? null); + if (CloudInstanceRuntimeService::normalizeState($state) === 'unknown') { + if ($kind === 'action') { + $action = self::payloadAction($payload); + $state = match ($action) { + 'start', 'reboot', 'reset' => 'running', + 'shutdown', 'poweroff' => 'stopped', + default => null, + }; + } elseif ($kind === 'reinstall') { + // The engine starts the replacement definition before + // returning a successful reinstall result. Older engine + // responses may omit the nested vm.state, so preserve + // the same successful postcondition explicitly. + $state = 'running'; + } + } + $instanceId = (int) $instance->id; + $hostId = (int) $instance->host_id; + $uuid = (string) $instance->uuid; + if ($kind === 'create') { + CloudInstanceRuntimeService::seed($instanceId, $hostId, $uuid); + } + if (CloudInstanceRuntimeService::normalizeState($state) !== 'unknown') { + CloudInstanceRuntimeService::markObserved( + $instanceId, $hostId, $uuid, $state + ); + } + } catch (\Throwable $error) { + error_log('cloud runtime observation write unavailable: ' . get_class($error)); + } + } + private static function exceptionDiagnostic(\Throwable $error, bool $dispatched, ?string $kind, bool $hasOrder, ?string $action): string { diff --git a/web/public/cloud/admin-instances.js b/web/public/cloud/admin-instances.js index 0139cea4..74ec0766 100644 --- a/web/public/cloud/admin-instances.js +++ b/web/public/cloud/admin-instances.js @@ -223,20 +223,35 @@ function copyButton(value, label = '复制') { return button; } -function powerInfo(value) { +function powerInfo(value, sync = '') { + const normalizedSync = String(sync || '').toLowerCase(); + if (['unavailable', 'offline', 'host_offline', 'host_unreachable'].includes(normalizedSync)) { + return ['暂时无法同步', 'secondary']; + } + if (['pending', 'never'].includes(normalizedSync)) { + return ['等待宿主机同步', 'secondary']; + } + const normalized = String(value || '').toLowerCase().replace(/[_\s-]+/g, ''); return ({ running: ['运行中', 'success'], blocked: ['运行中', 'success'], shutoff: ['已关机', 'secondary'], stopped: ['已关机', 'secondary'], shutdown: ['关机中', 'warning'], paused: ['已暂停', 'warning'], pmsuspended: ['已挂起', 'warning'], crashed: ['已崩溃', 'danger'], - })[String(value || '').toLowerCase()] || ['状态未知', 'secondary']; + })[normalized] || ['等待宿主机同步', 'secondary']; } function paintPower(node, vm) { - const [label, tone] = powerInfo(vm.state); - node.textContent = `电源:${label}`; + const sync = String(vm?.state_sync_status || '').toLowerCase(); + const [label, tone] = powerInfo(vm?.state, sync); + const stale = !['', 'fresh'].includes(sync) + && !['pending', 'never', 'unavailable', 'offline', 'host_offline', 'host_unreachable'].includes(sync); + node.textContent = `电源:${label}${stale ? ' · 状态同步中' : ''}`; node.className = `badge bg-${tone}-subtle text-${tone}-emphasis`; - node.title = vm.live_state_available === true ? '来自宿主机的实时电源状态' : '尚未获取实时状态,或宿主机暂时不可达;不表示已关机。'; + node.title = sync === 'offline' || sync === 'unavailable' + ? '宿主机暂时无法同步;保留最近一次状态,不表示已关机。' + : vm.live_state_available === true || sync === 'fresh' + ? '来自运行状态缓存的最近成功同步结果' + : '尚未获取成功状态,或状态已超过有效期。'; } function jobStatus(value) { @@ -365,25 +380,40 @@ async function refreshStatuses() { const payload = await api(`/api/v1/vms/statuses?ids=${encodeURIComponent(ids)}`, controller); if (controller.signal.aborted || serial !== state.serial) return; const updates = Array.isArray(payload?.vms) ? payload.vms : []; - let unknown = 0; + let syncing = 0; for (const vm of state.items) { const update = updates.find(item => String(item.api_id ?? item.id) === String(vm.id) && item.uuid === vm.uuid); - vm.state = update?.state || 'unknown'; + const candidate = String(update?.state || update?.status || '').trim(); + const normalized = candidate.toLowerCase().replace(/[_\s-]+/g, ''); + const hasState = candidate !== '' && !['unknown', 'nostate'].includes(normalized); + if (hasState) vm.state = candidate; vm.live_state_available = update?.live_state_available === true; - if (!vm.live_state_available) unknown += 1; + vm.state_fresh = update?.state_fresh === true || update?.fresh === true + || update?.fresh === 1 || update?.fresh === '1' || vm.live_state_available; + vm.state_sync_status = String(update?.state_sync_status ?? update?.sync_status + ?? (vm.live_state_available ? 'fresh' : vm.state_sync_status || 'pending')).toLowerCase(); + if (!['', 'fresh'].includes(vm.state_sync_status)) syncing += 1; const rowNode = Array.from(byId('instances-body').rows).find(node => node.dataset.instanceId === String(vm.id)); const badge = rowNode?.querySelector('[data-power-status]'); if (badge) paintPower(badge, vm); } state.statusFailures = 0; - byId('instances-live-status').textContent = `电源状态更新于 ${new Date().toLocaleTimeString('zh-CN')}${unknown ? ` · ${unknown} 台状态未知(宿主机暂不可达或未获取)` : ''}`; + byId('instances-live-status').textContent = `电源状态更新于 ${new Date().toLocaleTimeString('zh-CN')}${syncing ? ` · ${syncing} 台状态同步中` : ''}`; } catch (error) { if (controller.signal.aborted && controller.signal.reason !== 'timeout') return; if (serial !== state.serial) return; state.statusFailures = Math.min(2, state.statusFailures + 1); - for (const vm of state.items) { vm.state = 'unknown'; vm.live_state_available = false; } - root.querySelectorAll('[data-power-status]').forEach(node => paintPower(node, { state: 'unknown' })); - byId('instances-live-status').textContent = `${error.message} 列表已保留,电源状态暂未知。`; + for (const vm of state.items) { + vm.live_state_available = false; + vm.state_sync_status = 'offline'; + } + state.items.forEach(vm => { + const rowNode = Array.from(byId('instances-body').rows) + .find(node => node.dataset.instanceId === String(vm.id)); + const badge = rowNode?.querySelector('[data-power-status]'); + if (badge) paintPower(badge, vm); + }); + byId('instances-live-status').textContent = `${error.message} 列表已保留,电源状态暂时无法同步。`; } finally { if (state.statusRequest === controller) { state.statusRequest = null; scheduleStatuses(); } } diff --git a/web/public/cloud/instances.js b/web/public/cloud/instances.js index 8162b73a..a0bb0aa8 100644 --- a/web/public/cloud/instances.js +++ b/web/public/cloud/instances.js @@ -1,4 +1,4 @@ -import { toast } from './toast.js?v=2026100102'; +import { toast } from './toast.js?v=2026100206'; // The first twenty instances are rendered by PHP. This lightweight module only // handles local filtering, pagination and batched live-state refreshes. @@ -68,6 +68,8 @@ function firstText(...values) { function normalizeVm(vm) { const source = vm && typeof vm === 'object' ? vm : {}; + const fresh = source.state_fresh === true || source.fresh === true + || source.fresh === 1 || source.fresh === '1' || source.fresh === 'true'; return { ...source, uuid: String(source.uuid || source.id || ''), @@ -75,6 +77,11 @@ function normalizeVm(vm) { vcpu: source.vcpu ?? source.cpu_cores, memory_mib: source.memory_mib ?? source.ram_mib, public_ip_cidr: source.public_ip_cidr || source.public_ip || '', + state_fresh: fresh, + state_observed_at: source.state_observed_at ?? source.last_success_at ?? null, + state_sync_status: String(source.state_sync_status ?? source.sync_status + ?? (fresh ? 'fresh' : 'pending')).toLowerCase(), + state_label_hint: firstText(source.state_label_hint), }; } @@ -90,9 +97,52 @@ function stateLabel(value) { const labels = { running: '运行中', blocked: '运行中', shutoff: '已关机', stopped: '已关机', shutdown: '关机中', crashed: '已崩溃', paused: '已暂停', pmsuspended: '已挂起', - nostate: '未知', unknown: '未知', + nostate: '等待宿主机同步', unknown: '等待宿主机同步', + }; + return labels[normalized] || '等待宿主机同步'; +} + +function runtimeStateReady(vm) { + return hasKnownState(vm) + && (String(vm?.state_sync_status || '').toLowerCase() === '' + || String(vm?.state_sync_status || '').toLowerCase() === 'fresh'); +} + +function hasKnownState(vm) { + const state = String(vm?.state || '').toLowerCase(); + const sync = String(vm?.state_sync_status || '').toLowerCase(); + if (['pending', 'never'].includes(sync)) return false; + return state !== '' && state !== 'unknown' && state !== 'nostate' + && !['unavailable', 'offline', 'host_offline', 'host_unreachable'].includes(sync); +} + +function stateCategoryForVm(vm) { + const sync = String(vm?.state_sync_status || '').toLowerCase(); + if (['unavailable', 'offline', 'host_offline', 'host_unreachable'].includes(sync)) { + return 'other'; + } + if (!hasKnownState(vm)) return 'other'; + return stateCategory(vm?.state); +} + +function stateDisplay(vm) { + const sync = String(vm?.state_sync_status || '').toLowerCase(); + if (['unavailable', 'offline', 'host_offline', 'host_unreachable'].includes(sync)) { + return { label: '暂时无法同步', category: 'other', note: '', noteClass: 'offline' }; + } + if (!hasKnownState(vm)) { + const hint = firstText(vm?.state_label_hint, + ['creating', 'provisioning', 'deploying'].includes(String(vm?.instance_status || '')) + ? '部署中' : '等待宿主机同步'); + return { label: hint, category: 'other', note: '', noteClass: '' }; + } + const category = stateCategory(vm.state); + return { + label: stateLabel(vm.state), + category, + note: runtimeStateReady(vm) ? '' : '状态同步中', + noteClass: runtimeStateReady(vm) ? '' : 'stale', }; - return labels[normalized] || String(value || '未知'); } function vmRegion(vm) { @@ -123,8 +173,8 @@ function diskLabel(vm) { function normalizeStats(value, vms = []) { const source = value && typeof value === 'object' ? value : {}; - const pageRunning = vms.filter(vm => stateCategory(vm.state) === 'running').length; - const pageStopped = vms.filter(vm => stateCategory(vm.state) === 'stopped').length; + const pageRunning = vms.filter(vm => stateCategoryForVm(vm) === 'running').length; + const pageStopped = vms.filter(vm => stateCategoryForVm(vm) === 'stopped').length; const number = (candidate, fallback) => { const parsed = Number(candidate); return Number.isFinite(parsed) && parsed >= 0 ? Math.trunc(parsed) : fallback; @@ -156,8 +206,11 @@ function normalizePagination(value, fallbackPage = 1, fallbackTotal = 0) { function renderStats() { if (byId('stat-total')) byId('stat-total').textContent = String(state.stats.total); - const liveValue = value => state.pagination.total === 0 || state.liveStatsReady - ? String(value) : '—'; + // The PHP bootstrap and the cache-only status endpoint already provide the + // last known counts. Render them immediately, including while a host is + // being re-synchronised; a stale row is explicitly marked in its status + // cell and must not make the whole summary look like it is still loading. + const liveValue = value => String(value); if (byId('stat-running')) byId('stat-running').textContent = liveValue(state.stats.running); if (byId('stat-stopped')) byId('stat-stopped').textContent = liveValue(state.stats.stopped); } @@ -212,7 +265,7 @@ function matchingVms() { const textMatches = !query || [vm.name, vm.uuid, vm.public_ip_cidr, vm.public_ip, vm.region, vm.location, vm.os_name, vm.install_iso] .some(value => String(value || '').toLocaleLowerCase().includes(query)); - const statusMatches = filter === 'all' || stateCategory(vm.state) === filter; + const statusMatches = filter === 'all' || stateCategoryForVm(vm) === filter; return textMatches && statusMatches; }); } @@ -271,9 +324,13 @@ function renderVms(connectionError = false) { identity.append(identityText); makeCell(row, '实例').append(identity); - const category = stateCategory(vm.state); - const status = makeElement('span', `state-pill ${category} vm-state-pill`, stateLabel(vm.state)); - makeCell(row, '状态').append(status); + const display = stateDisplay(vm); + const status = makeElement('span', `state-pill ${display.category} vm-state-pill`, display.label); + const statusCell = makeCell(row, '状态'); + statusCell.append(status); + if (display.note) { + statusCell.append(makeElement('small', `vm-state-sync ${display.noteClass}`, display.note)); + } makeCell(row, '地区').append(makeElement('span', 'vm-metadata', vmRegion(vm))); const system = vmSystemInfo(vm); makeCell(row, system.label).append(makeElement('span', 'vm-metadata', system.value)); @@ -422,11 +479,28 @@ function applyStatuses(payload) { let statusFilterChanged = false; for (const update of updates) { const vm = state.vms.find(item => vmMatchesUpdate(item, update)); - if (!vm || !firstText(update.state, update.status)) continue; - const previousCategory = stateCategory(vm.state); - const nextState = firstText(update.state, update.status); - const nextCategory = stateCategory(nextState); - Object.assign(vm, update, { state: nextState }); + if (!vm) continue; + const candidateState = firstText(update.state, update.status); + const normalizedState = candidateState.toLowerCase().replace(/[_\s-]+/g, ''); + const hasState = candidateState !== '' && normalizedState !== 'unknown' + && normalizedState !== 'nostate'; + const previousCategory = stateCategoryForVm(vm); + const liveState = update?.live_state_available === true; + const nextState = hasState ? candidateState : vm.state; + const nextSync = firstText(update.state_sync_status, update.sync_status, + liveState ? 'fresh' : vm.state_sync_status || 'pending').toLowerCase(); + const nextFresh = update.state_fresh === true || update.fresh === true + || update.fresh === 1 || update.fresh === '1' || liveState; + Object.assign(vm, update, { + state: nextState, + state_fresh: nextFresh, + state_sync_status: nextSync, + state_observed_at: update.state_observed_at ?? update.last_success_at + ?? (liveState ? Math.floor(Date.now() / 1000) : vm.state_observed_at), + state_label_hint: Object.prototype.hasOwnProperty.call(update, 'state_label_hint') + ? firstText(update.state_label_hint) : vm.state_label_hint, + }); + const nextCategory = stateCategoryForVm(vm); if (previousCategory !== nextCategory) { if (previousCategory === 'running') state.stats.running = Math.max(0, state.stats.running - 1); if (previousCategory === 'stopped') state.stats.stopped = Math.max(0, state.stats.stopped - 1); @@ -438,15 +512,27 @@ function applyStatuses(payload) { .find(item => item.dataset.vmId === vm.api_id || item.dataset.vmUuid === vm.uuid); const pill = row?.querySelector('.vm-state-pill'); if (pill) { - pill.className = `state-pill ${nextCategory} vm-state-pill`; - pill.textContent = stateLabel(nextState); + const display = stateDisplay(vm); + pill.className = `state-pill ${display.category} vm-state-pill`; + pill.textContent = display.label; + const statusCell = pill.parentElement; + let note = statusCell?.querySelector('.vm-state-sync'); + if (display.note) { + if (!note) { + note = makeElement('small', 'vm-state-sync'); + statusCell?.append(note); + } + note.className = `vm-state-sync ${display.noteClass}`; + note.textContent = display.note; + } else if (note) { + note.remove(); + } } } if (payload?.stats && typeof payload.stats === 'object') { state.stats = normalizeStats(payload.stats, state.vms); } - state.liveStatsReady = state.vms.length === 0 - || state.vms.every(vm => vm.live_state_available === true); + state.liveStatsReady = state.vms.length === 0 || state.vms.every(runtimeStateReady); renderStats(); if (statusFilterChanged && (byId('state-filter')?.value || 'all') !== 'all') renderVms(); } @@ -490,6 +576,7 @@ async function loadPage(page = state.pagination.page, updateUrl = true) { state.vms = (Array.isArray(payload?.vms) ? payload.vms : []).map(normalizeVm); state.pagination = normalizePagination(payload?.pagination, requestedPage, state.vms.length); state.stats = normalizeStats(payload?.stats, state.vms); + state.liveStatsReady = state.vms.length === 0 || state.vms.every(runtimeStateReady); state.pollFailures = 0; renderStats(); renderVms(); @@ -518,7 +605,7 @@ function hydrate() { state.vms = payload.vms.map(normalizeVm); state.pagination = normalizePagination(payload.pagination, 1, state.vms.length); state.stats = normalizeStats(payload.stats, state.vms); - state.liveStatsReady = state.vms.length === 0; + state.liveStatsReady = state.vms.length === 0 || state.vms.every(runtimeStateReady); state.user = payload.user && typeof payload.user === 'object' ? payload.user : {}; renderStats(); renderPagination(); diff --git a/web/public/cloud/styles.css b/web/public/cloud/styles.css index 8b3eecd3..95a11389 100644 --- a/web/public/cloud/styles.css +++ b/web/public/cloud/styles.css @@ -271,3 +271,4 @@ body.vu-body .ssh-key-picker-manage:hover { .plan-option>:nth-child(6)::before{content:"价格"} } @media(max-width:510px){.deployment-step{padding-bottom:23px;margin-bottom:22px}.deployment-step-heading{margin-bottom:16px}.region-picker{grid-template-columns:1fr}.region-option{min-height:66px}.plan-option{grid-template-columns:repeat(4,minmax(0,1fr));grid-template-rows:auto auto;gap:12px 8px;min-height:108px;padding:15px 14px 14px 16px}.plan-option::after{content:"";position:absolute;top:13px;right:14px;display:grid;place-items:center;width:22px;height:22px;border:1.5px solid #bdccd4;border-radius:50%;background:#fff;color:transparent;font-size:12px;font-weight:900}.plan-option[aria-pressed="true"]{box-shadow:none}.plan-option[aria-pressed="true"]::after{content:"✓";top:13px;right:14px;width:22px;height:22px;border:1.5px solid #18a6b0;background:#18a6b0;color:#fff}.plan-option-name{grid-column:1/3;grid-row:1;align-self:center;font-size:14px}.plan-option-price{display:block;grid-column:3/5;grid-row:1;align-self:center;justify-self:end;padding-right:30px;font-size:14px;white-space:nowrap}.plan-option-price::before{display:none;content:none}.plan-option-value{display:grid;grid-row:2;gap:4px;white-space:normal}.plan-option-value::before{color:#718599;font-size:9px;font-weight:800;white-space:nowrap}.plan-option>:nth-child(2){grid-column:1}.plan-option>:nth-child(3){grid-column:2}.plan-option>:nth-child(4){grid-column:3}.plan-option>:nth-child(5){grid-column:4}.template-family-card{height:140px;min-height:140px;border:1px solid #dce7ec;border-radius:12px;background:#fff;overflow:hidden;box-shadow:none}.template-family-summary{height:100%;min-height:100%;border:0;border-radius:inherit;background:transparent}.template-family-card[open]{z-index:70;border-color:#a8dadd;box-shadow:0 12px 28px rgba(30,66,82,.14)}.template-family-card.has-selection{border-color:#2eabb4;background:#f5fbfb}.template-versions{position:absolute;inset:0;z-index:80;width:auto;height:auto;max-height:none;margin:0;padding:6px 8px 40px;overflow-y:auto;border:0;border-radius:inherit;background:#fbfdfd;box-shadow:none;overscroll-behavior:contain;-webkit-overflow-scrolling:touch}.template-family-card[open] .template-family-footer{position:absolute;right:7px;bottom:7px;z-index:90;width:32px;min-height:32px;border-radius:999px;background:#eaf8f8}.template-family-card[open] .template-family-selection{display:none}.user-iso-row{align-items:flex-start;flex-direction:column}.user-iso-row .row-button{width:100%}} +.vm-state-sync{display:block;margin-top:4px;color:#8b9aaa;font-size:10px;line-height:1.35;white-space:nowrap}.vm-state-sync.stale{color:#b98129}.vm-state-sync.offline{color:#bd5b63} diff --git a/web/resources/views/venix/cloud/pages/instances.tpl b/web/resources/views/venix/cloud/pages/instances.tpl index 5c5322eb..e480b63d 100644 --- a/web/resources/views/venix/cloud/pages/instances.tpl +++ b/web/resources/views/venix/cloud/pages/instances.tpl @@ -7,8 +7,8 @@
实例总数
{$cloud_vm_stats.total|default:0|escape:"html"}{$cloud_vm_scope_label|default:'我的实例'|escape:"html"}
-
当前页运行中
{if $cloud_vm_pagination.total > 0}—{else}0{/if} 实时电源状态
-
当前页已关机
{if $cloud_vm_pagination.total > 0}—{else}0{/if}实时电源状态
+
当前页运行中
{$cloud_vm_stats.running|default:0|escape:"html"} 最近同步状态
+
当前页已关机
{$cloud_vm_stats.stopped|default:0|escape:"html"}最近同步状态
@@ -19,7 +19,7 @@ {foreach $cloud_vm_rows as $vm}
{$vm.name|escape:"html"}{$vm.uuid|escape:"html"}{if $vm.uuid != ''}{/if}
- {$vm.state_label|escape:"html"} + {$vm.state_label|escape:"html"}{if $vm.state_sync_label|default:'' != ''}{$vm.state_sync_label|escape:"html"}{/if} {if $vm.has_ip}{/if} diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index eb453632..8405ddc8 100644 --- a/web/resources/views/venix/layout_assets.tpl +++ b/web/resources/views/venix/layout_assets.tpl @@ -1,2 +1,2 @@ {* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *} -{assign var=venix_asset_version value='2026100205' scope='global'} +{assign var=venix_asset_version value='2026100206' scope='global'} diff --git a/web/sql/cloud.sql b/web/sql/cloud.sql index c88007ae..2d591d7f 100644 --- a/web/sql/cloud.sql +++ b/web/sql/cloud.sql @@ -95,6 +95,47 @@ CREATE TABLE IF NOT EXISTS cloud_instances ( KEY cloud_instance_owner (user_id, status), KEY cloud_instance_lifecycle (service_status, expires_at, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +-- Last-known engine power state for fast instance-list first paint. This is +-- an observation cache only; cloud_instances remains the lifecycle source of +-- truth and the engine remains authoritative for the current power state. +CREATE TABLE IF NOT EXISTS cloud_instance_runtime ( + instance_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, + host_id BIGINT UNSIGNED NOT NULL, + uuid CHAR(36) NOT NULL, + power_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown', + sync_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'never', + last_success_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + last_attempt_at BIGINT UNSIGNED NOT NULL DEFAULT 0, + revision BIGINT UNSIGNED NOT NULL DEFAULT 0, + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + created_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_instance_runtime_identity (host_id, uuid), + KEY cloud_instance_runtime_sync (sync_state, last_success_at), + KEY cloud_instance_runtime_host (host_id, updated_at) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + power_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'unknown' AFTER uuid; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + sync_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'never' AFTER power_state; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + last_success_at BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER sync_state; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + last_attempt_at BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER last_success_at; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + revision BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER last_attempt_at; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '' AFTER revision; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + created_at BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER last_error_code; +ALTER TABLE cloud_instance_runtime ADD COLUMN IF NOT EXISTS + updated_at BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER created_at; +ALTER TABLE cloud_instance_runtime ADD UNIQUE INDEX IF NOT EXISTS + cloud_instance_runtime_identity (host_id, uuid); +ALTER TABLE cloud_instance_runtime ADD INDEX IF NOT EXISTS + cloud_instance_runtime_sync (sync_state, last_success_at); +ALTER TABLE cloud_instance_runtime ADD INDEX IF NOT EXISTS + cloud_instance_runtime_host (host_id, updated_at); CREATE TABLE IF NOT EXISTS cloud_orders ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, diff --git a/web/tests/CloudInstancePageServiceTest.php b/web/tests/CloudInstancePageServiceTest.php index d82be091..19b97ff9 100644 --- a/web/tests/CloudInstancePageServiceTest.php +++ b/web/tests/CloudInstancePageServiceTest.php @@ -61,6 +61,19 @@ namespace { $table->integer('ends_at'); $table->string('status'); }); + $schema->create('cloud_instance_runtime', static function (Blueprint $table): void { + $table->integer('instance_id')->primary(); + $table->integer('host_id'); + $table->string('uuid'); + $table->string('power_state')->default('unknown'); + $table->string('sync_state')->default('never'); + $table->integer('last_success_at')->default(0); + $table->integer('last_attempt_at')->default(0); + $table->integer('revision')->default(0); + $table->string('last_error_code')->default(''); + $table->integer('created_at')->default(0); + $table->integer('updated_at')->default(0); + }); Capsule::table('cloud_hosts')->insert([ 'id' => 7, 'region' => 'HongKong', 'country_code' => 'HK', @@ -119,8 +132,8 @@ namespace { && $page['pagination']['page_count'] === 1 && $page['vms'][0]['name'] === 'owner-vm', 'Ordinary user instance first page crossed the owner boundary.'); - $assert($pageQueryCount <= 3, - 'Instance first page regressed beyond count, rows and host batch queries.'); + $assert($pageQueryCount <= 4, + 'Instance first page regressed beyond count, rows, host and runtime batch queries.'); $assert($page['vms'][0]['state'] === 'unknown', 'Persisted specification power state was trusted before a live UUID check.'); $assert(count($page['vms'][0]['disks']) === 1