Search in sources :

Example 16 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method advanceExpunge.

protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException {
    if (vm == null || vm.getRemoved() != null) {
        s_logger.debug("Unable to find vm or vm is destroyed: " + vm);
        return;
    }
    advanceStop(vm.getUuid(), false);
    vm = _vmDao.findByUuid(vm.getUuid());
    try {
        if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) {
            s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
            throw new CloudRuntimeException("Unable to destroy " + vm);
        }
    } catch (final NoTransitionException e) {
        s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
        throw new CloudRuntimeException("Unable to destroy " + vm, e);
    }
    s_logger.debug("Destroying vm " + vm);
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
    s_logger.debug("Cleaning up NICS");
    final List<Command> nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, profile.getNics());
    _networkMgr.cleanupNics(profile);
    s_logger.debug("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage");
    final List<Command> volumeExpungeCommands = hvGuru.finalizeExpungeVolumes(vm);
    final Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
    if (volumeExpungeCommands != null && volumeExpungeCommands.size() > 0 && hostId != null) {
        final Commands cmds = new Commands(Command.OnError.Stop);
        for (final Command volumeExpungeCommand : volumeExpungeCommands) {
            cmds.addCommand(volumeExpungeCommand);
        }
        _agentMgr.send(hostId, cmds);
        if (!cmds.isSuccessful()) {
            for (final Answer answer : cmds.getAnswers()) {
                if (!answer.getResult()) {
                    s_logger.warn("Failed to expunge vm due to: " + answer.getDetails());
                    throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails());
                }
            }
        }
    }
    if (hostId != null) {
        volumeMgr.revokeAccess(vm.getId(), hostId);
    }
    // Clean up volumes based on the vm's instance id
    volumeMgr.cleanupVolumes(vm.getId());
    final VirtualMachineGuru guru = getVmGuru(vm);
    guru.finalizeExpunge(vm);
    // remove the overcommit detials from the uservm details
    _uservmDetailsDao.removeDetails(vm.getId());
    // send hypervisor-dependent commands before removing
    final List<Command> finalizeExpungeCommands = hvGuru.finalizeExpunge(vm);
    if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) {
        if (hostId != null) {
            final Commands cmds = new Commands(Command.OnError.Stop);
            for (final Command command : finalizeExpungeCommands) {
                cmds.addCommand(command);
            }
            if (nicExpungeCommands != null) {
                for (final Command command : nicExpungeCommands) {
                    cmds.addCommand(command);
                }
            }
            _agentMgr.send(hostId, cmds);
            if (!cmds.isSuccessful()) {
                for (final Answer answer : cmds.getAnswers()) {
                    if (!answer.getResult()) {
                        s_logger.warn("Failed to expunge vm due to: " + answer.getDetails());
                        throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails());
                    }
                }
            }
        }
    }
    s_logger.debug("Expunged " + vm);
}
Also used : UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) StartupCommand(com.cloud.legacymodel.communication.command.startup.StartupCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) MigrateCommand(com.cloud.legacymodel.communication.command.MigrateCommand) ScaleVmCommand(com.cloud.legacymodel.communication.command.ScaleVmCommand) ShutdownEventCommand(com.cloud.legacymodel.communication.command.agentcontrol.ShutdownEventCommand) StopCommand(com.cloud.legacymodel.communication.command.StopCommand) RestoreVMSnapshotCommand(com.cloud.legacymodel.communication.command.RestoreVMSnapshotCommand) AttachOrDettachConfigDriveCommand(com.cloud.legacymodel.communication.command.AttachOrDettachConfigDriveCommand) Command(com.cloud.legacymodel.communication.command.Command) AgentControlCommand(com.cloud.legacymodel.communication.command.agentcontrol.AgentControlCommand) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) CheckVirtualMachineCommand(com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand) PingRoutingCommand(com.cloud.legacymodel.communication.command.PingRoutingCommand) RebootCommand(com.cloud.legacymodel.communication.command.RebootCommand) PrepareForMigrationCommand(com.cloud.legacymodel.communication.command.PrepareForMigrationCommand) ClusterVMMetaDataSyncCommand(com.cloud.legacymodel.communication.command.ClusterVMMetaDataSyncCommand) StartupRoutingCommand(com.cloud.legacymodel.communication.command.startup.StartupRoutingCommand) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) Commands(com.cloud.agent.manager.Commands)

Example 17 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateStart.

