Search in sources :

Example 76 with ResourceAllocationException

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

the class IpAddressManagerImpl method associateIPToGuestNetwork.

@DB
@Override
public IPAddressVO associateIPToGuestNetwork(final long ipId, final long networkId, final boolean releaseOnFailure) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException {
    final Account caller = CallContext.current().getCallingAccount();
    Account owner = null;
    final IPAddressVO ipToAssoc = _ipAddressDao.findById(ipId);
    if (ipToAssoc != null) {
        final Network network = _networksDao.findById(networkId);
        if (network == null) {
            throw new InvalidParameterValueException("Invalid network id is given");
        }
        final DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
        if (zone.getNetworkType() == NetworkType.Advanced) {
            if (network.getGuestType() == Network.GuestType.Shared) {
                if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
                    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.UseEntry, false, network);
                } else {
                    throw new InvalidParameterValueException("IP can be associated with guest network of 'shared' type only if " + "network services Source Nat, Static Nat, Port Forwarding, Load balancing, firewall are enabled in the network");
                }
            }
        } else {
            _accountMgr.checkAccess(caller, null, true, ipToAssoc);
        }
        owner = _accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
    } else {
        s_logger.debug("Unable to find ip address by id: " + ipId);
        return null;
    }
    if (ipToAssoc.getAssociatedWithNetworkId() != null) {
        s_logger.debug("IP " + ipToAssoc + " is already assocaited with network id" + networkId);
        return ipToAssoc;
    }
    final Network network = _networksDao.findById(networkId);
    if (network != null) {
        _accountMgr.checkAccess(owner, AccessType.UseEntry, false, network);
    } else {
        s_logger.debug("Unable to find ip address by id: " + ipId);
        return null;
    }
    final DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    // allow associating IP addresses to guest network only
    if (network.getTrafficType() != TrafficType.Guest) {
        throw new InvalidParameterValueException("Ip address can be associated to the network with trafficType " + TrafficType.Guest);
    }
    // - and it belongs to the system
    if (network.getAccountId() != owner.getId()) {
        if (zone.getNetworkType() != NetworkType.Basic && !(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Shared)) {
            throw new InvalidParameterValueException("The owner of the network is not the same as owner of the IP");
        }
    }
    if (zone.getNetworkType() == NetworkType.Advanced) {
        // In Advance zone allow to do IP assoc only for Isolated networks with source nat service enabled
        if (network.getGuestType() == GuestType.Isolated && !(_networkModel.areServicesSupportedInNetwork(network.getId(), Service.SourceNat))) {
            throw new InvalidParameterValueException("In zone of type " + NetworkType.Advanced + " ip address can be associated only to the network of guest type " + GuestType.Isolated + " with the " + Service.SourceNat.getName() + " enabled");
        }
        // In Advance zone allow to do IP assoc only for shared networks with source nat/static nat/lb/pf services enabled
        if (network.getGuestType() == GuestType.Shared && !isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
            throw new InvalidParameterValueException("In zone of type " + NetworkType.Advanced + " ip address can be associated with network of guest type " + GuestType.Shared + "only if at " + "least one of the services " + Service.SourceNat.getName() + "/" + Service.StaticNat.getName() + "/" + Service.Lb.getName() + "/" + Service.PortForwarding.getName() + " is enabled");
        }
    }
    s_logger.debug("Associating ip " + ipToAssoc + " to network " + network);
    final IPAddressVO ip = _ipAddressDao.findById(ipId);
    // update ip address with networkId
    ip.setAssociatedWithNetworkId(networkId);
    _ipAddressDao.update(ipId, ip);
    boolean success = false;
    try {
        success = applyIpAssociations(network, false);
        if (success) {
            s_logger.debug("Successfully associated ip address " + ip.getAddress().addr() + " to network " + network);
        } else {
            s_logger.warn("Failed to associate ip address " + ip.getAddress().addr() + " to network " + network);
        }
        return ip;
    } finally {
        if (!success && releaseOnFailure) {
            if (ip != null) {
                try {
                    s_logger.warn("Failed to associate ip address, so releasing ip from the database " + ip);
                    _ipAddressDao.markAsUnavailable(ip.getId());
                    if (!applyIpAssociations(network, true)) {
                        // if fail to apply ip assciations again, unassign ip address without updating resource
                        // count and generating usage event as there is no need to keep it in the db
                        _ipAddressDao.unassignIpAddress(ip.getId());
                    }
                } catch (final Exception e) {
                    s_logger.warn("Unable to disassociate ip address for recovery", e);
                }
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) IPAddressVO(com.cloud.network.dao.IPAddressVO) AccountLimitException(com.cloud.exception.AccountLimitException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 77 with ResourceAllocationException

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

the class VolumeServiceImpl method handleVolumeSync.

@Override
public void handleVolumeSync(final DataStore store) {
    if (store == null) {
        s_logger.warn("Huh? image store is null");
        return;
    }
    final long storeId = store.getId();
    // add lock to make template sync for a data store only be done once
    final String lockString = "volumesync.storeId:" + storeId;
    final GlobalLock syncLock = GlobalLock.getInternLock(lockString);
    try {
        if (syncLock.lock(3)) {
            try {
                final Map<Long, TemplateProp> volumeInfos = listVolume(store);
                if (volumeInfos == null) {
                    return;
                }
                // find all the db volumes including those with NULL url column to avoid accidentally deleting volumes on image store later.
                final List<VolumeDataStoreVO> dbVolumes = _volumeStoreDao.listByStoreId(storeId);
                final List<VolumeDataStoreVO> toBeDownloaded = new ArrayList<>(dbVolumes);
                for (final VolumeDataStoreVO volumeStore : dbVolumes) {
                    final VolumeVO volume = volDao.findById(volumeStore.getVolumeId());
                    if (volume == null) {
                        s_logger.warn("Volume_store_ref table shows that volume " + volumeStore.getVolumeId() + " is on image store " + storeId + ", but the volume is not found in volumes table, potentially some bugs in deleteVolume, so we just treat this volume to be deleted and mark " + "it as destroyed");
                        volumeStore.setDestroyed(true);
                        _volumeStoreDao.update(volumeStore.getId(), volumeStore);
                        continue;
                    }
                    // Exists then don't download
                    if (volumeInfos.containsKey(volume.getId())) {
                        final TemplateProp volInfo = volumeInfos.remove(volume.getId());
                        toBeDownloaded.remove(volumeStore);
                        s_logger.info("Volume Sync found " + volume.getUuid() + " already in the volume image store table");
                        if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
                            volumeStore.setErrorString("");
                        }
                        if (volInfo.isCorrupted()) {
                            volumeStore.setDownloadState(Status.DOWNLOAD_ERROR);
                            String msg = "Volume " + volume.getUuid() + " is corrupted on image store";
                            volumeStore.setErrorString(msg);
                            s_logger.info(msg);
                            if (volume.getState() == State.NotUploaded || volume.getState() == State.UploadInProgress) {
                                s_logger.info("Volume Sync found " + volume.getUuid() + " uploaded using SSVM on image store " + storeId + " as corrupted, marking it as " + "failed");
                                _volumeStoreDao.update(volumeStore.getId(), volumeStore);
                                // mark volume as failed, so that storage GC will clean it up
                                final VolumeObject volObj = (VolumeObject) volFactory.getVolume(volume.getId());
                                volObj.processEvent(Event.OperationFailed);
                            } else if (volumeStore.getDownloadUrl() == null) {
                                msg = "Volume (" + volume.getUuid() + ") with install path " + volInfo.getInstallPath() + " is corrupted, please check in image store: " + volumeStore.getDataStoreId();
                                s_logger.warn(msg);
                            } else {
                                s_logger.info("Removing volume_store_ref entry for corrupted volume " + volume.getName());
                                _volumeStoreDao.remove(volumeStore.getId());
                                toBeDownloaded.add(volumeStore);
                            }
                        } else {
                            // Put them in right status
                            volumeStore.setDownloadPercent(100);
                            volumeStore.setDownloadState(Status.DOWNLOADED);
                            volumeStore.setState(ObjectInDataStoreStateMachine.State.Ready);
                            volumeStore.setInstallPath(volInfo.getInstallPath());
                            volumeStore.setSize(volInfo.getSize());
                            volumeStore.setPhysicalSize(volInfo.getPhysicalSize());
                            volumeStore.setLastUpdated(new Date());
                            _volumeStoreDao.update(volumeStore.getId(), volumeStore);
                            if (volume.getSize() == 0) {
                                // Set volume size in volumes table
                                volume.setSize(volInfo.getSize());
                                volDao.update(volumeStore.getVolumeId(), volume);
                            }
                            if (volume.getState() == State.NotUploaded || volume.getState() == State.UploadInProgress) {
                                final VolumeObject volObj = (VolumeObject) volFactory.getVolume(volume.getId());
                                volObj.processEvent(Event.OperationSuccessed);
                            }
                            if (volInfo.getSize() > 0) {
                                try {
                                    _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()), com.cloud.configuration.Resource.ResourceType.secondary_storage, volInfo.getSize() - volInfo.getPhysicalSize());
                                } catch (final ResourceAllocationException e) {
                                    s_logger.warn(e.getMessage());
                                    _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, volume.getDataCenterId(), volume.getPodId(), e.getMessage(), e.getMessage());
                                } finally {
                                    _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
                                }
                            }
                        }
                        continue;
                    } else if (volume.getState() == State.NotUploaded || volume.getState() == State.UploadInProgress) {
                        // failed uploads through SSVM
                        s_logger.info("Volume Sync did not find " + volume.getUuid() + " uploaded using SSVM on image store " + storeId + ", marking it as failed");
                        toBeDownloaded.remove(volumeStore);
                        volumeStore.setDownloadState(Status.DOWNLOAD_ERROR);
                        final String msg = "Volume " + volume.getUuid() + " is corrupted on image store";
                        volumeStore.setErrorString(msg);
                        _volumeStoreDao.update(volumeStore.getId(), volumeStore);
                        // mark volume as failed, so that storage GC will clean it up
                        final VolumeObject volObj = (VolumeObject) volFactory.getVolume(volume.getId());
                        volObj.processEvent(Event.OperationFailed);
                        continue;
                    }
                    // Volume is not on secondary but we should download.
                    if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
                        s_logger.info("Volume Sync did not find " + volume.getName() + " ready on image store " + storeId + ", will request download to start/resume shortly");
                    }
                }
                // Download volumes which haven't been downloaded yet.
                if (toBeDownloaded.size() > 0) {
                    for (final VolumeDataStoreVO volumeHost : toBeDownloaded) {
                        if (volumeHost.getDownloadUrl() == null) {
                            // If url is null, skip downloading
                            s_logger.info("Skip downloading volume " + volumeHost.getVolumeId() + " since no download url is specified.");
                            continue;
                        }
                        // means that this is a duplicate entry from migration of previous NFS to staging.
                        if (store.getScope().getScopeType() == ScopeType.REGION) {
                            if (volumeHost.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED && volumeHost.getInstallPath() == null) {
                                s_logger.info("Skip sync volume for migration of previous NFS to object store");
                                continue;
                            }
                        }
                        s_logger.debug("Volume " + volumeHost.getVolumeId() + " needs to be downloaded to " + store.getName());
                        // reset volume status back to Allocated
                        VolumeObject vol = (VolumeObject) volFactory.getVolume(volumeHost.getVolumeId());
                        // reset back volume status
                        vol.processEvent(Event.OperationFailed);
                        // remove leftover volume_store_ref entry since re-download will create it again
                        _volumeStoreDao.remove(volumeHost.getId());
                        // get an updated volumeVO
                        vol = (VolumeObject) volFactory.getVolume(volumeHost.getVolumeId());
                        final RegisterVolumePayload payload = new RegisterVolumePayload(volumeHost.getDownloadUrl(), volumeHost.getChecksum(), vol.getFormat().toString());
                        vol.addPayload(payload);
                        createVolumeAsync(vol, store);
                    }
                }
                // Delete volumes which are not present on DB.
                for (final Map.Entry<Long, TemplateProp> entry : volumeInfos.entrySet()) {
                    final Long uniqueName = entry.getKey();
                    final TemplateProp tInfo = entry.getValue();
                    // we cannot directly call expungeVolumeAsync here to reuse delete logic since in this case db does not have this volume at all.
                    final VolumeObjectTO tmplTO = new VolumeObjectTO();
                    tmplTO.setDataStore(store.getTO());
                    tmplTO.setPath(tInfo.getInstallPath());
                    tmplTO.setId(tInfo.getId());
                    final DeleteCommand dtCommand = new DeleteCommand(tmplTO);
                    final EndPoint ep = _epSelector.select(store);
                    Answer answer = null;
                    if (ep == null) {
                        final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                        s_logger.error(errMsg);
                        answer = new Answer(dtCommand, false, errMsg);
                    } else {
                        answer = ep.sendMessage(dtCommand);
                    }
                    if (answer == null || !answer.getResult()) {
                        s_logger.info("Failed to deleted volume at store: " + store.getName());
                    } else {
                        final String description = "Deleted volume " + tInfo.getTemplateName() + " on secondary storage " + storeId;
                        s_logger.info(description);
                    }
                }
            } finally {
                syncLock.unlock();
            }
        } else {
            s_logger.info("Couldn't get global lock on " + lockString + ", another thread may be doing volume sync on data store " + storeId + " now.");
        }
    } finally {
        syncLock.releaseRef();
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) ArrayList(java.util.ArrayList) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(com.cloud.storage.RemoteHostEndPoint) RegisterVolumePayload(com.cloud.storage.RegisterVolumePayload) Date(java.util.Date) GlobalLock(com.cloud.utils.db.GlobalLock) DeleteCommand(com.cloud.storage.command.DeleteCommand) Answer(com.cloud.agent.api.Answer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) VolumeVO(com.cloud.storage.VolumeVO) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 78 with ResourceAllocationException

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

