【原創】大數據基礎之Zookeeper(2)源代碼解析

核心枚舉html

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,而後觸發集羣的從新選舉。算法

具體代碼以及調用以下:apache

org.apache.zookeeper.server.quorum.QuorumPeerMain服務器

         initializeAndRunoop

                  runFromConfigpost

                          org.apache.zookeeper.server.quorum.QuorumPeer extends ZooKeeperThread大數據

                                   initializeflex

                                   startspa

                                            loadDataBasecode

                                            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)選舉算法

相關文章
相關標籤/搜索