@Override
public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfile.Param, Object> params, final DeploymentPlan planToDeploy, final DeploymentPlanner planner) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
    final CallContext cctxt = CallContext.current();
    final Account account = cctxt.getCallingAccount();
    final User caller = cctxt.getCallingUser();
    VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final Ternary<VMInstanceVO, ReservationContext, ItWorkVO> start = changeToStartState(vm, caller, account);
    if (start == null) {
        return;
    }
    vm = start.first();
    final ReservationContext ctx = start.second();
    ItWorkVO work = start.third();
    VMInstanceVO startedVm = null;
    final ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
    final VirtualMachineTemplate template = _entityMgr.findByIdIncludingRemoved(VirtualMachineTemplate.class, vm.getTemplateId());
    s_logger.debug("Trying to deploy VM, vm has dcId: " + vm.getDataCenterId() + " and podId: " + vm.getPodIdToDeployIn());
    DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterId(), vm.getPodIdToDeployIn(), null, null, null, null, ctx);
    if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) {
        s_logger.debug("advanceStart: DeploymentPlan is provided, using dcId:" + planToDeploy.getDataCenterId() + ", podId: " + planToDeploy.getPodId() + ", clusterId: " + planToDeploy.getClusterId() + ", hostId: " + planToDeploy.getHostId() + ", poolId: " + planToDeploy.getPoolId());
        plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), planToDeploy.getPoolId(), planToDeploy.getPhysicalNetworkId(), ctx);
    }
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
    final VirtualMachineGuru vmGuru = getVmGuru(vm);
    boolean canRetry = true;
    ExcludeList avoids = null;
    try {
        final Journal journal = start.second().getJournal();
        if (planToDeploy != null) {
            avoids = planToDeploy.getAvoids();
        }
        if (avoids == null) {
            avoids = new ExcludeList();
        }
        s_logger.debug("VM start orchestration will {}", avoids.toString());
        boolean planChangedByVolume = false;
        boolean reuseVolume = true;
        final DataCenterDeployment originalPlan = plan;
        int retry = StartRetry.value();
        while (retry-- != 0) {
            if (reuseVolume) {
                // edit plan if this vm's ROOT volume is in READY state already
                final List<VolumeVO> vols = _volsDao.findReadyRootVolumesByInstance(vm.getId());
                for (final VolumeVO vol : vols) {
                    // make sure if the templateId is unchanged. If it is changed,
                    // let planner
                    // reassign pool for the volume even if it ready.
                    final Long volTemplateId = vol.getTemplateId();
                    if (volTemplateId != null && volTemplateId != template.getId()) {
                        s_logger.debug(vol + " of " + vm + " is READY, but template ids don't match, let the planner reassign a new pool");
                        continue;
                    }
                    final StoragePool pool = (StoragePool) dataStoreMgr.getPrimaryDataStore(vol.getPoolId());
                    if (!pool.isInMaintenance()) {
                        s_logger.debug("Root volume is ready, need to place VM in volume's cluster");
                        final long rootVolDcId = pool.getDataCenterId();
                        final Long rootVolPodId = pool.getPodId();
                        final Long rootVolClusterId = pool.getClusterId();
                        if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) {
                            final Long clusterIdSpecified = planToDeploy.getClusterId();
                            if (clusterIdSpecified != null && rootVolClusterId != null) {
                                checkIfPlanIsDeployable(vm, rootVolClusterId, clusterIdSpecified);
                            }
                            plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), vol.getPoolId(), null, ctx);
                        } else {
                            plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId(), null, ctx);
                            s_logger.debug(vol + " is READY, changing deployment plan to use this pool's dcId: " + rootVolDcId + " , podId: " + rootVolPodId + " , and clusterId: " + rootVolClusterId);
                            planChangedByVolume = true;
                        }
                    }
                }
            }
            final Account owner = _entityMgr.findById(Account.class, vm.getAccountId());
            final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, owner, params);
            final DeployDestination dest = _dpMgr.planDeployment(vmProfile, plan, avoids, planner);
            if (dest == null) {
                if (planChangedByVolume) {
                    plan = originalPlan;
                    planChangedByVolume = false;
                    // do not enter volume reuse for next retry, since we want to look for resources outside the volume's cluster
                    reuseVolume = false;
                    continue;
                }
                throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId(), areAffinityGroupsAssociated(vmProfile));
            }
            if (dest != null) {
                avoids.addHost(dest.getHost().getId());
                journal.record("Deployment found ", vmProfile, dest);
            }
            long destHostId = dest.getHost().getId();
            vm.setPodIdToDeployIn(dest.getPod().getId());
            final Long cluster_id = dest.getCluster().getId();
            final ClusterDetailsVO cluster_detail_cpu = _clusterDetailsDao.findDetail(cluster_id, "cpuOvercommitRatio");
            final ClusterDetailsVO cluster_detail_ram = _clusterDetailsDao.findDetail(cluster_id, "memoryOvercommitRatio");
            // storing the value of overcommit in the vm_details table for doing a capacity check in case the cluster overcommit ratio is changed.
            if (_uservmDetailsDao.findDetail(vm.getId(), "cpuOvercommitRatio") == null && (Float.parseFloat(cluster_detail_cpu.getValue()) > 1f || Float.parseFloat(cluster_detail_ram.getValue()) > 1f)) {
                _uservmDetailsDao.addDetail(vm.getId(), "cpuOvercommitRatio", cluster_detail_cpu.getValue(), true);
                _uservmDetailsDao.addDetail(vm.getId(), "memoryOvercommitRatio", cluster_detail_ram.getValue(), true);
            } else if (_uservmDetailsDao.findDetail(vm.getId(), "cpuOvercommitRatio") != null) {
                _uservmDetailsDao.addDetail(vm.getId(), "cpuOvercommitRatio", cluster_detail_cpu.getValue(), true);
                _uservmDetailsDao.addDetail(vm.getId(), "memoryOvercommitRatio", cluster_detail_ram.getValue(), true);
            }
            vmProfile.setCpuOvercommitRatio(Float.parseFloat(cluster_detail_cpu.getValue()));
            vmProfile.setMemoryOvercommitRatio(Float.parseFloat(cluster_detail_ram.getValue()));
            StartAnswer startAnswer = null;
            try {
                if (!changeState(vm, Event.OperationRetry, destHostId, work, Step.Prepare)) {
                    throw new ConcurrentOperationException("Unable to update the state of the Virtual Machine " + vm.getUuid() + " oldstate: " + vm.getState() + "Event :" + Event.OperationRetry);
                }
            } catch (final NoTransitionException e1) {
                throw new ConcurrentOperationException(e1.getMessage());
            }
            try {
                s_logger.debug("VM is being created in podId: " + vm.getPodIdToDeployIn());
                _networkMgr.prepare(vmProfile, dest, ctx);
                volumeMgr.prepare(vmProfile, dest);
                // since StorageMgr succeeded in volume creation, reuse Volume for further tries until current cluster has capacity
                if (!reuseVolume) {
                    reuseVolume = true;
                }
                vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx);
                final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
                handlePath(vmTO.getDisks(), vm.getHypervisorType());
                final Commands cmds = new Commands(Command.OnError.Stop);
                cmds.addCommand(new StartCommand(vmTO, dest.getHost(), getExecuteInSequence(vm.getHypervisorType())));
                vmGuru.finalizeDeployment(cmds, vmProfile, dest, ctx);
                work = _workDao.findById(work.getId());
                if (work == null || work.getStep() != Step.Prepare) {
                    throw new ConcurrentOperationException("Work steps have been changed: " + work);
                }
                _workDao.updateStep(work, Step.Starting);
                _agentMgr.send(destHostId, cmds);
                _workDao.updateStep(work, Step.Started);
                startAnswer = cmds.getAnswer(StartAnswer.class);
                // Set date and version we start this VM
                vm.setLastStartDateTime(getCurrentLocalDateTimeStamp());
                vm.setLastStartVersion(VirtualMachineManagerImpl.class.getPackage().getImplementationVersion());
                // Reset VM compliance state
                if (vm.getComplianceStatus() == ComplianceStatus.VMNeedsRestart) {
                    vm.setComplianceStatus(ComplianceStatus.Compliant);
                }
                _vmDao.persist(vm);
                if (startAnswer != null && startAnswer.getResult()) {
                    handlePath(vmTO.getDisks(), startAnswer.getIqnToPath());
                    final String host_guid = startAnswer.getHost_guid();
                    if (host_guid != null) {
                        final HostVO finalHost = _resourceMgr.findHostByGuid(host_guid);
                        if (finalHost == null) {
                            throw new CloudRuntimeException("Host Guid " + host_guid + " doesn't exist in DB, something went wrong while processing start answer: " + startAnswer);
                        }
                        destHostId = finalHost.getId();
                    }
                    if (vmGuru.finalizeStart(vmProfile, destHostId, cmds, ctx)) {
                        syncDiskChainChange(startAnswer);
                        if (!changeState(vm, Event.OperationSucceeded, destHostId, work, Step.Done)) {
                            s_logger.error("Unable to transition to a new state. VM uuid: " + vm.getUuid() + "VM oldstate:" + vm.getState() + "Event:" + Event.OperationSucceeded);
                            throw new ConcurrentOperationException("Failed to deploy VM" + vm.getUuid());
                        }
                        // Update GPU device capacity
                        final GPUDeviceTO gpuDevice = startAnswer.getVirtualMachine().getGpuDevice();
                        if (gpuDevice != null) {
                            _resourceMgr.updateGPUDetails(destHostId, gpuDevice.getGroupDetails());
                        }
                        startedVm = vm;
                        s_logger.debug("Start completed for VM " + vm);
                        return;
                    } else {
                        s_logger.info("The guru did not like the answers so stopping " + vm);
                        final StopCommand cmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
                        final Answer answer = _agentMgr.easySend(destHostId, cmd);
                        if (answer != null && answer instanceof StopAnswer) {
                            final StopAnswer stopAns = (StopAnswer) answer;
                            if (vm.getType() == VirtualMachineType.User) {
                                final String platform = stopAns.getPlatform();
                                if (platform != null) {
                                    final Map<String, String> vmmetadata = new HashMap<>();
                                    vmmetadata.put(vm.getInstanceName(), platform);
                                    syncVMMetaData(vmmetadata);
                                }
                            }
                        }
                        if (answer == null || !answer.getResult()) {
                            s_logger.warn("Unable to stop " + vm + " due to " + (answer != null ? answer.getDetails() : "no answers"));
                            _haMgr.scheduleStop(vm, destHostId, HaWork.HaWorkType.ForceStop);
                            throw new ExecutionException("Unable to stop this VM, " + vm.getUuid() + " so we are unable to retry the start operation");
                        }
                        throw new ExecutionException("Unable to start  VM:" + vm.getUuid() + " due to error in finalizeStart, not retrying");
                    }
                }
                s_logger.info("Unable to start VM on " + dest.getHost() + " due to " + (startAnswer == null ? " no start answer" : startAnswer.getDetails()));
                if (startAnswer != null && startAnswer.getContextParam("stopRetry") != null) {
                    break;
                }
            } catch (final OperationTimedoutException e) {
                s_logger.debug("Unable to send the start command to host " + dest.getHost() + " failed to start VM: " + vm.getUuid());
                if (e.isActive()) {
                    _haMgr.scheduleStop(vm, destHostId, HaWork.HaWorkType.CheckStop);
                }
                canRetry = false;
                throw new AgentUnavailableException("Unable to start " + vm.getHostName(), destHostId, e);
            } catch (final ResourceUnavailableException e) {
                s_logger.info("Unable to contact resource.", e);
                if (!avoids.add(e)) {
                    if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
                        throw e;
                    } else {
                        s_logger.warn("unexpected ResourceUnavailableException : " + e.getScope().getName(), e);
                        throw e;
                    }
                }
            } catch (final InsufficientCapacityException e) {
                s_logger.info("Insufficient capacity ", e);
                if (!avoids.add(e)) {
                    if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
                        throw e;
                    } else {
                        s_logger.warn("unexpected InsufficientCapacityException : " + e.getScope().getName(), e);
                    }
                }
            } catch (final ExecutionException | NoTransitionException e) {
                s_logger.error("Failed to start instance " + vm, e);
                throw new AgentUnavailableException("Unable to start instance due to " + e.getMessage(), destHostId, e);
            } finally {
                if (startedVm == null && canRetry) {
                    final Step prevStep = work.getStep();
                    _workDao.updateStep(work, Step.Release);
                    // If previous step was started/ing && we got a valid answer
                    if ((prevStep == Step.Started || prevStep == Step.Starting) && startAnswer != null && startAnswer.getResult()) {
                        // TODO check the response of cleanup
                        // and record it in DB for retry
                        cleanup(vmGuru, vmProfile, work, Event.OperationFailed, false);
                    } else {
                        // if step is not starting/started, send cleanup command with force=true
                        cleanup(vmGuru, vmProfile, work, Event.OperationFailed, true);
                    }
                }
            }
        }
    } finally {
        if (startedVm == null) {
            if (canRetry) {
                try {
                    changeState(vm, Event.OperationFailed, null, work, Step.Done);
                } catch (final NoTransitionException e) {
                    throw new ConcurrentOperationException(e.getMessage());
                }
            }
        }
        if (planToDeploy != null) {
            planToDeploy.setAvoids(avoids);
        }
    }
    if (startedVm == null) {
        throw new CloudRuntimeException("Unable to start instance '" + vm.getHostName() + "' (" + vm.getUuid() + "), see management server log for details");
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) User(com.cloud.legacymodel.user.User) StoragePool(com.cloud.legacymodel.storage.StoragePool) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Journal(com.cloud.utils.Journal) Step(com.cloud.vm.ItWorkVO.Step) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ExecutionException(com.cloud.legacymodel.exceptions.ExecutionException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) InsufficientServerCapacityException(com.cloud.legacymodel.exceptions.InsufficientServerCapacityException) GPUDeviceTO(com.cloud.legacymodel.to.GPUDeviceTO) CallContext(com.cloud.context.CallContext) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) HostVO(com.cloud.host.HostVO) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) StopCommand(com.cloud.legacymodel.communication.command.StopCommand) DeployDestination(com.cloud.deploy.DeployDestination) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO)

