Search in sources :

Example 16 with InsufficientCapacityException

use of com.cloud.exception.InsufficientCapacityException in project cloudstack by apache.

the class UserVmManagerImpl method rebootVirtualMachine.

private UserVm rebootVirtualMachine(long userId, long vmId) throws InsufficientCapacityException, ResourceUnavailableException {
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) {
        s_logger.warn("Vm id=" + vmId + " doesn't exist");
        return null;
    }
    if (vm.getState() == State.Running && vm.getHostId() != null) {
        collectVmDiskStatistics(vm);
        DataCenterVO dc = _dcDao.findById(vm.getDataCenterId());
        try {
            if (dc.getNetworkType() == DataCenter.NetworkType.Advanced) {
                //List all networks of vm
                List<Long> vmNetworks = _vmNetworkMapDao.getNetworks(vmId);
                List<DomainRouterVO> routers = new ArrayList<DomainRouterVO>();
                //List the stopped routers
                for (long vmNetworkId : vmNetworks) {
                    List<DomainRouterVO> router = _routerDao.listStopped(vmNetworkId);
                    routers.addAll(router);
                }
                //and routers are started serially ,may revisit to make this process parallel
                for (DomainRouterVO routerToStart : routers) {
                    s_logger.warn("Trying to start router " + routerToStart.getInstanceName() + " as part of vm: " + vm.getInstanceName() + " reboot");
                    _virtualNetAppliance.startRouter(routerToStart.getId(), true);
                }
            }
        } catch (ConcurrentOperationException e) {
            throw new CloudRuntimeException("Concurrent operations on starting router. " + e);
        } catch (Exception ex) {
            throw new CloudRuntimeException("Router start failed due to" + ex);
        } finally {
            s_logger.info("Rebooting vm " + vm.getInstanceName());
            _itMgr.reboot(vm.getUuid(), null);
        }
        return _vmDao.findById(vmId);
    } else {
        s_logger.error("Vm id=" + vmId + " is not in Running state, failed to reboot");
        return null;
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) 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) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Example 17 with InsufficientCapacityException

use of com.cloud.exception.InsufficientCapacityException in project cloudstack by apache.

the class UserVmManagerImpl method upgradeRunningVirtualMachine.

private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
    Account caller = CallContext.current().getCallingAccount();
    VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
    if (vmInstance.getHypervisorType() != HypervisorType.XenServer && vmInstance.getHypervisorType() != HypervisorType.VMware && vmInstance.getHypervisorType() != HypervisorType.Simulator) {
        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);
    if (newServiceOffering.isDynamic()) {
        newServiceOffering.setDynamicFlag(true);
        validateCustomParameters(newServiceOffering, customParameters);
        newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customParameters);
    }
    // Check that the specified service offering ID is valid
    _itMgr.checkIfCanUpgrade(vmInstance, newServiceOffering);
    ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
    int newCpu = newServiceOffering.getCpu();
    int newMemory = newServiceOffering.getRamSize();
    int newSpeed = newServiceOffering.getSpeed();
    int currentCpu = currentServiceOffering.getCpu();
    int currentMemory = currentServiceOffering.getRamSize();
    int currentSpeed = currentServiceOffering.getSpeed();
    int memoryDiff = newMemory - currentMemory;
    int cpuDiff = newCpu * newSpeed - currentCpu * currentSpeed;
    // Don't allow to scale when (Any of the new values less than current values) OR (All current and new values are same)
    if ((newSpeed < currentSpeed || newMemory < currentMemory || newCpu < currentCpu) || (newSpeed == currentSpeed && newMemory == currentMemory && newCpu == currentCpu)) {
        throw new InvalidParameterValueException("Only scaling up the vm is supported, new service offering(speed=" + newSpeed + ",cpu=" + newCpu + ",memory=," + newMemory + ")" + " should have at least one value(cpu/ram) greater than old value and no resource value less than older(speed=" + currentSpeed + ",cpu=" + currentCpu + ",memory=," + currentMemory + ")");
    }
    _offeringDao.loadDetails(currentServiceOffering);
    _offeringDao.loadDetails(newServiceOffering);
    Map<String, String> currentDetails = currentServiceOffering.getDetails();
    Map<String, String> newDetails = newServiceOffering.getDetails();
    String currentVgpuType = currentDetails.get("vgpuType");
    String newVgpuType = newDetails.get("vgpuType");
    if (currentVgpuType != null) {
        if (newVgpuType == null || !newVgpuType.equalsIgnoreCase(currentVgpuType)) {
            throw new InvalidParameterValueException("Dynamic scaling of vGPU type is not supported. VM has vGPU Type: " + currentVgpuType);
        }
    }
    // 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;
        ExcludeList excludes = new ExcludeList();
        // Check zone wide flag
        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
        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, newSpeed) && _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);
                if (newServiceOffering.isDynamic()) {
                    //save the custom values to the database.
                    saveCustomOfferingDetails(vmId, newServiceOffering);
                }
                vmInstance = _vmInstanceDao.findById(vmId);
                _itMgr.reConfigureVm(vmInstance.getUuid(), currentServiceOffering, existingHostHasCapacity);
                success = true;
                if (currentServiceOffering.isDynamic() && !newServiceOffering.isDynamic()) {
                    removeCustomOfferingDetails(vmId);
                }
                return success;
            } catch (InsufficientCapacityException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (ResourceUnavailableException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (ConcurrentOperationException e) {
                s_logger.warn("Received exception while scaling ", e);
            } catch (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));
                    }
                    //restoring old service offering will take care of removing new SO.
                    if (currentServiceOffering.isDynamic()) {
                        saveCustomOfferingDetails(vmId, currentServiceOffering);
                    }
                    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) 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) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Example 18 with InsufficientCapacityException

