use of com.chinaunicom.etcd.v2.model.ResponseNode 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;
}
use of com.chinaunicom.etcd.v2.model.ResponseNode 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;
}
use of com.chinaunicom.etcd.v2.model.ResponseNode 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;
}
Aggregations