Search in sources :

Example 61 with SaturnJobConsoleException

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

the class JobServiceImpl method addOrCopyJob.

private void addOrCopyJob(String namespace, JobConfig jobConfig, String jobNameCopied, String createdBy) throws SaturnJobConsoleException {
    List<JobConfig> unSystemJobs = getUnSystemJobs(namespace);
    Set<JobConfig> streamChangedJobs = new HashSet<>();
    validateJobConfig(namespace, jobConfig, unSystemJobs, streamChangedJobs);
    // 如果数据存在相同作业名,则抛异常
    // 直接再查一次,不使用unSystemJobs,因为也不能与系统作业名相同
    String jobName = jobConfig.getJobName();
    if (currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobName) != null) {
        throw new SaturnJobConsoleException(ERROR_CODE_BAD_REQUEST, String.format("该作业(%s)已经存在", jobName));
    }
    // 如果zk存在该作业,则尝试删除
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
    if (curatorFrameworkOp.checkExists(JobNodePath.getJobNodePath(jobName))) {
        if (!removeJobFromZk(jobName, curatorFrameworkOp)) {
            throw new SaturnJobConsoleException(ERROR_CODE_BAD_REQUEST, String.format("该作业(%s)正在删除中,请稍后再试", jobName));
        }
    }
    // 该域作业总数不能超过一定数量
    int maxJobNum = getMaxJobNum();
    if (jobIncExceeds(namespace, maxJobNum, 1)) {
        throw new SaturnJobConsoleException(ERROR_CODE_BAD_REQUEST, String.format("总作业数超过最大限制(%d),作业名%s创建失败", maxJobNum, jobName));
    }
    // 如果是copy作业,则从数据库中复制被拷贝的作业的配置到新的作业配置
    JobConfig myJobConfig = jobConfig;
    if (jobNameCopied != null) {
        myJobConfig = currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobNameCopied);
        SaturnBeanUtils.copyPropertiesIgnoreNull(jobConfig, myJobConfig);
    }
    // 设置作业配置字段默认值,并且强制纠正某些字段
    correctConfigValueWhenAddJob(myJobConfig);
    // 添加该作业到数据库
    currentJobConfigService.create(constructJobConfig4DB(namespace, myJobConfig, createdBy, createdBy));
    // 更新关联作业的上下游
    for (JobConfig streamChangedJob : streamChangedJobs) {
        currentJobConfigService.updateStream(constructJobConfig4DB(namespace, streamChangedJob, null, createdBy));
    }
    // 添加该作业配置到zk,并联动更新关联作业的上下游
    createJobConfigToZk(myJobConfig, streamChangedJobs, curatorFrameworkOp);
}
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)

Example 62 with SaturnJobConsoleException

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

the class JobServiceImpl method exportJobs.

@Override
public File exportJobs(String namespace) 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> unSystemJobs = getUnSystemJobs(namespace);
        // sort by jobName
        Collections.sort(unSystemJobs, new Comparator<JobConfig>() {

            @Override
            public int compare(JobConfig o1, JobConfig o2) {
                return o1.getJobName().compareTo(o2.getJobName());
            }
        });
        setExcelContent(namespace, sheet1, unSystemJobs);
        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 63 with SaturnJobConsoleException

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

the class JobServiceImpl method updateJobConfig.

@Transactional(rollbackFor = Exception.class)
@Override
public void updateJobConfig(String namespace, JobConfig jobConfig, String updatedBy) throws SaturnJobConsoleException {
    JobConfig4DB oldJobConfig4DB = currentJobConfigService.findConfigByNamespaceAndJobName(namespace, jobConfig.getJobName());
    if (oldJobConfig4DB == null) {
        throw new SaturnJobConsoleException(ERROR_CODE_NOT_EXISTED, String.format("该作业(%s)不存在", jobConfig.getJobName()));
    }
    // 从数据库拿出老的数据,将需要更新的数据赋值(为空的字段视为不需要更新)
    JobConfig4DB newJobConfig4DB = new JobConfig4DB();
    SaturnBeanUtils.copyProperties(oldJobConfig4DB, newJobConfig4DB);
    SaturnBeanUtils.copyPropertiesIgnoreNull(jobConfig, newJobConfig4DB);
    // 与老的数据库中的该作业的配置对比,如果没有改变,则直接返回
    if (oldJobConfig4DB.equals(newJobConfig4DB)) {
        return;
    }
    // 设置作业配置字段默认值,并且强制纠正某些字段
    correctConfigValueWhenUpdateJob(newJobConfig4DB);
    // 校验作业配置
    List<JobConfig> unSystemJobs = getUnSystemJobs(namespace);
    Set<JobConfig> streamChangedJobs = new HashSet<>();
    validateJobConfig(namespace, newJobConfig4DB, unSystemJobs, streamChangedJobs);
    // 更新该作业到数据库
    currentJobConfigService.updateNewAndSaveOld2History(newJobConfig4DB, oldJobConfig4DB, updatedBy);
    // 更新关联作业的上下游
    for (JobConfig streamChangedJob : streamChangedJobs) {
        currentJobConfigService.updateStream(constructJobConfig4DB(namespace, streamChangedJob, null, updatedBy));
    }
    // 更新作业配置到zk,并联动更新关联作业的上下游
    updateJobConfigToZk(newJobConfig4DB, streamChangedJobs, registryCenterService.getCuratorFrameworkOp(namespace));
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) JobConfig4DB(com.vip.saturn.job.console.mybatis.entity.JobConfig4DB) Transactional(org.springframework.transaction.annotation.Transactional)

