From 840a8204dd5cd5c9c18c4b8e4e2a8cb02ee89c6c Mon Sep 17 00:00:00 2001 From: Rakesh Venkatesh Date: Thu, 7 Nov 2019 14:54:55 +0100 Subject: [PATCH] Fix resource count of expunged volume 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 7c63b9c75268..e8ed3343831b 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 List findByInstanceAndDeviceId(long instanceId, long deviceId) 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 List findByInstanceIdAndPoolId(long instanceId, long poolId) { 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 VolumeVO findByPoolIdName(long poolId, String name) { 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 VolumeDaoImpl() { 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 VolumeDaoImpl() { 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 VolumeDaoImpl() { 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 Pair getCountAndTotalByPool(long poolId) { 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 long primaryStorageUsedForAccount(long accountId, List virtualRoute } 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) {