Search in sources :

Example 86 with StoragePool

use of com.cloud.storage.StoragePool in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method createVolumeFromSnapshot.

@DB
@Override
public VolumeInfo createVolumeFromSnapshot(final Volume volume, final Snapshot snapshot, final UserVm vm) throws StorageUnavailableException {
    final Account account = _entityMgr.findById(Account.class, volume.getAccountId());
    final HashSet<StoragePool> poolsToAvoid = new HashSet<>();
    StoragePool pool = null;
    final Set<Long> podsToAvoid = new HashSet<>();
    Pair<Pod, Long> pod = null;
    final DiskOffering diskOffering = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
    final DataCenter dc = _entityMgr.findById(DataCenter.class, volume.getDataCenterId());
    final DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType());
    String msg = "There are no available storage pools to store the volume in";
    if (vm != null) {
        final Pod podofVM = _entityMgr.findById(Pod.class, vm.getPodIdToDeployIn());
        if (podofVM != null) {
            pod = new Pair<>(podofVM, podofVM.getId());
        }
    }
    if (vm != null && pod != null) {
        // if VM is running use the hostId to find the clusterID. If it is stopped, refer the cluster where the ROOT volume of the VM exists.
        Long hostId = null;
        Long clusterId = null;
        if (vm.getState() == State.Running) {
            hostId = vm.getHostId();
            if (hostId != null) {
                final Host vmHost = _entityMgr.findById(Host.class, hostId);
                clusterId = vmHost.getClusterId();
            }
        } else {
            final List<VolumeVO> rootVolumesOfVm = _volsDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT);
            if (rootVolumesOfVm.size() != 1) {
                throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state. Please contact Cloud Support.");
            } else {
                final VolumeVO rootVolumeOfVm = rootVolumesOfVm.get(0);
                final StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolumeOfVm.getPoolId());
                clusterId = (rootDiskPool == null ? null : rootDiskPool.getClusterId());
            }
        }
        // Determine what storage pool to store the volume in
        while ((pool = findStoragePool(dskCh, dc, pod.first(), clusterId, hostId, vm, poolsToAvoid)) != null) {
            break;
        }
        if (pool == null) {
            // pool could not be found in the VM's pod/cluster.
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Could not find any storage pool to create Volume in the pod/cluster of the provided VM " + vm.getUuid());
            }
            final StringBuilder addDetails = new StringBuilder(msg);
            addDetails.append(", Could not find any storage pool to create Volume in the pod/cluster of the VM ");
            addDetails.append(vm.getUuid());
            msg = addDetails.toString();
        }
    } else {
        // Determine what pod to store the volume in
        while ((pod = findPod(null, null, dc, account.getId(), podsToAvoid)) != null) {
            podsToAvoid.add(pod.first().getId());
            // Determine what storage pool to store the volume in
            while ((pool = findStoragePool(dskCh, dc, pod.first(), null, null, null, poolsToAvoid)) != null) {
                break;
            }
            if (pool != null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Found a suitable pool for create volume: " + pool.getId());
                }
                break;
            }
        }
    }
    if (pool == null) {
        s_logger.info(msg);
        throw new StorageUnavailableException(msg, -1);
    }
    final VolumeInfo vol = volFactory.getVolume(volume.getId());
    final DataStore store = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
    final DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
    SnapshotInfo snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
    if (snapInfo == null && dataStoreRole == DataStoreRole.Image) {
        // snapshot is not backed up to secondary, let's do that now.
        snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId());
        }
        // We need to copy the snapshot onto secondary.
        final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        snapshotStrategy.backupSnapshot(snapInfo);
        // Attempt to grab it again.
        snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId() + " on secondary and could not create backup");
        }
    }
    // don't try to perform a sync if the DataStoreRole of the snapshot is equal to DataStoreRole.Primary
    if (!DataStoreRole.Primary.equals(dataStoreRole)) {
        try {
            // sync snapshot to region store if necessary
            final DataStore snapStore = snapInfo.getDataStore();
            final long snapVolId = snapInfo.getVolumeId();
            _snapshotSrv.syncVolumeSnapshotsToRegionStore(snapVolId, snapStore);
        } catch (final Exception ex) {
            // log but ignore the sync error to avoid any potential S3 down issue, it should be sync next time
            s_logger.warn(ex.getMessage(), ex);
        }
    }
    // create volume on primary from snapshot
    final AsyncCallFuture<VolumeService.VolumeApiResult> future = volService.createVolumeFromSnapshot(vol, store, snapInfo);
    try {
        final VolumeService.VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.debug("Failed to create volume from snapshot:" + result.getResult());
            throw new CloudRuntimeException("Failed to create volume from snapshot:" + result.getResult());
        }
        return result.getVolume();
    } catch (final InterruptedException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    } catch (final ExecutionException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    }
}
Also used : Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) DiskOffering(com.cloud.offering.DiskOffering) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) DataStoreRole(com.cloud.storage.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeService(com.cloud.engine.subsystem.api.storage.VolumeService) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) PrimaryDataStore(com.cloud.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) HashSet(java.util.HashSet) Pod(com.cloud.dc.Pod) Host(com.cloud.host.Host) DiskProfile(com.cloud.vm.DiskProfile) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InsufficientStorageCapacityException(com.cloud.exception.InsufficientStorageCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.dc.DataCenter) DB(com.cloud.utils.db.DB)

