这篇文章主要介绍“zk服务预启动和启动选举的过程”,在日常操作中,相信很多人在zk服务预启动和启动选举的过程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”zk服务预启动和启动选举的过程”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
QuorumPeerMain类
public static void main(String[] args) {
QuorumPeerMain main = new QuorumPeerMain();
try {
main.initializeAndRun(args);
} catch (IllegalArgumentException e) {
LOG.error("Invalid arguments, exiting abnormally", e);
LOG.info(USAGE);
System.err.println(USAGE);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (ConfigException e) {
LOG.error("Invalid config, exiting abnormally", e);
System.err.println("Invalid config, exiting abnormally");
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (DatadirException e) {
LOG.error("Unable to access datadir, exiting abnormally", e);
System.err.println("Unable to access datadir, exiting abnormally");
System.exit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue());
} catch (AdminServerException e) {
LOG.error("Unable to start AdminServer, exiting abnormally", e);
System.err.println("Unable to start AdminServer, exiting abnormally");
System.exit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue());
} catch (Exception e) {
LOG.error("Unexpected exception, exiting abnormally", e);
System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
}
LOG.info("Exiting normally");
System.exit(ExitCode.EXECUTION_FINISHED.getValue());
}
预启动
1.由QuorumPeerMain 启动
2 解析配置文件 集群情况下配置多个节点,启动清理DatadirCleanupManager
2.1创建zk数据管理器(FileTxnSnapLog)
3 判断是集群还是单机模式
4通过配置初始化quorumPeer,包括(ElectionType,zkdatabase,quorumVerifier,cnxnFactory,learnerType等)
是zk服务器实例的托管者,quorumPeer会不断检测当前服务器实例运行状态,同时发起leader选举
5创建内存database,zkdb管理所有会话记录,事务信息和dataTree数据
6初始化QuorumPeer实例,将FileTxnSnapLog,ServerCnxnFactory,ZkDatabase注册到QuorumPeer中,配置参数服务器列表,竞选时选举算法和超时时间等
7加载数据库loadDataBase()
8启动serverCnxn连接
7-8在QuorumPeer中
public synchronized void start() {
if (!getView().containsKey(myid)) {
throw new RuntimeException("My id " + myid + " not in the peer list");
}
//加载数据库
loadDataBase();
//启动服务连接工厂
startServerCnxnFactory();
try {
adminServer.start();
} catch (AdminServerException e) {
LOG.warn("Problem starting AdminServer", e);
System.out.println(e);
}
//开始选举
startLeaderElection();
startJvmPauseMonitor();
super.start();
}
Leader选举
1初始化leader选举算法
2集群模式下,zookeeper首先会根据自身服务器id(sid),最新事务id(zxid)(lastLoggedZxid)
3和当前服务器epoch(currentEpoch)来创建选票
4在创建过程中,每个服务器都会给自己投票,然后,根据zoo.cfg配置,创建相应leader选举算法
zookeeper默认提供了三种算法(LeaderElection,AuthFastLeaderElection,FastLeaderElection),可以通过zoo.cfg的electionArg属性配置类型,
5但是现在默认支持的是FastLeaderElection选举算法
在初始化阶段 zk会创建选举需要的网络io管理器QuorumCnxnManager,同时监听选举指定端口,等待其他选举服务连接
到此,关于“zk服务预启动和启动选举的过程”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/iioschina/blog/3118792