Example 18 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method copyVolumeFromSecToPrimary.

@DB
public VolumeInfo copyVolumeFromSecToPrimary(final VolumeInfo volume, final VirtualMachine vm, final VirtualMachineTemplate template, final DataCenter dc, final Pod pod, final Long clusterId, final DiskOffering diskOffering, final List<StoragePool> avoids) {
    final HashSet<StoragePool> avoidPools = new HashSet<>(avoids);
    final DiskProfile dskCh = createDiskCharacteristics(volume, template, dc, diskOffering);
    dskCh.setHyperType(vm.getHypervisorType());
    // Find a suitable storage to create volume on
    final StoragePool destPool = findStoragePool(dskCh, dc, pod, clusterId, null, vm, avoidPools);
    final DataStore destStore = this.dataStoreMgr.getDataStore(destPool.getId(), DataStoreRole.Primary);
    final AsyncCallFuture<VolumeService.VolumeApiResult> future = this.volService.copyVolume(volume, destStore);
    try {
        final VolumeService.VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.debug("copy volume failed: " + result.getResult());
            throw new CloudRuntimeException("copy volume failed: " + result.getResult());
        }
        return result.getVolume();
    } catch (final InterruptedException | ExecutionException e) {
        s_logger.debug("Failed to copy volume: " + volume.getId(), e);
        throw new CloudRuntimeException("Failed to copy volume", e);
    }
}
Also used : StoragePool(com.cloud.legacymodel.storage.StoragePool) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) VolumeService(com.cloud.engine.subsystem.api.storage.VolumeService) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) PrimaryDataStore(com.cloud.engine.subsystem.api.storage.PrimaryDataStore) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) DB(com.cloud.utils.db.DB)