Example 87 with StoragePool

use of com.cloud.storage.StoragePool 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 = _storagePoolDao.findById(pool.getId());
            }
        }
        if (assignedPool == null && recreate) {
            assignedPool = _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 = _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 = _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 = _storagePoolDao.findById(vol.getPoolId());
            final VolumeTask task = new VolumeTask(VolumeTaskType.NOP, vol, pool);
            tasks.add(task);
        }
    }
    return tasks;
}
Also used : StoragePool(com.cloud.storage.StoragePool) DiskOffering(com.cloud.offering.DiskOffering) ArrayList(java.util.ArrayList) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO)

Example 88 with StoragePool

use of com.cloud.storage.StoragePool in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateMigrateWithStorage.

private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHostId, final long destHostId, final Map<Long, Long> volumeToPool) throws ResourceUnavailableException, ConcurrentOperationException {
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final HostVO srcHost = _hostDao.findById(srcHostId);
    final HostVO destHost = _hostDao.findById(destHostId);
    final VirtualMachineGuru vmGuru = getVmGuru(vm);
    final Zone zone = _zoneRepository.findOne(destHost.getDataCenterId());
    final HostPodVO pod = _podDao.findById(destHost.getPodId());
    final Cluster cluster = _clusterDao.findById(destHost.getClusterId());
    final DeployDestination destination = new DeployDestination(zone, pod, cluster, destHost);
    // Create a map of which volume should go in which storage pool.
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final Map<Volume, StoragePool> volumeToPoolMap = getPoolListForVolumesForMigration(profile, destHost, volumeToPool);
    // a vm and not migrating a vm with storage.
    if (volumeToPoolMap == null || volumeToPoolMap.isEmpty()) {
        throw new InvalidParameterValueException("Migration of the vm " + vm + "from host " + srcHost + " to destination host " + destHost + " doesn't involve migrating the volumes.");
    }
    AlertManager.AlertType alertType = AlertManager.AlertType.ALERT_TYPE_USERVM_MIGRATE;
    if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) {
        alertType = AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE;
    } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) {
        alertType = AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY_MIGRATE;
    }
    _networkMgr.prepareNicForMigration(profile, destination);
    volumeMgr.prepareForMigration(profile, destination);
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
    final VirtualMachineTO to = hvGuru.implement(profile);
    ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Migrating, vm.getType(), vm.getId());
    work.setStep(Step.Prepare);
    work.setResourceType(ItWorkVO.ResourceType.Host);
    work.setResourceId(destHostId);
    work = _workDao.persist(work);
    // Put the vm in migrating state.
    vm.setLastHostId(srcHostId);
    moveVmToMigratingState(vm, destHostId, work);
    boolean migrated = false;
    try {
        // config drive: Detach the config drive at source host
        // After migration successful attach the config drive in destination host
        // On migration failure VM will be stopped, So configIso will be deleted
        final Nic defaultNic = _networkModel.getDefaultNic(vm.getId());
        List<String[]> vmData = null;
        if (defaultNic != null) {
            final UserVmVO userVm = _userVmDao.findById(vm.getId());
            final Map<String, String> details = _vmDetailsDao.listDetailsKeyPairs(vm.getId());
            vm.setDetails(details);
            final Network network = _networkModel.getNetwork(defaultNic.getNetworkId());
            if (_networkModel.isSharedNetworkWithoutServices(network.getId())) {
                final String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId()).getDisplayText();
                final String zoneName = _dcDao.findById(vm.getDataCenterId()).getName();
                final boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
                vmData = _networkModel.generateVmData(userVm.getUserData(), serviceOffering, zoneName, vm.getInstanceName(), vm.getId(), (String) profile.getParameter(VirtualMachineProfile.Param.VmSshPubKey), (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword), isWindows, network);
                final String vmName = vm.getInstanceName();
                final String configDriveIsoRootFolder = "/tmp";
                final String isoFile = configDriveIsoRootFolder + "/" + vmName + "/configDrive/" + vmName + ".iso";
                profile.setVmData(vmData);
                profile.setConfigDriveLabel(VmConfigDriveLabel.value());
                profile.setConfigDriveIsoRootFolder(configDriveIsoRootFolder);
                profile.setConfigDriveIsoFile(isoFile);
                // At source host detach the config drive iso.
                final AttachOrDettachConfigDriveCommand dettachCommand = new AttachOrDettachConfigDriveCommand(vm.getInstanceName(), vmData, VmConfigDriveLabel.value(), false);
                try {
                    _agentMgr.send(srcHost.getId(), dettachCommand);
                    s_logger.debug("Deleted config drive ISO for  vm " + vm.getInstanceName() + " In host " + srcHost);
                } catch (final OperationTimedoutException e) {
                    s_logger.debug("TIme out occured while exeuting command AttachOrDettachConfigDrive " + e.getMessage());
                }
            }
        }
        // Migrate the vm and its volume.
        volumeMgr.migrateVolumes(vm, to, srcHost, destHost, volumeToPoolMap);
        // Put the vm back to running state.
        moveVmOutofMigratingStateOnSuccess(vm, destHost.getId(), work);
        try {
            if (!checkVmOnHost(vm, destHostId)) {
                s_logger.error("Vm not found on destination host. Unable to complete migration for " + vm);
                try {
                    _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null);
                } catch (final AgentUnavailableException e) {
                    s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId);
                }
                cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true);
                throw new CloudRuntimeException("VM not found on desintation host. Unable to complete migration for " + vm);
            }
        } catch (final OperationTimedoutException e) {
            s_logger.warn("Error while checking the vm " + vm + " is on host " + destHost, e);
        }
        migrated = true;
    } finally {
        if (!migrated) {
            s_logger.info("Migration was unsuccessful.  Cleaning up: " + vm);
            _alertMgr.sendAlert(alertType, srcHost.getDataCenterId(), srcHost.getPodId(), "Unable to migrate vm " + vm.getInstanceName() + " from host " + srcHost.getName() + " in zone " + zone.getName() + " and pod " + zone.getName(), "Migrate Command failed.  Please check logs.");
            try {
                _agentMgr.send(destHostId, new Commands(cleanup(vm.getInstanceName())), null);
                stateTransitTo(vm, Event.OperationFailed, srcHostId);
            } catch (final AgentUnavailableException e) {
                s_logger.warn("Looks like the destination Host is unavailable for cleanup.", e);
            } catch (final NoTransitionException e) {
                s_logger.error("Error while transitioning vm from migrating to running state.", e);
            }
        }
        work.setStep(Step.Done);
        _workDao.update(work.getId(), work);
    }
}
Also used : AlertManager(com.cloud.alert.AlertManager) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) StoragePool(com.cloud.storage.StoragePool) HostPodVO(com.cloud.dc.HostPodVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) Zone(com.cloud.db.model.Zone) TimeZone(java.util.TimeZone) Cluster(com.cloud.org.Cluster) HostVO(com.cloud.host.HostVO) AttachOrDettachConfigDriveCommand(com.cloud.agent.api.AttachOrDettachConfigDriveCommand) Volume(com.cloud.storage.Volume) DeployDestination(com.cloud.deploy.DeployDestination) NoTransitionException(com.cloud.utils.fsm.NoTransitionException)

