bug 8465: track network usage per router in user_statistics table

status 8465: resolved fixed
This commit is contained in:
kishan 2011-02-09 18:38:01 +05:30
parent 67a92e3f1d
commit 77719be46a
6 changed files with 55 additions and 40 deletions

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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;
}

View File

@ -25,9 +25,9 @@ import com.cloud.user.UserStatisticsVO;
import com.cloud.utils.db.GenericDao;
public interface UserStatisticsDao extends GenericDao<UserStatisticsVO, Long> {
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<UserStatisticsVO> listBy(long accountId);

View File

@ -39,11 +39,11 @@ import com.cloud.utils.db.Transaction;
@Local(value={UserStatisticsDao.class})
public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long> 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<UserStatisticsVO> AccountDcIpHostSearch;
private final SearchBuilder<UserStatisticsVO> AccountDcIpDeviceSearch;
private final SearchBuilder<UserStatisticsVO> AccountSearch;
public UserStatisticsDaoImpl() {
@ -51,31 +51,34 @@ public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long
AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountSearch.done();
AccountDcIpHostSearch = createSearchBuilder();
AccountDcIpHostSearch.and("account", AccountDcIpHostSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.and("dc", AccountDcIpHostSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.and("ip", AccountDcIpHostSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.and("host", AccountDcIpHostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.done();
AccountDcIpDeviceSearch = createSearchBuilder();
AccountDcIpDeviceSearch.and("account", AccountDcIpDeviceSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountDcIpDeviceSearch.and("dc", AccountDcIpDeviceSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AccountDcIpDeviceSearch.and("ip", AccountDcIpDeviceSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ);
AccountDcIpDeviceSearch.and("device", AccountDcIpDeviceSearch.entity().getDeviceId(), SearchCriteria.Op.EQ);
AccountDcIpDeviceSearch.and("deviceType", AccountDcIpDeviceSearch.entity().getDeviceType(), SearchCriteria.Op.EQ);
AccountDcIpDeviceSearch.done();
}
@Override
public UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long hostId) {
SearchCriteria<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
public UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long deviceId, String deviceType) {
SearchCriteria<UserStatisticsVO> 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<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
public UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long deviceId, String deviceType) {
SearchCriteria<UserStatisticsVO> 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);
}

View File

@ -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` (