Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Map;

import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.log4j.Logger;

import org.apache.cloudstack.acl.RoleType;
Expand Down Expand Up @@ -263,8 +264,13 @@ public long getEntityOwnerId() {
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
UserVm result = _userVmService.updateVirtualMachine(this);
if (result != null){
UserVm result = null;
try {
result = _userVmService.updateVirtualMachine(this);
} catch (CloudRuntimeException e) {
throw new CloudRuntimeException(String.format("Failed to update VM, due to: %s", e.getLocalizedMessage()), e);
}
if (result != null) {
UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", result).get(0);
response.setResponseName(getCommandName());
setResponseObject(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public interface VirtualMachineManager extends Manager {
ConfigKey<Boolean> VmConfigDriveForceHostCacheUse = new ConfigKey<>("Advanced", Boolean.class, "vm.configdrive.force.host.cache.use", "false",
"If true, config drive is forced to create on the host cache storage. Currently only supported for KVM.", true, ConfigKey.Scope.Zone);

ConfigKey<Boolean> ResoureCountRunningVMsonly = new ConfigKey<Boolean>("Advanced", Boolean.class, "resource.count.running.vms.only", "false",
ConfigKey<Boolean> ResourceCountRunningVMsonly = new ConfigKey<Boolean>("Advanced", Boolean.class, "resource.count.running.vms.only", "false",
"Count the resources of only running VMs in resource limitation.", true);

ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new ConfigKey<Boolean>("Advanced", Boolean.class, "account.allow.expose.host.hostname",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,9 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil

final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());

// check resource count if ResoureCountRunningVMsonly.value() = true
// check resource count if ResourceCountRunningVMsonly.value() = true
final Account owner = _entityMgr.findById(Account.class, vm.getAccountId());
if (VirtualMachine.Type.User.equals(vm.type) && ResoureCountRunningVMsonly.value()) {
if (VirtualMachine.Type.User.equals(vm.type) && ResourceCountRunningVMsonly.value()) {
resourceCountIncrement(owner.getAccountId(),new Long(offering.getCpu()), new Long(offering.getRamSize()));
}

Expand Down Expand Up @@ -1367,7 +1367,7 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
}
} finally {
if (startedVm == null) {
if (VirtualMachine.Type.User.equals(vm.type) && ResoureCountRunningVMsonly.value()) {
if (VirtualMachine.Type.User.equals(vm.type) && ResourceCountRunningVMsonly.value()) {
resourceCountDecrement(owner.getAccountId(),new Long(offering.getCpu()), new Long(offering.getRamSize()));
}
if (canRetry) {
Expand Down Expand Up @@ -2133,7 +2133,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl

boolean result = stateTransitTo(vm, Event.OperationSucceeded, null);
if (result) {
if (VirtualMachine.Type.User.equals(vm.type) && ResoureCountRunningVMsonly.value()) {
if (VirtualMachine.Type.User.equals(vm.type) && ResourceCountRunningVMsonly.value()) {
//update resource count if stop successfully
ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
resourceCountDecrement(vm.getAccountId(),new Long(offering.getCpu()), new Long(offering.getRamSize()));
Expand Down Expand Up @@ -4829,7 +4829,7 @@ public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] { ClusterDeltaSyncInterval, StartRetry, VmDestroyForcestop, VmOpCancelInterval, VmOpCleanupInterval, VmOpCleanupWait,
VmOpLockStateRetry, VmOpWaitInterval, ExecuteInSequence, VmJobCheckInterval, VmJobTimeout, VmJobStateReportInterval,
VmConfigDriveLabel, VmConfigDriveOnPrimaryPool, VmConfigDriveForceHostCacheUse, VmConfigDriveUseHostCacheOnUnsupportedPool,
HaVmRestartHostUp, ResoureCountRunningVMsonly, AllowExposeHypervisorHostname, AllowExposeHypervisorHostnameAccountLevel };
HaVmRestartHostUp, ResourceCountRunningVMsonly, AllowExposeHypervisorHostname, AllowExposeHypervisorHostnameAccountLevel };
}

public List<StoragePoolAllocator> getStoragePoolAllocators() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ public Long doInTransaction(TransactionStatus status) {
protected long recalculateAccountResourceCount(final long accountId, final ResourceType type) {
final Long newCount;
if (type == Resource.ResourceType.user_vm) {
newCount = _userVmDao.countAllocatedVMsForAccount(accountId, VirtualMachineManager.ResoureCountRunningVMsonly.value());
newCount = _userVmDao.countAllocatedVMsForAccount(accountId, VirtualMachineManager.ResourceCountRunningVMsonly.value());
} else if (type == Resource.ResourceType.volume) {
long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size();
newCount = _volumeDao.countAllocatedVolumesForAccount(accountId) - virtualRouterCount; // don't count the volumes of virtual router
Expand Down Expand Up @@ -963,7 +963,7 @@ public long countCpusForAccount(long accountId) {

SearchCriteria<UserVmJoinVO> sc1 = userVmSearch.create();
sc1.setParameters("accountId", accountId);
if (VirtualMachineManager.ResoureCountRunningVMsonly.value())
if (VirtualMachineManager.ResourceCountRunningVMsonly.value())
sc1.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging, State.Stopped});
else
sc1.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging});
Expand All @@ -987,7 +987,7 @@ public long calculateMemoryForAccount(long accountId) {

SearchCriteria<UserVmJoinVO> sc1 = userVmSearch.create();
sc1.setParameters("accountId", accountId);
if (VirtualMachineManager.ResoureCountRunningVMsonly.value())
if (VirtualMachineManager.ResourceCountRunningVMsonly.value())
sc1.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging, State.Stopped});
else
sc1.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging});
Expand Down
79 changes: 64 additions & 15 deletions server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -627,15 +628,15 @@ private void resourceLimitCheck(Account owner, Boolean displayVm, Long cpu, Long
}

protected void resourceCountIncrement(long accountId, Boolean displayVm, Long cpu, Long memory) {
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
_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, Boolean displayVm, Long cpu, Long memory) {
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.user_vm, displayVm);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.cpu, displayVm, cpu);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.memory, displayVm, memory);
Expand Down Expand Up @@ -1112,7 +1113,7 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
int currentMemory = currentServiceOffering.getRamSize();

Account owner = _accountMgr.getActiveAccountById(vmInstance.getAccountId());
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
if (newCpu > currentCpu) {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, newCpu - currentCpu);
}
Expand All @@ -1129,7 +1130,7 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
_itMgr.upgradeVmDb(vmId, newServiceOffering, currentServiceOffering);

// Increment or decrement CPU and Memory count accordingly.
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
if (newCpu > currentCpu) {
_resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.cpu, new Long(newCpu - currentCpu));
} else if (currentCpu > newCpu) {
Expand Down Expand Up @@ -1233,7 +1234,7 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String
int currentMemory = currentServiceOffering.getRamSize();

Account owner = _accountMgr.getActiveAccountById(vmInstance.getAccountId());
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
if (newCpu > currentCpu) {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, newCpu - currentCpu);
}
Expand Down Expand Up @@ -1269,7 +1270,7 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String
_itMgr.upgradeVmDb(vmId, newServiceOffering, currentServiceOffering);

// Increment or decrement CPU and Memory count accordingly.
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
if (newCpu > currentCpu) {
_resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.cpu, new Long(newCpu - currentCpu));
} else if (currentCpu > newCpu) {
Expand Down Expand Up @@ -2178,7 +2179,7 @@ public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationE

// First check that the maximum number of UserVMs, CPU and Memory limit for the given
// accountId will not be exceeded
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
resourceLimitCheck(account, vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
}

Expand Down Expand Up @@ -2584,6 +2585,53 @@ protected void runInContext() {
}
}

private void verifyVmLimits(UserVmVO vmInstance, Map<String, String> details) {
Account owner = _accountDao.findById(vmInstance.getAccountId());
if (owner == null) {
throw new InvalidParameterValueException("The owner of " + vmInstance + " does not exist: " + vmInstance.getAccountId());
}

long newCpu = NumberUtils.toLong(details.get(VmDetailConstants.CPU_NUMBER));
long newMemory = NumberUtils.toLong(details.get(VmDetailConstants.MEMORY));
Comment thread
Pearl1594 marked this conversation as resolved.
ServiceOfferingVO currentServiceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
ServiceOfferingVO svcOffering = _serviceOfferingDao.findById(vmInstance.getServiceOfferingId());
boolean isDynamic = currentServiceOffering.isDynamic();
if (isDynamic) {
Map<String, String> customParameters = new HashMap<>();
customParameters.put(VmDetailConstants.CPU_NUMBER, String.valueOf(newCpu));
customParameters.put(VmDetailConstants.MEMORY, String.valueOf(newMemory));
validateCustomParameters(svcOffering, customParameters);
}
if (VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
return;
}
long currentCpu = currentServiceOffering.getCpu();
long currentMemory = currentServiceOffering.getRamSize();

try {
if (newCpu > currentCpu) {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, newCpu - currentCpu);
}
if (newMemory > currentMemory) {
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.memory, newMemory - currentMemory);
}
} catch (ResourceAllocationException e) {
s_logger.error(String.format("Failed to updated VM due to: %s", e.getLocalizedMessage()));
throw new InvalidParameterValueException(e.getLocalizedMessage());
}

