Search in sources :

Example 81 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException 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);
    }
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) ParseException(java.text.ParseException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) CuratorFrameworkOp(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)

Example 82 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class JobServiceImpl method exportSelectedJobs.

@Override
public File exportSelectedJobs(String namespace, List<String> jobList) throws SaturnJobConsoleException {
    try {
        File tmp = new File(SaturnConstants.CACHES_FILE_PATH, "tmp_exportFile_" + System.currentTimeMillis() + "_" + random.nextInt(1000) + ".xls");
        if (!tmp.exists()) {
            FileUtils.forceMkdir(tmp.getParentFile());
            tmp.createNewFile();
        }
        WritableWorkbook writableWorkbook = Workbook.createWorkbook(tmp);
        WritableSheet sheet1 = writableWorkbook.createSheet("Sheet1", 0);
        setExcelHeader(sheet1);
        // 单个域下作业量不会很大,直接复用之前的方法取该域下全部作业再过滤
        List<JobConfig> targetJobs = filterTargetJobs(jobList, getUnSystemJobs(namespace));
        // sort by jobName
        Collections.sort(targetJobs, new Comparator<JobConfig>() {

            @Override
            public int compare(JobConfig o1, JobConfig o2) {
                return o1.getJobName().compareTo(o2.getJobName());
            }
        });
        setExcelContent(namespace, sheet1, targetJobs);
        writableWorkbook.write();
        writableWorkbook.close();
        return tmp;
    } catch (Exception e) {
        throw new SaturnJobConsoleException(e);
    }
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) ParseException(java.text.ParseException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 83 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException 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));
    }
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) CuratorFrameworkOp(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)

Example 84 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class JobServiceImpl method removeJobFromZk.

/**
 * 删除zk上的作业结点。先持久化config/toDelete结点,让executor收到该事件,shutdown自身的该作业。如果所有executor都已经shutdown该作业,则才可以安全删除作业结点。
 *
 * @return 等待executor shutdown作业,等待一定时间后,如果executor还没完全shutdown,则放弃等待,返回false。
 *         否则,在等待时间内,executor都shutdown完全,则删除作业结点,并返回true。
 */
private boolean removeJobFromZk(String jobName, CuratorRepository.CuratorFrameworkOp curatorFrameworkOp) throws SaturnJobConsoleException {
    // 1.作业的executor全online的情况,添加toDelete节点,触发监听器动态删除节点
    String toDeleteNodePath = JobNodePath.getConfigNodePath(jobName, "toDelete");
    if (curatorFrameworkOp.checkExists(toDeleteNodePath)) {
        curatorFrameworkOp.deleteRecursive(toDeleteNodePath);
    }
    curatorFrameworkOp.create(toDeleteNodePath);
    for (int i = 0; i < 20; i++) {
        // 2.作业的executor全offline的情况,或有几个online,几个offline的情况
        String jobServerPath = JobNodePath.getServerNodePath(jobName);
        if (!curatorFrameworkOp.checkExists(jobServerPath)) {
            // (1)如果不存在$Job/JobName/servers节点,说明该作业没有任何executor接管,可直接删除作业节点
            curatorFrameworkOp.deleteRecursive(JobNodePath.getJobNodePath(jobName));
            return true;
        }
        // (2)如果该作业servers下没有任何executor,可直接删除作业节点
        List<String> executors = curatorFrameworkOp.getChildren(jobServerPath);
        if (CollectionUtils.isEmpty(executors)) {
            curatorFrameworkOp.deleteRecursive(JobNodePath.getJobNodePath(jobName));
            return true;
        }
        // (3)只要该作业没有一个能运行的该作业的executor在线,那么直接删除作业节点
        boolean hasOnlineExecutor = false;
        for (String executor : executors) {
            if (curatorFrameworkOp.checkExists(ExecutorNodePath.getExecutorNodePath(executor, "ip")) && curatorFrameworkOp.checkExists(JobNodePath.getServerStatus(jobName, executor))) {
                hasOnlineExecutor = true;
            } else {
                curatorFrameworkOp.deleteRecursive(JobNodePath.getServerNodePath(jobName, executor));
            }
        }
        if (!hasOnlineExecutor) {
            curatorFrameworkOp.deleteRecursive(JobNodePath.getJobNodePath(jobName));
            return true;
        }
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            throw new SaturnJobConsoleException(e);
        }
    }
    return false;
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) ParseException(java.text.ParseException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 85 with SaturnJobConsoleException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleException in project Saturn by vipshop.

the class JobServiceImpl method getJobStatus.

@Override
public JobStatus getJobStatus(String namespace, String jobName) throws SaturnJobConsoleException {
    JobConfig4DB jobConfig = currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobName);
    if (jobConfig == null) {
        throw new SaturnJobConsoleException(ERROR_CODE_NOT_EXISTED, "不能获取该作业(" + jobName + ")的状态,因为该作业不存在");
    }
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
    return getJobStatus(jobName, curatorFrameworkOp, jobConfig.getEnabled());
}
Also used : CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) CuratorFrameworkOp(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp) JobConfig4DB(com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)

Aggregations

SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)190 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)56 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)56 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)39 RequestResult (com.vip.saturn.job.console.domain.RequestResult)31 ParseException (java.text.ParseException)28 ResponseEntity (org.springframework.http.ResponseEntity)26 Transactional (org.springframework.transaction.annotation.Transactional)23 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)22 CuratorFrameworkOp (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)22 IOException (java.io.IOException)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 StatusLine (org.apache.http.StatusLine)18 HttpEntity (org.apache.http.HttpEntity)16 Audit (com.vip.saturn.job.console.aop.annotation.Audit)13 JobConfig (com.vip.saturn.job.console.domain.JobConfig)11 HttpHeaders (org.springframework.http.HttpHeaders)11 CurrentJobConfig (com.vip.saturn.job.console.mybatis.entity.CurrentJobConfig)9 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)8 ArrayList (java.util.ArrayList)8