Search in sources :

Example 26 with SaturnJobConsoleHttpException

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

the class RestApiServiceImplTest method testUpdateJobFailAsSaturnsIsNotStopped.

@Test
public void testUpdateJobFailAsSaturnsIsNotStopped() throws SaturnJobConsoleException {
    String jobName = "testJob";
    when(jobService.getJobStatus(TEST_NAME_SPACE_NAME, jobName)).thenReturn(JobStatus.STOPPING);
    // run
    try {
        restApiService.updateJob(TEST_NAME_SPACE_NAME, jobName, buildUpdateJobConfig(jobName));
    } catch (SaturnJobConsoleHttpException e) {
        assertEquals("status code is not 400", 400, e.getStatusCode());
        assertEquals("error message is not equals", "job's status is not {STOPPED}", e.getMessage());
    }
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Test(org.junit.Test)

Example 27 with SaturnJobConsoleHttpException

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

the class RestApiServiceImplTest method testDeleteJobFailAsStatusIsNotStopped.

@Test
public void testDeleteJobFailAsStatusIsNotStopped() throws SaturnJobConsoleException {
    // prepare
    String jobName = "testJob";
    when(jobService.getJobStatus(TEST_NAME_SPACE_NAME, jobName)).thenReturn(JobStatus.STOPPING);
    // run
    try {
        restApiService.deleteJob(TEST_NAME_SPACE_NAME, jobName);
    } catch (SaturnJobConsoleHttpException e) {
        assertEquals("status code is not 400", 400, e.getStatusCode());
        assertEquals("error message is not equals", "job's status is not {STOPPED}", e.getMessage());
    }
    // verify
    verify(jobService, times(0)).removeJob(TEST_NAME_SPACE_NAME, jobName);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Test(org.junit.Test)

Example 28 with SaturnJobConsoleHttpException

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

the class NamespaceServiceImplTest method testSrcNamespaceIdenticalToDestNamespace.

@Test
public void testSrcNamespaceIdenticalToDestNamespace() throws SaturnJobConsoleException {
    SaturnJobConsoleHttpException exception = null;
    String srcNamespace = "saturn.vip.vip.com";
    String destNamespace = "saturn.vip.vip.com";
    try {
        namespaceService.importJobsFromNamespaceToNamespace(srcNamespace, destNamespace, null);
    } catch (SaturnJobConsoleHttpException e) {
        exception = e;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(exception.getMessage(), "srcNamespace and destNamespace should be difference");
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) Test(org.junit.Test)

Example 29 with SaturnJobConsoleHttpException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException 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 30 with SaturnJobConsoleHttpException

use of com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException 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

SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)68 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)40 ResponseEntity (org.springframework.http.ResponseEntity)30 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)24 Test (org.junit.Test)15 HttpHeaders (org.springframework.http.HttpHeaders)13 Audit (com.vip.saturn.job.console.aop.annotation.Audit)12 JobConfig (com.vip.saturn.job.console.domain.JobConfig)10 Matchers.anyString (org.mockito.Matchers.anyString)8 NamespaceDomainInfo (com.vip.saturn.job.console.domain.NamespaceDomainInfo)7 AbstractSaturnConsoleTest (com.vip.saturn.job.console.AbstractSaturnConsoleTest)5 CurrentJobConfig (com.vip.saturn.job.console.mybatis.entity.CurrentJobConfig)5 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)5 MvcResult (org.springframework.test.web.servlet.MvcResult)5 Transactional (org.springframework.transaction.annotation.Transactional)5 NamespaceInfo (com.vip.saturn.job.console.mybatis.entity.NamespaceInfo)4 AlarmInfo (com.vip.saturn.job.integrate.entity.AlarmInfo)4 ParseException (java.text.ParseException)4 Map (java.util.Map)4 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)3