Search in sources :

Example 1 with DockerStatus

use of com.chinaunicom.rundocker.bean.DockerStatus in project mesosFramework by zhizuqiu.

the class RestHandler method postJobs.

/**
 * 添加任务,如果任务存在,会覆盖
 *
 * @param jsonParam 请求参数
 * @param response  请求响应
 * @return 返回参数
 */
@HttpMap(path = "/jobs", paramType = HttpMap.ParamType.JSON, method = HttpMap.Method.POST)
public Jobs postJobs(String jsonParam, DefaultFullHttpResponse response) {
    Long logTime = System.currentTimeMillis();
    Jobs jobs = new Gson().fromJson(jsonParam, Jobs.class);
    List<DockerJob> unSuccessJobs = new ArrayList<>();
    for (DockerJob dockerJob : jobs.getApps()) {
        String id = dockerJob.getId();
        if (id == null) {
            unSuccessJobs.add(dockerJob);
            continue;
        }
        // 设置id
        dockerJob.setId(Tools.getStringByJsonpathRecursion(id));
        // todo 检查配置是否正确
        String key = Tools.getStringByJsonpathRecursion(id.replaceAll("/", "."));
        DockerStatus dockerStatus;
        if (AppDataStore.statusGet(key) == null) {
            // 生成dockerStatus
            dockerStatus = DockerStatus.DockerStatusBuilder.newBuilder().defaultCreate(Tools.getStringByJsonpathRecursion(id)).setPersistenceStatusNew(dockerJob.hasPersistence()).builder();
        } else {
            dockerStatus = AppDataStore.statusGet(key);
            // 如果之前使用了持久卷,不能修改
            if (!dockerStatus.get_persistenceInfo().getPersistenceStatus().equals(DockerStatus.PersistenceStatus.NONE)) {
                unSuccessJobs.add(dockerJob);
                logger.info("[" + logTime + "]" + "pre task is use persistence volumes ,please delete task first");
                continue;
            }
            // 如果在running或running途中,认为失败
            if (dockerStatus.getStatus() == JobState.RUNNING || dockerStatus.getStatus() == JobState.STAGING) {
                unSuccessJobs.add(dockerJob);
                logger.info("[" + logTime + "]" + "task is " + dockerStatus.getStatus() + " ,please stop task first");
                continue;
            }
            dockerStatus.setSubmitted(false);
            dockerStatus.setTryCount(0);
        }
        AppDataStore.JobAndStatusTemp jobAndStatusTemp = new AppDataStore.JobAndStatusTemp(key, dockerJob, dockerStatus);
        // 原子的插入job和status,返回值用于etcd操作失败时的回退
        AppDataStore.JobAndStatusTemp jobAndStatusTempResult = AppDataStore.postJobsAndStatus(jobAndStatusTemp);
        // 原子的插入job和status到etcd
        Boolean setSuccess = etcdService.setJobAndStatusAtomic(logTime, key, jobAndStatusTemp);
        if (!setSuccess) {
            // 回滚
            AppDataStore.postJobsAndStatus(jobAndStatusTempResult);
            unSuccessJobs.add(dockerJob);
        }
    }
    jobs.getApps().removeAll(unSuccessJobs);
    // 判断是否还有任务未下发,是则请求offer
    if (!SchedulerService.checkAllAppsIsAccept()) {
        AppDataStore.getSchedulerDriver().reviveOffers();
        logger.info("reviveOffers");
    }
    return jobs;
}
Also used : Jobs(com.chinaunicom.rundocker.bean.returnparm.Jobs) ArrayList(java.util.ArrayList) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) Gson(com.google.gson.Gson) AppDataStore(com.chinaunicom.rundocker.store.AppDataStore) DockerJob(com.chinaunicom.rundocker.bean.DockerJob) HttpMap(com.zhizuqiu.nettyrestful.annotation.HttpMap)

Example 2 with DockerStatus

use of com.chinaunicom.rundocker.bean.DockerStatus in project mesosFramework by zhizuqiu.

the class EtcdService method getJobsFromEtcd.

public Boolean getJobsFromEtcd(Long logTime) {
    logger.info("[" + logTime + "]" + "---------------------");
    logger.info("[" + logTime + "]" + "start getJobsFromEtcd");
    // 获取队列所有节点
    List<Node> jobNode = getNodes(logTime, AppDataStore.getConfig().getFrameworkName() + StaticStr.JOBS_PATH);
    if (jobNode == null) {
        logger.info("[" + logTime + "]" + "node is null");
        return false;
    }
    Map<String, DockerJob> jobMap = new HashMap<>();
    for (Node node : jobNode) {
        DockerJob job;
        try {
            job = new Gson().fromJson(node.getValue(), DockerJob.class);
        } catch (JsonSyntaxException e) {
            logger.error("[" + logTime + "]" + "getJobsFromEtcd->" + node.getKey() + "->JsonSyntaxException:" + e.getMessage());
            continue;
        }
        if (job == null) {
            continue;
        }
        String id = job.getId();
        String key = Tools.getStringByJsonpathRecursion(id.replaceAll("/", "."));
        jobMap.put(key, job);
    }
    AppDataStore.jobsPutAll(jobMap);
    // 获取队列所有节点
    List<Node> statusNodes = getNodes(logTime, AppDataStore.getConfig().getFrameworkName() + JOBSTATUS_PATH);
    if (statusNodes == null) {
        logger.info("[" + logTime + "]" + "node is null");
        return false;
    }
    Map<String, DockerStatus> statusMap = new HashMap<>();
    for (Node node : statusNodes) {
        DockerStatus jobStatus;
        try {
            jobStatus = new Gson().fromJson(node.getValue(), DockerStatus.class);
        } catch (JsonSyntaxException e) {
            logger.error("[" + logTime + "]" + "getJobsFromEtcd->" + node.getKey() + "->JsonSyntaxException:" + e.getMessage());
            continue;
        }
        if (jobStatus == null) {
            continue;
        }
        String id = jobStatus.getId();
        String key = Tools.getStringByJsonpathRecursion(id.replaceAll("/", "."));
        statusMap.put(key, jobStatus);
    }
    AppDataStore.statusPutAll(statusMap);
    return true;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) Node(com.chinaunicom.etcd.v2.model.Node) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) Gson(com.google.gson.Gson) DockerJob(com.chinaunicom.rundocker.bean.DockerJob)

