use of com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp in project Saturn by vipshop.
the class JobServiceImpl method createJobConfigToZk.
private void createJobConfigToZk(JobConfig jobConfig, Set<JobConfig> streamChangedJobs, CuratorRepository.CuratorFrameworkOp curatorFrameworkOp) throws SaturnJobConsoleException {
try {
String jobName = jobConfig.getJobName();
// 添加作业根节点和config结点
curatorFrameworkOp.create(JobNodePath.getConfigNodePath(jobName), "");
CuratorFrameworkOp.CuratorTransactionOp curatorTransactionOp = curatorFrameworkOp.inTransaction();
// 数据库有可能有重复作业的数据,去重,zk无需更新两次
Collection<JobConfig> streamChangedJobsNew = removeDuplicateByJobName(streamChangedJobs);
// 更新关联作业的上下游
for (JobConfig streamChangedJob : streamChangedJobsNew) {
String changedJobName = streamChangedJob.getJobName();
if (!curatorFrameworkOp.checkExists(JobNodePath.getConfigNodePath(changedJobName))) {
// 数据库存在该作业,但是zk不存在该作业,为垃圾数据
log.warn("the job({}) config node is not existing in zk", changedJobName);
continue;
}
curatorTransactionOp.replaceIfChanged(JobNodePath.getConfigNodePath(changedJobName, CONFIG_ITEM_UPSTREAM), streamChangedJob.getUpStream()).replaceIfChanged(JobNodePath.getConfigNodePath(changedJobName, CONFIG_ITEM_DOWNSTREAM), streamChangedJob.getDownStream());
}
// 添加作业
curatorTransactionOp.create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_ENABLED), jobConfig.getEnabled()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_DESCRIPTION), jobConfig.getDescription()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_CUSTOM_CONTEXT), jobConfig.getCustomContext()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_JOB_TYPE), jobConfig.getJobType()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_JOB_MODE), jobConfig.getJobMode()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_SHARDING_ITEM_PARAMETERS), jobConfig.getShardingItemParameters()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_JOB_PARAMETER), jobConfig.getJobParameter()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_QUEUE_NAME), jobConfig.getQueueName()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_CHANNEL_NAME), jobConfig.getChannelName()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_FAILOVER), jobConfig.getFailover()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_MONITOR_EXECUTION), "true").create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_TIMEOUT_4_ALARM_SECONDS), jobConfig.getTimeout4AlarmSeconds()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_TIMEOUT_SECONDS), jobConfig.getTimeoutSeconds()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_TIME_ZONE), jobConfig.getTimeZone()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_CRON), jobConfig.getCron()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_PAUSE_PERIOD_DATE), jobConfig.getPausePeriodDate()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_PAUSE_PERIOD_TIME), jobConfig.getPausePeriodTime()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_PROCESS_COUNT_INTERVAL_SECONDS), jobConfig.getProcessCountIntervalSeconds()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_SHARDING_TOTAL_COUNT), jobConfig.getShardingTotalCount()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_SHOW_NORMAL_LOG), jobConfig.getShowNormalLog()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_LOAD_LEVEL), jobConfig.getLoadLevel()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_JOB_DEGREE), jobConfig.getJobDegree()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_ENABLED_REPORT), jobConfig.getEnabledReport()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_PREFER_LIST), jobConfig.getPreferList()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_USE_DISPREFER_LIST), jobConfig.getUseDispreferList()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_LOCAL_MODE), jobConfig.getLocalMode()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_USE_SERIAL), jobConfig.getUseSerial()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_DEPENDENCIES), jobConfig.getDependencies()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_GROUPS), jobConfig.getGroups()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_RERUN), jobConfig.getRerun()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_UPSTREAM), jobConfig.getUpStream()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_DOWNSTREAM), jobConfig.getDownStream()).create(JobNodePath.getConfigNodePath(jobName, CONFIG_ITEM_JOB_CLASS), jobConfig.getJobClass());
// 注意!!! jobClass要最后更新,因为executor认为该结点为作业添加完成
// 提交事务
curatorTransactionOp.commit();
} catch (Exception e) {
log.error("create job to zk failed", e);
throw new SaturnJobConsoleException(e);
}
}
use of com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp in project Saturn by vipshop.
the class JobServiceImpl method persistJobFromDB.
@Override
public void persistJobFromDB(String namespace, JobConfig jobConfig) throws SaturnJobConsoleException {
jobConfig.setDefaultValues();
CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
saveJobConfigToZk(jobConfig, curatorFrameworkOp);
}
use of com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp in project Saturn by vipshop.
the class JobServiceImpl method getJobServers.
@Override
public List<JobServer> getJobServers(String namespace, String jobName) throws SaturnJobConsoleException {
CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
String serverNodePath = JobNodePath.getServerNodePath(jobName);
List<String> executors = curatorFrameworkOp.getChildren(serverNodePath);
List<JobServer> result = new ArrayList<>();
if (executors != null && !executors.isEmpty()) {
String leaderIp = curatorFrameworkOp.getData(JobNodePath.getLeaderNodePath(jobName, "election/host"));
JobStatus jobStatus = getJobStatus(namespace, jobName);
for (String each : executors) {
JobServer jobServer = getJobServer(jobName, leaderIp, each, curatorFrameworkOp);
jobServer.setJobStatus(jobStatus);
result.add(jobServer);
}
}
return result;
}
use of com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp in project Saturn by vipshop.
the class JobServiceImpl method stopAtOnce.
@Override
public void stopAtOnce(String namespace, String jobName) throws SaturnJobConsoleException {
JobStatus jobStatus = getJobStatus(namespace, jobName);
if (!JobStatus.STOPPING.equals(jobStatus)) {
throw new SaturnJobConsoleException(ERROR_CODE_BAD_REQUEST, String.format("该作业(%s)不处于STOPPING状态,不能立即终止", jobName));
}
List<String> jobServerList = getJobServerList(namespace, jobName);
if (jobServerList != null && !jobServerList.isEmpty()) {
CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
for (String executorName : jobServerList) {
String path = JobNodePath.getStopOneTimePath(jobName, executorName);
if (curatorFrameworkOp.checkExists(path)) {
curatorFrameworkOp.delete(path);
}
curatorFrameworkOp.create(path);
log.info("stopAtOnce namespace:{}, jobName:{}, executorName:{}", namespace, jobName, executorName);
}
} else {
throw new SaturnJobConsoleException(ERROR_CODE_BAD_REQUEST, String.format("没有executor接管该作业(%s),不能立即终止", jobName));
}
}
use of com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp in project Saturn by vipshop.
the class JobServiceImpl method getExecutionStatus.
@Override
public List<ExecutionInfo> getExecutionStatus(String namespace, String jobName) throws SaturnJobConsoleException {
CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
JobConfig jobConfig = getJobConfig(namespace, jobName);
if (!jobConfig.getEnabled() && JobStatus.STOPPED.equals(getJobStatus(jobName, curatorFrameworkOp, false))) {
return Lists.newArrayList();
}
// update report node and sleep for 500ms
updateReportNodeAndWait(jobName, curatorFrameworkOp, 500L);
// 如果execution节点不存在则返回空List
String executionNodePath = JobNodePath.getExecutionNodePath(jobName);
List<String> shardItems = curatorFrameworkOp.getChildren(executionNodePath);
if (shardItems == null || shardItems.isEmpty()) {
return Lists.newArrayList();
}
List<ExecutionInfo> result = Lists.newArrayList();
Map<String, String> itemExecutorMap = buildItem2ExecutorMap(jobName, curatorFrameworkOp);
for (Map.Entry<String, String> itemExecutorEntry : itemExecutorMap.entrySet()) {
result.add(buildExecutionInfo(jobName, itemExecutorEntry.getKey(), itemExecutorEntry.getValue(), curatorFrameworkOp, jobConfig));
}
// 可能有漏掉的running分片,比如新的机器接管了failover分片
for (String shardItem : shardItems) {
if (itemExecutorMap.containsKey(shardItem)) {
// 已经在之前的步骤计算了
continue;
}
String runningNodePath = JobNodePath.getExecutionNodePath(jobName, shardItem, "running");
boolean running = curatorFrameworkOp.checkExists(runningNodePath);
if (running) {
result.add(buildExecutionInfo(jobName, shardItem, null, curatorFrameworkOp, jobConfig));
}
}
Collections.sort(result);
return result;
}
Aggregations