if (newCpu > currentCpu) {
_resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.cpu, newCpu - currentCpu);
} else if (newCpu > 0 && currentCpu > newCpu){
_resourceLimitMgr.decrementResourceCount(owner.getAccountId(), ResourceType.cpu, currentCpu - newCpu);
}
if (newMemory > currentMemory) {
_resourceLimitMgr.incrementResourceCount(owner.getAccountId(), ResourceType.memory, newMemory - currentMemory);
} else if (newMemory > 0 && currentMemory > newMemory){
_resourceLimitMgr.decrementResourceCount(owner.getAccountId(), ResourceType.memory, currentMemory - newMemory);
}
}

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_UPDATE, eventDescription = "updating Vm")
public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException {
Expand Down Expand Up @@ -2657,6 +2705,8 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
}
}
}

verifyVmLimits(vmInstance, details);
vmInstance.setDetails(details);
_vmDao.saveDetails(vmInstance);
}
Expand All @@ -2679,9 +2729,9 @@ protected void updateDisplayVmFlag(Boolean isDisplayVm, Long id, UserVmVO vmInst
// Resource limit changes
ServiceOffering offering = _serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
if (isDisplayVm) {
resourceCountIncrement(vmInstance.getAccountId(), true, new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountIncrement(vmInstance.getAccountId(), true, Long.valueOf(offering.getCpu()), Long.valueOf(offering.getRamSize()));
} else {
resourceCountDecrement(vmInstance.getAccountId(), true, new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceCountDecrement(vmInstance.getAccountId(), true, Long.valueOf(offering.getCpu()), Long.valueOf(offering.getRamSize()));
}

// Usage
Expand Down Expand Up @@ -3747,7 +3797,7 @@ private UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOffe
}
size += _diskOfferingDao.findById(diskOfferingId).getDiskSize();
}
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
resourceLimitCheck(owner, isDisplayVm, new Long(offering.getCpu()), new Long(offering.getRamSize()));
}

