Skip to content

Commit fc3905f

Browse files
ustcweizhouKishan Kavala
authored andcommitted
CLOUDSTACK-962:setAggBytesSent/setAggBytesReceived in NetworkUsageTask when not in case of dailyor hourly
Removed duplicate userstatsdao injection
1 parent 8cb5c00 commit fc3905f

3 files changed

Lines changed: 33 additions & 20 deletions

File tree

server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,6 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
293293
UserVmDao _userVmDao;
294294
@Inject VMInstanceDao _vmDao;
295295
@Inject
296-
UserStatisticsDao _statsDao = null;
297-
@Inject
298296
NetworkOfferingDao _networkOfferingDao = null;
299297
@Inject
300298
GuestOSDao _guestOSDao = null;
@@ -364,7 +362,9 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
364362
private String _usageTimeZone = "GMT";
365363
private final long mgmtSrvrId = MacAddress.getMacAddress().toLong();
366364
private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 5; // 5 seconds
367-
365+
private static final int USAGE_AGGREGATION_RANGE_MIN = 10; // 10 minutes, same as com.cloud.usage.UsageManagerImpl.USAGE_AGGREGATION_RANGE_MIN
366+
private boolean _dailyOrHourly = false;
367+
368368
ScheduledExecutorService _executor;
369369
ScheduledExecutorService _checkExecutor;
370370
ScheduledExecutorService _networkStatsUpdateExecutor;
@@ -728,6 +728,7 @@ public boolean start() {
728728
cal.roll(Calendar.DAY_OF_YEAR, true);
729729
cal.add(Calendar.MILLISECOND, -1);
730730
endDate = cal.getTime().getTime();
731+
_dailyOrHourly = true;
731732
} else if (_usageAggregationRange == HOURLY_TIME) {
732733
cal.roll(Calendar.HOUR_OF_DAY, false);
733734
cal.set(Calendar.MINUTE, 0);
@@ -736,8 +737,15 @@ public boolean start() {
736737
cal.roll(Calendar.HOUR_OF_DAY, true);
737738
cal.add(Calendar.MILLISECOND, -1);
738739
endDate = cal.getTime().getTime();
740+
_dailyOrHourly = true;
739741
} else {
740742
endDate = cal.getTime().getTime();
743+
_dailyOrHourly = false;
744+
}
745+
746+
if (_usageAggregationRange < USAGE_AGGREGATION_RANGE_MIN) {
747+
s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + USAGE_AGGREGATION_RANGE_MIN);
748+
_usageAggregationRange = USAGE_AGGREGATION_RANGE_MIN;
741749
}
742750

