CLOUDSTACK-5281:

Resource limit shouldnt be counted for resources with display flag = 0. Correcting this for the vms at the moment.
This commit is contained in:
Nitin Mehta 2013-12-20 15:33:37 -08:00
parent 08a69b0053
commit f083f91b0b
4 changed files with 54 additions and 37 deletions

View File

@ -140,7 +140,7 @@ public interface ResourceLimitService {
public long getResourceCount(Account account, ResourceType type);
/**
* Checks if a limit has been exceeded for an account depending on the displayResource flag
* Checks if a limit has been exceeded for an account if displayResource flag is on
*
* @param account
* @param type
@ -153,7 +153,7 @@ public interface ResourceLimitService {
void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count) throws ResourceAllocationException;
/**
* Increments the resource count depending on the displayResource flag
* Increments the resource count if displayResource flag is on
*
* @param accountId
* @param type
@ -163,7 +163,7 @@ public interface ResourceLimitService {
void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
/**
* Increments/Decrements the resource count depending on the displayResource flag
* Increments/Decrements the resource count depending on the displayResource flag is turned on or off respectively
*
* @param accountId
* @param type
@ -172,5 +172,13 @@ public interface ResourceLimitService {
*/
void changeResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
/**
* Decrements the resource count if displayResource flag is on
*
* @param accountId
* @param type
* @param displayResource
* @param delta
*/
void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
}

View File

@ -175,6 +175,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
CountByAccount.and("account", CountByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
CountByAccount.and("type", CountByAccount.entity().getType(), SearchCriteria.Op.EQ);
CountByAccount.and("state", CountByAccount.entity().getState(), SearchCriteria.Op.NIN);
CountByAccount.and("displayVm", CountByAccount.entity().isDisplayVm(), SearchCriteria.Op.EQ);
CountByAccount.done();
SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
@ -560,6 +561,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
sc.setParameters("account", accountId);
sc.setParameters("type", VirtualMachine.Type.User);
sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging});
sc.setParameters("displayVm", 1);
return customSearch(sc, null).get(0);
}

View File

