Search in sources :

Example 1 with ServerException

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

the class EtcdService method deleteNodeOnlyReturnPre.

public ReturnPre deleteNodeOnlyReturnPre(Long logTime, String key) {
    boolean deleteSuccess = false;
    String preValue = null;
    try {
        ResponseNodeAndPrevnode responseNodeAndPrevnode = etcd.deleteKey(key);
        if (EtcdAction.DELETE.equals(responseNodeAndPrevnode.getAction())) {
            if (responseNodeAndPrevnode.getPrevNode() != null) {
                preValue = responseNodeAndPrevnode.getPrevNode().getValue();
            }
            deleteSuccess = true;
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "deleteNodeOnlyReturnPre->ClientException->" + e.getDetail().getMessage());
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "deleteNodeOnlyReturnPre->ServerException->" + e.getDetail().getMessage());
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "deleteNodeOnlyReturnPre->ServerException->" + e.getMessage());
    }
    return new ReturnPre(deleteSuccess, deleteSuccess ? preValue : null);
}
Also used : ResponseNodeAndPrevnode(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 2 with ServerException

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

the class EtcdService method setNodeAtomic.

/**
 * 设置
 *
 * @param logTime 任务标识
 * @param key     key
 * @param value   value
 * @return 成功与否
 */
public Boolean setNodeAtomic(Long logTime, String key, String value) {
    boolean getSuccess = false;
    ResponseNode responseNode;
    try {
        responseNode = etcd.getKey(key);
        if (EtcdAction.GET.equals(responseNode.getAction())) {
            getSuccess = true;
        }
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + e.getMessage());
        return false;
    }
    if (!getSuccess) {
        return false;
    }
    boolean setSuccess = false;
    try {
        ResponseNodeAndPrevnode responseNodeAndPrevnode = etcd.setKeyAtomic(key, value, String.valueOf(responseNode.getNode().getModifiedIndex()));
        if (EtcdAction.SET.equals(responseNodeAndPrevnode.getAction())) {
            setSuccess = true;
        }
    } 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 setSuccess;
}
Also used : ResponseNodeAndPrevnode(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 3 with ServerException

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

the class EtcdService method setNodeOnlyReturnPre.

/**
 * 如果设置成功返回之前的value值
 *
 * @param logTime 任务标识
 * @param key     key
 * @param value   value
 * @return 之前的值
 */
public ReturnPre setNodeOnlyReturnPre(Long logTime, String key, String value) {
    boolean setSuccess = false;
    String preValue = null;
    try {
        ResponseNodeAndPrevnode responseNodeAndPrevnode = etcd.setKey(key, value);
        if (EtcdAction.SET.equals(responseNodeAndPrevnode.getAction())) {
            if (responseNodeAndPrevnode.getPrevNode() != null) {
                preValue = responseNodeAndPrevnode.getPrevNode().getValue();
            }
            setSuccess = true;
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "setNodeOnlyReturnPre->ClientException->" + e.getDetail().getMessage());
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "setNodeOnlyReturnPre->ServerException->" + e.getDetail().getMessage());
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "setNodeOnlyReturnPre->Exception->" + e.getMessage());
    }
    return new ReturnPre(setSuccess, setSuccess ? preValue : null);
}
Also used : ResponseNodeAndPrevnode(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 4 with ServerException

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

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

the class EtcdService method setStatusAtomic.

/**
 * 设置任务状态,当状态更新时间 < etcd节点的时间
 *
 * @param logTime   任务标识
 * @param jobStatus jobStatus
 * @return 成功与否
 */
public SetResult setStatusAtomic(Long logTime, DockerStatus jobStatus) {
    String key = AppDataStore.getConfig().getFrameworkName() + JOBSTATUS_PATH + Tools.getStringByJsonpathRecursion(jobStatus.getId().replaceAll("/", "."));
    DockerStatus jobStatusTemp = null;
    boolean getSuccess = false;
    Integer modifiedIndex = null;
    /*
         * JsonSyntaxException|ClientException表示节点异常,可以直接覆盖
         * ServerException|Exception表示未知异常,需要同步程序处理
        */
    try {
        ResponseNode responseNode = etcd.getKey(key);
        if (EtcdAction.GET.equals(responseNode.getAction())) {
            jobStatusTemp = new Gson().fromJson(responseNode.getNode().getValue(), DockerStatus.class);
            modifiedIndex = responseNode.getNode().getModifiedIndex();
            getSuccess = true;
        }
    } catch (JsonSyntaxException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->fromJson->JsonSyntaxException->" + e.getMessage());
        getSuccess = true;
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->ClientException->" + e.getDetail().getMessage());
        getSuccess = true;
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->ServerException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->Exception->" + e.getMessage());
        return HAS_EXCEPTION;
    }
    if (!getSuccess) {
        return HAS_EXCEPTION;
    }
    // 如果状态更新时间<etcd节点的时间,不更新
    if (jobStatusTemp != null) {
        Double timestampTemp = jobStatusTemp.getTimestamp();
        if (timestampTemp != null) {
            Double timestamp = jobStatus.getTimestamp();
            if (timestamp < timestampTemp) {
                return UNNECESSARY;
            }
        }
    }
    boolean setSuccess = false;
    try {
        ResponseNodeAndPrevnode responseNodeAndPrevnode;
        if (modifiedIndex != null) {
            responseNodeAndPrevnode = etcd.setKeyAtomic(key, new Gson().toJson(jobStatus), String.valueOf(modifiedIndex));
        } else {
            responseNodeAndPrevnode = etcd.setKey(key, new Gson().toJson(jobStatus));
        }
        if (EtcdAction.COMPARE_AND_SWAP.equals(responseNodeAndPrevnode.getAction())) {
            setSuccess = true;
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->ClientException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->ServerException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->Exception->" + e.getMessage());
        return HAS_EXCEPTION;
    }
    return setSuccess ? SUCCESS : HAS_EXCEPTION;
}
Also used : ResponseNodeAndPrevnode(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) JsonSyntaxException(com.google.gson.JsonSyntaxException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) Gson(com.google.gson.Gson) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) JsonSyntaxException(com.google.gson.JsonSyntaxException) 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