【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式竞选
前言
上文【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式锁-升级版中,我们通过利用ZooKeeper的临时节点和Watcher特性,实现了一个分布式锁。
本文我们结合实际场景,完成一个分布式竞争选举。
设计
这里我们实现一个公平的选举方式,即先参加选举的优先被选为leader。
具体的实现思路 参考了ZooKeeper提供的官方示例:zookeeper-recipes-election
- START:服务器开始竞选
- OFFER:创建临时顺序结点
- DETERMINE:开始决策,将临时节点按末尾序号从小到大排序,如果当前节点的序号最小,则竞选成功,否则,则Watch前一个节点,当前一个节点被删除时,再次进行决策
- ELECTED:当前节点是序号最小的节点,竞选成功
- READY:当前节点不是序号最小的节点,竞选不成功,Watch前一个节点,进入READY态
- FAILED:当出现异常情况时,为失败状态
- STOP:结束竞选
LeaderElectionSupport
public class LeaderElectionSupport implements LeaderElection{
private static Logger logger = LoggerFactory.getLogger(LeaderElectionSupport.class);
//ZooKeeper客户端,进行ZooKeeper操作
private ZooKeeper zooKeeper;
//根节点名称
private String dir;
//节点前缀
private String node;
//ZooKeeper鉴权信息
private List<ACL> acls;
//要加锁节点
private String fullPath;
//选举状态
private State state;
//监听器
private Set<LeaderElectionListener> listeners;
//存当前节点的信息
private volatile LeaderNode leaderNode;
//监察器
private Watcher watcher;
/**
* Constructor.
*
* @param zooKeeper the zoo keeper
* @param dir the dir
* @param node the node
* @param acls the acls
*/
public LeaderElectionSupport(ZooKeeper zooKeeper, String dir, String node, List<ACL> acls) {
this.zooKeeper = zooKeeper;
this.dir = dir;
this.node = node;
this.acls = acls;
this.fullPath = dir.concat("/").concat(this.node);
init();
state = State.STOP;
listeners = Collections.synchronizedSet(new HashSet<>());
}
/**
* 初始化根节点、检查器等
* */
private void init() {
try {
watcher = new LeaderWatcher();
Stat stat = zooKeeper.exists(dir, false);
if (stat == null) {
zooKeeper.create(dir, null, acls, CreateMode.PERSISTENT);
}
} catch (Exception e) {
logger.error("[LeaderElectionSupport#init] error : " + e.toString(), e);
}
}
}
start
/**
* Start.
* 开始竞选
*/
@Override
public void start() {
synchronized (this) {
state = State.START;
dispatchEvent(EventType.START);
offerElection();
determineElection();
}
}
offerElection
/**
* 创建临时节点,参加竞选,并将主机信息保存在node中
* */
private void offerElection() {
dispatchEvent(EventType.OFFER_START);
state = State.OFFER;
if (leaderNode == null) {
synchronized (this) {
try {
if (leaderNode == null) {
InetAddress ia = InetAddress.getLocalHost();
LeaderNode tmpNode = new LeaderNode();
tmpNode.setHostName(ia.getHostName());
String path = zooKeeper.create(fullPath, ConversionUtil.objectToBytes(ia.getHostName()), acls, CreateMode.EPHEMERAL_SEQUENTIAL);
tmpNode.setNodePath(path);
tmpNode.setId(NodeUtil.getNodeId(path));
leaderNode = tmpNode;
}
} catch (Exception e) {
becomeFailed(e);
}
}
}
dispatchEvent(EventType.OFFER_COMPLETE);
}
determineElection
/**
* 决定竞选结果
* 1、竞选节点序号最低的赢取选举
* 2、未赢得选举的节点,监听上一个节点,直到上一个节点被删除,则尝试重新竞选
* */
private void determineElection() {
dispatchEvent(EventType.DETERMINE_START);
state = State.DETERMINE;
synchronized (this) {
TreeSet<String> nodePathSet = getNodePathSet();
if (nodePathSet.isEmpty()) {
becomeFailed(new Exception("no node"));
return;
}
String leaderPath = nodePathSet.first();
if (leaderNode.getNodePath().equalsIgnoreCase(leaderPath)) {
becomeLeader();
} else {
becomeReady(nodePathSet.headSet(leaderNode.getNodePath()).last());
}
}
dispatchEvent(EventType.DETERMINE_COMPLETE);
}
becomeLeader
/**
* 竞选成功
* */
private void becomeLeader() {
dispatchEvent(EventType.ELECTED_START);
state = State.ELECTED;
dispatchEvent(EventType.ELECTED_COMPLETE);
}
becomeReady
/**
* 竞选失败进入就绪态
* */
private void becomeReady(String path) {
try {
Stat stat = zooKeeper.exists(path, watcher);
if (stat == null) {
determineElection();
} else {
dispatchEvent(EventType.READY_START);
state = State.READY;
dispatchEvent(EventType.READY_COMPLETE);
}
} catch (KeeperException e) {
becomeFailed(e);
} catch (InterruptedException e) {
becomeFailed(e);
}
}
becomeFailed
/**
* 当发生异常时,更新为FAILED状态
* */
private void becomeFailed(Exception e) {
state = State.FAILED;
dispatchEvent(EventType.FAILED);
logger.error("[LeaderElectionSupport#becomeFailed] error : " + e.toString(), e);
}
getNodePathSet
/**
* 获取参加竞选的节点信息
* */
private TreeSet<String> getNodePathSet() {
TreeSet<String> nodeSet = new TreeSet<>();
try {
List<String> nodes = zooKeeper.getChildren(dir, false);
for (String node : nodes) {
nodeSet.add(dir.concat("/").concat(node));
}
} catch (KeeperException e) {
becomeFailed(e);
} catch (InterruptedException e) {
becomeFailed(e);
}
return nodeSet;
}
stop
/**
* Stop.
* 停止竞选
*/
@Override
public void stop() {
synchronized (this) {
dispatchEvent(EventType.STOP_START);
deleteNode();
state = State.STOP;
dispatchEvent(EventType.STOP_COMPLETE);
}
}
deleteNode
/**
* 停止时,删除节点,退出竞选
* */
private void deleteNode() {
try {
if (leaderNode != null) {
synchronized (this) {
zooKeeper.delete(leaderNode.getNodePath(), -1);
leaderNode = null;
}
}
} catch (InterruptedException e) {
becomeFailed(e);
} catch (KeeperException e) {
becomeFailed(e);
}
}
getLeaderHostName
/**
* Gets get leader host name.
*
* @return the get leader host name
*/
@Override
public String getLeaderHostName() {
synchronized (this) {
TreeSet<String> nodePathSet = getNodePathSet();
if (!nodePathSet.isEmpty()) {
try {
String leaderPath = nodePathSet.first();
return (String) ConversionUtil.bytesToObject(zooKeeper.getData(leaderPath, false, null));
} catch (KeeperException e) {
logger.error("[LeaderWatcher#getLeaderHostName] error : " + e.toString(), e);
} catch (InterruptedException e) {
logger.error("[LeaderWatcher#getLeaderHostName] error : " + e.toString(), e);
} catch (IOException e) {
logger.error("[LeaderWatcher#getLeaderHostName] error : " + e.toString(), e);
} catch (ClassNotFoundException e) {
logger.error("[LeaderWatcher#getLeaderHostName] error : " + e.toString(), e);
}
}
return null;
}
}
getLeaderNodePath
/**
* Gets get leader node path.
*
* @return the get leader node path
*/
@Override
public String getLeaderNodePath() {
synchronized (this) {
TreeSet<String> nodePathSet = getNodePathSet();
return nodePathSet.isEmpty() ? null : nodePathSet.first();
}
}
LeaderWatcher
/**
* 内部watcher类,当竞选失败时,watch前一个节点,当前一个节点别移除时,再次发起决策
* */
private class LeaderWatcher implements Watcher {
/**
* Process.
*
* @param watchedEvent the watched event
*/
@Override
public void process(WatchedEvent watchedEvent) {
try {
if (Event.EventType.NodeDeleted.equals(watchedEvent.getType()) && !State.STOP.equals(state)) {
determineElection();
}
} catch (Exception e) {
logger.error("[LeaderWatcher#process] error : " + e.toString(), e);
}
}
}
总结
以上就是我们利用ZooKeeper的临时节点和Watcher特性实现的公平模式分布式竞选。
可以进行简单的选主操作,适用于如执行单机定时任务、心跳检测等场景。实际上是实现的Master-Slave模型。
源代码可见:aloofJr
而对高可用要求较多的复杂选举场景,如分布式存储、同步等,则需要考虑集群一致性、脑裂等各种情况,则需要实现如Paxos、raft、Zab等一致性算法协议。如ZooKeeper集群的选举模式就是使用的Zab算法。
我们后续会进行深入的探讨。
更多文章
见我的博客:https://nc2era.com
written by AloofJr,转载请注明出处

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
为ECS Ubuntu 18.04实例配置辅助私网IP地址
本文介绍,如何给Ubuntu 18.04系统配置辅助私网ip。注:需要使用专有网络,经典网络不支持此方案。 ECS支持给网卡配置辅助私网ip地址,可以实现给一个网卡配置多个私网ip进行使用。关于辅助私网ip介绍,参见文档 https://help.aliyun.com/document_detail/101180.html 场景一:为主网卡配置辅助私网ip。 1、在ECS控制台,给ECS实例的主网卡,分配辅助私网ip。注:默认eth0网卡是主网卡,主网卡是ECS初始提供的网卡, 本例中,主网卡默认的私网ip是 192.168.50.59,手动增加的辅助私网ip是192.168.50.61 和 192.168.50.60。 2、在服务器系统内部,修改网卡配置文件,增加私网ip的配置。 2.1 查看服务器的内网网关。 命令 route -n root@iZ2ze79lofu2pwszuei3jsZ:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0....
-
下一篇
PingCAP 的 5 年远程办公实践
前言 2020 年的春节注定是一个不平凡的春节,全国都在抗击新型冠状病毒肺炎。除了不出门,勤洗手,戴口罩之类的常规操作,我们就在想,在这个大背景下,我们还能够做哪些事情?考虑到春节假期临近结束,返程的旅途中可能会加大传染的概率,延长隔离时间、远程在家办公也许是普通群众能给国家在这场战役中做的最大贡献。然而在我们国家,暂且不论别的行业,至少我们所在的高科技行业还没有普及远程办公的文化,所以我们在此将 PingCAP 实践了近五年的工程师远程办公经验介绍给大家。本文将尽量少描述理念,而更多的从实践方面讲述我们的落地经验,以期在这样的一个特殊的时刻帮助更多的朋友和公司尽快行动起来,为国家为社会贡献一份我们微薄的力量。 我们已经通过实践证明,在这个时代,至少对于类似软件工程这样的主要以脑力和创意为主的工作,已经有足够的方法论和基础设施,让远程工作的效率不比传统模式差,有时候甚至能有更好的产出(相信已经有同学想起了早上拥挤的交通对心情和思维的副作用)。下面我们聊聊一些具体落地的经验。 01 远程办公的管理哲学 远程办公在国外并不是一件新鲜的事情。在硅谷,尤其是新一代的科技公司几乎都有远程工作的基...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2全家桶,快速入门学习开发网站教程
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- MySQL数据库在高并发下的优化方案
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果