From f30b5ceba9001bb800ed2b6eabcf46ece73577aa Mon Sep 17 00:00:00 2001 From: Rakesh Date: Fri, 29 Nov 2019 06:42:04 -0800 Subject: [PATCH] Fix resource count of expunged volume (#3669) If the volume is in "Expunged" state then it should not be considered towards total resource count of "primarystoragetotal" field. Currently cloudstack takes into resource calculation even if the volume is expunged. The volume itself doesnt exist in primage storage and hence it should not be considered towrds resource caculation. Steps to reproduce the issue: 1 . Get the resource count of "primarystoragetotal" of a particular domain. 2 . Create a VM with 5GB root disk size and stop it. 3 . Now the value of "primarystoragetotal" should be intitial value plus 5. 4 . Navigate to "volumes" of the VM and select "Download Volume" option. 5 . Once the volume is downloaded, expunge the VM. 6 . Get the resource count of "primarystoragetotal". it will be same value as in step 3 But it should be same as initial value obtained in step 1. With this fix, the value obtained at step 6 will be same as in step 1. --- .../java/com/cloud/storage/dao/VolumeDaoImpl.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java index 7c63b9c7526..e8ed3343831 100644 --- a/engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java @@ -122,7 +122,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol public List findByPoolId(long poolId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("poolId", poolId); - sc.setParameters("notDestroyed", Volume.State.Destroy); + sc.setParameters("notDestroyed", Volume.State.Destroy, Volume.State.Expunged); sc.setParameters("vType", Volume.Type.ROOT.toString()); return listBy(sc); } @@ -132,7 +132,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("instanceId", instanceId); sc.setParameters("poolId", poolId); - sc.setParameters("notDestroyed", Volume.State.Destroy); + sc.setParameters("notDestroyed", Volume.State.Destroy, Volume.State.Expunged); return listBy(sc); } @@ -148,7 +148,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol public List findByPoolId(long poolId, Volume.Type volumeType) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("poolId", poolId); - sc.setParameters("notDestroyed", Volume.State.Destroy); + sc.setParameters("notDestroyed", Volume.State.Destroy, Volume.State.Expunged); if (volumeType != null) { sc.setParameters("vType", volumeType.toString()); @@ -349,7 +349,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol AllFieldsSearch.and("vType", AllFieldsSearch.entity().getVolumeType(), Op.EQ); AllFieldsSearch.and("id", AllFieldsSearch.entity().getId(), Op.EQ); AllFieldsSearch.and("destroyed", AllFieldsSearch.entity().getState(), Op.EQ); - AllFieldsSearch.and("notDestroyed", AllFieldsSearch.entity().getState(), Op.NEQ); + AllFieldsSearch.and("notDestroyed", AllFieldsSearch.entity().getState(), Op.NIN); AllFieldsSearch.and("updateTime", AllFieldsSearch.entity().getUpdated(), SearchCriteria.Op.LT); AllFieldsSearch.and("updatedCount", AllFieldsSearch.entity().getUpdatedCount(), Op.EQ); AllFieldsSearch.and("name", AllFieldsSearch.entity().getName(), Op.EQ); @@ -410,6 +410,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol primaryStorageSearch.cp(); primaryStorageSearch.and("displayVolume", primaryStorageSearch.entity().isDisplayVolume(), Op.EQ); primaryStorageSearch.and("isRemoved", primaryStorageSearch.entity().getRemoved(), Op.NULL); + primaryStorageSearch.and("NotCountStates", primaryStorageSearch.entity().getState(), Op.NIN); primaryStorageSearch.done(); primaryStorageSearch2 = createSearchBuilder(SumCount.class); @@ -423,6 +424,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol primaryStorageSearch2.cp(); primaryStorageSearch2.and("displayVolume", primaryStorageSearch2.entity().isDisplayVolume(), Op.EQ); primaryStorageSearch2.and("isRemoved", primaryStorageSearch2.entity().getRemoved(), Op.NULL); + primaryStorageSearch2.and("NotCountStates", primaryStorageSearch2.entity().getState(), Op.NIN); primaryStorageSearch2.done(); secondaryStorageSearch = createSearchBuilder(SumCount.class); @@ -448,7 +450,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol public Long countAllocatedVolumesForAccount(long accountId) { SearchCriteria sc = CountByAccount.create(); sc.setParameters("account", accountId); - sc.setParameters("state", Volume.State.Destroy); + sc.setParameters("state", Volume.State.Destroy, Volume.State.Expunged); sc.setParameters("displayVolume", 1); return customSearch(sc, null).get(0); } @@ -464,6 +466,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol } sc.setParameters("accountId", accountId); sc.setParameters("states", State.Allocated); + sc.setParameters("NotCountStates", State.Destroy, State.Expunged); sc.setParameters("displayVolume", 1); List storageSpace = customSearch(sc, null); if (storageSpace != null) {