@Override public void loadVmReservation(VMEntityVO vm) { VMReservationVO vmReservation = _vmReservationDao.findByVmId(vm.getId()); vm.setVmReservation(vmReservation); }
private void saveVmReservation(VMEntityVO vm) { if (vm.getVmReservation() != null) { _vmReservationDao.persist(vm.getVmReservation()); } }
@Override public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) { if (!status) { return false; } State oldState = transition.getCurrentState(); State newState = transition.getToState(); if ((oldState == State.Starting) && (newState != State.Starting)) { // cleanup all VM reservation entries SearchCriteria<VMReservationVO> sc = _reservationDao.createSearchCriteria(); sc.addAnd("vmId", SearchCriteria.Op.EQ, vo.getId()); _reservationDao.expunge(sc); } return true; } }
@Override public void cleanupVMReservations() { List<VMReservationVO> reservations = _reservationDao.listAll(); for (VMReservationVO reserv : reservations) { VMInstanceVO vm = _vmInstanceDao.findById(reserv.getVmId()); if (vm != null) { if (vm.getState() == State.Starting || (vm.getState() == State.Stopped && vm.getLastHostId() == null)) { continue; } else { // delete reservation _reservationDao.remove(reserv.getId()); } } else { // delete reservation _reservationDao.remove(reserv.getId()); } } }
@Override public void deployVirtualMachine(String reservationId, VMEntityVO vmEntityVO, String caller, Map<VirtualMachineProfile.Param, Object> params, boolean deployOnGivenHost) throws InsufficientCapacityException, ResourceUnavailableException { //grab the VM Id and destination using the reservationId. VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid()); VMReservationVO vmReservation = _reservationDao.findByReservationId(reservationId); if (vmReservation != null) { DataCenterDeployment reservedPlan = new DataCenterDeployment(vm.getDataCenterId(), vmReservation.getPodId(), vmReservation.getClusterId(), vmReservation.getHostId(), null, null); try { _itMgr.start(vm.getUuid(), params, reservedPlan, _planningMgr.getDeploymentPlannerByName(vmReservation.getDeploymentPlanner())); } catch (Exception ex) { // Retry the deployment without using the reservation plan // Retry is only done if host id is not passed in deploy virtual machine api. Otherwise // the instance may be started on another host instead of the intended one. if (!deployOnGivenHost) { DataCenterDeployment plan = new DataCenterDeployment(0, null, null, null, null, null); if (reservedPlan.getAvoids() != null) { plan.setAvoids(reservedPlan.getAvoids()); } _itMgr.start(vm.getUuid(), params, plan, null); } } } else { // no reservation found. Let VirtualMachineManager retry _itMgr.start(vm.getUuid(), params, null, null); } }
@Override public boolean check(VirtualMachineProfile vmProfile, DeployDestination plannedDestination) throws AffinityConflictException { if (plannedDestination.getHost() == null) { return true; } long plannedHostId = plannedDestination.getHost().getId(); VirtualMachine vm = vmProfile.getVirtualMachine(); List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType()); for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) { // if more than 1 VM's are present in the group then check for // conflict due to parallel deployment List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(vmGroupMapping.getAffinityGroupId()); groupVMIds.remove(vm.getId()); for (Long groupVMId : groupVMIds) { VMReservationVO vmReservation = _reservationDao.findByVmId(groupVMId); if (vmReservation != null && vmReservation.getHostId() != null && vmReservation.getHostId().equals(plannedHostId)) { if (s_logger.isDebugEnabled()) { s_logger.debug("Planned destination for VM " + vm.getId() + " conflicts with an existing VM " + vmReservation.getVmId() + " reserved on the same host " + plannedHostId); } return false; } } } return true; }
_reservationDao.persist(vmReservation); return vmReservation.getUuid();