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