@Before public void setUp() throws NoSuchFieldException { MockitoAnnotations.initMocks(this); ReflectionUtils.setFieldValue(leaderElectionService, "jobNodeStorage", jobNodeStorage); ReflectionUtils.setFieldValue(leaderElectionService, "localHostService", localHostService); when(localHostService.getIp()).thenReturn("mockedIP"); when(localHostService.getHostName()).thenReturn("mockedHostName"); when(jobNodeStorage.getJobConfiguration()).thenReturn(jobConfig); jobConfig.setOverwrite(true); }
@Test public void assertListenLeaderElection() { getRegistryCenter().persist("/testJob/leader/election/host", "host0"); assertFalse(getRegistryCenter().isExisted("/testJob/leader/sharding/necessary")); WaitingUtils.waitingShortTime(); getRegistryCenter().remove("/testJob/leader/election"); WaitingUtils.waitingShortTime(); assertThat( getRegistryCenter().get("/testJob/leader/election/host"), is(localHostService.getIp())); assertTrue(getRegistryCenter().isExisted("/testJob/leader/sharding/necessary")); }
protected void assertRegCenterCommonInfo() { assertThat( REG_CENTER.get("/" + jobName + "/leader/election/host"), is(localHostService.getIp())); assertThat(REG_CENTER.get("/" + jobName + "/config/shardingTotalCount"), is("3")); assertThat(REG_CENTER.get("/" + jobName + "/config/shardingItemParameters"), is("0=A,1=B,2=C")); assertThat(REG_CENTER.get("/" + jobName + "/config/cron"), is("0/1 * * * * ?")); assertThat( REG_CENTER.get("/" + jobName + "/servers/" + localHostService.getIp() + "/hostName"), is(localHostService.getHostName())); if (disabled) { assertTrue( REG_CENTER.isExisted( "/" + jobName + "/servers/" + localHostService.getIp() + "/disabled")); } else { assertFalse( REG_CENTER.isExisted( "/" + jobName + "/servers/" + localHostService.getIp() + "/disabled")); } assertFalse( REG_CENTER.isExisted("/" + jobName + "/servers/" + localHostService.getIp() + "/stoped")); assertThat( REG_CENTER.get("/" + jobName + "/servers/" + localHostService.getIp() + "/status"), is(ServerStatus.READY.name())); REG_CENTER.remove("/" + jobName + "/leader/election"); assertTrue(leaderElectionService.isLeader()); }
/** * 获取运行在本作业服务器的失效转移序列号. * * @return 运行在本作业服务器的失效转移序列号 */ public List<Integer> getLocalHostFailoverItems() { List<String> items = jobNodeStorage.getJobNodeChildrenKeys(ExecutionNode.ROOT); List<Integer> result = new ArrayList<>(items.size()); String ip = localHostService.getIp(); for (String each : items) { int item = Integer.parseInt(each); String node = FailoverNode.getExecutionFailoverNode(item); if (jobNodeStorage.isJobNodeExisted(node) && ip.equals(jobNodeStorage.getJobNodeDataDirectly(node))) { result.add(item); } } Collections.sort(result); return result; }