@ -879,6 +879,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
join1.and("accountId", join1.entity().getAccountId(), Op.EQ);
join1.and("type", join1.entity().getType(), Op.EQ);
join1.and("state", join1.entity().getState(), SearchCriteria.Op.NIN);
join1.and("displayVm", join1.entity().isDisplayVm(), Op.EQ);
cpuSearch.join("offerings", join1, cpuSearch.entity().getId(), join1.entity().getServiceOfferingId(), JoinBuilder.JoinType.INNER);
cpuSearch.done();
@ -886,6 +887,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
sc.setJoinParameters("offerings", "accountId", accountId);
sc.setJoinParameters("offerings", "type", VirtualMachine.Type.User);
sc.setJoinParameters("offerings", "state", new Object[] {State.Destroyed, State.Error, State.Expunging});
sc.setJoinParameters("offerings", "displayVm", 1);
List<SumCount> cpus = _serviceOfferingDao.customSearch(sc, null);
if (cpus != null) {
return cpus.get(0).sum;
@ -901,6 +903,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
join1.and("accountId", join1.entity().getAccountId(), Op.EQ);
join1.and("type", join1.entity().getType(), Op.EQ);
join1.and("state", join1.entity().getState(), SearchCriteria.Op.NIN);
join1.and("displayVm", join1.entity().isDisplayVm(), Op.EQ);
memorySearch.join("offerings", join1, memorySearch.entity().getId(), join1.entity().getServiceOfferingId(), JoinBuilder.JoinType.INNER);
memorySearch.done();
@ -908,6 +911,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
sc.setJoinParameters("offerings", "accountId", accountId);
sc.setJoinParameters("offerings", "type", VirtualMachine.Type.User);
sc.setJoinParameters("offerings", "state", new Object[] {State.Destroyed, State.Error, State.Expunging});
sc.setJoinParameters("offerings", "displayVm", 1);
List<SumCount> memory = _serviceOfferingDao.customSearch(sc, null);
if (memory != null) {
return memory.get(0).sum;
@ -954,14 +958,17 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
return _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type);
}
private boolean isDisplayFlagOn(Boolean displayResource){
// 1. If its null assume displayResource = 1
// 2. If its not null then send true if displayResource = 1
return (displayResource == null) || (displayResource != null && displayResource);
}
@Override
public void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count) throws ResourceAllocationException {
// By default its always on.
// TODO boilerplate code.
boolean displayflag = (displayResource == null) || (displayResource != null && displayResource);
if (displayflag) {
if (isDisplayFlagOn(displayResource)) {
checkResourceLimit(account, type, count);
}
}
@ -969,9 +976,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
@Override
public void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
// 1. If its null assume displayResource = 1
// 2. If its not null then increment if displayResource = 1
if (displayResource == null || (displayResource != null && displayResource)) {
if (isDisplayFlagOn(displayResource)) {
incrementResourceCount(accountId, type, delta);
}
}
@ -979,9 +984,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
@Override
public void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
// 1. If its null assume displayResource = 1
// 2. If its not null then decrement if displayResource = 1
if (displayResource == null || (displayResource != null && displayResource)) {
if (isDisplayFlagOn(displayResource)) {
decrementResourceCount(accountId, type, delta);
}
}
@ -995,7 +998,6 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
// Increment because the display is turned on.
if (displayResource) {
// checkResourceLimit((Account)_accountDao.findById(accountId), type, delta);
incrementResourceCount(accountId, type, delta);
} else {
decrementResourceCount(accountId, type, delta);

View File

@ -485,22 +485,22 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
return _vmDao.listByHostId(hostId);
}
protected void resourceLimitCheck(Account owner, Long cpu, Long memory) throws ResourceAllocationException {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.user_vm);
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, cpu);
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.memory, memory);
protected void resourceLimitCheck(Account owner, Boolean displayVm, Long cpu, Long memory) throws ResourceAllocationException {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.user_vm, displayVm);
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, displayVm, cpu);
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.memory, displayVm, memory);
}
protected void resourceCountIncrement(long accountId, Long cpu, Long memory) {
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.user_vm);
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.cpu, cpu);
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.memory, memory);
protected void resourceCountIncrement(long accountId, Boolean displayVm, Long cpu, Long memory) {
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.user_vm, displayVm);
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.cpu, displayVm, cpu);
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.memory, displayVm, memory);
}
protected void resourceCountDecrement(long accountId, Long cpu, Long memory) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.user_vm);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.cpu, cpu);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.memory, memory);
protected void resourceCountDecrement(long accountId, Boolean displayVm, Long cpu, Long memory) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.user_vm, displayVm);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.cpu, displayVm, cpu);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.memory, displayVm, memory);
}
@Override
@ -1538,7 +1538,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
// First check that the maximum number of UserVMs, CPU and Memory limit for the given
// accountId will not be exceeded
resourceLimitCheck(account, new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
resourceLimitCheck(account, vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
_haMgr.cancelDestroy(vm, vm.getHostId());
@ -1571,7 +1571,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
//Update Resource Count for the given account
resourceCountIncrement(account.getId(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
resourceCountIncrement(account.getId(), vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
}
});
@ -1782,7 +1782,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
// Update Resource Count for the given account
resourceCountDecrement(vm.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
}
}
}
@ -1846,10 +1846,15 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, vmInstance);
if (isDisplayVmEnabled != null) {
//If the flag is specified and is changed
if (isDisplayVmEnabled != null && isDisplayVmEnabled != vmInstance.isDisplayVm()) {
if (!_accountMgr.isRootAdmin(caller.getType())) {
throw new PermissionDeniedException("Cannot update parameter displayvm, only admin permitted ");
}
ServiceOffering offering = _serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId());
_resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.user_vm, isDisplayVmEnabled);
_resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.cpu, isDisplayVmEnabled, new Long(offering.getCpu()));
_resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.memory, isDisplayVmEnabled, new Long(offering.getRamSize()));
}
return updateVirtualMachine(id, displayName, group, ha, isDisplayVmEnabled, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod(), cmd.getCustomId());
@ -2535,7 +2540,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
size += _diskOfferingDao.findById(diskOfferingId).getDiskSize();
}
resourceLimitCheck(owner, new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceLimitCheck(owner, isDisplayVmEnabled, new Long(offering.getCpu()), new Long(offering.getRamSize()));
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.volume, (isIso || diskOfferingId == null ? 1 : 2));
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage, size);
@ -2917,7 +2922,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
//Update Resource Count for the given account
resourceCountIncrement(accountId, new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountIncrement(accountId, isDisplayVmEnabled, new Long(offering.getCpu()), new Long(offering.getRamSize()));
return vm;
}
});
@ -3425,7 +3430,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
//Update Resource Count for the given account
resourceCountDecrement(vm.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
}
return _vmDao.findById(vmId);
} else {
@ -4376,7 +4381,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
removeInstanceFromInstanceGroup(cmd.getVmId());
// VV 2: check if account/domain is with in resource limits to create a new vm
resourceLimitCheck(newAccount, new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceLimitCheck(newAccount, vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
// VV 3: check if volumes and primary storage space are with in resource limits
_resourceLimitMgr.checkResourceLimit(newAccount, ResourceType.volume, _volsDao.findByInstance(cmd.getVmId()).size());
@ -4405,7 +4410,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString(), VirtualMachine.class.getName(), vm.getUuid());
// update resource counts for old account
resourceCountDecrement(oldAccount.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountDecrement(oldAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
// OWNERSHIP STEP 1: update the vm owner
vm.setAccountId(newAccount.getAccountId());
@ -4433,7 +4438,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
//update resource count of new account
resourceCountIncrement(newAccount.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountIncrement(newAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
//generate usage events to account for this change
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), vm.getHostName(),