use of com.chinaunicom.etcd.v2.exception.ServerException 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;
}
use of com.chinaunicom.etcd.v2.exception.ServerException 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());
}
}
}
}
}
use of com.chinaunicom.etcd.v2.exception.ServerException 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;
}
use of com.chinaunicom.etcd.v2.exception.ServerException 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