the class ApiDispatcher method dispatch.

public void dispatch(final BaseCmd cmd, final Map<String, String> params, final boolean execute) throws CloudException {
    // Let the chain of responsibility dispatch gradually
    standardDispatchChain.dispatch(new DispatchTask(cmd, params));
    final CallContext ctx = CallContext.current();
    ctx.setEventDisplayEnabled(cmd.isDisplay());
    if (params.get(ApiConstants.PROJECT_ID) != null) {
        final Project project = _entityMgr.findByUuidIncludingRemoved(Project.class, params.get(ApiConstants.PROJECT_ID));
        ctx.setProject(project);
    }
    // TODO This if shouldn't be here. Use polymorphism and move it to validateSpecificParameters
    if (cmd instanceof BaseAsyncCmd) {
        final BaseAsyncCmd asyncCmd = (BaseAsyncCmd) cmd;
        final String startEventId = params.get(ApiConstants.CTX_START_EVENT_ID);
        ctx.setStartEventId(Long.parseLong(startEventId));
        // Synchronise job on the object if needed
        if (asyncCmd.getJob() != null && asyncCmd.getSyncObjId() != null && asyncCmd.getSyncObjType() != null) {
            final Long queueSizeLimit;
            if (asyncCmd.getSyncObjType() != null && asyncCmd.getSyncObjType().equalsIgnoreCase(BaseAsyncCmd.snapshotHostSyncObject)) {
                queueSizeLimit = _createSnapshotQueueSizeLimit;
            } else {
                queueSizeLimit = 1L;
            }
            if (queueSizeLimit != null) {
                if (!execute) {
                    // if we are not within async-execution context, enqueue the command
                    _asyncMgr.syncAsyncJobExecution((AsyncJob) asyncCmd.getJob(), asyncCmd.getSyncObjType(), asyncCmd.getSyncObjId().longValue(), queueSizeLimit);
                    return;
                }
            } else {
                s_logger.trace("The queue size is unlimited, skipping the synchronizing");
            }
        }
    }
    // TODO This if shouldn't be here. Use polymorphism and move it to validateSpecificParameters
    if (cmd instanceof BaseAsyncCustomIdCmd) {
        ((BaseAsyncCustomIdCmd) cmd).checkUuid();
    } else if (cmd instanceof BaseCustomIdCmd) {
        ((BaseCustomIdCmd) cmd).checkUuid();
    }
    try {
        cmd.execute();
    } catch (ResourceUnavailableException | InsufficientCapacityException | ResourceAllocationException | NetworkRuleConflictException e) {
        throw new CloudException("Caught exception while executing command", e);
    }
}
Also used : CloudException(com.cloud.exception.CloudException) CallContext(com.cloud.context.CallContext) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) Project(com.cloud.projects.Project) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) DispatchTask(com.cloud.api.dispatch.DispatchTask)

