HighAvailabilityManagerImpl extends ManagerBase implements HighAvailabilityManager, ClusterManagerListener { @Override public void scheduleRestartForVmsOnHost(final HostVO host, boolean investigate) { if (host.getType() != Host.Type.Routing) { return; } s_logger.warn("Scheduling restart for VMs on host " + host.getId() + "-" + host.getName()); final List<VMInstanceVO> vms = _instanceDao.listByHostId(host.getId()); final DataCenterVO dcVO = _dcDao.findById(host.getDataCenterId()); StringBuilder sb = null; final List<VMInstanceVO> reorderedVMList = new ArrayList<VMInstanceVO>(); if (vms != null && !vms.isEmpty()) { sb = new StringBuilder(); sb.append(" Starting HA on the following VMs:"); for (int i = 0; i < vms.size(); i++) { final VMInstanceVO vm = vms.get(i); if (vm.getType() == VirtualMachine.Type.User) { reorderedVMList.add(vm); } else { reorderedVMList.add(0, vm); } if (vm.isHaEnabled()) { sb.append(" " + vm.getHostName()); } } } final HostPodVO podVO = _podDao.findById(host.getPodId()); final String hostDesc = "name: " + host.getName() + " (id:" + host.getId() + "), availability zone: " + dcVO.getName() + ", pod: " + podVO.getName(); _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host is down, " + hostDesc, "Host [" + hostDesc + "] is down." + (sb != null ? sb.toString() : "")); for (VMInstanceVO vm : reorderedVMList) { if (s_logger.isDebugEnabled()) { s_logger.debug("Notifying HA Mgr of to restart vm " + vm.getId() + "-" + vm.getInstanceName()); } vm = _instanceDao.findByUuid(vm.getUuid()); final Long hostId = vm.getHostId(); if (hostId != null && !hostId.equals(host.getId())) { s_logger.debug("VM " + vm.getInstanceName() + " is not on down host " + host.getId() + " it is on other host " + hostId + " VM HA is done"); continue; } scheduleRestart(vm, investigate); } } protected  HighAvailabilityManagerImpl(); List<Investigator> getInvestigators(); void setInvestigators(List<Investigator> investigators); List<FenceBuilder> getFenceBuilders(); void setFenceBuilders(List<FenceBuilder> fenceBuilders); List<HAPlanner> getHaPlanners(); void setHaPlanners(List<HAPlanner> haPlanners); @Override Status investigate(final long hostId); @Override void scheduleRestartForVmsOnHost(final HostVO host, boolean investigate); @Override void scheduleStop(VMInstanceVO vm, long hostId, WorkType type); @Override boolean scheduleMigration(final VMInstanceVO vm); @Override void scheduleRestart(VMInstanceVO vm, boolean investigate); Long migrate(final HaWorkVO work); @Override void scheduleDestroy(VMInstanceVO vm, long hostId); @Override void cancelDestroy(VMInstanceVO vm, Long hostId); @Override void cancelScheduledMigrations(final HostVO host); @Override List<VMInstanceVO> findTakenMigrationWork(); @Override boolean configure(final String name, final Map<String, Object> xmlParams); @Override boolean start(); @Override boolean stop(); @Override void onManagementNodeJoined(List<? extends ManagementServerHost> nodeList, long selfNodeId); @Override void onManagementNodeLeft(List<? extends ManagementServerHost> nodeList, long selfNodeId); @Override void onManagementNodeIsolated(); @Override String getHaTag(); @Override DeploymentPlanner getHAPlanner(); @Override boolean hasPendingHaWork(long vmId);  }
@Test public void scheduleRestartForVmsOnHost() { Mockito.when(hostVO.getType()).thenReturn(Host.Type.Routing); Mockito.when(hostVO.getHypervisorType()).thenReturn(HypervisorType.KVM); Mockito.when(_instanceDao.listByHostId(42l)).thenReturn(Arrays.asList(Mockito.mock(VMInstanceVO.class))); Mockito.when(_podDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostPodVO.class)); Mockito.when(_dcDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(DataCenterVO.class)); highAvailabilityManager.scheduleRestartForVmsOnHost(hostVO, true); }
@Test public void scheduleRestartForVmsOnHostNonEmptyVMList() { Mockito.when(hostVO.getId()).thenReturn(1l); Mockito.when(hostVO.getType()).thenReturn(Host.Type.Routing); Mockito.when(hostVO.getHypervisorType()).thenReturn(HypervisorType.XenServer); final List<VMInstanceVO> vms = new ArrayList<VMInstanceVO>(); final VMInstanceVO vm1 = Mockito.mock(VMInstanceVO.class); Mockito.when(vm1.getHostId()).thenReturn(1l); Mockito.when(vm1.getInstanceName()).thenReturn("i-2-3-VM"); Mockito.when(vm1.getType()).thenReturn(VirtualMachine.Type.User); Mockito.when(vm1.isHaEnabled()).thenReturn(true); vms.add(vm1); final VMInstanceVO vm2 = Mockito.mock(VMInstanceVO.class); Mockito.when(vm2.getHostId()).thenReturn(1l); Mockito.when(vm2.getInstanceName()).thenReturn("r-2-VM"); Mockito.when(vm2.getType()).thenReturn(VirtualMachine.Type.DomainRouter); Mockito.when(vm2.isHaEnabled()).thenReturn(true); vms.add(vm2); Mockito.when(_instanceDao.listByHostId(Mockito.anyLong())).thenReturn(vms); Mockito.when(_instanceDao.findByUuid(vm1.getUuid())).thenReturn(vm1); Mockito.when(_instanceDao.findByUuid(vm2.getUuid())).thenReturn(vm2); Mockito.when(_podDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostPodVO.class)); Mockito.when(_dcDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(DataCenterVO.class)); Mockito.when(_haDao.findPreviousHA(Mockito.anyLong())).thenReturn(Arrays.asList(Mockito.mock(HaWorkVO.class))); Mockito.when(_haDao.persist((HaWorkVO)Mockito.anyObject())).thenReturn(Mockito.mock(HaWorkVO.class)); highAvailabilityManager.scheduleRestartForVmsOnHost(hostVO, true); }