abh1sar commented on code in PR #11531:
URL: https://github.com/apache/cloudstack/pull/11531#discussion_r2309313215


##########
usage/src/main/java/com/cloud/usage/UsageManagerImpl.java:
##########
@@ -1534,92 +1539,113 @@ private void createIPHelperEvent(UsageEventVO event) {
         }
     }
 
+    private void deleteExistingSecondaryStorageUsageForVolume(long volId, long 
accountId, Date deletedDate) {
+        List<UsageStorageVO> storageVOs = _usageStorageDao.listById(accountId, 
volId, StorageTypes.VOLUME);
+        for (UsageStorageVO storageVO : storageVOs) {
+            s_logger.debug(String.format("Setting the volume with id: {} to 
'deleted' in the usage_storage table for account: {}.", volId, accountId));
+            storageVO.setDeleted(deletedDate);
+            _usageStorageDao.update(storageVO);
+        }
+    }
+
+    private void deleteExistingInstanceVolumeUsage(long volId, long accountId, 
Date deletedDate) {
+        List<UsageVolumeVO> volumesVOs = _usageVolumeDao.listByVolumeId(volId, 
accountId);
+        for (UsageVolumeVO volumesVO : volumesVOs) {
+            if (volumesVO.getVmId() != null) {
+                s_logger.debug(String.format("Setting the volume with id: {} 
for instance id: {} to 'deleted' in the usage_volume table for account {}.",
+                        volumesVO.getVolumeId(), volumesVO.getVmId(), 
accountId));
+                volumesVO.setDeleted(deletedDate);
+                _usageVolumeDao.update(volumesVO.getId(), volumesVO);
+            }
+        }
+    }
+
+    private void deleteExistingVolumeUsage(long volId, long accountId, Date 
deletedDate) {
+        List<UsageVolumeVO> volumesVOs = _usageVolumeDao.listByVolumeId(volId, 
accountId);
+        for (UsageVolumeVO volumesVO : volumesVOs) {
+            s_logger.debug(String.format("Setting the volume with id: {} to 
'deleted' in the usage_storage table for account: {}.", volId, accountId));
+            volumesVO.setDeleted(deletedDate);
+            _usageVolumeDao.update(volumesVO.getId(), volumesVO);
+        }
+    }
+
     private void createVolumeHelperEvent(UsageEventVO event) {
 
         long volId = event.getResourceId();
+        Account acct = 
_accountDao.findByIdIncludingRemoved(event.getAccountId());
+        List<UsageVolumeVO> volumesVOs;
+        UsageVolumeVO volumeVO;
 
-        if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
-            //For volumes which are 'attached' successfully, set the 'deleted' 
column in the usage_storage table,
+        switch (event.getType()) {
+        case EventTypes.EVENT_VOLUME_CREATE:
+            //For volumes which are 'attached' successfully from uploaded 
state, set the 'deleted' column in the usage_storage table,
             //so that the secondary storage should stop accounting and only 
primary will be accounted.
-            SearchCriteria<UsageStorageVO> sc = 
_usageStorageDao.createSearchCriteria();
-            sc.addAnd("entityId", SearchCriteria.Op.EQ, volId);
-            sc.addAnd("storageType", SearchCriteria.Op.EQ, 
StorageTypes.VOLUME);
-            List<UsageStorageVO> volumesVOs = _usageStorageDao.search(sc, 
null);
-            if (volumesVOs != null) {
-                if (volumesVOs.size() == 1) {
-                    s_logger.debug("Setting the volume with id: " + volId + " 
to 'deleted' in the usage_storage table.");
-                    volumesVOs.get(0).setDeleted(event.getCreateDate());
-                    _usageStorageDao.update(volumesVOs.get(0));
-                }
-            }
-        }
-        if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType()) || 
EventTypes.EVENT_VOLUME_RESIZE.equals(event.getType())) {
-            SearchCriteria<UsageVolumeVO> sc = 
_usageVolumeDao.createSearchCriteria();
-            sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
-            sc.addAnd("volumeId", SearchCriteria.Op.EQ, volId);
-            sc.addAnd("deleted", SearchCriteria.Op.NULL);
-            List<UsageVolumeVO> volumesVOs = _usageVolumeDao.search(sc, null);
+            deleteExistingSecondaryStorageUsageForVolume(volId, 
event.getAccountId(), event.getCreateDate());
+
+            volumesVOs = _usageVolumeDao.listByVolumeId(volId, 
event.getAccountId());
             if (volumesVOs.size() > 0) {
                 //This is a safeguard to avoid double counting of volumes.
                 s_logger.error("Found duplicate usage entry for volume: " + 
volId + " assigned to account: " + event.getAccountId() + "; marking as 
deleted...");
+                deleteExistingVolumeUsage(volId, event.getAccountId(), 
event.getCreateDate());
             }
-            //an entry exists if it is a resize volume event. marking the 
existing deleted and creating a new one in the case of resize.
-            for (UsageVolumeVO volumesVO : volumesVOs) {
-                if (s_logger.isDebugEnabled()) {
-                    s_logger.debug("deleting volume: " + volumesVO.getId() + " 
from account: " + volumesVO.getAccountId());
-                }
-                volumesVO.setDeleted(event.getCreateDate());
-                _usageVolumeDao.update(volumesVO);
-            }
-            if (s_logger.isDebugEnabled()) {
-                s_logger.debug("create volume with id : " + volId + " for 
account: " + event.getAccountId());
-            }
-            Account acct = 
_accountDao.findByIdIncludingRemoved(event.getAccountId());
-            UsageVolumeVO volumeVO = new UsageVolumeVO(volId, 
event.getZoneId(), event.getAccountId(), acct.getDomainId(), 
event.getOfferingId(), event.getTemplateId(), event.getSize(), 
event.getCreateDate(), null);
+
+            s_logger.debug(String.format("Creating a new entry in usage_volume 
for volume with id: {} for account: {}", volId, event.getVmId(), 
event.getAccountId()));
+            volumeVO = new UsageVolumeVO(volId, event.getZoneId(), 
event.getAccountId(), acct.getDomainId(), event.getOfferingId(), 
event.getTemplateId(), null, event.getSize(), event.getCreateDate(), null);
             _usageVolumeDao.persist(volumeVO);
-        } else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
-            SearchCriteria<UsageVolumeVO> sc = 
_usageVolumeDao.createSearchCriteria();
-            sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
-            sc.addAnd("volumeId", SearchCriteria.Op.EQ, volId);
-            sc.addAnd("deleted", SearchCriteria.Op.NULL);
-            List<UsageVolumeVO> volumesVOs = _usageVolumeDao.search(sc, null);
-            if (volumesVOs.size() > 1) {
-                s_logger.warn("More that one usage entry for volume: " + volId 
+ " assigned to account: " + event.getAccountId() + "; marking them all as 
deleted...");
-            }
-            for (UsageVolumeVO volumesVO : volumesVOs) {
-                if (s_logger.isDebugEnabled()) {
-                    s_logger.debug("deleting volume: " + volumesVO.getId() + " 
from account: " + volumesVO.getAccountId());
-                }
-                volumesVO.setDeleted(event.getCreateDate()); // there really 
shouldn't be more than one
-                _usageVolumeDao.update(volumesVO);
-            }
-        } else if (EventTypes.EVENT_VOLUME_UPLOAD.equals(event.getType())) {
-            //For Upload event add an entry to the usage_storage table.
-            SearchCriteria<UsageStorageVO> sc = 
_usageStorageDao.createSearchCriteria();
-            sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
-            sc.addAnd("entityId", SearchCriteria.Op.EQ, volId);
-            sc.addAnd("storageType", SearchCriteria.Op.EQ, 
StorageTypes.VOLUME);
-            sc.addAnd("deleted", SearchCriteria.Op.NULL);
-            List<UsageStorageVO> volumesVOs = _usageStorageDao.search(sc, 
null);
 
-            if (volumesVOs.size() > 0) {
-                //This is a safeguard to avoid double counting of volumes.
-                s_logger.error("Found duplicate usage entry for volume: " + 
volId + " assigned to account: " + event.getAccountId() + "; marking as 
deleted...");
+            if (event.getVmId() != null) {

Review Comment:
   VOLUME_CREATE event will contain the vmId also when it is created during 
Instance deployment. Need to add a vm specific entry also in that case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to