Example 19 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method getTasks.

private List<VolumeTask> getTasks(final List<VolumeVO> vols, final Map<Volume, StoragePool> destVols, final VirtualMachineProfile vm) throws StorageUnavailableException {
    final boolean recreate = RecreatableSystemVmEnabled.value();
    final List<VolumeTask> tasks = new ArrayList<>();
    for (final VolumeVO vol : vols) {
        StoragePoolVO assignedPool = null;
        if (destVols != null) {
            final StoragePool pool = destVols.get(vol);
            if (pool != null) {
                assignedPool = this._storagePoolDao.findById(pool.getId());
            }
        }
        if (assignedPool == null && recreate) {
            assignedPool = this._storagePoolDao.findById(vol.getPoolId());
        }
        if (assignedPool != null) {
            final Volume.State state = vol.getState();
            if (state == Volume.State.Allocated || state == Volume.State.Creating) {
                final VolumeTask task = new VolumeTask(VolumeTaskType.RECREATE, vol, null);
                tasks.add(task);
            } else {
                if (vol.isRecreatable()) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Volume " + vol + " will be recreated on storage pool " + assignedPool + " assigned by deploymentPlanner");
                    }
                    final VolumeTask task = new VolumeTask(VolumeTaskType.RECREATE, vol, null);
                    tasks.add(task);
                } else {
                    if (assignedPool.getId() != vol.getPoolId()) {
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Mismatch in storage pool " + assignedPool + " assigned by deploymentPlanner and the one associated with volume " + vol);
                        }
                        final DiskOffering diskOffering = this._entityMgr.findById(DiskOffering.class, vol.getDiskOfferingId());
                        if (diskOffering.getUseLocalStorage()) {
                            // Currently migration of local volume is not supported so bail out
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Local volume " + vol + " cannot be recreated on storagepool " + assignedPool + " assigned by deploymentPlanner");
                            }
                            throw new CloudRuntimeException("Local volume " + vol + " cannot be recreated on storagepool " + assignedPool + " assigned by deploymentPlanner");
                        } else {
                            // Check if storage migration is enabled in config
                            final Boolean isHAOperation = (Boolean) vm.getParameter(VirtualMachineProfile.Param.HaOperation);
                            Boolean storageMigrationEnabled = true;
                            if (isHAOperation != null && isHAOperation) {
                                storageMigrationEnabled = StorageHAMigrationEnabled.value();
                            } else {
                                storageMigrationEnabled = StorageMigrationEnabled.value();
                            }
                            // Always allow ISOs volumes to be "migrated"
                            if (storageMigrationEnabled || vol.getIsoId() != null) {
                                if (s_logger.isDebugEnabled()) {
                                    s_logger.debug("Shared volume " + vol + " will be migrated on storage pool " + assignedPool + " assigned by deploymentPlanner");
                                }
                                final VolumeTask task = new VolumeTask(VolumeTaskType.MIGRATE, vol, assignedPool);
                                tasks.add(task);
                            } else {
                                throw new CloudRuntimeException("Cannot start VM on the hypervisor it was last running on, due to not enough capacity. Please try to start on" + " " + "another hypervisor in the same cluster, or migrate the volumes to another storage pool. Automatic Volume Migration is disabled, " + "so this is not handled automatically.");
                            }
                        }
                    } else {
                        final StoragePoolVO pool = this._storagePoolDao.findById(vol.getPoolId());
                        final VolumeTask task = new VolumeTask(VolumeTaskType.NOP, vol, pool);
                        tasks.add(task);
                    }
                }
            }
        } else {
            if (vol.getPoolId() == null) {
                throw new StorageUnavailableException("Volume has no pool associate and also no storage pool assigned in DeployDestination, Unable to create " + vol, Volume.class, vol.getId());
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("No need to recreate the volume: " + vol + ", since it already has a pool assigned: " + vol.getPoolId() + ", adding disk to VM");
            }
            final StoragePoolVO pool = this._storagePoolDao.findById(vol.getPoolId());
            final VolumeTask task = new VolumeTask(VolumeTaskType.NOP, vol, pool);
            tasks.add(task);
        }
    }
    return tasks;
}
Also used : StoragePool(com.cloud.legacymodel.storage.StoragePool) DiskOffering(com.cloud.legacymodel.storage.DiskOffering) ArrayList(java.util.ArrayList) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) Volume(com.cloud.legacymodel.storage.Volume) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO)