Expand Down Expand Up @@ -4974,12 +5024,11 @@ public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMach
if (owner.getState() == Account.State.disabled) {
throw new PermissionDeniedException("The owner of " + vm + " is disabled: " + vm.getAccountId());
}
if (VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
// check if account/domain is with in resource limits to start a new vm
ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
resourceLimitCheck(owner, vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
resourceLimitCheck(owner, vm.isDisplayVm(), Long.valueOf(offering.getCpu()), Long.valueOf(offering.getRamSize()));
Comment thread
Pearl1594 marked this conversation as resolved.
}

// check if vm is security group enabled
if (_securityGroupMgr.isVmSecurityGroupEnabled(vmId) && _securityGroupMgr.getSecurityGroupsForVm(vmId).isEmpty()
&& !_securityGroupMgr.isVmMappedToDefaultSecurityGroup(vmId) && _networkModel.canAddDefaultSecurityGroup()) {
Expand Down Expand Up @@ -6672,7 +6721,7 @@ public UserVm moveVMToUser(final AssignVMCmd cmd) throws ResourceAllocationExcep
removeInstanceFromInstanceGroup(cmd.getVmId());

// VV 2: check if account/domain is with in resource limits to create a new vm
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
resourceLimitCheck(newAccount, vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
}

Expand Down Expand Up @@ -6730,7 +6779,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
}

//update resource count of new account
if (! VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
if (! VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
resourceCountIncrement(newAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
}

Expand Down
Loading