当前位置: 首页 > news >正文

【zookeeper核心源码解析】第二课:俯瞰QuorumPeer启动核心流程,实现选举关键流程

系列文章目录

【zookeeper核心源码解析】第一课:zk启动类核心流程序列图
【zookeeper核心源码解析】第二课:俯瞰QuorumPeer启动核心流程,实现选举关键流程
【zookeeper核心源码解析】第三课:leader与follower何时开始同步,如何同步数据
【zookeeper核心源码解析】第四课:客户端与服务端读写的io核心流程

【zookeeper核心源码解析】第二课:俯瞰QuorumPeer启动核心流程,实现选举关键流程

  • 系列文章目录
  • 1. QuorumPeer.run() 核心流程执行流程进行选举(选举过程与结果)
  • 2. run方法解释


1. QuorumPeer.run() 核心流程执行流程进行选举(选举过程与结果)

 @Overridepublic void run() {setName("QuorumPeer" + "[myid=" + getId() + "]" +cnxnFactory.getLocalAddress());LOG.debug("Starting quorum peer");try {jmxQuorumBean = new QuorumBean(this);MBeanRegistry.getInstance().register(jmxQuorumBean, null);for(QuorumServer s: getView().values()){ZKMBeanInfo p;if (getId() == s.id) {p = jmxLocalPeerBean = new LocalPeerBean(this);try {MBeanRegistry.getInstance().register(p, jmxQuorumBean);} catch (Exception e) {LOG.warn("Failed to register with JMX", e);jmxLocalPeerBean = null;}} else {p = new RemotePeerBean(s);try {MBeanRegistry.getInstance().register(p, jmxQuorumBean);} catch (Exception e) {LOG.warn("Failed to register with JMX", e);}}}} catch (Exception e) {LOG.warn("Failed to register with JMX", e);jmxQuorumBean = null;}try {/** Main loop*/while (running) {switch (getPeerState()) {case LOOKING:LOG.info("LOOKING");if (Boolean.getBoolean("readonlymode.enabled")) {LOG.info("Attempting to start ReadOnlyZooKeeperServer");// Create read-only server but don't start it immediatelyfinal ReadOnlyZooKeeperServer roZk = new ReadOnlyZooKeeperServer(logFactory, this,new ZooKeeperServer.BasicDataTreeBuilder(),this.zkDb);// Instead of starting roZk immediately, wait some grace// period before we decide we're partitioned.//// Thread is used here because otherwise it would require// changes in each of election strategy classes which is// unnecessary code coupling.Thread roZkMgr = new Thread() {public void run() {try {// lower-bound grace period to 2 secssleep(Math.max(2000, tickTime));if (ServerState.LOOKING.equals(getPeerState())) {roZk.startup();}} catch (InterruptedException e) {LOG.info("Interrupted while attempting to start ReadOnlyZooKeeperServer, not started");} catch (Exception e) {LOG.error("FAILED to start ReadOnlyZooKeeperServer", e);}}};try {roZkMgr.start();setCurrentVote(makeLEStrategy().lookForLeader());} catch (Exception e) {LOG.warn("Unexpected exception",e);setPeerState(ServerState.LOOKING);} finally {// If the thread is in the the grace period, interrupt// to come out of waiting.roZkMgr.interrupt();roZk.shutdown();}} else {try {setCurrentVote(makeLEStrategy().lookForLeader());} catch (Exception e) {LOG.warn("Unexpected exception", e);setPeerState(ServerState.LOOKING);}}break;case OBSERVING:try {LOG.info("OBSERVING");setObserver(makeObserver(logFactory));observer.observeLeader();} catch (Exception e) {LOG.warn("Unexpected exception",e );                        } finally {observer.shutdown();setObserver(null);setPeerState(ServerState.LOOKING);}break;case FOLLOWING:try {LOG.info("FOLLOWING");setFollower(makeFollower(logFactory));follower.followLeader();} catch (Exception e) {LOG.warn("Unexpected exception",e);} finally {follower.shutdown();setFollower(null);setPeerState(ServerState.LOOKING);}break;case LEADING:LOG.info("LEADING");try {setLeader(makeLeader(logFactory));leader.lead();setLeader(null);} catch (Exception e) {LOG.warn("Unexpected exception",e);} finally {if (leader != null) {leader.shutdown("Forcing shutdown");setLeader(null);}setPeerState(ServerState.LOOKING);}break;}}} finally {LOG.warn("QuorumPeer main thread exited");try {MBeanRegistry.getInstance().unregisterAll();} catch (Exception e) {LOG.warn("Failed to unregister with JMX", e);}jmxQuorumBean = null;jmxLocalPeerBean = null;}}

2. run方法解释

1.选举状态有四种,一种是LOOKING,FOLLOWING,LEADING,OBSERVING
2. 一开始是Looking状态,进行选举,选举结束后,才决定leader和follower,分别创建leader和follower对象,并执行leader.lead(), follower.followLeader() 的方式,这时,完成leader和follower进程启动

http://www.lryc.cn/news/510668.html

相关文章:

  • 数据流图和流程图的区别
  • 关于内网服务器依托可上网电脑实现访问互联网
  • 期权懂|期权入门知识:如何选择期权合约?
  • 如何用gpt来分析链接里面的内容(比如分析论文链接)和分析包含多个文件中的一块代码
  • Bash 脚本教程
  • Pinia最简单使用(vite+vue3)
  • 计算机网络——期末复习(4)协议或技术汇总、思维导图
  • Microsoft word@【标题样式】应用不生效(主要表现为在导航窗格不显示)
  • 轮播图带详情插件、uniApp插件
  • 云计算时代携程的网络架构变迁
  • USB 状态机及状态转换
  • Go C编程 第6课 无人机 --- 计算旋转角
  • C++-----图
  • mysql 数据库迁移到达梦数据库
  • 【记录】使用R2 CDN替换本地项目图片以加速图片加载
  • 12.13[java exp4][debug]nginx 500,究极未解之谜,出自重启,解决自重启,迷???
  • Disruptor 高性能环形消息框架
  • Python列表(二)
  • 计算机网络:应用层 —— 网络应用模式
  • @Repository注解和@mapper的区别
  • 解锁成长密码:探寻刻意练习之道
  • cuda-cuDnn
  • 如何使用Python和PIL库生成带竖排文字的封面图像
  • 低代码开发 实战转型案例一览
  • SQL Server中FIRST_VALUE和 LAST_VALUE窗口函数允许在一个指定的窗口内返回第一个或最后一个值
  • 机器学习-高斯混合模型
  • 微信V3支付报错 平台证书及平台证书序列号
  • 41.欠采样技术下变频不能用与跨两个nyquist的情况下
  • 20241227通过配置nomodeset参数解决更新grub之后,ubuntu20.04.5无法启动的问题
  • 从 GitLab.com 到 JihuLab.com 的迁移指南