Changed schema to keep track of which public IP address and host a user statistics entry is for

This commit is contained in:
keshav 2010-12-29 19:12:31 -08:00
parent b46b3918ef
commit 3da09345c9
6 changed files with 52 additions and 28 deletions

View File

@ -39,6 +39,12 @@ public class UserStatisticsVO {
@Column(name="account_id", updatable=false)
private long accountId;
@Column(name="public_ip_address")
private String publicIpAddress;
@Column(name="host_id")
private Long hostId;
@Column(name="net_bytes_received")
private long netBytesReceived;
@ -54,13 +60,15 @@ public class UserStatisticsVO {
protected UserStatisticsVO() {
}
public UserStatisticsVO(long accountId, long dcId) {
public UserStatisticsVO(long accountId, long dcId, String publicIpAddress, Long hostId) {
this.accountId = accountId;
this.dataCenterId = dcId;
this.publicIpAddress = publicIpAddress;
this.hostId = hostId;
this.netBytesReceived = 0;
this.netBytesSent = 0;
currentBytesReceived = 0;
currentBytesSent = 0;
dataCenterId = dcId;
this.currentBytesReceived = 0;
this.currentBytesSent = 0;
}
public long getAccountId() {
@ -75,6 +83,14 @@ public class UserStatisticsVO {
return dataCenterId;
}
public String getPublicIpAddress() {
return publicIpAddress;
}
public Long getHostId() {
return hostId;
}
public long getCurrentBytesReceived() {
return currentBytesReceived;
}

View File

@ -326,12 +326,12 @@ 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);
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);
stats = new UserStatisticsVO(ownerId, dcId, null, null);
_userStatsDao.persist(stats);
}
txn.commit();

View File

@ -661,7 +661,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
final Transaction txn = Transaction.currentTxn();
try {
txn.start();
final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId());
final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), null, null);
if (userStats != null) {
final RebootAnswer sa = (RebootAnswer) answer;
final Long received = sa.getBytesReceived();
@ -1249,7 +1249,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
continue;
}
txn.start();
UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterId());
UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterId(), null, null);
if (stats == null) {
s_logger.warn("unable to find stats for account: " + router.getAccountId());
continue;

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);
UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long hostId);
UserStatisticsVO lock(long accountId, long dcId);
UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId);
List<UserStatisticsVO> listBy(long accountId);

View File

@ -43,39 +43,45 @@ public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long
"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> UserDcSearch;
private final SearchBuilder<UserStatisticsVO> UserSearch;
private final SearchBuilder<UserStatisticsVO> AccountDcIpHostSearch;
private final SearchBuilder<UserStatisticsVO> AccountSearch;
public UserStatisticsDaoImpl() {
UserSearch = createSearchBuilder();
UserSearch.and("account", UserSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
UserSearch.done();
AccountSearch = createSearchBuilder();
AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountSearch.done();
UserDcSearch = createSearchBuilder();
UserDcSearch.and("account", UserDcSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
UserDcSearch.and("dc", UserDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
UserDcSearch.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();
}
@Override
public UserStatisticsVO findBy(long accountId, long dcId) {
SearchCriteria<UserStatisticsVO> sc = UserDcSearch.create();
public UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long hostId) {
SearchCriteria<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
sc.setParameters("account", accountId);
sc.setParameters("dc", dcId);
sc.setParameters("ip", publicIp);
sc.setParameters("host", hostId);
return findOneBy(sc);
}
@Override
public UserStatisticsVO lock(long accountId, long dcId) {
SearchCriteria<UserStatisticsVO> sc = UserDcSearch.create();
public UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId) {
SearchCriteria<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
sc.setParameters("account", accountId);
sc.setParameters("dc", dcId);
sc.setParameters("ip", publicIp);
sc.setParameters("host", hostId);
return lockOneRandomRow(sc, true);
}
@Override
public List<UserStatisticsVO> listBy(long accountId) {
SearchCriteria<UserStatisticsVO> sc = UserSearch.create();
SearchCriteria<UserStatisticsVO> sc = AccountSearch.create();
sc.setParameters("account", accountId);
return search(sc, null);
}

View File

@ -662,6 +662,8 @@ CREATE TABLE `cloud`.`user_statistics` (
`id` bigint unsigned UNIQUE NOT NULL AUTO_INCREMENT,
`data_center_id` bigint unsigned NOT NULL,
`account_id` bigint unsigned NOT NULL,
`public_ip_address` varchar(15),
`host_id` bigint unsigned,
`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',