1. 程式人生 > >大資料基礎(1)zookeeper原始碼解析

大資料基礎(1)zookeeper原始碼解析

五 原始碼解析

 

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