Example 79 with ResourceAllocationException

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

the class UserVmManagerImpl method moveVMToUser.

@DB
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_MOVE, eventDescription = "move VM to another user", async = false)
public UserVm moveVMToUser(final AssignVMCmd cmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    // VERIFICATIONS and VALIDATIONS
    // VV 1: verify the two users
    final Account caller = CallContext.current().getCallingAccount();
    if (!_accountMgr.isRootAdmin(caller.getId()) && !_accountMgr.isDomainAdmin(caller.getId())) {
        // VMs
        throw new InvalidParameterValueException("Only domain admins are allowed to assign VMs and not " + caller.getType());
    }
    // get and check the valid VM
    final UserVmVO vm = _vmDao.findById(cmd.getVmId());
    if (vm == null) {
        throw new InvalidParameterValueException("There is no vm by that id " + cmd.getVmId());
    } else if (vm.getState() == State.Running) {
        // running
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is Running, unable to move the vm " + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("VM is Running, unable to move the vm with specified vmId");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    final Account oldAccount = _accountService.getActiveAccountById(vm.getAccountId());
    if (oldAccount == null) {
        throw new InvalidParameterValueException("Invalid account for VM " + vm.getAccountId() + " in domain.");
    }
    // don't allow to move the vm from the project
    if (oldAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Specified Vm id belongs to the project and can't be moved");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    final Account newAccount = _accountService.getActiveAccountByName(cmd.getAccountName(), cmd.getDomainId());
    if (newAccount == null || newAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        throw new InvalidParameterValueException("Invalid accountid=" + cmd.getAccountName() + " in domain " + cmd.getDomainId());
    }
    if (newAccount.getState() == Account.State.disabled) {
        throw new InvalidParameterValueException("The new account owner " + cmd.getAccountName() + " is disabled.");
    }
    // check caller has access to both the old and new account
    _accountMgr.checkAccess(caller, null, true, oldAccount);
    _accountMgr.checkAccess(caller, null, true, newAccount);
    // make sure the accounts are not same
    if (oldAccount.getAccountId() == newAccount.getAccountId()) {
        throw new InvalidParameterValueException("The new account is the same as the old account. Account id =" + oldAccount.getAccountId());
    }
    // don't allow to move the vm if there are existing PF/LB/Static Nat
    // rules, or vm is assigned to static Nat ip
    final List<PortForwardingRuleVO> pfrules = _portForwardingDao.listByVm(cmd.getVmId());
    if (pfrules != null && pfrules.size() > 0) {
        throw new InvalidParameterValueException("Remove the Port forwarding rules for this VM before assigning to another user.");
    }
    final List<FirewallRuleVO> snrules = _rulesDao.listStaticNatByVmId(vm.getId());
    if (snrules != null && snrules.size() > 0) {
        throw new InvalidParameterValueException("Remove the StaticNat rules for this VM before assigning to another user.");
    }
    final List<LoadBalancerVMMapVO> maps = _loadBalancerVMMapDao.listByInstanceId(vm.getId());
    if (maps != null && maps.size() > 0) {
        throw new InvalidParameterValueException("Remove the load balancing rules for this VM before assigning to another user.");
    }
    // check for one on one nat
    final List<IPAddressVO> ips = _ipAddressDao.findAllByAssociatedVmId(cmd.getVmId());
    for (final IPAddressVO ip : ips) {
        if (ip.isOneToOneNat()) {
            throw new InvalidParameterValueException("Remove the one to one nat rule for this VM for ip " + ip.toString());
        }
    }
    final Zone zone = zoneRepository.findOne(vm.getDataCenterId());
    // Get serviceOffering and Volumes for Virtual Machine
    final ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
    final List<VolumeVO> volumes = _volsDao.findByInstance(cmd.getVmId());
    // Remove vm from instance group
    removeInstanceFromInstanceGroup(cmd.getVmId());
    // VV 2: check if account/domain is with in resource limits to create a new vm
    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());
    Long totalVolumesSize = (long) 0;
    for (final VolumeVO volume : volumes) {
        totalVolumesSize += volume.getSize();
    }
    _resourceLimitMgr.checkResourceLimit(newAccount, ResourceType.primary_storage, totalVolumesSize);
    // VV 4: Check if new owner can use the vm template
    final VirtualMachineTemplate template = _templateDao.findById(vm.getTemplateId());
    if (!template.isPublicTemplate()) {
        final Account templateOwner = _accountMgr.getAccount(template.getAccountId());
        _accountMgr.checkAccess(newAccount, null, true, templateOwner);
    }
    // VV 5: check the new account can create vm in the domain
    final DomainVO domain = _domainDao.findById(cmd.getDomainId());
    _accountMgr.checkAccess(newAccount, domain);
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            // update resource counts for old account
            resourceCountDecrement(oldAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
            // OWNERSHIP STEP 1: update the vm owner
            vm.setAccountId(newAccount.getAccountId());
            vm.setDomainId(cmd.getDomainId());
            _vmDao.persist(vm);
            // OS 2: update volume
            for (final VolumeVO volume : volumes) {
                _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.volume);
                _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize()));
                volume.setAccountId(newAccount.getAccountId());
                volume.setDomainId(newAccount.getDomainId());
                _volsDao.persist(volume);
                _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.volume);
                _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize()));
                // snapshots: mark these removed in db
                final List<SnapshotVO> snapshots = _snapshotDao.listByVolumeIdIncludingRemoved(volume.getId());
                for (final SnapshotVO snapshot : snapshots) {
                    _snapshotDao.remove(snapshot.getId());
                }
            }
            // update resource count of new account
            resourceCountIncrement(newAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
        }
    });
    final VirtualMachine vmoi = _itMgr.findById(vm.getId());
    final VirtualMachineProfileImpl vmOldProfile = new VirtualMachineProfileImpl(vmoi);
    // OS 3: update the network
    final List<Long> networkIdList = cmd.getNetworkIds();
    if (zone.getNetworkType() == NetworkType.Basic) {
        if (networkIdList != null && !networkIdList.isEmpty()) {
            throw new InvalidParameterValueException("Can't move vm with network Ids; this is a basic zone VM");
        }
        // cleanup the network for the oldOwner
        _networkMgr.cleanupNics(vmOldProfile);
        _networkMgr.expungeNics(vmOldProfile);
        // security groups will be recreated for the new account, when the
        // VM is started
        final List<NetworkVO> networkList = new ArrayList<>();
        // Get default guest network in Basic zone
        final Network defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
        if (defaultNetwork == null) {
            throw new InvalidParameterValueException("Unable to find a default network to start a vm");
        } else {
            networkList.add(_networkDao.findById(defaultNetwork.getId()));
        }
        final LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<>();
        final NicProfile profile = new NicProfile();
        profile.setDefaultNic(true);
        networks.put(networkList.get(0), new ArrayList<>(Arrays.asList(profile)));
        final VirtualMachine vmi = _itMgr.findById(vm.getId());
        final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmi);
        _networkMgr.allocate(vmProfile, networks);
    } else {
        // cleanup the network for the oldOwner
        _networkMgr.cleanupNics(vmOldProfile);
        _networkMgr.expungeNics(vmOldProfile);
        final Set<NetworkVO> applicableNetworks = new HashSet<>();
        if (networkIdList != null && !networkIdList.isEmpty()) {
            // add any additional networks
            for (final Long networkId : networkIdList) {
                final NetworkVO network = _networkDao.findById(networkId);
                if (network == null) {
                    final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find specified network id");
                    ex.addProxyObject(networkId.toString(), "networkId");
                    throw ex;
                }
                _networkModel.checkNetworkPermissions(newAccount, network);
                // don't allow to use system networks
                final NetworkOffering networkOffering = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId());
                if (networkOffering.isSystemOnly()) {
                    final InvalidParameterValueException ex = new InvalidParameterValueException("Specified Network id is system only and can't be used for vm deployment");
                    ex.addProxyObject(network.getUuid(), "networkId");
                    throw ex;
                }
                applicableNetworks.add(network);
            }
        } else {
            final NetworkVO defaultNetwork;
            final List<NetworkOfferingVO> requiredOfferings = _networkOfferingDao.listByAvailability(Availability.Required, false);
            if (requiredOfferings.size() < 1) {
                throw new InvalidParameterValueException("Unable to find network offering with availability=" + Availability.Required + " to automatically create the network as a part of vm creation");
            }
            if (requiredOfferings.get(0).getState() == NetworkOffering.State.Enabled) {
                // get Virtual networks
                final List<? extends Network> virtualNetworks = _networkModel.listNetworksForAccount(newAccount.getId(), zone.getId(), Network.GuestType.Isolated);
                if (virtualNetworks.isEmpty()) {
                    final long physicalNetworkId = _networkModel.findPhysicalNetworkId(zone.getId(), requiredOfferings.get(0).getTags(), requiredOfferings.get(0).getTrafficType());
                    // Validate physical network
                    final PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
                    if (physicalNetwork == null) {
                        throw new InvalidParameterValueException("Unable to find physical network with id: " + physicalNetworkId + " and tag: " + requiredOfferings.get(0).getTags());
                    }
                    s_logger.debug("Creating network for account " + newAccount + " from the network offering id=" + requiredOfferings.get(0).getId() + " as a part of deployVM process");
                    Network newNetwork = _networkMgr.createGuestNetwork(requiredOfferings.get(0).getId(), newAccount.getAccountName() + "-network", newAccount.getAccountName() + "-network", null, null, null, null, newAccount, null, physicalNetwork, zone.getId(), ACLType.Account, null, null, null, null, true, null, null, null, null);
                    // if the network offering has persistent set to true, implement the network
                    if (requiredOfferings.get(0).getIsPersistent()) {
                        final DeployDestination dest = new DeployDestination(zone, null, null, null);
                        final UserVO callerUser = _userDao.findById(CallContext.current().getCallingUserId());
                        final Journal journal = new Journal.LogJournal("Implementing " + newNetwork, s_logger);
                        final ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), journal, callerUser, caller);
                        s_logger.debug("Implementing the network for account" + newNetwork + " as a part of" + " network provision for persistent networks");
                        try {
                            final Pair<? extends NetworkGuru, ? extends Network> implementedNetwork = _networkMgr.implementNetwork(newNetwork.getId(), dest, context);
                            if (implementedNetwork == null || implementedNetwork.first() == null) {
                                s_logger.warn("Failed to implement the network " + newNetwork);
                            }
                            newNetwork = implementedNetwork.second();
                        } catch (final Exception ex) {
                            s_logger.warn("Failed to implement network " + newNetwork + " elements and" + " resources as a part of network provision for persistent network due to ", ex);
                            final CloudRuntimeException e = new CloudRuntimeException("Failed to implement network" + " (with specified id) elements and resources as a part of network provision");
                            e.addProxyObject(newNetwork.getUuid(), "networkId");
                            throw e;
                        }
                    }
                    defaultNetwork = _networkDao.findById(newNetwork.getId());
                } else if (virtualNetworks.size() > 1) {
                    throw new InvalidParameterValueException("More than 1 default Isolated networks are found " + "for account " + newAccount + "; please specify networkIds");
                } else {
                    defaultNetwork = _networkDao.findById(virtualNetworks.get(0).getId());
                }
            } else {
                throw new InvalidParameterValueException("Required network offering id=" + requiredOfferings.get(0).getId() + " is not in " + NetworkOffering.State.Enabled);
            }
            applicableNetworks.add(defaultNetwork);
        }
        // add the new nics
        final LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<>();
        int toggle = 0;
        for (final NetworkVO appNet : applicableNetworks) {
            final NicProfile defaultNic = new NicProfile();
            if (toggle == 0) {
                defaultNic.setDefaultNic(true);
                toggle++;
            }
            networks.put(appNet, new ArrayList<>(Arrays.asList(defaultNic)));
        }
        final VirtualMachine vmi = _itMgr.findById(vm.getId());
        final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmi);
        _networkMgr.allocate(vmProfile, networks);
        s_logger.debug("AssignVM: Advance virtual, adding networks no " + networks.size() + " to " + vm.getInstanceName());
    }
    // END IF ADVANCED
    s_logger.info("AssignVM: vm " + vm.getInstanceName() + " now belongs to account " + cmd.getAccountName());
    return vm;
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) Journal(com.cloud.utils.Journal) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) LinkedHashMap(java.util.LinkedHashMap) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) HashSet(java.util.HashSet) PortForwardingRuleVO(com.cloud.network.rules.PortForwardingRuleVO) NetworkVO(com.cloud.network.dao.NetworkVO) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) DomainVO(com.cloud.domain.DomainVO) DeployDestination(com.cloud.deploy.DeployDestination) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkOffering(com.cloud.offering.NetworkOffering) Zone(com.cloud.db.model.Zone) 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) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) UserVO(com.cloud.user.UserVO) IPAddressVO(com.cloud.network.dao.IPAddressVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 80 with ResourceAllocationException

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

the class VolumeApiServiceImplTest method testResourceLimitCheckForUploadedVolume.

// The resource limit check for primary storage should not be skipped for Volume in 'Uploaded' state.
@Test
public void testResourceLimitCheckForUploadedVolume() throws NoSuchFieldException, IllegalAccessException, ResourceAllocationException {
    doThrow(new ResourceAllocationException("primary storage resource limit check failed", Resource.ResourceType.primary_storage)).when(_svc._resourceLimitMgr).checkResourceLimit(any(AccountVO.class), any(Resource.ResourceType.class), any(Long.class));
    UserVmVO vm = Mockito.mock(UserVmVO.class);
    VolumeInfo volumeToAttach = Mockito.mock(VolumeInfo.class);
    when(volumeToAttach.getId()).thenReturn(9L);
    when(volumeToAttach.getDataCenterId()).thenReturn(34L);
    when(volumeToAttach.getVolumeType()).thenReturn(Volume.Type.DATADISK);
    when(volumeToAttach.getInstanceId()).thenReturn(null);
    when(_userVmDao.findById(anyLong())).thenReturn(vm);
    when(vm.getType()).thenReturn(VirtualMachine.Type.User);
    when(vm.getState()).thenReturn(State.Running);
    when(vm.getDataCenterId()).thenReturn(34L);
    when(_svc._volsDao.findByInstanceAndType(anyLong(), any(Volume.Type.class))).thenReturn(new ArrayList(10));
    when(_svc.volFactory.getVolume(9L)).thenReturn(volumeToAttach);
    when(volumeToAttach.getState()).thenReturn(Volume.State.Uploaded);
    DataCenterVO zoneWithDisabledLocalStorage = Mockito.mock(DataCenterVO.class);
    when(_svc._dcDao.findById(anyLong())).thenReturn(zoneWithDisabledLocalStorage);
    when(zoneWithDisabledLocalStorage.isLocalStorageEnabled()).thenReturn(true);
    try {
        _svc.attachVolumeToVM(2L, 9L, null);
    } catch (InvalidParameterValueException e) {
        Assert.assertEquals(e.getMessage(), ("primary storage resource limit check failed"));
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) UserVmVO(com.cloud.vm.UserVmVO) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) AccessType(com.cloud.acl.SecurityChecker.AccessType) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Matchers.anyLong(org.mockito.Matchers.anyLong) ArrayList(java.util.ArrayList) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Aggregations

ResourceAllocationException (com.cloud.exception.ResourceAllocationException)127 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)81 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)76 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)74 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)55 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)41 ServerApiException (org.apache.cloudstack.api.ServerApiException)37 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)33 Account (com.cloud.user.Account)33 DB (com.cloud.utils.db.DB)30 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)27 ArrayList (java.util.ArrayList)25 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)22 ConfigurationException (javax.naming.ConfigurationException)22 ServerApiException (com.cloud.api.ServerApiException)19 TransactionStatus (com.cloud.utils.db.TransactionStatus)18 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)18 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)17 ActionEvent (com.cloud.event.ActionEvent)15 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)15