Search in sources :

Example 1 with Node

use of com.chinaunicom.etcd.v2.model.Node 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 2 with Node

use of com.chinaunicom.etcd.v2.model.Node in project mesosFramework by zhizuqiu.

the class EtcdService method getNodes.

/**
 * 从请求队列中获取请求
 *
 * @param logTime   任务标识
 * @param queuePath 队列路径
 * @return 返回请求节点,如果不存在第一个请求,返回null
 */
public List<Node> getNodes(Long logTime, String queuePath) {
    try {
        ResoponseAtom result = etcd.getInOdrderKeys(queuePath);
        logger.debug("[" + logTime + "]" + "getNodes");
        List<Node> nodes = result.getNode().getNodes();
        if (nodes != null && nodes.size() > 0) {
            logger.info("[" + logTime + "]" + "getNodes size:" + nodes.size());
            return nodes;
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "getNodes->ClientException->" + e.getDetail().getMessage());
        return null;
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "getNodes->ServerException->" + e.getDetail().getMessage());
        return null;
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "getNodes->Exception->" + e.getMessage());
        return null;
    }
    return new ArrayList<>();
}
Also used : ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) Node(com.chinaunicom.etcd.v2.model.Node) ArrayList(java.util.ArrayList) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ResoponseAtom(com.chinaunicom.etcd.v2.model.ResoponseAtom) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 3 with Node

use of com.chinaunicom.etcd.v2.model.Node in project mesosFramework by zhizuqiu.

the class EtcdService method getFirstNode.

/**
 * 从请求队列中获取第一个请求
 *
 * @param logTime   任务标识
 * @param queuePath 队列路径
 * @return 返回请求节点,如果不存在第一个请求,返回null
 */
public Node getFirstNode(Long logTime, String queuePath) {
    Node requestNode = null;
    try {
        ResoponseAtom result = etcd.getInOdrderKeys(queuePath);
        logger.debug("[" + logTime + "]" + "从请求队列中获取请求成功");
        List<Node> nodes = result.getNode().getNodes();
        if (nodes != null && nodes.size() > 0) {
            requestNode = nodes.get(0);
            logger.info("[" + logTime + "]" + "当前队列中请求数:" + nodes.size());
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "从请求队列中获取请求失败->请求无效->" + e.getDetail().getMessage());
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "从请求队列中获取请求失败->服务器异常->" + e.getMessage());
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "从请求队列中获取请求失败->其他异常->" + e.getMessage());
    }
    return requestNode;
}
Also used : ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) Node(com.chinaunicom.etcd.v2.model.Node) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ResoponseAtom(com.chinaunicom.etcd.v2.model.ResoponseAtom) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 4 with Node

use of com.chinaunicom.etcd.v2.model.Node in project mesosFramework by zhizuqiu.

the class LeaderSelect method mainTest.

@Test
public void mainTest() {
    String leaderId = UUID.randomUUID().toString();
    while (true) {
        // 判断是否有leader节点
        boolean hasLeaderNode = false;
        try {
            etcd.getKey(leaderDir);
            logger.info("leader node exists");
            hasLeaderNode = true;
        } catch (ClientException e) {
            System.out.println("getKey ClientException:" + e.getDetail().getMessage());
        } catch (ServerException e) {
            System.out.println("getKey ServerException:" + e.getMessage());
            continue;
        } catch (Exception e) {
            System.out.println("getKey Exception:" + e.getMessage());
            continue;
        }
        // 如果有,watch leader节点
        if (hasLeaderNode) {
            boolean hasDelete = false;
            try {
                // 这里会阻塞,直到有节点更改
                ResponseNodeAndPrevnodeTTL responseNodeAndPrevnodeTTL = etcd.waitForChange(leaderDir);
                if (EtcdAction.DELETE.equals(responseNodeAndPrevnodeTTL.getAction())) {
                    hasDelete = true;
                }
            } catch (ClientException e) {
                System.out.println("waitForChange ClientException:" + e.getDetail().getMessage());
            } catch (ServerException e) {
                System.out.println("waitForChange ServerException:" + e.getMessage());
                continue;
            } catch (Exception e) {
                System.out.println("waitForChange Exception:" + e.getMessage());
                continue;
            }
            // 如果没有被删除(是其他改变),重新流程
            if (!hasDelete) {
                continue;
            }
        }
        // 如果没有,创建一个
        boolean getLeader = false;
        try {
            etcd.setKeyTTLWhenNotExists(leaderDir, leaderId, ttl);
            getLeader = true;
        } catch (ClientException e) {
            System.out.println("setKeyTTL ClientException:" + e.getDetail().getMessage());
        } catch (ServerException e) {
            System.out.println("setKeyTTL ServerException:" + e.getMessage());
            continue;
        } catch (Exception e) {
            System.out.println("setKeyTTL Exception:" + e.getMessage());
            continue;
        }
        // 如果创建成功,说明获得leader
        if (getLeader) {
            // 以下为获得leader后的操作
            while (true) {
                try {
                    Thread.sleep(updateTtlInterval);
                } catch (InterruptedException e) {
                    logger.error("sleep InterruptedException");
                }
                try {
                    etcd.refreshKeyTTLByPre(leaderDir, ttl, leaderId);
                    logger.info("refreshKeyTTLByPre success");
                } catch (ClientException e) {
                    System.out.println("refreshKeyTTLByPre ClientException:" + e.getDetail().getMessage());
                    System.out.println("lost leader");
                    break;
                } catch (ServerException e) {
                    System.out.println("refreshKeyTTLByPre ServerException:" + e.getMessage());
                } catch (Exception e) {
                    System.out.println("refreshKeyTTLByPre Exception:" + e.getMessage());
                }
            }
        }
    }
}
Also used : ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ResponseNodeAndPrevnodeTTL(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnodeTTL) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) Test(org.junit.Test)

