Search in sources :

Example 96 with SaturnJobConsoleException

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

the class MarathonRestClient method getRegistryRepositoriesTagsList.

public static String getRegistryRepositoriesTagsList(String repository) throws SaturnJobConsoleException {
    String urlStr = SaturnEnvProperties.VIP_SATURN_DCOS_REGISTRY_URI + "/v2/" + repository + "/tags/list";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpGet httpGet = new HttpGet(urlStr);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            String entityContent = getEntityContent(entity);
            StatusLine statusLine = httpResponse.getStatusLine();
            if (statusLine != null && statusLine.getStatusCode() == 200) {
                return entityContent;
            } else {
                throw new SaturnJobConsoleException(entityContent);
            }
        } else {
            throw new SaturnJobConsoleException("Not data returned, url is " + urlStr);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) IOException(java.io.IOException)

Example 97 with SaturnJobConsoleException

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

the class MarathonRestClient method destroy.

public static void destroy(String userName, String password, String appId) throws SaturnJobConsoleException {
    String urlStr = SaturnEnvProperties.VIP_SATURN_DCOS_REST_URI + "/v2/apps/" + appId;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpDelete httpDelete = new HttpDelete(urlStr);
        httpDelete.setHeader("Authorization", "Basic " + Base64.encodeBase64String((userName + ":" + password).getBytes("UTF-8")));
        CloseableHttpResponse httpResponse = httpClient.execute(httpDelete);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine != null) {
            int statusCode = statusLine.getStatusCode();
            String reasonPhrase = statusLine.getReasonPhrase();
            if (statusCode == 200) {
            } else {
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    String entityContent = getEntityContent(entity);
                    throw new SaturnJobConsoleException(entityContent);
                } else {
                    throw new SaturnJobConsoleException("statusCode is " + statusCode + ", reasonPhrase is " + reasonPhrase);
                }
            }
        } else {
            throw new SaturnJobConsoleException("Not status returned, url is " + urlStr);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) IOException(java.io.IOException)

Example 98 with SaturnJobConsoleException

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

the class ContainerServiceImpl method saveOrUpdateContainerToken.

@Override
public void saveOrUpdateContainerToken(ContainerToken containerToken) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.inSessionClient();
    String dcosConfigTokenNodePath = ContainerNodePath.getDcosConfigTokenNodePath();
    try {
        // Update scale job's shardingItemParameters. Disable job, sleep 1s, update shardingItemParameters, sleep
        // 1s, enable job
        Map<String, List<ContainerScaleJob>> allContainerScaleJobs = new HashMap<>();
        List<String> tasks = getTasks(curatorFrameworkOp);
        for (String task : tasks) {
            List<ContainerScaleJob> containerScaleJobs = getContainerScaleJobs(task);
            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, getContainerRestService().serializeContainerToken(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 99 with SaturnJobConsoleException

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

the class ContainerServiceImpl method getContainerScaleJobVo.

@Override
public ContainerScaleJobVo getContainerScaleJobVo(String taskId, String jobName) throws SaturnJobConsoleException {
    ContainerScaleJobVo containerScaleJobVo = null;
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.inSessionClient();
    try {
        ContainerScaleJob containerScaleJob = getContainerScaleJob(curatorFrameworkOp, taskId, jobName);
        ContainerScaleJobConfig containerScaleJobConfig = containerScaleJob.getContainerScaleJobConfig();
        containerScaleJobVo = new ContainerScaleJobVo();
        containerScaleJobVo.setJobName(containerScaleJobConfig.getJobName());
        containerScaleJobVo.setJobDesc(containerScaleJobConfig.getJobDesc());
        if (containerScaleJobConfig.getInstances() != null) {
            containerScaleJobVo.setInstances(containerScaleJobConfig.getInstances().toString());
        }
        if (containerScaleJobConfig.getTimeZone() == null) {
            containerScaleJobVo.setTimeZone(SaturnConstants.TIME_ZONE_ID_DEFAULT);
        } else {
            containerScaleJobVo.setTimeZone(containerScaleJobConfig.getTimeZone());
        }
        containerScaleJobVo.setCron(containerScaleJobConfig.getCron());
        containerScaleJobVo.setEnabled(containerScaleJob.getEnabled().toString());
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    }
    return containerScaleJobVo;
}
Also used : ContainerScaleJobVo(com.vip.saturn.job.console.domain.container.vo.ContainerScaleJobVo) 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)

Example 100 with SaturnJobConsoleException

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

the class ContainerServiceImpl method addContainerScaleJob.

@Override
public void addContainerScaleJob(String taskId, String jobDesc, Integer instances, String timeZone, String cron) throws SaturnJobConsoleException {
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.inSessionClient();
    String jobName = SaturnConstants.SYSTEM_SCALE_JOB_PREFEX + System.currentTimeMillis();
    ContainerToken containerToken = getContainerToken();
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setDescription(jobDesc);
    jobConfig.setTimeZone(timeZone);
    jobConfig.setCron(cron);
    jobConfig.setJobMode(JobMode.system_scale);
    jobConfig.setJobType(JobBriefInfo.JobType.SHELL_JOB.name());
    jobConfig.setPreferList("@" + taskId);
    jobConfig.setShardingTotalCount(1);
    jobConfig.setShardingItemParameters(getContainerScaleJobShardingItemParameters(containerToken, taskId, instances));
    jobConfig.setUseDispreferList(false);
    jobConfig.setTimeout4AlarmSeconds(30);
    jobConfig.setTimeoutSeconds(30);
    jobConfig.setJobParameter("");
    jobConfig.setQueueName("");
    jobConfig.setChannelName("");
    jobConfig.setPausePeriodDate("");
    jobConfig.setPausePeriodTime("");
    RequestResult requestResult = executorService.addJobs(jobConfig);
    if (!requestResult.isSuccess()) {
        throw new SaturnJobConsoleException(requestResult.getMessage());
    }
    ContainerScaleJobConfig containerScaleJobConfig = new ContainerScaleJobConfig();
    containerScaleJobConfig.setJobName(jobName);
    containerScaleJobConfig.setJobDesc(jobDesc);
    containerScaleJobConfig.setInstances(instances);
    containerScaleJobConfig.setTimeZone(timeZone);
    containerScaleJobConfig.setCron(cron);
    try {
        String containerScaleJobStr = JSON.toJSONString(containerScaleJobConfig);
        String dcosTaskScaleJobNodePath = ContainerNodePath.getDcosTaskScaleJobNodePath(taskId, jobName);
        curatorFrameworkOp.fillJobNodeIfNotExist(dcosTaskScaleJobNodePath, containerScaleJobStr);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e.getMessage(), e);
    }
}
Also used : 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