@@ -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 ();
0 commit comments