', 'deleted') ->where('service_status', '<>', 'exempt') ->select(['id', 'host_id', 'user_id', 'uuid']) ->chunkById($batchSize, function ($instances) use (&$updated) { $updated += self::syncBatch($instances); }); return $updated; } private static function syncBatch($instances): int { if ($instances->isEmpty()) { return 0; } $groups = []; foreach ($instances as $instance) { $groups[(int) $instance->host_id][] = $instance; } $updated = 0; $log = User::getConnectionResolver()->connection('log'); foreach ($groups as $hostId => $owned) { $scope = hash('sha256', 'virt-cloud/host/' . $hostId); $uuids = array_map(static fn ($instance) => (string) $instance->uuid, $owned); $rows = $log->table('engine_vm_traffic_totals') ->where('host_scope', $scope)->whereIn('vm_uuid', $uuids)->get(); $byUuid = []; foreach ($rows as $row) { $byUuid[(string) $row->vm_uuid] = $row; } foreach ($owned as $instance) { $row = $byUuid[(string) $instance->uuid] ?? null; // An absent or changed owner is unknown usage, not grounds to // stop another tenant's VM. The next successful sample repairs it. if ($row === null || $row->user_id === null || (int) $row->user_id !== (int) $instance->user_id || ($row->host_id !== null && (string) $row->host_id !== (string) $hostId)) { continue; } $download = max(0, (int) $row->download_rx_bytes); $upload = max(0, (int) $row->upload_tx_bytes); $observed = max(0, (int) ($row->last_observed_at ?? $row->updated_at ?? 0)); $changed = CloudInstance::where('id', $instance->id) ->where('host_id', $hostId)->where('user_id', $instance->user_id) ->where('uuid', $instance->uuid) ->where('traffic_synced_at', '<=', $observed) // Engine totals are durable monotonic counters. A rebuilt, // restored or corrupt log database must never roll paid // usage backwards and accidentally restore a blocked NIC. ->where('lifetime_download_bytes', '<=', $download) ->where('lifetime_upload_bytes', '<=', $upload) ->update(['lifetime_download_bytes' => $download, 'lifetime_upload_bytes' => $upload, 'traffic_synced_at' => $observed]); $updated += $changed; } } return $updated; } /** * Reconstruct a conservative period boundary from durable cumulative * checkpoints. Chart samples are intentionally retained for only 30 * days, while the engine keeps the first cumulative total in every hour * indefinitely. Using the first observation *after* the boundary excludes * the straddling interval and bounds uncharged traffic to less than one * checkpoint hour even after a long lifecycle-daemon outage. */ public static function periodBaseline(CloudInstance $instance, int $startsAt): ?array { if ((int) $instance->traffic_synced_at < $startsAt) { return null; } $log = User::getConnectionResolver()->connection('log'); $scope = hash('sha256', 'virt-cloud/host/' . (int) $instance->host_id); if ($log->getSchemaBuilder()->hasTable('engine_vm_traffic_checkpoints')) { $checkpoint = $log->table('engine_vm_traffic_checkpoints') ->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid) ->where('user_id', (int) $instance->user_id) ->where('host_id', (string) (int) $instance->host_id) ->where('first_observed_at', '>', $startsAt) ->where('first_observed_at', '<=', (int) $instance->traffic_synced_at) ->orderBy('first_observed_at')->first([ 'download_rx_bytes', 'upload_tx_bytes', ]); if ($checkpoint !== null) { $download = max(0, (int) $checkpoint->download_rx_bytes); $upload = max(0, (int) $checkpoint->upload_tx_bytes); if ($download <= (int) $instance->lifetime_download_bytes && $upload <= (int) $instance->lifetime_upload_bytes) { return ['download' => $download, 'upload' => $upload]; } return null; } } // Upgrade compatibility for a boundary still covered by the old // 30-day sample table. Refuse partial retained history: otherwise a // long outage would subtract only the tail and silently undercount. if (!$log->getSchemaBuilder()->hasTable('engine_vm_metric_samples')) { return null; } $rows = $log->table('engine_vm_metric_samples') ->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid) ->where('user_id', (int) $instance->user_id) ->where('host_id', (string) (int) $instance->host_id) ->where('sampled_at', '>', $startsAt) ->where('sampled_at', '<=', (int) $instance->traffic_synced_at) ->orderBy('sampled_at')->get([ 'sampled_at', 'network_rx_bytes', 'network_tx_bytes', ]); $download = 0; $upload = 0; $skippedBoundaryInterval = false; foreach ($rows as $row) { if ($row->network_rx_bytes === null || $row->network_tx_bytes === null) { continue; } if (!$skippedBoundaryInterval) { // The legacy sample fallback is safe only when retention still // covers the boundary. Normal collection is every 60 seconds; // five minutes tolerates jitter without accepting a truncated // 30-day tail as complete history. if ((int) $row->sampled_at > $startsAt + 300) { return null; } $skippedBoundaryInterval = true; continue; } $download += max(0, (int) $row->network_rx_bytes); $upload += max(0, (int) $row->network_tx_bytes); } $lifetimeDownload = max(0, (int) $instance->lifetime_download_bytes); $lifetimeUpload = max(0, (int) $instance->lifetime_upload_bytes); if ($download > $lifetimeDownload || $upload > $lifetimeUpload) { return null; } return ['download' => $lifetimeDownload - $download, 'upload' => $lifetimeUpload - $upload]; } }