Search in sources :

Example 6 with ClientException

use of com.chinaunicom.etcd.v2.exception.ClientException 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 7 with ClientException

use of com.chinaunicom.etcd.v2.exception.ClientException 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 8 with ClientException

use of com.chinaunicom.etcd.v2.exception.ClientException 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)

Example 9 with ClientException

use of com.chinaunicom.etcd.v2.exception.ClientException in project mesosFramework by zhizuqiu.

the class WatchClientImpl method getFrameworkId.

@Override
public String getFrameworkId() {
    String frameworkId = null;
    try {
        System.out.println(rootDir + WatchStaticStr.FRAMEWORK_ID);
        ResponseNode responseNode = etcd.getKey(rootDir + WatchStaticStr.FRAMEWORK_ID);
        if (EtcdAction.GET.equals(responseNode.getAction())) {
            frameworkId = responseNode.getNode().getValue();
        }
    } catch (ClientException e) {
        logger.error("getDir ClientException:" + e.getDetail().getMessage());
    } catch (ServerException e) {
        logger.error("getDir ServerException:" + e.getMessage());
    } catch (Exception e) {
        logger.error("getDir Exception:" + e.getMessage());
    }
    if (frameworkId != null && frameworkId.trim().isEmpty()) {
        frameworkId = null;
    }
    return frameworkId;
}
Also used : ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Aggregations

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