From 539eb38908ede53a36d4bb680b848caf9fe469c4 Mon Sep 17 00:00:00 2001 From: Sanjay Tripathi Date: Tue, 16 Jul 2013 14:52:56 +0530 Subject: [PATCH] CLOUDSTACK-3492: [Regression]Update Resource Count for an account is updating the primary storage incorretly. The issue is that while calculating the used primary storage size, the updateResourceCount API is also calculating the disk size of virtual router VM, created for that account and because of this, the API is returning the incorrect result. --- .../src/com/cloud/storage/dao/VolumeDao.java | 3 ++- .../com/cloud/storage/dao/VolumeDaoImpl.java | 13 +++++++++-- .../src/com/cloud/vm/dao/VMInstanceDao.java | 2 +- .../com/cloud/vm/dao/VMInstanceDaoImpl.java | 22 +++++++++---------- .../ResourceLimitManagerImpl.java | 5 +++-- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java index fb7dc706f10..7b58e7d3400 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java @@ -80,9 +80,10 @@ public interface VolumeDao extends GenericDao, StateDao virtualRouters); /** * Gets the Total Secondary Storage space used by volumes allocated for an diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java index ba85466a930..f87cd4eff44 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java @@ -318,7 +318,10 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol primaryStorageSearch = createSearchBuilder(SumCount.class); primaryStorageSearch.select("sum", Func.SUM, primaryStorageSearch.entity().getSize()); primaryStorageSearch.and("accountId", primaryStorageSearch.entity().getAccountId(), Op.EQ); - primaryStorageSearch.and("path", primaryStorageSearch.entity().getPath(), Op.NNULL); + primaryStorageSearch.and("virtualRouterVmIds", primaryStorageSearch.entity().getInstanceId(), Op.NIN); + primaryStorageSearch.and().op("path", primaryStorageSearch.entity().getPath(), Op.NNULL); + primaryStorageSearch.or("states", primaryStorageSearch.entity().getState(), Op.IN); + primaryStorageSearch.cp(); primaryStorageSearch.and("isRemoved", primaryStorageSearch.entity().getRemoved(), Op.NULL); primaryStorageSearch.done(); @@ -326,6 +329,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol secondaryStorageSearch.select("sum", Func.SUM, secondaryStorageSearch.entity().getSize()); secondaryStorageSearch.and("accountId", secondaryStorageSearch.entity().getAccountId(), Op.EQ); secondaryStorageSearch.and("path", secondaryStorageSearch.entity().getPath(), Op.NULL); + secondaryStorageSearch.and("states", secondaryStorageSearch.entity().getState(), Op.NIN); secondaryStorageSearch.and("isRemoved", secondaryStorageSearch.entity().getRemoved(), Op.NULL); secondaryStorageSearch.done(); } @@ -349,9 +353,13 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol } @Override - public long primaryStorageUsedForAccount(long accountId) { + public long primaryStorageUsedForAccount(long accountId, List virtualRouters) { SearchCriteria sc = primaryStorageSearch.create(); sc.setParameters("accountId", accountId); + if (!virtualRouters.isEmpty()) { + sc.setParameters("virtualRouterVmIds", virtualRouters.toArray(new Object[virtualRouters.size()])); + } + sc.setParameters("states", State.Allocated); List storageSpace = customSearch(sc, null); if (storageSpace != null) { return storageSpace.get(0).sum; @@ -364,6 +372,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol public long secondaryStorageUsedForAccount(long accountId) { SearchCriteria sc = secondaryStorageSearch.create(); sc.setParameters("accountId", accountId); + sc.setParameters("states", State.Allocated); List storageSpace = customSearch(sc, null); if (storageSpace != null) { return storageSpace.get(0).sum; diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java index e5640528ec1..2fe8140f1cd 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java @@ -90,7 +90,7 @@ public interface VMInstanceDao extends GenericDao, StateDao< List listByTypeAndState(VirtualMachine.Type type, State state); List listByAccountId(long accountId); - public Long countAllocatedVirtualRoutersForAccount(long accountId); + public List findIdsOfAllocatedVirtualRoutersForAccount(long accountId); List listByClusterId(long clusterId); // this does not pull up VMs which are starting List listLHByClusterId(long clusterId); // get all the VMs even starting one on this cluster diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index ec04085826e..8cbc6b64dea 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -79,7 +79,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem protected SearchBuilder HostUpSearch; protected SearchBuilder InstanceNameSearch; protected SearchBuilder HostNameSearch; - protected GenericSearchBuilder CountVirtualRoutersByAccount; + protected GenericSearchBuilder FindIdsOfVirtualRoutersByAccount; protected GenericSearchBuilder CountRunningByHost; protected GenericSearchBuilder CountRunningByAccount; protected SearchBuilder NetworkTypeSearch; @@ -197,12 +197,12 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem HostNameSearch.and("hostName", HostNameSearch.entity().getHostName(), Op.EQ); HostNameSearch.done(); - CountVirtualRoutersByAccount = createSearchBuilder(Long.class); - CountVirtualRoutersByAccount.select(null, Func.COUNT, null); - CountVirtualRoutersByAccount.and("account", CountVirtualRoutersByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); - CountVirtualRoutersByAccount.and("type", CountVirtualRoutersByAccount.entity().getType(), SearchCriteria.Op.EQ); - CountVirtualRoutersByAccount.and("state", CountVirtualRoutersByAccount.entity().getState(), SearchCriteria.Op.NIN); - CountVirtualRoutersByAccount.done(); + FindIdsOfVirtualRoutersByAccount = createSearchBuilder(Long.class); + FindIdsOfVirtualRoutersByAccount.selectField(FindIdsOfVirtualRoutersByAccount.entity().getId()); + FindIdsOfVirtualRoutersByAccount.and("account", FindIdsOfVirtualRoutersByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); + FindIdsOfVirtualRoutersByAccount.and("type", FindIdsOfVirtualRoutersByAccount.entity().getType(), SearchCriteria.Op.EQ); + FindIdsOfVirtualRoutersByAccount.and("state", FindIdsOfVirtualRoutersByAccount.entity().getState(), SearchCriteria.Op.NIN); + FindIdsOfVirtualRoutersByAccount.done(); CountRunningByHost = createSearchBuilder(Long.class); CountRunningByHost.select(null, Func.COUNT, null); @@ -441,12 +441,12 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem } @Override - public Long countAllocatedVirtualRoutersForAccount(long accountId) { - SearchCriteria sc = CountVirtualRoutersByAccount.create(); + public List findIdsOfAllocatedVirtualRoutersForAccount(long accountId) { + SearchCriteria sc = FindIdsOfVirtualRoutersByAccount.create(); sc.setParameters("account", accountId); sc.setParameters("type", VirtualMachine.Type.DomainRouter); - sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging}); - return customSearch(sc, null).get(0); + sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging}); + return customSearch(sc, null); } @Override diff --git a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java index 665c2e1d859..bfa6981e3fb 100755 --- a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java +++ b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java @@ -820,7 +820,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim newCount = _userVmDao.countAllocatedVMsForAccount(accountId); } else if (type == Resource.ResourceType.volume) { newCount = _volumeDao.countAllocatedVolumesForAccount(accountId); - long virtualRouterCount = _vmDao.countAllocatedVirtualRoutersForAccount(accountId); + long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size(); newCount = newCount - virtualRouterCount; // don't count the volumes of virtual router } else if (type == Resource.ResourceType.snapshot) { newCount = _snapshotDao.countSnapshotsForAccount(accountId); @@ -839,7 +839,8 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim } else if (type == Resource.ResourceType.memory) { newCount = calculateMemoryForAccount(accountId); } else if (type == Resource.ResourceType.primary_storage) { - newCount = _volumeDao.primaryStorageUsedForAccount(accountId); + List virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId); + newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters); } else if (type == Resource.ResourceType.secondary_storage) { newCount = calculateSecondaryStorageForAccount(accountId); } else {