You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by nv...@apache.org on 2021/09/15 02:55:00 UTC

[cloudstack] branch main updated: VM has wrong network statistics with multiple nics in shared networks (#4741)

This is an automated email from the ASF dual-hosted git repository.

nvazquez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 3c2360c  VM has wrong network statistics with multiple nics in shared networks (#4741)
3c2360c is described below

commit 3c2360c9d28fab1f9092dd5f275231427716937d
Author: Rakesh <ra...@gmail.com>
AuthorDate: Wed Sep 15 04:54:34 2021 +0200

    VM has wrong network statistics with multiple nics in shared networks (#4741)
    
    If vm has multiple nics belonging to different shared networks then
    wrong statistics will be collected since network id is not considred
    as primary key. Make the change so that primary key contains network
    id so that traffic belonging to that corresponding network is shown
    
    If network id is not added to primary key then all the traffic of all
    shared networks will show up in one nic.
    
    Co-authored-by: Rakesh Venkatesh <ra...@apache.org>
---
 .../schema/src/main/java/com/cloud/usage/dao/UsageNetworkDaoImpl.java  | 2 +-
 engine/schema/src/main/resources/META-INF/db/schema-41520to41600.sql   | 3 +++
 usage/src/main/java/com/cloud/usage/UsageManagerImpl.java              | 2 +-
 usage/src/main/java/com/cloud/usage/parser/NetworkUsageParser.java     | 2 +-
 4 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworkDaoImpl.java b/engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworkDaoImpl.java
index 8922e1f..b0ff291 100644
--- a/engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworkDaoImpl.java
+++ b/engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworkDaoImpl.java
@@ -68,7 +68,7 @@ public class UsageNetworkDaoImpl extends GenericDaoBase<UsageNetworkVO, Long> im
                 long aggBytesSent = rs.getLong(9);
                 long eventTimeMillis = rs.getLong(10);
                 if (hostId != 0) {
-                    returnMap.put(zoneId + "-" + accountId + "-Host-" + hostId, new UsageNetworkVO(accountId, zoneId, hostId, hostType, networkId, bytesSent,
+                    returnMap.put(zoneId + "-" + accountId + "-Host-" + hostId + "-Network-" + networkId, new UsageNetworkVO(accountId, zoneId, hostId, hostType, networkId, bytesSent,
                         bytesReceived, aggBytesReceived, aggBytesSent, eventTimeMillis));
                 } else {
                     returnMap.put(zoneId + "-" + accountId, new UsageNetworkVO(accountId, zoneId, hostId, hostType, networkId, bytesSent, bytesReceived,
diff --git a/engine/schema/src/main/resources/META-INF/db/schema-41520to41600.sql b/engine/schema/src/main/resources/META-INF/db/schema-41520to41600.sql
index 0493c07..cd595ee 100644
--- a/engine/schema/src/main/resources/META-INF/db/schema-41520to41600.sql
+++ b/engine/schema/src/main/resources/META-INF/db/schema-41520to41600.sql
@@ -774,3 +774,6 @@ ALTER TABLE cloud.user_vm_deploy_as_is_details MODIFY value text NOT NULL;
 UPDATE cloud.user_vm_details SET value='' WHERE value IS NULL;
 ALTER TABLE cloud.user_vm_details MODIFY value varchar(5120) NOT NULL;
 
+ALTER TABLE cloud_usage.usage_network DROP PRIMARY KEY, ADD PRIMARY KEY (`account_id`,`zone_id`,`host_id`,`network_id`,`event_time_millis`);
+ALTER TABLE `cloud`.`user_statistics` DROP INDEX `account_id`, ADD UNIQUE KEY `account_id`  (`account_id`,`data_center_id`,`public_ip_address`,`device_id`,`device_type`, `network_id`);
+ALTER TABLE `cloud_usage`.`user_statistics` DROP INDEX `account_id`, ADD UNIQUE KEY `account_id`  (`account_id`,`data_center_id`,`public_ip_address`,`device_id`,`device_type`, `network_id`);
diff --git a/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java b/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
index 899f558..dff8b5a 100644
--- a/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
+++ b/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
@@ -669,7 +669,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
                     if (userStats != null) {
                         for (UserStatisticsVO userStat : userStats) {
                             if (userStat.getDeviceId() != null) {
-                                String hostKey = userStat.getDataCenterId() + "-" + userStat.getAccountId() + "-Host-" + userStat.getDeviceId();
+                                String hostKey = userStat.getDataCenterId() + "-" + userStat.getAccountId() + "-Host-" + userStat.getDeviceId() + "-Network-" + userStat.getNetworkId();
                                 UserStatisticsVO hostAggregatedStat = aggregatedStats.get(hostKey);
                                 if (hostAggregatedStat == null) {
                                     hostAggregatedStat =
diff --git a/usage/src/main/java/com/cloud/usage/parser/NetworkUsageParser.java b/usage/src/main/java/com/cloud/usage/parser/NetworkUsageParser.java
index a19f621..d849e6a 100644
--- a/usage/src/main/java/com/cloud/usage/parser/NetworkUsageParser.java
+++ b/usage/src/main/java/com/cloud/usage/parser/NetworkUsageParser.java
@@ -80,7 +80,7 @@ public class NetworkUsageParser {
             long zoneId = usageNetwork.getZoneId();
             String key = "" + zoneId;
             if (usageNetwork.getHostId() != 0) {
-                key += "-Host" + usageNetwork.getHostId();
+                key += "-Host" + usageNetwork.getHostId() + "-Network-" + usageNetwork.getNetworkId();
             }
             NetworkInfo networkInfo = networkUsageByZone.get(key);