集羣版服務啓動過程java
LeaderElection算法算法
完成本身投票和投票算法的獲取數據庫
集羣特有,zookeeper首先會根據自身服務器id(sid)服務器
最新的zxid(lastloggedZxid)和當前的服務器epoch(currentEpoch)網絡
來生成一個初始化投票ide
初始化過程當中,每一個服務都會給本身投票,而後,根據zoo.cfg的配置this
建立相應Leader選舉算法實現code
zk配置中提供默認三種算法(Leaderelection,AuthFastLeaderElection,FastLeaderElection).經過zoo.cfg的electionalg屬性指定,默認支持FastLeaderElectionblog
在初始化階段,會建立選舉須要的網絡i/o層 QuorumCnxManager,等待其餘服務的鏈接get
@Override public synchronized void start() { if (!getView().containsKey(myid)) { throw new RuntimeException("My id " + myid + " not in the peer list"); } //加載數據庫 loadDataBase(); //啓動服務鏈接工廠 startServerCnxnFactory(); try { adminServer.start(); } catch (AdminServerException e) { LOG.warn("Problem starting AdminServer", e); System.out.println(e); } //開始選舉 startLeaderElection(); startJvmPauseMonitor(); super.start(); } public synchronized void startLeaderElection() { try { if (getPeerState() == ServerState.LOOKING) { //認爲當前是leader currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch()); } } catch (IOException e) { RuntimeException re = new RuntimeException(e.getMessage()); re.setStackTrace(e.getStackTrace()); throw re; } this.electionAlg = createElectionAlgorithm(electionType); }
Leader和Follower啓動交互過程