Example 20 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method destroyVolume.

@Override
public void destroyVolume(final Volume volume) {
    try {
        // Mark volume as removed if volume has not been created on primary
        if (volume.getState() == Volume.State.Allocated) {
            this._volsDao.remove(volume.getId());
            stateTransitTo(volume, Volume.Event.DestroyRequested);
        } else {
            this.volService.destroyVolume(volume.getId());
        }
        this._resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, volume.isDisplay());
        // FIXME - why recalculate and not decrement
        this._resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.primary_storage.getOrdinal());
    } catch (final Exception e) {
        s_logger.debug("Failed to destroy volume" + volume.getId(), e);
        throw new CloudRuntimeException("Failed to destroy volume" + volume.getId(), e);
    }
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException)

Aggregations

CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)587 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)159 ArrayList (java.util.ArrayList)110 DB (com.cloud.utils.db.DB)90 Account (com.cloud.legacymodel.user.Account)84 SQLException (java.sql.SQLException)84 ActionEvent (com.cloud.event.ActionEvent)73 ConfigurationException (javax.naming.ConfigurationException)73 PreparedStatement (java.sql.PreparedStatement)68 HashMap (java.util.HashMap)68 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)62 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)52 HostVO (com.cloud.host.HostVO)50 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)50 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)47 Answer (com.cloud.legacymodel.communication.answer.Answer)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)45 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 IOException (java.io.IOException)44