Search in sources :

Example 1 with ContainerConfig

use of com.vip.saturn.job.console.domain.container.ContainerConfig in project Saturn by vipshop.

the class DashboardServiceImpl method isContainerInstanceMismatch.

private AbnormalContainer isContainerInstanceMismatch(AbnormalContainer abnormalContainer, CuratorFrameworkOp curatorFrameworkOp) {
    try {
        String taskId = abnormalContainer.getTaskId();
        String dcosTaskConfigNodePath = ContainerNodePath.getDcosTaskConfigNodePath(taskId);
        long configMtime = curatorFrameworkOp.getMtime(dcosTaskConfigNodePath);
        String dcosTaskScaleJobsNodePath = ContainerNodePath.getDcosTaskScaleJobsNodePath(taskId);
        List<String> scaleJobs = curatorFrameworkOp.getChildren(dcosTaskScaleJobsNodePath);
        long maxItemMtime = 0L;
        String lastScalaJob = null;
        if (scaleJobs != null && !taskId.isEmpty()) {
            for (String scaleJob : scaleJobs) {
                // 如果存在running节点,则不做检查
                if (curatorFrameworkOp.checkExists(JobNodePath.getExecutionNodePath(scaleJob, "0", "running"))) {
                    return null;
                }
                String completedNodePath = JobNodePath.getExecutionNodePath(scaleJob, "0", "completed");
                long completedMtime = curatorFrameworkOp.getMtime(completedNodePath);
                if (completedMtime > maxItemMtime) {
                    lastScalaJob = scaleJob;
                    maxItemMtime = completedMtime;
                }
            }
        }
        Integer myInstance = -1;
        long lastMtime = 0L;
        if (configMtime > maxItemMtime) {
            String taskConfigData = curatorFrameworkOp.getData(dcosTaskConfigNodePath);
            if (taskConfigData != null && taskConfigData.trim().length() > 0) {
                ContainerConfig containerConfig = JSON.parseObject(taskConfigData, ContainerConfig.class);
                myInstance = containerConfig.getInstances();
                lastMtime = configMtime;
            }
        } else if (configMtime < maxItemMtime) {
            String dcosTaskScaleJobNodePath = ContainerNodePath.getDcosTaskScaleJobNodePath(taskId, lastScalaJob);
            String scaleJobData = curatorFrameworkOp.getData(dcosTaskScaleJobNodePath);
            if (scaleJobData != null && scaleJobData.trim().length() > 0) {
                ContainerScaleJob containerScaleJob = JSON.parseObject(scaleJobData, ContainerScaleJob.class);
                myInstance = containerScaleJob.getContainerScaleJobConfig().getInstances();
                lastMtime = maxItemMtime;
            }
        }
        if (myInstance != -1) {
            boolean timeAllowed = Math.abs(System.currentTimeMillis() - lastMtime) <= ALLOW_CONTAINER_DELAY_MILLIONSECONDS;
            if (timeAllowed) {
                return null;
            }
            int count = containerService.getContainerRunningInstances(taskId, curatorFrameworkOp);
            if (myInstance != count) {
                abnormalContainer.setCause(AbnormalContainer.Cause.CONTAINER_INSTANCE_MISMATCH.name());
                abnormalContainer.setConfigInstances(myInstance);
                abnormalContainer.setRunningInstances(count);
                try {
                    reportAlarmService.dashboardContainerInstancesMismatch(abnormalContainer.getDomainName(), abnormalContainer.getTaskId(), abnormalContainer.getConfigInstances(), abnormalContainer.getRunningInstances());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
                return abnormalContainer;
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : ContainerConfig(com.vip.saturn.job.console.domain.container.ContainerConfig) ContainerScaleJob(com.vip.saturn.job.console.domain.container.ContainerScaleJob) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) JobConsoleException(com.vip.saturn.job.console.exception.JobConsoleException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException)

Example 2 with ContainerConfig

use of com.vip.saturn.job.console.domain.container.ContainerConfig in project Saturn by vipshop.

the class MarathonController method addContainer.

@PostMapping(value = "/container")
@ResponseBody
public SuccessResponseEntity addContainer(@AuditParam("namespace") @PathVariable String namespace, AddContainerModel addContainerModel, HttpServletRequest request) throws SaturnJobConsoleException {
    if (addContainerModel.getContainerToken() == null) {
        throw new SaturnJobConsoleException("Please input container token");
    }
    marathonService.checkContainerTokenNotNull(namespace, addContainerModel.getContainerToken());
    marathonService.saveOrUpdateContainerTokenIfNecessary(namespace, addContainerModel.getContainerToken());
    ContainerConfig containerConfig = new ContainerConfig();
    containerConfig.setTaskId(addContainerModel.getTaskId());
    containerConfig.setCmd(addContainerModel.getCmd());
    containerConfig.setCpus(addContainerModel.getCpus());
    containerConfig.setMem(addContainerModel.getMem());
    containerConfig.setInstances(addContainerModel.getInstances());
    containerConfig.setConstraints(addContainerModel.getConstraints());
    containerConfig.setEnv(addContainerModel.getEnv());
    containerConfig.setPrivileged(addContainerModel.getPrivileged() == null ? false : addContainerModel.getPrivileged());
    containerConfig.setForcePullImage(addContainerModel.getForcePullImage() == null ? true : addContainerModel.getForcePullImage());
    containerConfig.setParameters(addContainerModel.getParameters());
    containerConfig.setVolumes(addContainerModel.getVolumes());
    containerConfig.setImage(addContainerModel.getImage());
    containerConfig.setCreateTime(System.currentTimeMillis());
    String imageNew = "";
    String vipSaturnDcosRegistryUri = SaturnEnvProperties.VIP_SATURN_DCOS_REGISTRY_URI;
    if (vipSaturnDcosRegistryUri == null || vipSaturnDcosRegistryUri.trim().length() == 0) {
        throw new SaturnJobConsoleException("VIP_SATURN_DCOS_REGISTRY_URI is not configured");
    } else {
        if (vipSaturnDcosRegistryUri.startsWith("http://")) {
            String tmp = vipSaturnDcosRegistryUri.substring("http://".length());
            while (tmp.endsWith("/")) {
                tmp = tmp.substring(0, tmp.length() - 1);
            }
            imageNew = tmp + "/" + addContainerModel.getImage();
        } else if (vipSaturnDcosRegistryUri.startsWith("https://")) {
            String tmp = vipSaturnDcosRegistryUri.substring("https://".length());
            while (tmp.endsWith("/")) {
                tmp = tmp.substring(0, tmp.length() - 1);
            }
            imageNew = tmp + "/" + addContainerModel.getImage();
        }
    }
    containerConfig.setImage(imageNew);
    if (containerConfig.getEnv() == null) {
        containerConfig.setEnv(new HashMap<String, String>());
    }
    if (!containerConfig.getEnv().containsKey(SaturnEnvProperties.NAME_VIP_SATURN_ZK_CONNECTION)) {
        containerConfig.getEnv().put(SaturnEnvProperties.NAME_VIP_SATURN_ZK_CONNECTION, getCurrentZkAddr(request.getSession()));
    }
    marathonService.addContainer(namespace, containerConfig);
    return new SuccessResponseEntity();
}
Also used : ContainerConfig(com.vip.saturn.job.console.domain.container.ContainerConfig) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SuccessResponseEntity(com.vip.saturn.job.console.controller.SuccessResponseEntity)

Example 3 with ContainerConfig

use of com.vip.saturn.job.console.domain.container.ContainerConfig in project Saturn by vipshop.

the class ContainerController method addContainer.

@RequestMapping(value = "/addContainer", method = RequestMethod.POST)
@ResponseBody
public RequestResult addContainer(AddContainerModel addContainerModel, HttpServletRequest request) {
    RequestResult requestResult = new RequestResult();
    try {
        if (addContainerModel.getContainerToken() == null) {
            throw new SaturnJobConsoleException("Please input container token");
        }
        containerService.checkContainerTokenNotNull(addContainerModel.getContainerToken());
        containerService.saveOrUpdateContainerTokenIfNecessary(addContainerModel.getContainerToken());
        ContainerConfig containerConfig = new ContainerConfig();
        containerConfig.setTaskId(addContainerModel.getTaskId());
        containerConfig.setCmd(addContainerModel.getCmd());
        containerConfig.setCpus(addContainerModel.getCpus());
        containerConfig.setMem(addContainerModel.getMem());
        containerConfig.setInstances(addContainerModel.getInstances());
        containerConfig.setConstraints(addContainerModel.getConstraints());
        containerConfig.setEnv(addContainerModel.getEnv());
        containerConfig.setPrivileged(addContainerModel.getPrivileged() == null ? false : addContainerModel.getPrivileged());
        containerConfig.setForcePullImage(addContainerModel.getForcePullImage() == null ? true : addContainerModel.getForcePullImage());
        containerConfig.setParameters(addContainerModel.getParameters());
        containerConfig.setVolumes(addContainerModel.getVolumes());
        containerConfig.setImage(addContainerModel.getImage());
        containerConfig.setCreateTime(System.currentTimeMillis());
        String imageNew = "";
        String vipSaturnDcosRegistryUri = SaturnEnvProperties.VIP_SATURN_DCOS_REGISTRY_URI;
        if (vipSaturnDcosRegistryUri == null || vipSaturnDcosRegistryUri.trim().length() == 0) {
            String errorMsg = "VIP_SATURN_DCOS_REGISTRY_URI is not configured";
            requestResult.setSuccess(false);
            requestResult.setMessage(errorMsg);
            return requestResult;
        } else {
            if (vipSaturnDcosRegistryUri.startsWith("http://")) {
                String tmp = vipSaturnDcosRegistryUri.substring("http://".length());
                while (tmp.endsWith("/")) {
                    tmp = tmp.substring(0, tmp.length() - 1);
                }
                imageNew = tmp + "/" + addContainerModel.getImage();
            } else if (vipSaturnDcosRegistryUri.startsWith("https://")) {
                String tmp = vipSaturnDcosRegistryUri.substring("https://".length());
                while (tmp.endsWith("/")) {
                    tmp = tmp.substring(0, tmp.length() - 1);
                }
                imageNew = tmp + "/" + addContainerModel.getImage();
            }
        }
        containerConfig.setImage(imageNew);
        if (containerConfig.getEnv() == null) {
            containerConfig.setEnv(new HashMap<String, String>());
        }
        if (!containerConfig.getEnv().containsKey(SaturnEnvProperties.NAME_VIP_SATURN_ZK_CONNECTION)) {
            containerConfig.getEnv().put(SaturnEnvProperties.NAME_VIP_SATURN_ZK_CONNECTION, getCurrentZkAddr(request.getSession()));
        }
        containerService.addContainer(containerConfig);
        requestResult.setSuccess(true);
    } catch (SaturnJobConsoleException e) {
        requestResult.setSuccess(false);
        requestResult.setMessage(e.getMessage());
    } catch (Throwable t) {
        requestResult.setSuccess(false);
        requestResult.setMessage(t.toString());
    }
    return requestResult;
}
Also used : ContainerConfig(com.vip.saturn.job.console.domain.container.ContainerConfig) RequestResult(com.vip.saturn.job.console.domain.RequestResult) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ContainerConfig (com.vip.saturn.job.console.domain.container.ContainerConfig)3 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)3 SuccessResponseEntity (com.vip.saturn.job.console.controller.SuccessResponseEntity)1 RequestResult (com.vip.saturn.job.console.domain.RequestResult)1 ContainerScaleJob (com.vip.saturn.job.console.domain.container.ContainerScaleJob)1 JobConsoleException (com.vip.saturn.job.console.exception.JobConsoleException)1 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1