From 7b5cbc7f664bb1f71eecfd1d94a19818df2027b6 Mon Sep 17 00:00:00 2001 From: kishan Date: Wed, 9 Feb 2011 18:38:01 +0530 Subject: [PATCH] bug 8465: track network usage per router in user_statistics table status 8465: resolved fixed --- core/src/com/cloud/user/UserStatisticsVO.java | 22 ++++++------- .../com/cloud/network/NetworkManagerImpl.java | 8 ----- .../VirtualNetworkApplianceManagerImpl.java | 23 ++++++++++++-- .../com/cloud/user/dao/UserStatisticsDao.java | 4 +-- .../cloud/user/dao/UserStatisticsDaoImpl.java | 31 ++++++++++--------- setup/db/create-schema.sql | 7 +++-- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/core/src/com/cloud/user/UserStatisticsVO.java b/core/src/com/cloud/user/UserStatisticsVO.java index 3d1e1fa04e4..49886c15803 100644 --- a/core/src/com/cloud/user/UserStatisticsVO.java +++ b/core/src/com/cloud/user/UserStatisticsVO.java @@ -42,11 +42,11 @@ public class UserStatisticsVO { @Column(name="public_ip_address") private String publicIpAddress; - @Column(name="host_id") - private Long hostId; + @Column(name="device_id") + private Long deviceId; - @Column(name="host_type") - private String hostType; + @Column(name="device_type") + private String deviceType; @Column(name="net_bytes_received") private long netBytesReceived; @@ -63,12 +63,12 @@ public class UserStatisticsVO { protected UserStatisticsVO() { } - public UserStatisticsVO(long accountId, long dcId, String publicIpAddress, Long hostId, String hostType) { + public UserStatisticsVO(long accountId, long dcId, String publicIpAddress, Long deviceId, String deviceType) { this.accountId = accountId; this.dataCenterId = dcId; this.publicIpAddress = publicIpAddress; - this.hostId = hostId; - this.hostType = hostType; + this.deviceId = deviceId; + this.deviceType = deviceType; this.netBytesReceived = 0; this.netBytesSent = 0; this.currentBytesReceived = 0; @@ -91,8 +91,8 @@ public class UserStatisticsVO { return publicIpAddress; } - public Long getHostId() { - return hostId; + public Long getDeviceId() { + return deviceId; } public long getCurrentBytesReceived() { @@ -127,7 +127,7 @@ public class UserStatisticsVO { this.netBytesSent = netBytesSent; } - public String getHostType() { - return hostType; + public String getDeviceType() { + return deviceType; } } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 02c56424bf4..89e2a4e616d 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -385,14 +385,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag ip = new PublicIp(sourceNat, _vlanDao.findById(sourceNat.getVlanId()), NetUtils.createSequenceBasedMacAddress(sourceNat.getMacAddress())); } - UserStatisticsVO stats = _userStatsDao.findBy(ownerId, dcId, null, null); - if (stats == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Creating statistics for the owner: " + ownerId); - } - stats = new UserStatisticsVO(ownerId, dcId, null, null, null); - _userStatsDao.persist(stats); - } txn.commit(); return ip; } finally { diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 78ca9bb8da2..6d811416d83 100644 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -434,7 +434,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian final Transaction txn = Transaction.currentTxn(); try { txn.start(); - final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), null, null); + final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), null, router.getId(), router.getType().toString()); if (userStats != null) { final RebootAnswer sa = (RebootAnswer) answer; final Long received = sa.getBytesReceived(); @@ -684,7 +684,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian continue; } txn.start(); - UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterId(), null, null); + UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterId(), null, router.getId(), router.getType().toString()); if (stats == null) { s_logger.warn("unable to find stats for account: " + router.getAccountId()); continue; @@ -790,6 +790,15 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); } + // Creating stats entry for router + UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + if (stats == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: "+router.getId()); + } + stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + _userStatsDao.persist(stats); + } return router; } @@ -849,6 +858,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (state != State.Starting && state != State.Running) { router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); } + // Creating stats entry for router + UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + if (stats == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: "+router.getId()); + } + stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + _userStatsDao.persist(stats); + } + return router; } diff --git a/server/src/com/cloud/user/dao/UserStatisticsDao.java b/server/src/com/cloud/user/dao/UserStatisticsDao.java index 606e11cbde6..d2054eec4c2 100644 --- a/server/src/com/cloud/user/dao/UserStatisticsDao.java +++ b/server/src/com/cloud/user/dao/UserStatisticsDao.java @@ -25,9 +25,9 @@ import com.cloud.user.UserStatisticsVO; import com.cloud.utils.db.GenericDao; public interface UserStatisticsDao extends GenericDao { - UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long hostId); + UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long deviceId, String deviceType); - UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId); + UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId, String deviceType); List listBy(long accountId); diff --git a/server/src/com/cloud/user/dao/UserStatisticsDaoImpl.java b/server/src/com/cloud/user/dao/UserStatisticsDaoImpl.java index 07021535891..647c85f36a6 100644 --- a/server/src/com/cloud/user/dao/UserStatisticsDaoImpl.java +++ b/server/src/com/cloud/user/dao/UserStatisticsDaoImpl.java @@ -39,11 +39,11 @@ import com.cloud.utils.db.Transaction; @Local(value={UserStatisticsDao.class}) public class UserStatisticsDaoImpl extends GenericDaoBase implements UserStatisticsDao { private static final Logger s_logger = Logger.getLogger(UserStatisticsDaoImpl.class); - private static final String ACTIVE_AND_RECENTLY_DELETED_SEARCH = "SELECT us.id, us.data_center_id, us.account_id, us.public_ip_address, us.host_id, us.host_type, us.net_bytes_received, us.net_bytes_sent, us.current_bytes_received, us.current_bytes_sent " + + private static final String ACTIVE_AND_RECENTLY_DELETED_SEARCH = "SELECT us.id, us.data_center_id, us.account_id, us.public_ip_address, us.device_id, us.device_type, us.net_bytes_received, us.net_bytes_sent, us.current_bytes_received, us.current_bytes_sent " + "FROM user_statistics us, account a " + "WHERE us.account_id = a.id AND (a.removed IS NULL OR a.removed >= ?) " + "ORDER BY us.id"; - private final SearchBuilder AccountDcIpHostSearch; + private final SearchBuilder AccountDcIpDeviceSearch; private final SearchBuilder AccountSearch; public UserStatisticsDaoImpl() { @@ -51,31 +51,34 @@ public class UserStatisticsDaoImpl extends GenericDaoBase sc = AccountDcIpHostSearch.create(); + public UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long deviceId, String deviceType) { + SearchCriteria sc = AccountDcIpDeviceSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("ip", publicIp); - sc.setParameters("host", hostId); + sc.setParameters("device", deviceId); + sc.setParameters("deviceType", deviceType); return findOneBy(sc); } @Override - public UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId) { - SearchCriteria sc = AccountDcIpHostSearch.create(); + public UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long deviceId, String deviceType) { + SearchCriteria sc = AccountDcIpDeviceSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("ip", publicIp); - sc.setParameters("host", hostId); + sc.setParameters("device", deviceId); + sc.setParameters("deviceType", deviceType); return lockOneRandomRow(sc, true); } diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index e3fca3f5ad3..8eac6733044 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -685,13 +685,14 @@ CREATE TABLE `cloud`.`user_statistics` ( `data_center_id` bigint unsigned NOT NULL, `account_id` bigint unsigned NOT NULL, `public_ip_address` varchar(15), - `host_id` bigint unsigned, - `host_type` varchar(32), + `device_id` bigint unsigned NOT NULL, + `device_type` varchar(32) NOT NULL, `net_bytes_received` bigint unsigned NOT NULL default '0', `net_bytes_sent` bigint unsigned NOT NULL default '0', `current_bytes_received` bigint unsigned NOT NULL default '0', `current_bytes_sent` bigint unsigned NOT NULL default '0', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY (`account_id`, `data_center_id`, `device_id`, `device_type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `cloud`.`vm_template` (