Zookeeper笔记之使用zk实现集群选主
一、需求
在主从结构的集群中,我们假设硬件机器是很脆弱的,随时可能会宕机,当master挂掉之后需要从slave中选出一个节点作为新的master,使用zookeeper可以很简单的实现集群选主功能。
二、分析
下面为了方便叙述,将使用更通用的技术术语,即使用leader表示master,使用follower表示slave。
集群选主涉及到两个问题:
1. 谁来做leader
2. leader挂掉了怎么被follower感知到
首先是第一个问题,谁来做leader,其实可以将这个问题看做是多线程中的互斥锁抢占,锁只有一把,并且只能被一个人抢到,这里就把一个zookeeper上的一个节点/leader-info看做是锁,集群中的每台机器都尝试去创建这个节点(抢占锁),因为zookeeper创建节点是原子性操作,所以只有一台机器能够创建成功其它都会失败,创建成功的那台机器就作为leader,其它机器做follower,一般leader抢占成功了之后会在/leader-info节点上存储一些与自己相关的信息,比如hostname、id之类的,以让follower知道谁抢占成功成为了leader,然后去连接leader进行一些数据交换或指令控制之类的,那就是选主之后的事了不在此篇文章的讨论范围之内。
第二个问题是leader挂掉了怎么通知其它的follower,zookeeper中的节点按照有效时间分为持久节点和临时节点,临时节点跟session绑定,而一个session表示一个客户端,当客户端下线的时候session失效,当session失效的时候跟它绑定的临时节点就会被删除,利用这个特性可以检测节点是否还在存活状态,实现follower对leader下线的感知,只是需要注意在创建/leader-info节点的时候要将其创建为临时节点,然后众多follower都在这个节点上添加一个watcher监听其删除事件,这样当leader挂掉的时候session失效,然后与此session绑定的临时节点会被删除,即/leader-info节点将被删除,同时给所有的follower发送事件通知,follower一看leader挂了就燥起来了,将自己的状态置为looking,开始新一轮的选举。
总结一下选主的流程:
1. 集群中的所有机器将自己置为looking状态,准备开始选举。
2. 所有looking状态的机器尝试去创建/leader-info节点。
3. 创建成功的将自己的状态修改为leader,同时将自己的一些信息写入到/leader-info这个节点上。
4. 创建失败的将自己的状态置为follower,同时尝试从/leader-info获取leader信息进行一些leader改变的逻辑(在这里这个不是重点,打印一下即可),follower在获取/leader-info节点数据的同时要设置一个watcher,监听此节点的删除事件,当节点被删除事件触发时启动新一轮的选举,因为获取数据设置watcher这个操作是原子性的,所以要么这个节点存在获取数据成功,并且设置watcher也成功,要么节点不存在抛出KeeperException.NoNodeException异常。
5. 为什么在follower设置watcher的时候还有可能会抛异常呢,leader不是已经创建了这个节点了吗?因为follower从尝试创建/leader-info节点失败到去获取此节点的数据同时设置watcher这一段操作不是原子性的,在这中间可能会发生一些变故,leader可能刚成为leader就挂掉了(或者因为一些网络抖动原因,总之是session失效了),leader挂掉之后它创建的临时节点就被zookeeper删除了,所以当follower在设置watcher的时候如果检测到KeeperException.NoNodeException,说明之前的leader挂掉了,此时集群中已经没有了leader,follower又燥起来了,它将自己的状态置为looking开始新一轮的选举。
三、实现
Node.java:
package cc11001100.zookeeper.leaderElection; import cc11001100.zookeeper.utils.ZooKeeperUtil;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper; import java.io.IOException;
import java.io.UnsupportedEncodingException; /**
* 表示集群中的一个节点
*
* @author CC11001100
*/
public class Node { private Status status;
private String nodeForLeaderInfo;
private ZooKeeper zooKeeper; public Node(String listenerNodeForLeader) throws IOException {
this.nodeForLeaderInfo = listenerNodeForLeader;
this.zooKeeper = ZooKeeperUtil.getZooKeeper();
lookingForLeader();
} public void lookingForLeader() {
status = Status.LOOKING;
try {
String leaderInfo = Thread.currentThread().getName();
// 需要注意这里创建的是临时节点
zooKeeper.create(nodeForLeaderInfo, leaderInfo.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
// 如果上一步没有抛异常,说明自己已经是leader了
status = Status.LEADER;
String logMsg = Thread.currentThread().getName() + " is leader";
System.out.println(logMsg);
} catch (KeeperException.NodeExistsException e) {
// 节点已经存在,说明leader已经被别人注册成功了,自己是follower
status = Status.FOLLOWER;
try {
byte[] leaderInfoBytes = zooKeeper.getData(nodeForLeaderInfo, event -> {
if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
lookingForLeader();
}
}, null);
String logMsg = Thread.currentThread().getName() + " is follower, master is " + new String(leaderInfoBytes, "UTF-8");
System.out.println(logMsg);
} catch (KeeperException.NoNodeException e1) {
// 如果在获取leader信息的时候报了节点不存在,说明这个leader比较短命,刚抢到leader就又挂掉了
lookingForLeader();
} catch (KeeperException | InterruptedException | UnsupportedEncodingException e1) {
e1.printStackTrace();
}
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
} public void shutdown() {
try {
if (zooKeeper != null) {
zooKeeper.close();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} public Status getStatus() {
return status;
} // 当前节点的状态,节点的状态必须在这三个中的一个
public enum Status {
LOOKING, // 选举中
LEADER, // 选举完毕,当前节点为leader
FOLLOWER; // 选举完毕,当前节点为follower
} }
LeaderElectionTest.java:
package cc11001100.zookeeper.leaderElection; import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; /**
* @author CC11001100
*/
public class LeaderElectionTest { private static void sleep(long mils) {
try {
TimeUnit.MILLISECONDS.sleep(mils);
} catch (InterruptedException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException { final String LEADER_INFO_NODE = "/leader-info";
int nodeNum = 10;
AtomicLong idGenerator = new AtomicLong();
AtomicInteger activeNodeCount = new AtomicInteger();
while (true) {
if (activeNodeCount.get() >= nodeNum) {
sleep(10);
continue;
} // 线程启动需要一定时间,将线程启动看做开机过程,在开机之前就算一台新的机器加入了
activeNodeCount.incrementAndGet();
new Thread(() -> {
try {
Node node = new Node(LEADER_INFO_NODE);
while (true) {
sleep(1000);
// 这里为了试验就让leader有轻微自杀倾向...
if (node.getStatus() == Node.Status.LEADER && Math.random() < 0.3) {
String logMsg = "----------------------------- " + Thread.currentThread().getName() + " shutdown -----------------------------";
System.out.println(logMsg);
node.shutdown();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
activeNodeCount.decrementAndGet();
}
}, "node-" + idGenerator.getAndIncrement()).start();
}
} }
控制台输出:
...
node-4 is leader
node-3 is follower, master is node-4
node-0 is follower, master is node-4
node-9 is follower, master is node-4
node-7 is follower, master is node-4
node-5 is follower, master is node-4
node-1 is follower, master is node-4
node-6 is follower, master is node-4
node-8 is follower, master is node-4
node-2 is follower, master is node-4
----------------------------- node-4 shutdown -----------------------------
node-0-EventThread is leader
node-6-EventThread is follower, master is node-0-EventThread
node-3-EventThread is follower, master is node-0-EventThread
node-7-EventThread is follower, master is node-0-EventThread
node-1-EventThread is follower, master is node-0-EventThread
node-5-EventThread is follower, master is node-0-EventThread
node-9-EventThread is follower, master is node-0-EventThread
node-2-EventThread is follower, master is node-0-EventThread
node-8-EventThread is follower, master is node-0-EventThread
node-10 is follower, master is node-0-EventThread
----------------------------- node-0 shutdown -----------------------------
node-6-EventThread is leader
node-7-EventThread is follower, master is node-6-EventThread
node-1-EventThread is follower, master is node-6-EventThread
node-3-EventThread is follower, master is node-6-EventThread
node-10-EventThread is follower, master is node-6-EventThread
node-9-EventThread is follower, master is node-6-EventThread
node-5-EventThread is follower, master is node-6-EventThread
node-2-EventThread is follower, master is node-6-EventThread
node-8-EventThread is follower, master is node-6-EventThread
node-11 is follower, master is node-6-EventThread
----------------------------- node-6 shutdown -----------------------------
node-1-EventThread is leader
node-10-EventThread is follower, master is node-1-EventThread
node-7-EventThread is follower, master is node-1-EventThread
node-11-EventThread is follower, master is node-1-EventThread
node-8-EventThread is follower, master is node-1-EventThread
node-5-EventThread is follower, master is node-1-EventThread
node-9-EventThread is follower, master is node-1-EventThread
node-3-EventThread is follower, master is node-1-EventThread
node-2-EventThread is follower, master is node-1-EventThread
node-12 is follower, master is node-1-EventThread
----------------------------- node-1 shutdown -----------------------------
node-3-EventThread is leader
node-12-EventThread is follower, master is node-3-EventThread
node-11-EventThread is follower, master is node-3-EventThread
node-5-EventThread is follower, master is node-3-EventThread
node-7-EventThread is follower, master is node-3-EventThread
node-9-EventThread is follower, master is node-3-EventThread
node-2-EventThread is follower, master is node-3-EventThread
node-10-EventThread is follower, master is node-3-EventThread
node-8-EventThread is follower, master is node-3-EventThread
node-13 is follower, master is node-3-EventThread
----------------------------- node-3 shutdown -----------------------------
node-5-EventThread is leader
node-13-EventThread is follower, master is node-5-EventThread
node-12-EventThread is follower, master is node-5-EventThread
node-7-EventThread is follower, master is node-5-EventThread
node-11-EventThread is follower, master is node-5-EventThread
node-10-EventThread is follower, master is node-5-EventThread
node-9-EventThread is follower, master is node-5-EventThread
node-2-EventThread is follower, master is node-5-EventThread
node-8-EventThread is follower, master is node-5-EventThread
node-14 is follower, master is node-5-EventThread
----------------------------- node-5 shutdown -----------------------------
node-7-EventThread is leader
node-13-EventThread is follower, master is node-7-EventThread
node-12-EventThread is follower, master is node-7-EventThread
node-9-EventThread is follower, master is node-7-EventThread
node-11-EventThread is follower, master is node-7-EventThread
node-14-EventThread is follower, master is node-7-EventThread
node-10-EventThread is follower, master is node-7-EventThread
node-8-EventThread is follower, master is node-7-EventThread
node-2-EventThread is follower, master is node-7-EventThread
node-15 is follower, master is node-7-EventThread
----------------------------- node-7 shutdown -----------------------------
node-14-EventThread is leader
node-13-EventThread is follower, master is node-14-EventThread
node-11-EventThread is follower, master is node-14-EventThread
node-2-EventThread is follower, master is node-14-EventThread
node-12-EventThread is follower, master is node-14-EventThread
node-15-EventThread is follower, master is node-14-EventThread
node-10-EventThread is follower, master is node-14-EventThread
node-9-EventThread is follower, master is node-14-EventThread
node-8-EventThread is follower, master is node-14-EventThread
node-16 is follower, master is node-14-EventThread
----------------------------- node-14 shutdown -----------------------------
node-13-EventThread is leader
node-12-EventThread is follower, master is node-13-EventThread
node-15-EventThread is follower, master is node-13-EventThread
node-9-EventThread is follower, master is node-13-EventThread
node-10-EventThread is follower, master is node-13-EventThread
node-2-EventThread is follower, master is node-13-EventThread
node-8-EventThread is follower, master is node-13-EventThread
node-11-EventThread is follower, master is node-13-EventThread
node-16-EventThread is follower, master is node-13-EventThread
node-17 is follower, master is node-13-EventThread
...
最后有个需要注意的地方就是是否需要将leader节点设置为顺序临时节点呢?类似于分布式锁那样,这样的话每次唤醒一个节点就可以了,这看上去像是一个可以优化的点。
其实并不是,当leader挂掉的时候必须所有follower都能够感知到leader的变更,即使他们不参与抢主也必须醒来执行leader变更的逻辑。
.
Zookeeper笔记之使用zk实现集群选主的更多相关文章
- ZooKeeper实践:(2)集群管理
前言: 随着业务的扩大,用户的增多,访问量的增加,单机模式已经不能支撑,从而出现了从单机模式->垂直应用模式->集群模式,集群模式诞生了,伴随着一堆问题也油然而生,Master怎么选举,机 ...
- zookeeper的单实例和伪集群部署
原文链接: http://gudaoyufu.com/?p=1395 zookeeper工作方式 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现 ...
- ZooKeeper实践:(1)集群管理
前言: 随着业务的扩大,用户的增多,访问量的增加,单机模式已经不能支撑,从而出现了从单机模式->垂直应用模式->集群模式,集群模式诞生了,伴随着一堆问题也油然而生,Master怎么选举,机 ...
- zookeeper在windows下的伪集群模式
参考:zookeeper在windows下的伪集群模式 踩到的坑: 注意windows下路径需要使用\ dataDir=D:\Program Files\Java\zookeeper-3.4.10-c ...
- zk伪集群部署
jdk 配置 # tar xf jdk-8u161-linux-x64.tar.gz -C /usr/local/ # vim /etc/profile.d/jdk.sh export JAVA_HO ...
- Zookeeper节点增删改查与集群搭建(笔记)
1.上传文件目录说明 上传的文件一般放在 /home/下 安装文件一般在 /usr/local/下 2. 安装zookeeper 2.1将zookeeper-3.4.11.tar.gz拷贝到/home ...
- Zookeeper的基本原理(zk架构、zk存储结构、watch机制、独立安装zk、集群间同步复制)
1.Hbase集群的高可用性与伸缩性 HBase可以实现对Regionserver的监控,当个别Regionserver不可访问时,将其负责的分区分给其他Regionsever,其转移过程较快,因为只 ...
- 基于zookeeper的高可用Hadoop HA集群安装
(1)hadoop2.7.1源码编译 http://aperise.iteye.com/blog/2246856 (2)hadoop2.7.1安装准备 http://aperise.iteye.com ...
- zookeeper笔记之基于zk实现分布式锁
一.分布式锁概述 Java中基于AQS框架提供了一系列的锁,但是当需要在集群中的多台机器上互斥执行一段代码或使用资源时Java提供的这种单机锁就没了用武之地,此时需要使用分布式锁协调它们.分布式锁有很 ...
随机推荐
- petapoco 对存储过程的扩展 干货
好久没发表文章了.心血来潮,简单的介绍下这次工作中的问题. 项目中运用了Petapoco,可是petapoco对存储过程的支持不够好.或者说对于某些特殊场景,petapoco的sql支持度有限. 比如 ...
- Github与SmartGit使用说明与建议
当使用github做协同的时候,我们常常需要在客户端安装相应的软件,SmartGit就是一款非常出色的软件,不过是要付费的,我们可以使用non-commercial版本. Download: http ...
- Hyper-V下WINXP无网卡问题解决
- PHP4个载入语句的区别
4个载入语句的区别 include和require的区别: include载入文件失败时(即没有找到该文件),报一个“提示错误”,然后继续执行后续代码: requre载入文件失败时,报错并立即终止执行 ...
- Delphi7如何实现让Tedit显示文字垂直居中(上下居中)
通过下面的组件,可以在输入文字的时候自动垂直居中 直接把下面代码保存到Unit1.pas即可------------------------------------------ unit Unit1; ...
- Semi synchronous replication
目标 主库宕机不丢数据(Master Failover without data loss) facebook有两篇不错的文章: 2015/01: performance-issues-and-fix ...
- 【刷题】LOJ 6001 「网络流 24 题」太空飞行计划
题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合 \(E = \{ E_1, E_2, \cdots, E_m ...
- 【题解】 [HNOI/AHOI2018]道路 (动态规划)
懒得复制,戳我戳我 Solution: \(dp[i][j][k]\)以\(i\)为子树根节点,到根节点中有\(j\)条公路没修,\(k\)条铁路没修,存子树不便利和 \(dp[i][j][k]=mi ...
- 分库分表中间件sharding-jdbc的使用
数据分片产生的背景,可以查看https://shardingsphere.apache.org/document/current/cn/features/sharding/,包括了垂直拆分和水平拆分的 ...
- 新年的展望,2018 hello world~
虽然离$2017$到$2018$的跨年已经过去很久了,但还是想写点东西试图拯救一下最近有些颓势的自己~ $2017$对我来说是意义重大的一年,这一年里,我有欢笑也有泪水,有收获也有挫折,有坚强也有脆弱 ...