/** * 从待执行队列中获取所有有资格执行的作业上下文. * * @param ineligibleJobContexts 无资格执行的作业上下文 * @return 有资格执行的作业上下文集合 */ public Collection<JobContext> getAllEligibleJobContexts( final Collection<JobContext> ineligibleJobContexts) { if (!regCenter.isExisted(ReadyNode.ROOT)) { return Collections.emptyList(); } Collection<String> ineligibleJobNames = Collections2.transform( ineligibleJobContexts, new Function<JobContext, String>() { @Override public String apply(final JobContext input) { return input.getJobConfig().getJobName(); } }); List<String> jobNames = regCenter.getChildrenKeys(ReadyNode.ROOT); List<JobContext> result = new ArrayList<>(jobNames.size()); for (String each : jobNames) { if (ineligibleJobNames.contains(each)) { continue; } Optional<CloudJobConfiguration> jobConfig = configService.load(each); if (!jobConfig.isPresent()) { regCenter.remove(ReadyNode.getReadyJobNodePath(each)); continue; } if (!runningService.isJobRunning(each) || JobExecutionType.DAEMON == jobConfig.get().getJobExecutionType()) { result.add(JobContext.from(jobConfig.get(), ExecutionType.READY)); } } return result; }
/** * 设置禁用错过重执行. * * @param jobName 作业名称 */ public void setMisfireDisabled(final String jobName) { Optional<CloudJobConfiguration> cloudJobConfig = configService.load(jobName); if (cloudJobConfig.isPresent() && null != regCenter.getDirectly(ReadyNode.getReadyJobNodePath(jobName))) { regCenter.persist(ReadyNode.getReadyJobNodePath(jobName), "1"); } }
/** * 从待执行队列中删除相关作业. * * @param jobNames 待删除的作业名集合 */ public void remove(final Collection<String> jobNames) { for (String each : jobNames) { String readyJobNode = ReadyNode.getReadyJobNodePath(each); String timesStr = regCenter.getDirectly(readyJobNode); int times = null == timesStr ? 0 : Integer.parseInt(timesStr); if (times <= 1) { regCenter.remove(readyJobNode); } else { regCenter.persist(readyJobNode, Integer.toString(times - 1)); } } }
/** * 将常驻作业放入待执行队列. * * @param jobName 作业名称 */ public void addDaemon(final String jobName) { if (regCenter.getNumChildren(ReadyNode.ROOT) > env.getFrameworkConfiguration().getJobStateQueueSize()) { log.warn( "Cannot add daemon job, caused by read state queue size is larger than {}.", env.getFrameworkConfiguration().getJobStateQueueSize()); return; } Optional<CloudJobConfiguration> cloudJobConfig = configService.load(jobName); if (!cloudJobConfig.isPresent() || JobExecutionType.DAEMON != cloudJobConfig.get().getJobExecutionType()) { return; } regCenter.persist(ReadyNode.getReadyJobNodePath(jobName), "1"); }
@Test public void assertGetAllServersBriefInfo() { when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job1", "test_job2")); when(regCenter.getChildrenKeys("/test_job1/servers")).thenReturn(Arrays.asList("ip1", "ip2")); when(regCenter.getChildrenKeys("/test_job2/servers")).thenReturn(Arrays.asList("ip3", "ip4")); when(regCenter.get("/test_job1/servers/ip1/hostName")).thenReturn("host1"); when(regCenter.get("/test_job1/servers/ip2/hostName")).thenReturn("host2"); when(regCenter.get("/test_job2/servers/ip3/hostName")).thenReturn("host3"); when(regCenter.get("/test_job2/servers/ip4/hostName")).thenReturn("host4"); when(regCenter.isExisted("/test_job1/servers/ip1/shutdown")).thenReturn(false); when(regCenter.isExisted("/test_job1/servers/ip1/status")).thenReturn(true); when(regCenter.isExisted("/test_job1/servers/ip2/shutdown")).thenReturn(true); when(regCenter.isExisted("/test_job2/servers/ip3/shutdown")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/ip3/status")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/ip4/shutdown")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/ip4/status")).thenReturn(true); int i = 0; for (ServerBriefInfo each : serverStatisticsAPI.getAllServersBriefInfo()) { i++; assertThat(each.getServerIp(), is("ip" + i)); assertThat(each.getServerHostName(), is("host" + i)); switch (i) { case 1: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK)); break; case 2: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED)); break; case 3: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED)); break; case 4: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK)); break; default: fail(); } } }
/** * 将瞬时作业放入待执行队列. * * @param jobName 作业名称 */ public void addTransient(final String jobName) { if (regCenter.getNumChildren(ReadyNode.ROOT) > env.getFrameworkConfiguration().getJobStateQueueSize()) { log.warn( "Cannot add transient job, caused by read state queue size is larger than {}.", env.getFrameworkConfiguration().getJobStateQueueSize()); return; } Optional<CloudJobConfiguration> cloudJobConfig = configService.load(jobName); if (!cloudJobConfig.isPresent() || JobExecutionType.TRANSIENT != cloudJobConfig.get().getJobExecutionType()) { return; } String readyJobNode = ReadyNode.getReadyJobNodePath(jobName); String times = regCenter.getDirectly(readyJobNode); if (cloudJobConfig.get().getTypeConfig().getCoreConfig().isMisfire()) { regCenter.persist( readyJobNode, Integer.toString(null == times ? 1 : Integer.parseInt(times) + 1)); } else { regCenter.persist(ReadyNode.getReadyJobNodePath(jobName), "1"); } }
@Test public void assertGetJobs() { when(regCenter.getChildrenKeys("/")) .thenReturn(Arrays.asList("test_job1", "test_job2", "test_job3")); when(regCenter.isExisted("/test_job1/servers/localhost")).thenReturn(true); when(regCenter.isExisted("/test_job2/servers/localhost")).thenReturn(true); when(regCenter.isExisted("/test_job3/servers/localhost")).thenReturn(false); when(regCenter.get("/test_job1/servers/localhost/hostName")).thenReturn("localhost"); when(regCenter.get("/test_job2/servers/localhost/hostName")).thenReturn("localhost"); when(regCenter.get("/test_job1/servers/localhost/sharding")).thenReturn("0,1"); when(regCenter.get("/test_job2/servers/localhost/sharding")).thenReturn("2"); when(regCenter.get("/test_job1/servers/localhost/status")).thenReturn("RUNNING"); when(regCenter.get("/test_job2/servers/localhost/status")).thenReturn("RUNNING"); when(regCenter.isExisted("/test_job1/servers/localhost/disabled")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/localhost/disabled")).thenReturn(false); when(regCenter.isExisted("/test_job1/servers/localhost/paused")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/localhost/paused")).thenReturn(false); when(regCenter.isExisted("/test_job1/servers/localhost/shutdown")).thenReturn(false); when(regCenter.isExisted("/test_job2/servers/localhost/shutdown")).thenReturn(false); int i = 0; for (ServerInfo each : serverStatisticsAPI.getJobs("localhost")) { i++; assertThat(each.getJobName(), is("test_job" + i)); assertThat(each.getIp(), is("localhost")); assertThat(each.getHostName(), is("localhost")); assertThat(each.getStatus(), is(ServerInfo.ServerStatus.RUNNING)); switch (i) { case 1: assertThat(each.getSharding(), is("0,1")); break; case 2: assertThat(each.getSharding(), is("2")); break; default: fail(); } } }