Example 5 with Node

use of com.chinaunicom.etcd.v2.model.Node in project mesosFramework by zhizuqiu.

the class WatchClientImpl method selectLeader.

@Override
public String selectLeader() {
    while (!closeState.get()) {
        try {
            Thread.sleep(tryInterval);
        } catch (InterruptedException e) {
            logger.error("sleep InterruptedException:" + e.getMessage());
        }
        // 判断是否有leader节点
        boolean hasLeaderNode = false;
        try {
            etcd.getKey(this.leaderPath);
            logger.debug("leader node exists");
            hasLeaderNode = true;
        } catch (ClientException e) {
            logger.error("getDir ClientException:" + e.getDetail().getMessage());
        } catch (ServerException e) {
            logger.error("getDir ServerException:" + e.getMessage());
            continue;
        } catch (Exception e) {
            logger.error("getDir Exception:" + e.getMessage());
            continue;
        }
        // 如果有,watch leader节点
        if (hasLeaderNode) {
            boolean hasDelete = false;
            try {
                // 这里会阻塞,直到有节点更改
                logger.debug("start waitForChange...");
                ResponseNodeAndPrevnodeTTL responseNodeAndPrevnodeTTL = etcd.waitForChange(this.leaderPath);
                if (EtcdAction.EXPIRE.equals(responseNodeAndPrevnodeTTL.getAction())) {
                    logger.debug("node has changed");
                    hasDelete = true;
                } else {
                    logger.debug("waitForChange: " + responseNodeAndPrevnodeTTL.getAction());
                }
            } catch (ClientException e) {
                logger.error("waitForChange ClientException:" + e.getDetail().getMessage());
            } catch (ServerException e) {
                logger.error("waitForChange ServerException:" + e.getMessage());
                continue;
            } catch (Exception e) {
                logger.error("waitForChange Exception:" + e.getMessage());
                continue;
            }
            // 如果没有被删除(是其他改变),重新流程
            if (!hasDelete) {
                continue;
            }
        }
        // 如果没有,创建一个
        boolean getLeader = false;
        try {
            etcd.setKeyTTLWhenNotExists(this.leaderPath, leaderId, ttl);
            getLeader = true;
            logger.debug("setKeyTTLWhenNotExists success");
        } catch (ClientException e) {
            logger.debug("setKeyTTLWhenNotExists ClientException:" + e.getDetail().getMessage());
        } catch (ServerException e) {
            logger.error("setKeyTTLWhenNotExists ServerException:" + e.getMessage());
            continue;
        } catch (Exception e) {
            logger.error("setKeyTTLWhenNotExists Exception:" + e.getMessage());
            continue;
        }
        // 如果创建成功,说明获得leader
        if (getLeader) {
            return leaderId;
        }
    }
    return null;
}
Also used : ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ResponseNodeAndPrevnodeTTL(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnodeTTL) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Aggregations

ClientException (com.chinaunicom.etcd.v2.exception.ClientException)4 ServerException (com.chinaunicom.etcd.v2.exception.ServerException)4 Node (com.chinaunicom.etcd.v2.model.Node)3 ResponseNode (com.chinaunicom.etcd.v2.model.ResponseNode)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ResoponseAtom (com.chinaunicom.etcd.v2.model.ResoponseAtom)2 ResponseNodeAndPrevnodeTTL (com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnodeTTL)2 DockerJob (com.chinaunicom.rundocker.bean.DockerJob)1 DockerStatus (com.chinaunicom.rundocker.bean.DockerStatus)1 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1