/** * Get host ids set from vm ids list */ protected Set<Long> getHostIdSet(List<Long> vmIds) { Set<Long> hostIds = new HashSet<>(); for (Long groupVMId : vmIds) { VMInstanceVO groupVM = _vmInstanceDao.findById(groupVMId); hostIds.add(groupVM.getHostId()); } return hostIds; }
HostVO findHost(VMInstanceVO vm) { return _hostDao.findById(vm.getHostId()); }
private boolean checkHostSuitabilityForImplicitDedication(Long accountId, List<VMInstanceVO> allVmsOnHost) { boolean suitable = true; if (allVmsOnHost.isEmpty()) return false; for (VMInstanceVO vm : allVmsOnHost) { if (vm.getAccountId() != accountId) { s_logger.info("Host " + vm.getHostId() + " found to be unsuitable for implicit dedication as it is " + "running instances of another account"); suitable = false; break; } else { if (!isImplicitPlannerUsedByOffering(vm.getServiceOfferingId())) { s_logger.info("Host " + vm.getHostId() + " found to be unsuitable for implicit dedication as it " + "is running instances of this account which haven't been created using implicit dedication."); suitable = false; break; } } } return suitable; }
private boolean checkIfAllVmsCreatedInStrictMode(Long accountId, List<VMInstanceVO> allVmsOnHost) { boolean createdByImplicitStrict = true; if (allVmsOnHost.isEmpty()) return false; for (VMInstanceVO vm : allVmsOnHost) { if (!isImplicitPlannerUsedByOffering(vm.getServiceOfferingId())) { s_logger.info("Host " + vm.getHostId() + " found to be running a vm created by a planner other" + " than implicit."); createdByImplicitStrict = false; break; } else if (isServiceOfferingUsingPlannerInPreferredMode(vm.getServiceOfferingId())) { s_logger.info("Host " + vm.getHostId() + " found to be running a vm created by an implicit planner" + " in preferred mode."); createdByImplicitStrict = false; break; } } return createdByImplicitStrict; }
private boolean checkIfAllVmsCreatedInStrictMode(Long accountId, List<VMInstanceVO> allVmsOnHost) { boolean createdByImplicitStrict = true; if (allVmsOnHost.isEmpty()) { return false; } for (VMInstanceVO vm : allVmsOnHost) { if (!isImplicitPlannerUsedByOffering(vm.getServiceOfferingId()) || vm.getAccountId() != accountId) { s_logger.info("Host " + vm.getHostId() + " found to be running a vm created by a planner other" + " than implicit, or running vms of other account"); createdByImplicitStrict = false; break; } else if (isServiceOfferingUsingPlannerInPreferredMode(vm.getServiceOfferingId()) || vm.getAccountId() != accountId) { s_logger.info("Host " + vm.getHostId() + " found to be running a vm created by an implicit planner" + " in preferred mode, or running vms of other account"); createdByImplicitStrict = false; break; } } return createdByImplicitStrict; }
public Long chooseHostForStoragePool(StoragePoolVO poolVO, List<Long> avoidHosts, boolean sendToVmResidesOn, Long vmId) { if (sendToVmResidesOn) { if (vmId != null) { VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); if (vmInstance != null) { Long hostId = vmInstance.getHostId(); if (hostId != null && !avoidHosts.contains(vmInstance.getHostId())) { return hostId; } } } /* * Can't find the vm where host resides on(vm is destroyed? or * volume is detached from vm), randomly choose a host to send the * cmd */ } List<StoragePoolHostVO> poolHosts = _storagePoolHostDao.listByHostStatus(poolVO.getId(), Status.Up); Collections.shuffle(poolHosts); if (poolHosts != null && poolHosts.size() > 0) { for (StoragePoolHostVO sphvo : poolHosts) { if (!avoidHosts.contains(sphvo.getHostId())) { return sphvo.getHostId(); } } } return null; }
@Override public boolean stateTransitTo(final VirtualMachine vm1, final VirtualMachine.Event e, final Long hostId) throws NoTransitionException { final VMInstanceVO vm = (VMInstanceVO)vm1; final State oldState = vm.getState(); if (oldState == State.Starting) { if (e == Event.OperationSucceeded) { vm.setLastHostId(hostId); } } else if (oldState == State.Stopping) { if (e == Event.OperationSucceeded) { vm.setLastHostId(vm.getHostId()); } } return _stateMachine.transitTo(vm, e, new Pair<Long, Long>(vm.getHostId(), hostId), _vmDao); }
protected boolean stateTransitTo(final VMInstanceVO vm, final VirtualMachine.Event e, final Long hostId, final String reservationId) throws NoTransitionException { // if there are active vm snapshots task, state change is not allowed vm.setReservationId(reservationId); return _stateMachine.transitTo(vm, e, new Pair<Long, Long>(vm.getHostId(), hostId), _vmDao); }
@Override public void doInTransactionWithoutResult(final TransactionStatus status) throws CloudRuntimeException { VMInstanceVO vm = _vmDao.findByUuid(vmUuid); try { if (!stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, 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); } else { if (expunge) { if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) { s_logger.debug("Unable to expunge the vm because it is not in the correct state: " + vm); throw new CloudRuntimeException("Unable to expunge " + vm); } } } } catch (final NoTransitionException e) { s_logger.debug(e.getMessage()); throw new CloudRuntimeException("Unable to destroy " + vm, e); } } });
private List<String> getStoragePaths(long clusterId, long storagePoolId) { List<String> storagePaths = new ArrayList<>(); // If you do not pass in null for the second parameter, you only get back applicable ROOT disks. List<VolumeVO> volumes = volumeDao.findByPoolId(storagePoolId, null); if (volumes != null) { for (VolumeVO volume : volumes) { Long instanceId = volume.getInstanceId(); if (instanceId != null) { VMInstanceVO vmInstance = vmDao.findById(instanceId); Long hostIdForVm = vmInstance.getHostId() != null ? vmInstance.getHostId() : vmInstance.getLastHostId(); if (hostIdForVm != null) { HostVO hostForVm = hostDao.findById(hostIdForVm); if (hostForVm != null && hostForVm.getClusterId().equals(clusterId)) { storagePaths.add(volume.get_iScsiName()); } } } } } return storagePaths; }
@Override public boolean scheduleMigration(final VMInstanceVO vm) { if (vm.getHostId() != null) { final HaWorkVO work = new HaWorkVO(vm.getId(), vm.getType(), WorkType.Migration, Step.Scheduled, vm.getHostId(), vm.getState(), 0, vm.getUpdated()); _haDao.persist(work); wakeupWorkers(); } return true; }
private List<Map<String, String>> getTargets(long clusterId, long storagePoolId) { List<Map<String, String>> targets = new ArrayList<>(); StoragePoolVO storagePool = storagePoolDao.findById(storagePoolId); // If you do not pass in null for the second parameter, you only get back applicable ROOT disks. List<VolumeVO> volumes = volumeDao.findByPoolId(storagePoolId, null); if (volumes != null) { for (VolumeVO volume : volumes) { Long instanceId = volume.getInstanceId(); if (instanceId != null) { VMInstanceVO vmInstance = vmDao.findById(instanceId); Long hostIdForVm = vmInstance.getHostId() != null ? vmInstance.getHostId() : vmInstance.getLastHostId(); if (hostIdForVm != null) { HostVO hostForVm = hostDao.findById(hostIdForVm); if (hostForVm.getClusterId().equals(clusterId)) { Map<String, String> details = new HashMap<>(); details.put(ModifyTargetsCommand.IQN, volume.get_iScsiName()); details.put(ModifyTargetsCommand.STORAGE_HOST, storagePool.getHostAddress()); details.put(ModifyTargetsCommand.STORAGE_PORT, String.valueOf(storagePool.getPort())); targets.add(details); } } } } } return targets; }
private boolean doDisableAccount(long accountId) throws ConcurrentOperationException, ResourceUnavailableException { List<VMInstanceVO> vms = _vmDao.listByAccountId(accountId); boolean success = true; for (VMInstanceVO vm : vms) { try { try { _itMgr.advanceStop(vm.getUuid(), false); } catch (OperationTimedoutException ote) { s_logger.warn("Operation for stopping vm timed out, unable to stop vm " + vm.getHostName(), ote); success = false; } } catch (AgentUnavailableException aue) { s_logger.warn("Agent running on host " + vm.getHostId() + " is unavailable, unable to stop vm " + vm.getHostName(), aue); success = false; } } return success; }
private void migrateThroughHypervisorOrStorage(StoragePool destPool, VMInstanceVO vm) throws StorageUnavailableException, InsufficientCapacityException { final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); final Long srchostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId(); final HostVO srcHost = _hostDao.findById(srchostId); final Long srcClusterId = srcHost.getClusterId(); Answer[] hypervisorMigrationResults = attemptHypervisorMigration(destPool, vm); boolean migrationResult = false; if (hypervisorMigrationResults == null) { // OfflineVmwareMigration: if the HypervisorGuru can't do it, let the volume manager take care of it. migrationResult = volumeMgr.storageMigration(profile, destPool); if (migrationResult) { afterStorageMigrationCleanup(destPool, vm, srcHost, srcClusterId); } else { s_logger.debug("Storage migration failed"); } } else { afterHypervisorMigrationCleanup(destPool, vm, srcHost, srcClusterId, hypervisorMigrationResults); } }
private Answer[] attemptHypervisorMigration(StoragePool destPool, VMInstanceVO vm) { final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); // OfflineVmwareMigration: in case of vmware call vcenter to do it for us. // OfflineVmwareMigration: should we check the proximity of source and destination // OfflineVmwareMigration: if we are in the same cluster/datacentre/pool or whatever? // OfflineVmwareMigration: we are checking on success to optionally delete an old vm if we are not List<Command> commandsToSend = hvGuru.finalizeMigrate(vm, destPool); Long hostId = vm.getHostId(); // OfflineVmwareMigration: probably this is null when vm is stopped if(hostId == null) { hostId = vm.getLastHostId(); if (s_logger.isDebugEnabled()) { s_logger.debug(String.format("host id is null, using last host id %d", hostId) ); } } if(CollectionUtils.isNotEmpty(commandsToSend)) { Commands commandsContainer = new Commands(Command.OnError.Stop); commandsContainer.addCommands(commandsToSend); try { // OfflineVmwareMigration: change to the call back variety? // OfflineVmwareMigration: getting a Long seq to be filled with _agentMgr.send(hostId, commandsContainer, this) return _agentMgr.send(hostId, commandsContainer); } catch (AgentUnavailableException | OperationTimedoutException e) { throw new CloudRuntimeException(String.format("Failed to migrate VM: %s", vm.getUuid()),e); } } return null; }
protected void handleVmMigrated(VMInstanceVO vm) { if (!isVmSecurityGroupEnabled(vm.getId())) { return; } if (vm.getType() != VirtualMachine.Type.User) { Commands cmds = null; NetworkRulesSystemVmCommand nrc = new NetworkRulesSystemVmCommand(vm.getInstanceName(), vm.getType()); cmds = new Commands(nrc); try { _agentMgr.send(vm.getHostId(), cmds); } catch (AgentUnavailableException e) { s_logger.debug(e.toString()); } catch (OperationTimedoutException e) { s_logger.debug(e.toString()); } } else { List<Long> affectedVms = new ArrayList<Long>(); affectedVms.add(vm.getId()); scheduleRulesetUpdateToHosts(affectedVms, true, null); } }
private HostVO getHost(long volumeId) { VolumeVO volumeVO = volumeDao.findById(volumeId); Long vmInstanceId = volumeVO.getInstanceId(); VMInstanceVO vmInstanceVO = vmInstanceDao.findById(vmInstanceId); Long hostId = null; // if the volume to snapshot is associated with a VM if (vmInstanceVO != null) { hostId = vmInstanceVO.getHostId(); // if the VM is not associated with a host if (hostId == null) { hostId = vmInstanceVO.getLastHostId(); } } return getHost(volumeVO.getDataCenterId(), hostId); }
@Override public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage) throws UnableDeleteHostException { if (host.getType() != Host.Type.Routing || host.getHypervisorType() != HypervisorType.BareMetal) { return null; } List<VMInstanceVO> deadVms = _vmDao.listByLastHostId(host.getId()); for (VMInstanceVO vm : deadVms) { if (vm.getState() == State.Running || vm.getHostId() != null) { throw new CloudRuntimeException("VM " + vm.getId() + "is still running on host " + host.getId()); } _vmDao.remove(vm.getId()); } return new DeleteHostAnswer(true); }