|
|
|
|
@ -79,11 +79,21 @@ final class CloudTrafficService
|
|
|
|
|
if ($attempted >= $limit) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$scope = self::hostScope((int) $hostId);
|
|
|
|
|
try {
|
|
|
|
|
$totals = $log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('host_scope', $scope)->whereIn('vm_uuid',
|
|
|
|
|
array_column($owned, 'uuid'))->get()->keyBy('vm_uuid');
|
|
|
|
|
$storedUuids = array_map(
|
|
|
|
|
static fn (array $item): string =>
|
|
|
|
|
CloudLogIdentity::uuid($item['uuid']),
|
|
|
|
|
$owned
|
|
|
|
|
);
|
|
|
|
|
$totalRows = $log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('host_id', (int) $hostId)
|
|
|
|
|
->whereIn('vm_uuid', $storedUuids)->get();
|
|
|
|
|
$totals = [];
|
|
|
|
|
foreach ($totalRows as $row) {
|
|
|
|
|
$totals[CloudLogIdentity::storedUuidKey(
|
|
|
|
|
(string) $row->vm_uuid
|
|
|
|
|
)] = $row;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $error) {
|
|
|
|
|
$result['errors'][] = 'traffic_collection_failed';
|
|
|
|
|
continue;
|
|
|
|
|
@ -98,7 +108,9 @@ final class CloudTrafficService
|
|
|
|
|
$result['skipped']++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$previous = $totals->get($item['uuid']);
|
|
|
|
|
$previous = $totals[
|
|
|
|
|
CloudLogIdentity::uuidKey($item['uuid'])
|
|
|
|
|
] ?? null;
|
|
|
|
|
$lastObservedAt = $previous === null ? 0
|
|
|
|
|
: (int) ($previous->last_observed_at ?? 0);
|
|
|
|
|
if ($lastObservedAt <= $now
|
|
|
|
|
@ -191,9 +203,6 @@ final class CloudTrafficService
|
|
|
|
|
return CloudMetricHistoryStore::series(
|
|
|
|
|
$log,
|
|
|
|
|
(int) $instance->id,
|
|
|
|
|
self::hostScope((int) $instance->host_id),
|
|
|
|
|
$uuid,
|
|
|
|
|
(string) (int) $instance->host_id,
|
|
|
|
|
(int) $instance->user_id,
|
|
|
|
|
$range,
|
|
|
|
|
time()
|
|
|
|
|
@ -224,12 +233,29 @@ final class CloudTrafficService
|
|
|
|
|
'rx_bytes' => self::counter($metrics, 'rx_bytes'),
|
|
|
|
|
'tx_bytes' => self::counter($metrics, 'tx_bytes'),
|
|
|
|
|
];
|
|
|
|
|
$scope = self::hostScope((int) $instance->host_id);
|
|
|
|
|
$storedUuid = CloudLogIdentity::uuid($uuid);
|
|
|
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
|
|
|
$spec = is_array($spec) && !array_is_list($spec) ? $spec : [];
|
|
|
|
|
$generation = $spec['traffic_counter_generation'] ?? null;
|
|
|
|
|
$generation = is_array($generation) && !array_is_list($generation)
|
|
|
|
|
&& (int) ($generation['host_id'] ?? 0)
|
|
|
|
|
=== (int) $instance->host_id
|
|
|
|
|
&& hash_equals(
|
|
|
|
|
strtolower((string) ($generation['uuid'] ?? '')),
|
|
|
|
|
$uuid
|
|
|
|
|
)
|
|
|
|
|
&& is_int($generation['download_base_bytes'] ?? null)
|
|
|
|
|
&& $generation['download_base_bytes'] >= 0
|
|
|
|
|
&& is_int($generation['upload_base_bytes'] ?? null)
|
|
|
|
|
&& $generation['upload_base_bytes'] >= 0
|
|
|
|
|
? $generation : null;
|
|
|
|
|
$log->transaction(static function () use (
|
|
|
|
|
$log, $instance, $uuid, $collectedAt, $current, $scope
|
|
|
|
|
$log, $instance, $storedUuid, $collectedAt, $current, $spec,
|
|
|
|
|
$generation
|
|
|
|
|
): void {
|
|
|
|
|
$previous = $log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('host_scope', $scope)->where('vm_uuid', $uuid)
|
|
|
|
|
->where('host_id', (int) $instance->host_id)
|
|
|
|
|
->where('vm_uuid', $storedUuid)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($previous !== null) {
|
|
|
|
|
if (($previous->user_id !== null
|
|
|
|
|
@ -239,6 +265,15 @@ final class CloudTrafficService
|
|
|
|
|
!== (string) (int) $instance->host_id)) {
|
|
|
|
|
throw new CloudError('性能采样归属与实例不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
} elseif ($generation === null
|
|
|
|
|
&& $log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('vm_uuid', $storedUuid)
|
|
|
|
|
->where('host_id', '<>', (int) $instance->host_id)
|
|
|
|
|
->lockForUpdate()->first(['host_id']) !== null) {
|
|
|
|
|
// A cross-host UUID collision without a frozen migration
|
|
|
|
|
// generation is ambiguous. Do not create a second billing
|
|
|
|
|
// counter identity from potentially foreign data.
|
|
|
|
|
throw new CloudError('性能采样归属与实例不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$baseline = $previous === null ? [] : json_decode(
|
|
|
|
|
@ -255,8 +290,6 @@ final class CloudTrafficService
|
|
|
|
|
// timestamp is not suitable for chart ordering or rate math.
|
|
|
|
|
$elapsed = $orderedSample && $previous !== null
|
|
|
|
|
? $collectedAt - $previousObservedAt : 0;
|
|
|
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
|
|
|
$spec = is_array($spec) && !array_is_list($spec) ? $spec : [];
|
|
|
|
|
$vcpu = filter_var($spec['vcpu'] ?? ($spec['cpu_cores'] ?? null),
|
|
|
|
|
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 64]]);
|
|
|
|
|
$memoryTotal = filter_var($spec['memory_mib'] ?? null,
|
|
|
|
|
@ -299,19 +332,6 @@ final class CloudTrafficService
|
|
|
|
|
// first PHP observation they are already the authoritative lifetime
|
|
|
|
|
// total (including after a panel log-database restore), while there
|
|
|
|
|
// is deliberately no interval from which to derive a rate.
|
|
|
|
|
$generation = $spec['traffic_counter_generation'] ?? null;
|
|
|
|
|
$generation = is_array($generation) && !array_is_list($generation)
|
|
|
|
|
&& (int) ($generation['host_id'] ?? 0)
|
|
|
|
|
=== (int) $instance->host_id
|
|
|
|
|
&& hash_equals(
|
|
|
|
|
strtolower((string) ($generation['uuid'] ?? '')),
|
|
|
|
|
$uuid
|
|
|
|
|
)
|
|
|
|
|
&& is_int($generation['download_base_bytes'] ?? null)
|
|
|
|
|
&& $generation['download_base_bytes'] >= 0
|
|
|
|
|
&& is_int($generation['upload_base_bytes'] ?? null)
|
|
|
|
|
&& $generation['upload_base_bytes'] >= 0
|
|
|
|
|
? $generation : null;
|
|
|
|
|
$baseRx = $previous === null && $generation !== null
|
|
|
|
|
? $generation['download_base_bytes'] : 0;
|
|
|
|
|
$baseTx = $previous === null && $generation !== null
|
|
|
|
|
@ -340,9 +360,6 @@ final class CloudTrafficService
|
|
|
|
|
}
|
|
|
|
|
$sample = [
|
|
|
|
|
'instance_id' => (int) $instance->id,
|
|
|
|
|
'host_scope' => $scope,
|
|
|
|
|
'vm_uuid' => $uuid,
|
|
|
|
|
'host_id' => (string) (int) $instance->host_id,
|
|
|
|
|
'user_id' => (int) $instance->user_id,
|
|
|
|
|
'sampled_at' => $collectedAt,
|
|
|
|
|
'cpu_percent' => $cpu,
|
|
|
|
|
@ -368,10 +385,9 @@ final class CloudTrafficService
|
|
|
|
|
CloudMetricHistoryStore::recordRollups($log, $sample);
|
|
|
|
|
}
|
|
|
|
|
$log->table('cloud_vm_traffic_checkpoints')->insertOrIgnore([
|
|
|
|
|
'host_scope' => $scope,
|
|
|
|
|
'vm_uuid' => $uuid,
|
|
|
|
|
'host_id' => (int) $instance->host_id,
|
|
|
|
|
'vm_uuid' => $storedUuid,
|
|
|
|
|
'checkpoint_hour' => intdiv($collectedAt, 3600) * 3600,
|
|
|
|
|
'host_id' => (string) (int) $instance->host_id,
|
|
|
|
|
'user_id' => (int) $instance->user_id,
|
|
|
|
|
'first_observed_at' => $collectedAt,
|
|
|
|
|
'download_rx_bytes' => $panelRx,
|
|
|
|
|
@ -389,11 +405,11 @@ final class CloudTrafficService
|
|
|
|
|
],
|
|
|
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
|
|
|
|
|
$values = [
|
|
|
|
|
'host_id' => (string) (int) $instance->host_id,
|
|
|
|
|
'user_id' => (int) $instance->user_id,
|
|
|
|
|
'last_observed_at' => max($previousObservedAt, $collectedAt),
|
|
|
|
|
'network_signature' => hash('sha256',
|
|
|
|
|
'virt-agent-durable-traffic/v1'),
|
|
|
|
|
'network_signature' => CloudLogIdentity::sha256(
|
|
|
|
|
'virt-agent-durable-traffic/v1'
|
|
|
|
|
),
|
|
|
|
|
'network_baseline_json' => $encoded,
|
|
|
|
|
'download_rx_bytes' => $panelRx,
|
|
|
|
|
'upload_tx_bytes' => $panelTx,
|
|
|
|
|
@ -401,11 +417,13 @@ final class CloudTrafficService
|
|
|
|
|
];
|
|
|
|
|
if ($previous === null) {
|
|
|
|
|
$log->table('cloud_vm_traffic_totals')->insert($values + [
|
|
|
|
|
'host_scope' => $scope, 'vm_uuid' => $uuid,
|
|
|
|
|
'host_id' => (int) $instance->host_id,
|
|
|
|
|
'vm_uuid' => $storedUuid,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('host_scope', $scope)->where('vm_uuid', $uuid)
|
|
|
|
|
->where('host_id', (int) $instance->host_id)
|
|
|
|
|
->where('vm_uuid', $storedUuid)
|
|
|
|
|
->update($values);
|
|
|
|
|
}
|
|
|
|
|
}, 3);
|
|
|
|
|
@ -450,11 +468,6 @@ final class CloudTrafficService
|
|
|
|
|
return $timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function hostScope(int $hostId): string
|
|
|
|
|
{
|
|
|
|
|
return hash('sha256', 'virt-cloud/host/' . $hostId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function syncBatch($instances): int
|
|
|
|
|
{
|
|
|
|
|
if ($instances->isEmpty()) {
|
|
|
|
|
@ -472,16 +485,24 @@ final class CloudTrafficService
|
|
|
|
|
'cloud_vm_daily_traffic'
|
|
|
|
|
);
|
|
|
|
|
foreach ($groups as $hostId => $owned) {
|
|
|
|
|
$scope = self::hostScope($hostId);
|
|
|
|
|
$uuids = array_map(static fn ($instance) => (string) $instance->uuid, $owned);
|
|
|
|
|
$uuids = array_map(
|
|
|
|
|
static fn ($instance): string => CloudLogIdentity::uuid(
|
|
|
|
|
(string) $instance->uuid
|
|
|
|
|
),
|
|
|
|
|
$owned
|
|
|
|
|
);
|
|
|
|
|
$rows = $log->table('cloud_vm_traffic_totals')
|
|
|
|
|
->where('host_scope', $scope)->whereIn('vm_uuid', $uuids)->get();
|
|
|
|
|
->where('host_id', $hostId)->whereIn('vm_uuid', $uuids)->get();
|
|
|
|
|
$byUuid = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$byUuid[(string) $row->vm_uuid] = $row;
|
|
|
|
|
$byUuid[CloudLogIdentity::storedUuidKey(
|
|
|
|
|
(string) $row->vm_uuid
|
|
|
|
|
)] = $row;
|
|
|
|
|
}
|
|
|
|
|
foreach ($owned as $instance) {
|
|
|
|
|
$row = $byUuid[(string) $instance->uuid] ?? null;
|
|
|
|
|
$row = $byUuid[CloudLogIdentity::uuidKey(
|
|
|
|
|
(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
|
|
|
|
|
@ -541,12 +562,12 @@ final class CloudTrafficService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$log = User::getConnectionResolver()->connection('log');
|
|
|
|
|
$scope = self::hostScope((int) $instance->host_id);
|
|
|
|
|
$storedUuid = CloudLogIdentity::uuid((string) $instance->uuid);
|
|
|
|
|
if ($log->getSchemaBuilder()->hasTable('cloud_vm_traffic_checkpoints')) {
|
|
|
|
|
$checkpoint = $log->table('cloud_vm_traffic_checkpoints')
|
|
|
|
|
->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid)
|
|
|
|
|
->where('host_id', (int) $instance->host_id)
|
|
|
|
|
->where('vm_uuid', $storedUuid)
|
|
|
|
|
->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([
|
|
|
|
|
@ -570,9 +591,8 @@ final class CloudTrafficService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$rows = $log->table('cloud_vm_metric_samples')
|
|
|
|
|
->where('host_scope', $scope)->where('vm_uuid', (string) $instance->uuid)
|
|
|
|
|
->where('instance_id', (int) $instance->id)
|
|
|
|
|
->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([
|
|
|
|
|
|