use of com.cloud.exception.InsufficientCapacityException in project cloudstack by apache.

the class AclOnPrivateGwTest method testExecuteFail.

@Test
public void testExecuteFail() {
    VpcService vpcService = Mockito.mock(VpcService.class);
    createPrivateGwCmd._vpcService = vpcService;
    try {
        Mockito.when(vpcService.applyVpcPrivateGateway(Matchers.anyLong(), Matchers.anyBoolean())).thenReturn(null);
    } catch (ResourceUnavailableException e) {
        e.printStackTrace();
    } catch (ConcurrentOperationException e) {
        e.printStackTrace();
    }
    try {
        createPrivateGwCmd.execute();
    } catch (ServerApiException exception) {
        Assert.assertEquals("Failed to create private gateway", exception.getDescription());
    } catch (ResourceAllocationException e) {
        e.printStackTrace();
    } catch (InsufficientCapacityException e) {
        e.printStackTrace();
    } catch (ConcurrentOperationException e) {
        e.printStackTrace();
    } catch (ResourceUnavailableException e) {
        e.printStackTrace();
    }
}
Also used : VpcService(com.cloud.network.vpc.VpcService) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) Test(org.junit.Test)

Example 19 with InsufficientCapacityException

use of com.cloud.exception.InsufficientCapacityException in project cloudstack by apache.

the class CreatePrivateNetworkTest method createInvalidlyHostedPrivateNetwork.

@Test
@DB
public void createInvalidlyHostedPrivateNetwork() {
    TransactionLegacy __txn;
    __txn = TransactionLegacy.open("createInvalidlyHostedPrivateNetworkTest");
    /* Network nw; */
    try {
        /* nw = */
        networkService.createPrivateNetwork("bla", "fake", 1L, "vlan:1", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, true, 1L);
        /* nw = */
        networkService.createPrivateNetwork("bla", "fake", 1L, "lswitch:3", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, false, 1L);
        boolean invalid = false;
        boolean unsupported = false;
        try {
            /* nw = */
            networkService.createPrivateNetwork("bla", "fake", 1, "bla:2", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, true, 1L);
        } catch (CloudRuntimeException e) {
            Assert.assertEquals("unexpected parameter exception", "string 'bla:2' has an unknown BroadcastDomainType.", e.getMessage());
            invalid = true;
        }
        try {
            /* nw = */
            networkService.createPrivateNetwork("bla", "fake", 1, "mido://4", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, false, 1L);
        } catch (InvalidParameterValueException e) {
            Assert.assertEquals("unexpected parameter exception", "unsupported type of broadcastUri specified: mido://4", e.getMessage());
            unsupported = true;
        }
        Assert.assertEquals("'bla' should not be accepted as scheme", true, invalid);
        Assert.assertEquals("'mido' should not yet be supported as scheme", true, unsupported);
    } catch (ResourceAllocationException e) {
        s_logger.error("no resources", e);
        fail("no resources");
    } catch (ConcurrentOperationException e) {
        s_logger.error("another one is in the way", e);
        fail("another one is in the way");
    } catch (InsufficientCapacityException e) {
        s_logger.error("no capacity", e);
        fail("no capacity");
    } finally {
        __txn.close("createInvalidlyHostedPrivateNetworkTest");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) Test(org.junit.Test) DB(com.cloud.utils.db.DB)

Example 20 with InsufficientCapacityException

use of com.cloud.exception.InsufficientCapacityException in project cloudstack by apache.

the class VpcVirtualRouterElement method getRouters.

@Override
protected List<DomainRouterVO> getRouters(final Network network, final DeployDestination dest) {
    //1st time it runs the domain router of the VM shall be returned
    List<DomainRouterVO> routers = super.getRouters(network, dest);
    if (routers.size() > 0) {
        return routers;
    }
    //For the 2nd time it returns the VPC routers.
    final Long vpcId = network.getVpcId();
    if (vpcId == null) {
        s_logger.error("Network " + network + " is not associated with any VPC");
        return routers;
    }
    final Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
    if (vpc == null) {
        s_logger.warn("Unable to find Enabled VPC by id " + vpcId);
        return routers;
    }
    final RouterDeploymentDefinition routerDeploymentDefinition = routerDeploymentDefinitionBuilder.create().setGuestNetwork(network).setVpc(vpc).setDeployDestination(dest).setAccountOwner(_accountMgr.getAccount(vpc.getAccountId())).build();
    try {
        routers = routerDeploymentDefinition.deployVirtualRouter();
    } catch (final ConcurrentOperationException e) {
        s_logger.error("Error occurred when loading routers from routerDeploymentDefinition.deployVirtualRouter()!", e);
    } catch (final InsufficientCapacityException e) {
        s_logger.error("Error occurred when loading routers from routerDeploymentDefinition.deployVirtualRouter()!", e);
    } catch (final ResourceUnavailableException e) {
        s_logger.error("Error occurred when loading routers from routerDeploymentDefinition.deployVirtualRouter()!", e);
    }
    return routers;
}
Also used : RouterDeploymentDefinition(org.cloud.network.router.deployment.RouterDeploymentDefinition) Vpc(com.cloud.network.vpc.Vpc) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)85 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)77 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)70 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)41 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)36 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)23 ServerApiException (org.apache.cloudstack.api.ServerApiException)23 Account (com.cloud.user.Account)18 ConfigurationException (javax.naming.ConfigurationException)17 ArrayList (java.util.ArrayList)15 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)14 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)14 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)12 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)11 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)11 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)10 NetworkVO (com.cloud.network.dao.NetworkVO)10 DB (com.cloud.utils.db.DB)10 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)9 Network (com.cloud.network.Network)9