zoukankan      html  css  js  c++  java
  • 【原创】大数据基础之Zookeeper(2)源代码解析

    核心枚举

    public enum ServerState {
        LOOKING, FOLLOWING, LEADING, OBSERVING;
    }
    zookeeper服务器状态:刚启动LOOKING,follower是FOLLOWING,leader是LEADING,observer是OBSERVING;
    public enum LearnerType {
        PARTICIPANT, OBSERVER;
    }

    简单来说,zookeeper启动的核心类是QuorumPeerMain,启动之后会加载配置,同时启动QuorumPeer,QuorumPeer会从本地数据目录加载数据,然后开始选举,选举的核心类是FastLeaderElection,选举完成之后会设置状态,根据状态的不同,启动不同的类,比如Leader、Follower、Observer,leader会设置一个新的epoch,同时会等待follower向自己注册,注册完成后follower会向leader同步最新的数据,而leader则定期ping所有的follower,一旦存活的follower不足半数,leader会shutdown,然后触发集群的重新选举。

    具体代码以及调用如下:

    org.apache.zookeeper.server.quorum.QuorumPeerMain

             initializeAndRun

                      runFromConfig

                              org.apache.zookeeper.server.quorum.QuorumPeer extends ZooKeeperThread

                                       initialize

                                       start

                                                loadDataBase

                                                org.apache.zookeeper.server.ServerCnxnFactory

                                                         start

                                                startLeaderElection

                                                         org.apache.zookeeper.server.quorum.QuorumPeer.ResponderThread

                                                                 start

                                                         createElectionAlgorithm

                                                run

                                                         org.apache.zookeeper.server.quorum.FastLeaderElection implements Election

                                                                 lookForLeader

                                                                          sendNotifications

                                                                          totalOrderPredicate

                                                                          termPredicate

                                                                                   org.apache.zookeeper.server.quorum.flexible.QuorumMaj

                                                                                            containsQuorum

                                                                          QuorumPeer.setPeerState

                                                                 1 org.apache.zookeeper.server.quorum.Leader extends Learner

                                                                          lead

                                                                                   org.apache.zookeeper.server.quorum.Leader.LearnerCnxAcceptor extends ZooKeeperThread

                                                                                            run

                                                                                                     org.apache.zookeeper.server.quorum.LearnerHandler extends ZooKeeperThread

                                                                                                             start

                                                                                                                      receive Leader.FOLLOWERINFO

                                                                                   epoch++

                                                                                   setZxid

                                                                                   setCurrentEpoch

                                                                                   waitForNewLeaderAck

                                                                                   startZkServer

                                                                                   loop

                                                                                            org.apache.zookeeper.server.quorum.LearnerHandler

                                                                                                    ping

                                                                                                             org.apache.zookeeper.server.quorum.LearnerHandler.SyncLimitCheck

                                                                                                                      check

                                                                                                             shutdown

                                                                                                                      Leader.removeLearnerHandler

                                                                                            containsQuorum

                                                                                            shutdown

                                                                 2 org.apache.zookeeper.server.quorum.Follower extends Learner

                                                                          followLeader

                                                                                   findLeader

                                                                                   connectToLeader

                                                                                   registerWithLeader

                                                                                            send Leader.FOLLOWERINFO

                                                                                   syncWithLeader

                                                                 3 org.apache.zookeeper.server.quorum.Observer extends Learner

                                                                          observeLeader

    选举代码解析请见  大数据基础之Zookeeper(3)选举算法

  • 相关阅读:
    JZOJ5809 数羊
    P3313 [SDOI2014]旅行
    2019.10.22 NOIP模拟测试 day2
    P4322 [JSOI2016]最佳团体
    P1850 换教室
    P3225 [HNOI2012]矿场搭建
    P2607 [ZJOI2008]骑士
    2019.10.21 NOIP模拟测试 day1
    AFO
    禁止加载浏览器图片
  • 原文地址:https://www.cnblogs.com/barneywill/p/9883808.html
Copyright © 2011-2022 走看看