Example 89 with StoragePool

use of com.cloud.storage.StoragePool 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);
            DeployDestination dest;
            try {
                dest = _dpMgr.planDeployment(vmProfile, plan, avoids, planner);
            } catch (final AffinityConflictException e2) {
                s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2);
                throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict");
            }
            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);
                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() == VirtualMachine.Type.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, WorkType.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, WorkType.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.user.Account) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) User(com.cloud.user.User) StoragePool(com.cloud.storage.StoragePool) StartAnswer(com.cloud.agent.api.StartAnswer) StartCommand(com.cloud.agent.api.StartCommand) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Journal(com.cloud.utils.Journal) Step(com.cloud.vm.ItWorkVO.Step) AffinityConflictException(com.cloud.exception.AffinityConflictException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ExecutionException(com.cloud.utils.exception.ExecutionException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) StopAnswer(com.cloud.agent.api.StopAnswer) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) CallContext(com.cloud.context.CallContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) HostVO(com.cloud.host.HostVO) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) StopCommand(com.cloud.agent.api.StopCommand) DeployDestination(com.cloud.deploy.DeployDestination) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO)

Example 90 with StoragePool

use of com.cloud.storage.StoragePool in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method getPoolListForVolumesForMigration.

private Map<Volume, StoragePool> getPoolListForVolumesForMigration(final VirtualMachineProfile profile, final Host host, final Map<Long, Long> volumeToPool) {
    final List<VolumeVO> allVolumes = _volsDao.findUsableVolumesForInstance(profile.getId());
    final Map<Volume, StoragePool> volumeToPoolObjectMap = new HashMap<>();
    for (final VolumeVO volume : allVolumes) {
        final Long poolId = volumeToPool.get(Long.valueOf(volume.getId()));
        final StoragePoolVO pool = _storagePoolDao.findById(poolId);
        final StoragePoolVO currentPool = _storagePoolDao.findById(volume.getPoolId());
        final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
        if (pool != null) {
            // created is compliant with the pool type.
            if (_poolHostDao.findByPoolHost(pool.getId(), host.getId()) == null || pool.isLocal() != diskOffering.getUseLocalStorage()) {
                // Cannot find a pool for the volume. Throw an exception.
                throw new CloudRuntimeException("Cannot migrate volume " + volume + " to storage pool " + pool + " while migrating vm to host " + host + ". Either the pool is not accessible from the host or because of the offering with which the volume is created it cannot be placed on " + "the given pool.");
            } else if (pool.getId() == currentPool.getId()) {
            // If the pool to migrate too is the same as current pool, the volume doesn't need to be migrated.
            } else {
                volumeToPoolObjectMap.put(volume, pool);
            }
        } else {
            // Find a suitable pool for the volume. Call the storage pool allocator to find the list of pools.
            final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
            final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), null, null);
            final ExcludeList avoid = new ExcludeList();
            boolean currentPoolAvailable = false;
            final List<StoragePool> poolList = new ArrayList<>();
            for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
                final List<StoragePool> poolListFromAllocator = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
                if (poolListFromAllocator != null && !poolListFromAllocator.isEmpty()) {
                    poolList.addAll(poolListFromAllocator);
                }
            }
            if (poolList != null && !poolList.isEmpty()) {
                // Volume needs to be migrated. Pick the first pool from the list. Add a mapping to migrate the
                // volume to a pool only if it is required; that is the current pool on which the volume resides
                // is not available on the destination host.
                final Iterator<StoragePool> iter = poolList.iterator();
                while (iter.hasNext()) {
                    if (currentPool.getId() == iter.next().getId()) {
                        currentPoolAvailable = true;
                        break;
                    }
                }
                if (!currentPoolAvailable) {
                    volumeToPoolObjectMap.put(volume, _storagePoolDao.findByUuid(poolList.get(0).getUuid()));
                }
            }
            if (!currentPoolAvailable && !volumeToPoolObjectMap.containsKey(volume)) {
                // Cannot find a pool for the volume. Throw an exception.
                throw new CloudRuntimeException("Cannot find a storage pool which is available for volume " + volume + " while migrating virtual machine " + profile.getVirtualMachine() + " to host " + host);
            }
        }
    }
    return volumeToPoolObjectMap;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VolumeVO(com.cloud.storage.VolumeVO) Volume(com.cloud.storage.Volume) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) StoragePoolAllocator(com.cloud.engine.subsystem.api.storage.StoragePoolAllocator)

Aggregations

StoragePool (com.cloud.storage.StoragePool)234 Answer (com.cloud.agent.api.Answer)81 Test (org.junit.Test)70 ArrayList (java.util.ArrayList)69 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)63 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)56 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)56 Volume (com.cloud.storage.Volume)55 HashMap (java.util.HashMap)47 VolumeVO (com.cloud.storage.VolumeVO)37 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)36 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)35 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)29 DiskProfile (com.cloud.vm.DiskProfile)29 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)28 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)28 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool)28 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)28 KvmStoragePool (com.cloud.hypervisor.kvm.storage.KvmStoragePool)28 AttachAnswer (com.cloud.storage.command.AttachAnswer)28