Example 3 with DockerStatus

use of com.chinaunicom.rundocker.bean.DockerStatus in project mesosFramework by zhizuqiu.

the class Tools method initPortsUsed.

/**
 * 初始化PORTSUSED
 */
public static void initPortsUsed() {
    for (String idKey : AppDataStore.statusKeySet()) {
        DockerStatus dockerStatus = AppDataStore.statusGet(idKey);
        if (dockerStatus.getStatus() == RUNNING) {
            Map<String, String> portNeed = dockerStatus.get_portNeed();
            if (portNeed != null) {
                for (String port : portNeed.values()) {
                    HostAndPort hostAndPort = new HostAndPort(dockerStatus.getHostname(), port);
                    AppDataStore.portsPut(hostAndPort, idKey);
                }
            }
        }
    }
}
Also used : HostAndPort(com.chinaunicom.rundocker.bean.HostAndPort) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus)

Example 4 with DockerStatus

use of com.chinaunicom.rundocker.bean.DockerStatus in project mesosFramework by zhizuqiu.

the class Tools method reconcileTasks.

public static List<Protos.TaskStatus> reconcileTasks() {
    List<Protos.TaskStatus> runningTasks = new ArrayList<>();
    if (AppDataStore.getSchedulerDriver() != null) {
        for (String key : AppDataStore.statusKeySet()) {
            DockerStatus dockerStatus = AppDataStore.statusGet(key);
            if (dockerStatus.getStatus() == RUNNING) {
                Protos.TaskID id = Protos.TaskID.newBuilder().setValue(key).build();
                if (dockerStatus.getSlaveId() == null) {
                    continue;
                }
                Protos.SlaveID slaveId = Protos.SlaveID.newBuilder().setValue(dockerStatus.getSlaveId()).build();
                runningTasks.add(Protos.TaskStatus.newBuilder().setSlaveId(slaveId).setTaskId(id).setState(Protos.TaskState.TASK_RUNNING).build());
            }
        }
        AppDataStore.getSchedulerDriver().reconcileTasks(runningTasks);
    }
    return runningTasks;
}
Also used : Protos(org.apache.mesos.Protos) ArrayList(java.util.ArrayList) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus)

Example 5 with DockerStatus

use of com.chinaunicom.rundocker.bean.DockerStatus in project mesosFramework by zhizuqiu.

the class AppDataStore method postJobsAndStatus.

/**
 * post一个job和status,status会被覆盖
 *
 * @param jobAndStatusTemp jobAndStatusTemp
 * @return 当true时,返回用于回退的JobAndStatusTemp
 */
public static synchronized JobAndStatusTemp postJobsAndStatus(JobAndStatusTemp jobAndStatusTemp) {
    String key = jobAndStatusTemp.getKey();
    // 要put的值
    DockerJob dockerJob = jobAndStatusTemp.getDockerJobTemp();
    DockerStatus dockerStatus = jobAndStatusTemp.getDockerStatusTemp();
    // put之前的值,作为返回值
    DockerJob dockerJobResult;
    DockerStatus dockerStatusResult;
    if (dockerJob == null) {
        if (jobsGet(key) != null) {
            dockerJobResult = jobsRemove(key);
        } else {
            dockerJobResult = null;
        }
    } else {
        dockerJobResult = jobsPut(key, dockerJob);
    }
    if (dockerStatus == null) {
        if (statusGet(key) != null) {
            dockerStatusResult = statusRemove(key);
        } else {
            dockerStatusResult = null;
        }
    } else {
        dockerStatusResult = statusPut(key, dockerStatus);
    }
    return new JobAndStatusTemp(key, dockerJobResult, dockerStatusResult);
}
Also used : DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) DockerJob(com.chinaunicom.rundocker.bean.DockerJob)

Aggregations

DockerStatus (com.chinaunicom.rundocker.bean.DockerStatus)16 DockerJob (com.chinaunicom.rundocker.bean.DockerJob)9 Task (com.chinaunicom.rundocker.bean.returnparm.Task)3 EtcdService (com.chinaunicom.rundocker.service.EtcdService)3 Gson (com.google.gson.Gson)3 ArrayList (java.util.ArrayList)3 Protos (org.apache.mesos.Protos)3 ResponseNode (com.chinaunicom.etcd.v2.model.ResponseNode)2 AppDataStore (com.chinaunicom.rundocker.store.AppDataStore)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 Test (org.junit.Test)2 ClientException (com.chinaunicom.etcd.v2.exception.ClientException)1 ServerException (com.chinaunicom.etcd.v2.exception.ServerException)1 Node (com.chinaunicom.etcd.v2.model.Node)1 ResponseNodeAndPrevnode (com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode)1 HostAndPort (com.chinaunicom.rundocker.bean.HostAndPort)1 Jobs (com.chinaunicom.rundocker.bean.returnparm.Jobs)1 MesosService (com.chinaunicom.rundocker.service.MesosService)1 OfferTemp (com.chinaunicom.rundocker.service.OfferTemp)1 Config (com.chinaunicom.rundocker.store.Config)1