743751
_networkStatsUpdateExecutor.scheduleAtFixedRate(new NetworkStatsUpdateTask(), (endDate - System.currentTimeMillis()),
@@ -854,7 +862,7 @@ public void run() {
854862
final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(),
855863
forVpc, routerNic.getIp4Address());
856864
String routerType = router.getType().toString();
857-
UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(),
865+
UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(),
858866
router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType);
859867
NetworkUsageAnswer answer = null;
860868
try {
@@ -876,7 +884,7 @@ public void run() {
876884
continue;
877885
}
878886
txn.start();
879-
UserStatisticsVO stats = _statsDao.lock(router.getAccountId(),
887+
UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(),
880888
router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType);
881889
if (stats == null) {
882890
s_logger.warn("unable to find stats for account: " + router.getAccountId());
@@ -912,7 +920,12 @@ public void run() {
912920
stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
913921
}
914922
stats.setCurrentBytesSent(answer.getBytesSent());
915-
_statsDao.update(stats.getId(), stats);
923+
if (! _dailyOrHourly) {
924+
//update agg bytes
925+
stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
926+
stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
927+
}
928+
_userStatsDao.update(stats.getId(), stats);
916929
txn.commit();
917930
} catch (Exception e) {
918931
txn.rollback();
@@ -954,7 +967,7 @@ public void run() {
954967
try {
955968
txn.start();
956969
//get all stats with delta > 0
957-
List<UserStatisticsVO> updatedStats = _statsDao.listUpdatedStats();
970+
List<UserStatisticsVO> updatedStats = _userStatsDao.listUpdatedStats();
958971
Date updatedTime = new Date();
959972
for(UserStatisticsVO stat : updatedStats){
960973
//update agg bytes
@@ -3598,7 +3611,7 @@ public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile){
35983611
boolean forVpc = router.getVpcId() != null;
35993612
final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(),
36003613
forVpc, routerNic.getIp4Address());
3601-
UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(),
3614+
UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(),
36023615
router.getDataCenterId(), network.getId(), null, router.getId(), router.getType().toString());
36033616
NetworkUsageAnswer answer = null;
36043617
try {
@@ -3620,7 +3633,7 @@ public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile){
36203633
continue;
36213634
}
36223635
txn.start();
3623-
UserStatisticsVO stats = _statsDao.lock(router.getAccountId(),
3636+
UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(),
36243637
router.getDataCenterId(), network.getId(), null, router.getId(), router.getType().toString());
36253638
if (stats == null) {
36263639
s_logger.warn("unable to find stats for account: " + router.getAccountId());
@@ -3656,7 +3669,7 @@ public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile){
36563669
stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
36573670
}
36583671
stats.setCurrentBytesSent(answer.getBytesSent());
3659-
_statsDao.update(stats.getId(), stats);
3672+
_userStatsDao.update(stats.getId(), stats);
36603673
txn.commit();
36613674
} catch (Exception e) {
36623675
txn.rollback();

server/src/com/cloud/user/dao/UserStatisticsDaoImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long
4444
"WHERE us.account_id = a.id AND (a.removed IS NULL OR a.removed >= ?) " +
4545
"ORDER BY us.id";
4646
private static final String UPDATED_STATS_SEARCH = "SELECT id, current_bytes_received, current_bytes_sent, net_bytes_received, net_bytes_sent, agg_bytes_received, agg_bytes_sent from user_statistics " +
47-
"where (agg_bytes_received < net_bytes_received + current_bytes_received) OR (agg_bytes_sent < net_bytes_sent + current_bytes_sent)";
47+
"where (agg_bytes_received < net_bytes_received + current_bytes_received) OR (agg_bytes_sent < net_bytes_sent + current_bytes_sent)";
4848
private final SearchBuilder<UserStatisticsVO> AllFieldsSearch;
4949
private final SearchBuilder<UserStatisticsVO> AccountSearch;
50-
51-
50+
51+
5252
public UserStatisticsDaoImpl() {
53-
AccountSearch = createSearchBuilder();
54-
AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
55-
AccountSearch.done();
53+
AccountSearch = createSearchBuilder();
54+
AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
55+
AccountSearch.done();
5656

57-
AllFieldsSearch = createSearchBuilder();
57+
AllFieldsSearch = createSearchBuilder();
5858
AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
5959
AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
6060
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
@@ -63,7 +63,7 @@ public UserStatisticsDaoImpl() {
6363
AllFieldsSearch.and("deviceType", AllFieldsSearch.entity().getDeviceType(), SearchCriteria.Op.EQ);
6464
AllFieldsSearch.done();
6565
}
66-
66+
6767
@Override
6868
public UserStatisticsVO findBy(long accountId, long dcId, long networkId, String publicIp, Long deviceId, String deviceType) {
6969
SearchCriteria<UserStatisticsVO> sc = AllFieldsSearch.create();
@@ -133,5 +133,5 @@ public List<UserStatisticsVO> listUpdatedStats() {
133133
}
134134
return userStats;
135135
}
136-
136+
137137
}

usage/src/com/cloud/usage/UsageManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ public void run() {
14301430
timeSinceJob = now - lastSuccess;
14311431
}
14321432

1433-
if ((timeSinceJob > 0) && (timeSinceJob > aggregationDurationMillis)) {
1433+
if ((timeSinceJob > 0) && (timeSinceJob > (aggregationDurationMillis - 100)) {
14341434
if (timeToJob > (aggregationDurationMillis/2)) {
14351435
if (s_logger.isDebugEnabled()) {
14361436
s_logger.debug("it's been " + timeSinceJob + " ms since last usage job and " + timeToJob + " ms until next job, scheduling an immediate job to catch up (aggregation duration is " + m_aggregationDuration + " minutes)");

0 commit comments

Comments
 (0)