Example 64 with SaturnJobConsoleException

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

the class NamespaceServiceImpl method importJobsFromNamespaceToNamespace.

@Override
public Map<String, List> importJobsFromNamespaceToNamespace(String srcNamespace, String destNamespace, String createdBy) throws SaturnJobConsoleException {
    if (StringUtils.isBlank(srcNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "srcNamespace should not be null");
    }
    if (StringUtils.isBlank(destNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "destNamespace should not be null");
    }
    if (StringUtils.equals(srcNamespace, destNamespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), "srcNamespace and destNamespace should be difference");
    }
    try {
        List<String> successfullyImportedJobs = new ArrayList<>();
        List<String> failedJobs = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        result.put("success", successfullyImportedJobs);
        result.put("fail", failedJobs);
        List<JobConfig> jobConfigs = jobService.getUnSystemJobs(srcNamespace);
        List<JobConfig> jobConfigUpdatedList = new ArrayList<>();
        for (JobConfig jobConfig : jobConfigs) {
            String jobName = jobConfig.getJobName();
            try {
                // 如果存在上下游关联关系,直接导入会检验不通过;需要先解除关联关系,创建成功后再更新关联关系
                JobConfig jobConfigUpdated = null;
                if (StringUtils.isBlank(jobConfig.getUpStream()) || StringUtils.isBlank(jobConfig.getDownStream())) {
                    jobConfigUpdated = new JobConfig();
                    jobConfigUpdated.setJobName(jobName);
                    jobConfigUpdated.setUpStream(jobConfig.getUpStream());
                    jobConfigUpdated.setDownStream(jobConfig.getDownStream());
                    jobConfig.setUpStream(null);
                    jobConfig.setDownStream(null);
                }
                jobService.addJob(destNamespace, jobConfig, createdBy);
                if (jobConfigUpdated != null) {
                    jobConfigUpdatedList.add(jobConfigUpdated);
                }
                successfullyImportedJobs.add(jobName);
            } catch (SaturnJobConsoleException e) {
                log.warn("fail to import job {} from {} to {}", jobName, srcNamespace, destNamespace, e);
                failedJobs.add(jobName);
            }
        }
        for (JobConfig jobConfig : jobConfigUpdatedList) {
            String jobName = jobConfig.getJobName();
            try {
                jobService.updateJobConfig(destNamespace, jobConfig, createdBy);
            } catch (SaturnJobConsoleException e) {
                log.warn("fail to update job upStream or downStream, namespace is {} jobName is {}", destNamespace, jobName, e);
                failedJobs.add(jobName);
                successfullyImportedJobs.remove(jobName);
            }
        }
        return result;
    } catch (SaturnJobConsoleException e) {
        log.warn("import jobs from {} to {} fail", srcNamespace, destNamespace, e);
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) JobConfig(com.vip.saturn.job.console.domain.JobConfig)

Example 65 with SaturnJobConsoleException

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

the class RegistryCenterServiceImpl method bindNamespaceAndZkCluster.

@Transactional(rollbackFor = { Exception.class })
@Override
public void bindNamespaceAndZkCluster(String namespace, String zkClusterKey, String updatedBy) throws SaturnJobConsoleException {
    ZkCluster currentCluster = getZkCluster(zkClusterKey);
    if (currentCluster == null) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(ERR_MSG_TEMPLATE_FAIL_TO_CREATE, namespace, "not found zkcluster" + zkClusterKey));
    }
    // namespace必须要存在
    if (!checkNamespaceExists(namespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), ERR_MSG_NS_NOT_FOUND);
    }
    // 判断其它集群是否有该域
    String zkClusterKeyOther = namespaceZkClusterMapping4SqlService.getZkClusterKey(namespace);
    if (zkClusterKeyOther != null) {
        ZkCluster zkClusterOther = getZkCluster(zkClusterKeyOther);
        if (zkClusterOther == null) {
            throw new SaturnJobConsoleException("zk cluster 不存在:" + zkClusterKeyOther);
        }
        if (zkClusterOther.getZkClusterKey().equals(zkClusterKey)) {
            throw new SaturnJobConsoleException("Namespace已经存在于此zk集群,不能重复添加");
        } else {
            throw new SaturnJobConsoleException("Namespace存在于另外的zk集群:" + zkClusterOther.getZkClusterKey() + ",不能重复添加");
        }
    }
    try {
        namespaceZkClusterMapping4SqlService.insert(namespace, "", zkClusterKey, updatedBy);
        postBindNamespaceAndZkCluster(namespace, currentCluster);
        // refresh
        notifyRefreshRegCenter();
    } catch (Exception e) {
        namespaceZkClusterMapping4SqlService.remove(namespace, updatedBy);
        throw new SaturnJobConsoleException(e.getMessage());
    }
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) ZkCluster(com.vip.saturn.job.console.domain.ZkCluster) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Transactional(org.springframework.transaction.annotation.Transactional)

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