Search in sources :

Example 31 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method upgradeRunningVirtualMachine.

private boolean upgradeRunningVirtualMachine(final Long vmId, final Long newServiceOfferingId, final Map<String, String> customParameters) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
    final Account caller = CallContext.current().getCallingAccount();
    VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
    if (vmInstance.getHypervisorType() != HypervisorType.XenServer) {
        s_logger.info("Scaling the VM dynamically is not supported for VMs running on Hypervisor " + vmInstance.getHypervisorType());
        throw new InvalidParameterValueException("Scaling the VM dynamically is not supported for VMs running on Hypervisor " + vmInstance.getHypervisorType());
    }
    _accountMgr.checkAccess(caller, null, true, vmInstance);
    // Check if its a scale "up"
    ServiceOfferingVO newServiceOffering = _offeringDao.findById(newServiceOfferingId);
    // Check that the specified service offering ID is valid
    _itMgr.checkIfCanUpgrade(vmInstance, newServiceOffering);
    final ServiceOffering currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
    final int newCpu = newServiceOffering.getCpu();
    final int newMemory = newServiceOffering.getRamSize();
    final int currentCpu = currentServiceOffering.getCpu();
    final int currentMemory = currentServiceOffering.getRamSize();
    final int memoryDiff = newMemory - currentMemory;
    final int cpuDiff = newCpu - currentCpu;
    // Don't allow to scale when (Any of the new values less than current values) OR (All current and new values are same)
    if (newMemory < currentMemory || newCpu < currentCpu || newMemory == currentMemory && newCpu == currentCpu) {
        throw new InvalidParameterValueException("Only scaling up the vm is supported, new service offering(cpu=" + newCpu + ",memory=," + newMemory + ")" + " should have at least one value(cpu/ram) greater than old value and no resource value less than older(cpu=" + currentCpu + ",memory=," + currentMemory + ")");
    }
    // Check resource limits
    if (newCpu > currentCpu) {
        _resourceLimitMgr.checkResourceLimit(caller, ResourceType.cpu, newCpu - currentCpu);
    }
    if (newMemory > currentMemory) {
        _resourceLimitMgr.checkResourceLimit(caller, ResourceType.memory, newMemory - currentMemory);
    }
    // Dynamically upgrade the running vms
    boolean success = false;
    if (vmInstance.getState().equals(State.Running)) {
        int retry = _scaleRetry;
        final ExcludeList excludes = new ExcludeList();
        // Check zone wide flag
        final boolean enableDynamicallyScaleVm = EnableDynamicallyScaleVm.valueIn(vmInstance.getDataCenterId());
        if (!enableDynamicallyScaleVm) {
            throw new PermissionDeniedException("Dynamically scaling virtual machines is disabled for this zone, please contact your admin");
        }
        // Check vm flag
        if (!vmInstance.isDynamicallyScalable()) {
            throw new CloudRuntimeException("Unable to Scale the vm: " + vmInstance.getUuid() + " as vm does not have tools to support dynamic scaling");
        }
        // Check disable threshold for cluster is not crossed
        final HostVO host = _hostDao.findById(vmInstance.getHostId());
        if (_capacityMgr.checkIfClusterCrossesThreshold(host.getClusterId(), cpuDiff, memoryDiff)) {
            throw new CloudRuntimeException("Unable to scale vm: " + vmInstance.getUuid() + " due to insufficient resources");
        }
        while (retry-- != 0) {
            // It's != so that it can match -1.
            try {
                boolean existingHostHasCapacity = false;
                // Increment CPU and Memory count accordingly.
                if (newCpu > currentCpu) {
                    _resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long(newCpu - currentCpu));
                }
                if (memoryDiff > 0) {
                    _resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long(memoryDiff));
                }
                // #1 Check existing host has capacity
                if (!excludes.shouldAvoid(ApiDBUtils.findHostById(vmInstance.getHostId()))) {
                    existingHostHasCapacity = _capacityMgr.checkIfHostHasCpuCapability(vmInstance.getHostId(), newCpu) && _capacityMgr.checkIfHostHasCapacity(vmInstance.getHostId(), cpuDiff, memoryDiff * 1024L * 1024L, false, _capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_CPU), _capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_MEMORY), false);
                    excludes.addHost(vmInstance.getHostId());
                }
                // #2 migrate the vm if host doesn't have capacity or is in avoid set
                if (!existingHostHasCapacity) {
                    _itMgr.findHostAndMigrate(vmInstance.getUuid(), newServiceOfferingId, excludes);
                }
                // #3 scale the vm now
                _itMgr.upgradeVmDb(vmId, newServiceOfferingId);
                vmInstance = _vmInstanceDao.findById(vmId);
                _itMgr.reConfigureVm(vmInstance.getUuid(), currentServiceOffering, existingHostHasCapacity);
                success = true;
                return success;
            } catch (final InsufficientCapacityException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (final ResourceUnavailableException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (final ConcurrentOperationException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (final Exception e) {
                s_logger.warn("Received exception while scaling ", e);
            } finally {
                if (!success) {
                    // rollback
                    _itMgr.upgradeVmDb(vmId, currentServiceOffering.getId());
                    // Decrement CPU and Memory count accordingly.
                    if (newCpu > currentCpu) {
                        _resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long(newCpu - currentCpu));
                    }
                    if (memoryDiff > 0) {
                        _resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long(memoryDiff));
                    }
                }
            }
        }
    }
    return success;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.user.Account) ServiceOffering(com.cloud.offering.ServiceOffering) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) HostVO(com.cloud.host.HostVO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Example 32 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cosmic by MissionCriticalCloud.

the class ScaleVMCmdByAdmin method execute.

@Override
public void execute() {
    final UserVm result;
    try {
        result = _userVmService.upgradeVirtualMachine(this);
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (final ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (final VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        final List<UserVmResponse> responseList = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result);
        final UserVmResponse response = responseList.get(0);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Aggregations

ManagementServerException (com.cloud.exception.ManagementServerException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)25 UserVm (com.cloud.uservm.UserVm)18 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)17 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)16 VirtualMachine (com.cloud.vm.VirtualMachine)10 ServerApiException (com.cloud.api.ServerApiException)8 Host (com.cloud.host.Host)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)6 UserVmResponse (com.cloud.api.response.UserVmResponse)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 ArrayList (java.util.ArrayList)5 StoragePool (com.cloud.storage.StoragePool)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)4 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)4 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)3 KubernetesClusterVmMapVO (com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO)3 IpAddress (com.cloud.network.IpAddress)3