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

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

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())); 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 (stats == null) {
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating statistics for the owner: " + ownerId); s_logger.debug("Creating statistics for the owner: " + ownerId);
} }
stats = new UserStatisticsVO(ownerId, dcId); stats = new UserStatisticsVO(ownerId, dcId, null, null);
_userStatsDao.persist(stats); _userStatsDao.persist(stats);
} }
txn.commit(); txn.commit();

View File

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

View File

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

View File

@ -43,39 +43,45 @@ public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long
"FROM user_statistics us, account a " + "FROM user_statistics us, account a " +
"WHERE us.account_id = a.id AND (a.removed IS NULL OR a.removed >= ?) " + "WHERE us.account_id = a.id AND (a.removed IS NULL OR a.removed >= ?) " +
"ORDER BY us.id"; "ORDER BY us.id";
private final SearchBuilder<UserStatisticsVO> UserDcSearch; private final SearchBuilder<UserStatisticsVO> AccountDcIpHostSearch;
private final SearchBuilder<UserStatisticsVO> UserSearch; private final SearchBuilder<UserStatisticsVO> AccountSearch;
public UserStatisticsDaoImpl() { public UserStatisticsDaoImpl() {
UserSearch = createSearchBuilder(); AccountSearch = createSearchBuilder();
UserSearch.and("account", UserSearch.entity().getAccountId(), SearchCriteria.Op.EQ); AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
UserSearch.done(); AccountSearch.done();
UserDcSearch = createSearchBuilder(); AccountDcIpHostSearch = createSearchBuilder();
UserDcSearch.and("account", UserDcSearch.entity().getAccountId(), SearchCriteria.Op.EQ); AccountDcIpHostSearch.and("account", AccountDcIpHostSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
UserDcSearch.and("dc", UserDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); AccountDcIpHostSearch.and("dc", AccountDcIpHostSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
UserDcSearch.done(); AccountDcIpHostSearch.and("ip", AccountDcIpHostSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.and("host", AccountDcIpHostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
AccountDcIpHostSearch.done();
} }
@Override @Override
public UserStatisticsVO findBy(long accountId, long dcId) { public UserStatisticsVO findBy(long accountId, long dcId, String publicIp, Long hostId) {
SearchCriteria<UserStatisticsVO> sc = UserDcSearch.create(); SearchCriteria<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
sc.setParameters("account", accountId); sc.setParameters("account", accountId);
sc.setParameters("dc", dcId); sc.setParameters("dc", dcId);
sc.setParameters("ip", publicIp);
sc.setParameters("host", hostId);
return findOneBy(sc); return findOneBy(sc);
} }
@Override @Override
public UserStatisticsVO lock(long accountId, long dcId) { public UserStatisticsVO lock(long accountId, long dcId, String publicIp, Long hostId) {
SearchCriteria<UserStatisticsVO> sc = UserDcSearch.create(); SearchCriteria<UserStatisticsVO> sc = AccountDcIpHostSearch.create();
sc.setParameters("account", accountId); sc.setParameters("account", accountId);
sc.setParameters("dc", dcId); sc.setParameters("dc", dcId);
sc.setParameters("ip", publicIp);
sc.setParameters("host", hostId);
return lockOneRandomRow(sc, true); return lockOneRandomRow(sc, true);
} }
@Override @Override
public List<UserStatisticsVO> listBy(long accountId) { public List<UserStatisticsVO> listBy(long accountId) {
SearchCriteria<UserStatisticsVO> sc = UserSearch.create(); SearchCriteria<UserStatisticsVO> sc = AccountSearch.create();
sc.setParameters("account", accountId); sc.setParameters("account", accountId);
return search(sc, null); return search(sc, null);
} }

View File

@ -662,6 +662,8 @@ CREATE TABLE `cloud`.`user_statistics` (
`id` bigint unsigned UNIQUE NOT NULL AUTO_INCREMENT, `id` bigint unsigned UNIQUE NOT NULL AUTO_INCREMENT,
`data_center_id` bigint unsigned NOT NULL, `data_center_id` bigint unsigned NOT NULL,
`account_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_received` bigint unsigned NOT NULL default '0',
`net_bytes_sent` 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_received` bigint unsigned NOT NULL default '0',