Search in sources :

Example 66 with SaturnJobConsoleException

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

the class RegistryCenterServiceImpl method getCuratorFrameworkOp.

@Override
public CuratorRepository.CuratorFrameworkOp getCuratorFrameworkOp(String namespace) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = null;
    try {
        RegistryCenterConfiguration registryCenterConfiguration = findConfigByNamespace(namespace);
        if (registryCenterConfiguration == null) {
            throw new SaturnJobConsoleException("find registryCenterConfiguration failed");
        }
        String nns = registryCenterConfiguration.getNameAndNamespace();
        if (nns == null) {
            throw new SaturnJobConsoleException("get name and namespace failed");
        }
        String zkAddressList = registryCenterConfiguration.getZkAddressList();
        String digest = registryCenterConfiguration.getDigest();
        synchronized (getNnsLock(nns)) {
            if (!registryCenterClientMap.containsKey(nns)) {
                final RegistryCenterClient registryCenterClient = new RegistryCenterClient();
                registryCenterClient.setNameAndNamespace(nns);
                registryCenterClient.setZkAddr(zkAddressList);
                CuratorFramework curatorFramework = curatorRepository.connect(zkAddressList, namespace, digest);
                if (curatorFramework != null) {
                    registryCenterClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
                    registryCenterClient.setCuratorClient(curatorFramework);
                    registryCenterClientMap.put(nns, registryCenterClient);
                    curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorFramework);
                }
            } else {
                RegistryCenterClient registryCenterClient = registryCenterClientMap.get(nns);
                if (registryCenterClient != null) {
                    CuratorFramework curatorFramework = registryCenterClient.getCuratorClient();
                    if (curatorFramework != null) {
                        registryCenterClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
                        curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorFramework);
                    }
                }
            }
        }
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        throw new SaturnJobConsoleException(e);
    }
    if (curatorFrameworkOp == null) {
        throw new SaturnJobConsoleException("Connect zookeeper failed");
    }
    return curatorFrameworkOp;
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)

Example 67 with SaturnJobConsoleException

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

the class SystemConfigServiceImpl method createConfig.

