zookeeper源碼解析01 選舉

選舉類 FastLeaderElection

主要參數:

  • int finalizeWait=200:一次選舉不能超過的時間上限,如果時限內未完成選舉,則重新發起一次選舉
  • int maxNotificationInterval=60000:節點之間接收選票通知,如果在此通知時限內未收到其他節點的通知,則認爲連接出現問題
  • QuorumCnxManager manager:【連接管理者】FastLeaderElection通過TCP管理同輩節點之間的通信,QuorumCnxManager管理這些連接
  • QuorumPeer self:當前參與選舉的server
  • Messenger messenger
  • AtomicLong logicalclock:邏輯時鐘——一輪新的選舉標誌
  • long proposedLeader + proposedZxid + proposedEpoch:記錄當前sever推薦的情況
    /**
     * Determine how much time a process has to wait
     * once it believes that it has reached the end of
     * leader election.
     * 一次選舉不能超過的時間上限,如果時限內未完成選舉,則重新發起一次選舉
     */
    static final int finalizeWait = 200;
    /**
     * Upper bound on the amount of time between two consecutive
     * notification checks. This impacts the amount of time to get
     * the system up again after long partitions. Currently 60 seconds.
     * maxNotificationInterval指定了兩個連續的notification檢查間的時間間隔上限
     * 它影響了系統在經歷了長時間分割後再次重啓的時間,目前爲60s
     * 說明:節點之間接收選票通知,如果在此通知時限內未收到其他節點的通知,則認爲連接出現問題
     */
    private static int maxNotificationInterval = 60000;
    /**
     * Connection manager. Fast leader election uses TCP for
     * communication between peers, and QuorumCnxManager manages
     * such connections.
     * 【連接管理者】
     * FastLeaderElection通過TCP管理同輩節點之間的通信,QuorumCnxManager管理這些連接
     */
    QuorumCnxManager manager;
    /** 當前參與選舉的server */
    QuorumPeer self;
    Messenger messenger;
    /** 邏輯時鐘——一輪新的選舉標誌 */
    AtomicLong logicalclock = new AtomicLong(); /* Election instance */
    /** 記錄當前sever推薦的情況 */
    long proposedLeader;
    long proposedZxid;
    long proposedEpoch;

內部靜態類 Notification
作用: 通知其他server當前節點投票信息已經改變
發起通知情況:1. 新一輪投票,選舉自己 2.自身的zxid最大,或者zxid相同,但是serverId更大

    public static class Notification {
        public static final int CURRENTVERSION = 0x2;
        int version;
        /** Proposed leader 被推薦作leader的serverId */ 
		long leader;
        /** zxid of the proposed leader 被推薦作leader的最大的zxid */ 
		long zxid;
        /** Epoch 本輪選舉的時代 */ 
		long electionEpoch;
        /** current state of sender */ 
		QuorumPeer.ServerState state;
        /** Address of sender */ 
		long sid;
		
        QuorumVerifier qv;
        /** epoch of the proposed leader */ 
		long peerEpoch;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章