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.
This commit is contained in:
Sanjay Tripathi 2013-07-16 14:52:56 +05:30 committed by Devdeep Singh
parent 6e63f10a4f
commit 539eb38908
5 changed files with 28 additions and 17 deletions

View File

@ -80,9 +80,10 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
* Gets the Total Primary Storage space allocated for an account
*
* @param account
* @param list of ids of virtual router VMs under this account
* @return total Primary Storage space (in bytes) used
*/
long primaryStorageUsedForAccount(long accountId);
long primaryStorageUsedForAccount(long accountId, List<Long> virtualRouters);
/**
* Gets the Total Secondary Storage space used by volumes allocated for an

View File

@ -318,7 +318,10 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> 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<VolumeVO, Long> 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<VolumeVO, Long> implements Vol
}
@Override
public long primaryStorageUsedForAccount(long accountId) {
public long primaryStorageUsedForAccount(long accountId, List<Long> virtualRouters) {
SearchCriteria<SumCount> 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<SumCount> storageSpace = customSearch(sc, null);
if (storageSpace != null) {
return storageSpace.get(0).sum;
@ -364,6 +372,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
public long secondaryStorageUsedForAccount(long accountId) {
SearchCriteria<SumCount> sc = secondaryStorageSearch.create();
sc.setParameters("accountId", accountId);
sc.setParameters("states", State.Allocated);
List<SumCount> storageSpace = customSearch(sc, null);
if (storageSpace != null) {
return storageSpace.get(0).sum;

View File

@ -90,7 +90,7 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
List<VMInstanceVO> listByTypeAndState(VirtualMachine.Type type, State state);
List<VMInstanceVO> listByAccountId(long accountId);
public Long countAllocatedVirtualRoutersForAccount(long accountId);
public List<Long> findIdsOfAllocatedVirtualRoutersForAccount(long accountId);
List<VMInstanceVO> listByClusterId(long clusterId); // this does not pull up VMs which are starting
List<VMInstanceVO> listLHByClusterId(long clusterId); // get all the VMs even starting one on this cluster

View File

@ -79,7 +79,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
protected SearchBuilder<VMInstanceVO> HostUpSearch;
protected SearchBuilder<VMInstanceVO> InstanceNameSearch;
protected SearchBuilder<VMInstanceVO> HostNameSearch;
protected GenericSearchBuilder<VMInstanceVO, Long> CountVirtualRoutersByAccount;
protected GenericSearchBuilder<VMInstanceVO, Long> FindIdsOfVirtualRoutersByAccount;
protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByHost;
protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByAccount;
protected SearchBuilder<VMInstanceVO> NetworkTypeSearch;
@ -197,12 +197,12 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> 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<VMInstanceVO, Long> implem
}
@Override
public Long countAllocatedVirtualRoutersForAccount(long accountId) {
SearchCriteria<Long> sc = CountVirtualRoutersByAccount.create();
public List<Long> findIdsOfAllocatedVirtualRoutersForAccount(long accountId) {
SearchCriteria<Long> 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

View File

@ -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<Long> virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId);
newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters);
} else if (type == Resource.ResourceType.secondary_storage) {
newCount = calculateSecondaryStorageForAccount(accountId);
} else {