@Override
public Integer createConfig(SystemConfig systemConfig) throws SaturnJobConsoleException {
    List<String> properties = new ArrayList<>();
    properties.add(systemConfig.getProperty());
    List<SystemConfig> systemConfigs = systemConfig4SqlService.selectByProperty(systemConfig.getProperty());
    boolean found = false;
    if (systemConfigs != null) {
        for (int i = 0; i < systemConfigs.size(); i++) {
            SystemConfig config = systemConfigs.get(i);
            if (StringUtils.equals(config.getProperty(), systemConfig.getProperty())) {
                found = true;
                break;
            }
        }
    }
    if (found) {
        throw new SaturnJobConsoleException(String.format("systemConfig %s already existed", systemConfig.getProperty()));
    }
    int result = systemConfig4SqlService.insert(systemConfig);
    updateCacheIfNeed(systemConfig, result);
    return result;
}
Also used : SystemConfig(com.vip.saturn.job.console.mybatis.entity.SystemConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 68 with SaturnJobConsoleException

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

the class UtilsServiceImpl method checkAndForecastCron.

@Override
public ForecastCronResult checkAndForecastCron(final String timeZone, final String cron) throws SaturnJobConsoleException {
    if (StringUtils.isBlank(timeZone)) {
        throw new SaturnJobConsoleException("timeZone不能为空");
    }
    if (StringUtils.isBlank(cron)) {
        throw new SaturnJobConsoleException("cron不能为空");
    }
    String timeZoneTrim = timeZone.trim();
    String cronTrim = cron.trim();
    if (!SaturnConstants.TIME_ZONE_IDS.contains(timeZoneTrim)) {
        throw new SaturnJobConsoleException(String.format("timeZone(%s)无效", timeZoneTrim));
    }
    TimeZone tz = TimeZone.getTimeZone(timeZoneTrim);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setTimeZone(tz);
    CronExpression cronExpression = null;
    try {
        cronExpression = new CronExpression(cronTrim);
    } catch (ParseException e) {
        throw new SaturnJobConsoleException(String.format("cron(%s)格式错误:%s", cronTrim, e.getMessage()));
    }
    cronExpression.setTimeZone(tz);
    ForecastCronResult forecastCronResult = new ForecastCronResult();
    forecastCronResult.setTimeZone(timeZoneTrim);
    forecastCronResult.setCron(cronTrim);
    forecastCronResult.setNextFireTimes(new ArrayList<String>());
    Date now = new Date();
    for (int i = 0; i < 10; i++) {
        Date next = cronExpression.getNextValidTimeAfter(now);
        if (next != null) {
            forecastCronResult.getNextFireTimes().add(dateFormat.format(next));
            now = next;
        }
    }
    if (forecastCronResult.getNextFireTimes().isEmpty()) {
        throw new SaturnJobConsoleException(String.format("cron(%s)可能描述的是一个过去的时间,将不会被执行", cronTrim));
    }
    return forecastCronResult;
}
Also used : TimeZone(java.util.TimeZone) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ForecastCronResult(com.vip.saturn.job.console.domain.ForecastCronResult) CronExpression(com.vip.saturn.job.console.utils.CronExpression) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 69 with SaturnJobConsoleException

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

the class MarathonServiceImpl method saveContainerToken.

@Override
public void saveContainerToken(String namespace, ContainerToken containerToken) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
    String dcosConfigTokenNodePath = ContainerNodePath.getDcosConfigTokenNodePath();
    try {
        // Need to update scalaJob's shardingItemParameters:
        // 1. update scale job's shardingItemParameters.
        // 2. disable job, sleep 1s.
        // 3. update shardingItemParameters, sleep 1s.
        // 4. enable job.
        Map<String, List<ContainerScaleJob>> allContainerScaleJobs = new HashMap<>();
        List<String> tasks = getTasks(curatorFrameworkOp);
        for (String task : tasks) {
            List<ContainerScaleJob> containerScaleJobs = getContainerScaleJobs(task, curatorFrameworkOp);
            allContainerScaleJobs.put(task, containerScaleJobs);
        }
        Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator = allContainerScaleJobs.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<ContainerScaleJob>> next = iterator.next();
            List<ContainerScaleJob> containerScaleJobs = next.getValue();
            for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
                if (containerScaleJob.getEnabled()) {
                    curatorFrameworkOp.update(JobNodePath.getConfigNodePath(containerScaleJob.getContainerScaleJobConfig().getJobName(), "enabled"), "false");
                }
            }
        }
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        // NOSONAR
        }
        Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator2 = allContainerScaleJobs.entrySet().iterator();
        while (iterator2.hasNext()) {
            Map.Entry<String, List<ContainerScaleJob>> next = iterator2.next();
            String taskId = next.getKey();
            List<ContainerScaleJob> containerScaleJobs = next.getValue();
            for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
                String jobName = containerScaleJob.getContainerScaleJobConfig().getJobName();
                Integer instances = containerScaleJob.getContainerScaleJobConfig().getInstances();
                String shardingItemParametersNodePath = JobNodePath.getConfigNodePath(jobName, "shardingItemParameters");
                String scaleShardingItemParameters = getContainerScaleJobShardingItemParameters(containerToken, taskId, instances);
                curatorFrameworkOp.update(shardingItemParametersNodePath, scaleShardingItemParameters);
            }
        }
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        // NOSONAR
        }
        Iterator<Map.Entry<String, List<ContainerScaleJob>>> iterator3 = allContainerScaleJobs.entrySet().iterator();
        while (iterator3.hasNext()) {
            Map.Entry<String, List<ContainerScaleJob>> next = iterator3.next();
            List<ContainerScaleJob> containerScaleJobs = next.getValue();
            for (ContainerScaleJob containerScaleJob : containerScaleJobs) {
                if (containerScaleJob.getEnabled()) {
                    curatorFrameworkOp.update(JobNodePath.getConfigNodePath(containerScaleJob.getContainerScaleJobConfig().getJobName(), "enabled"), "true");
                }
            }
        }
        curatorFrameworkOp.update(dcosConfigTokenNodePath, JSON.toJSONString(containerToken));
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    }
}
Also used : SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 70 with SaturnJobConsoleException

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

the class MarathonServiceImpl method deleteContainerScaleJob.

@Override
public void deleteContainerScaleJob(String namespace, String taskId, String jobName) throws SaturnJobConsoleException {
    // disable job, and delete it
    // wait 5s to disable job at most
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.getCuratorFrameworkOp(namespace);
    try {
        String jobNodePath = JobNodePath.getJobNodePath(jobName);
        if (curatorFrameworkOp.checkExists(jobNodePath)) {
            String enabledNodePath = JobNodePath.getConfigNodePath(jobName, "enabled");
            String enabledStr = curatorFrameworkOp.getData(enabledNodePath);
            Boolean enabled = Boolean.valueOf(enabledStr);
            if (enabled) {
                curatorFrameworkOp.update(enabledNodePath, false);
            }
            long waitStopTime = 5000L;
            while (waitStopTime > 0L) {
                Thread.sleep(100);
                waitStopTime -= 100;
                JobStatus jobStatus = jobService.getJobStatus(namespace, jobName);
                if (JobStatus.STOPPED.equals(jobStatus)) {
                    jobService.removeJob(namespace, jobName);
                    deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
                    return;
                }
            }
            throw new SaturnJobConsoleException("The job is not stopped, cannot be deleted, please retry later");
        } else {
            deleteScaleJobNodePath(curatorFrameworkOp, taskId, jobName);
        }
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e.getMessage(), e);
    }
}
Also used : JobStatus(com.vip.saturn.job.console.domain.JobStatus) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

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