leader选举的源码分析-startLeaderElection
生活随笔
收集整理的這篇文章主要介紹了
leader选举的源码分析-startLeaderElection
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看到這個方法,有沒有兩眼放光的感覺?沒錯,前面鋪墊了這么長,終于進入leader選舉的方法了
synchronized public void startLeaderElection() { try { //構建一個票據,用于投票 currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch()); } catch(IOException e) { RuntimeException re = new RuntimeException(e.getMessage()); re.setStackTrace(e.getStackTrace()); throw re; } //這個getView返回的就是在配置文件中配置的server.myid=ip:port:port。view在哪里解析的呢? for (QuorumServer p : getView().values()) { if (p.id == myid) {//獲得當前zkserver myid對應的ip地址 myQuorumAddr = p.addr; break; } } if (myQuorumAddr == null) { throw new RuntimeException("My id " + myid + " not in the peer list"); } //根據electionType匹配對應的選舉算法,electionType默認值為3.可以在配置文件中動態配置 if (electionType == 0) { try { udpSocket = new DatagramSocket(myQuorumAddr.getPort()); responder = new ResponderThread(); responder.start(); } catch (SocketException e) { throw new RuntimeException(e); } } this.electionAlg = createElectionAlgorithm(electionType); }?
總結
以上是生活随笔為你收集整理的leader选举的源码分析-startLeaderElection的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leader选举的源码分析-Quorum
- 下一篇: leader选举的源码分析-quorum