if(listener != null){ listener.start(); FastLeaderElection fle = new FastLeaderElection(this, qcm); fle.start(); le = fle; } else {
updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); sendNotifications(); sendNotifications(); } else { manager.connectAll(); else if (validVoter(n.sid) && validVoter(n.leader)) { if (getInitLastLoggedZxid() == -1) { LOG.debug("Ignoring notification as our zxid is -1"); break; logicalclock.set(n.electionEpoch); recvset.clear(); if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, getInitId(), getInitLastLoggedZxid(), getPeerEpoch())) { updateProposal(n.leader, n.zxid, n.peerEpoch); } else { updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); sendNotifications(); } else if (n.electionEpoch < logicalclock.get()) { if(LOG.isDebugEnabled()){ } else if (totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
static ByteBuffer createMsg(int state, long leader, long zxid, long epoch){ return FastLeaderElection.buildMsg(state, leader, zxid, 1, epoch); }
/** * This predicate checks that a leader has been elected. It doesn't * make a lot of sense without context (check lookForLeader) and it * has been separated for testing purposes. * * @param recv map of received votes * @param ooe map containing out of election votes (LEADING or FOLLOWING) * @param n Notification * @return */ protected boolean ooePredicate(HashMap<Long,Vote> recv, HashMap<Long,Vote> ooe, Notification n) { return (termPredicate(recv, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)) && checkLeader(ooe, n.leader, n.electionEpoch)); }
updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); sendNotifications(); sendNotifications(); } else { manager.connectAll(); else if(validVoter(n.sid) && validVoter(n.leader)) { logicalclock.set(n.electionEpoch); recvset.clear(); if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, getInitId(), getInitLastLoggedZxid(), getPeerEpoch())) { updateProposal(n.leader, n.zxid, n.peerEpoch); } else { updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); sendNotifications(); } else if (n.electionEpoch < logicalclock.get()) { if(LOG.isDebugEnabled()){ } else if (totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, proposedLeader, proposedZxid, proposedEpoch)) { updateProposal(n.leader, n.zxid, n.peerEpoch); sendNotifications();
updateProposal(getInitId(), getInitLastLoggedZxid()); sendNotifications(); sendNotifications(); } else { manager.connectAll(); logicalclock = n.epoch; recvset.clear(); if(totalOrderPredicate(n.leader, n.zxid, getInitId(), getInitLastLoggedZxid())) updateProposal(n.leader, n.zxid); else updateProposal(getInitId(), getInitLastLoggedZxid()); sendNotifications(); } else if (n.epoch < logicalclock) { if(LOG.isDebugEnabled()){ } else if (totalOrderPredicate(n.leader, n.zxid, proposedLeader, proposedZxid)) { LOG.info("Updating proposal"); updateProposal(n.leader, n.zxid); sendNotifications(); (self.getQuorumVerifier().getWeight(proposedLeader) != 0)){ self.setPeerState((proposedLeader == self.getId()) ? ServerState.LEADING: learningState());
@Before public void setUp() throws Exception { File tmpdir = ClientBase.createTmpDir(); Map<Long, QuorumServer> peers = new HashMap<Long,QuorumServer>(); for(int i = 0; i < 5; i++) { peers.put(Long.valueOf(i), new QuorumServer(Long.valueOf(i), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); } QuorumPeer peer = new QuorumPeer(peers, tmpdir, tmpdir, PortAssignment.unique(), 3, 3, 1000, 2, 2); fle = new FastLeaderElection(peer, peer.createCnxnManager()); }
Vote v = getVote(); QuorumVerifier qv = self.getQuorumVerifier(); ToSend notmsg = new ToSend(ToSend.mType.notification,
int lc = (int) ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock(); FastLeaderElection election = (FastLeaderElection) peer.getElectionAlg(); election.shutdown(); Assert.assertEquals(-1, election.getVote().getId()); LOG.info("Leader " + i + " dead");
@Test public void testOutofElection() { HashMap<Long,Vote> outofelection = new HashMap<Long,Vote>(); outofelection.put(1L, new Vote(0x0, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x17, ServerState.FOLLOWING)); outofelection.put(2L, new Vote(0x0, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x17, ServerState.FOLLOWING)); outofelection.put(4L, new Vote(0x1, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x18, ServerState.FOLLOWING)); Vote vote = new Vote(0x1, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x18, ServerState.LEADING); outofelection.put(5L, vote); Notification n = new Notification(); n.version = vote.getVersion(); n.leader = vote.getId(); n.zxid = vote.getZxid(); n.electionEpoch = vote.getElectionEpoch(); n.state = vote.getState(); n.peerEpoch = vote.getPeerEpoch(); n.sid = 5L; // Set the logical clock to 1 on fle instance of server 3. fle.logicalclock.set(0x1); Assert.assertTrue("Quorum check failed", fle.getVoteTracker(outofelection, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)).hasAllQuorums()); Assert.assertTrue("Leader check failed", fle.checkLeader(outofelection, n.leader, n.electionEpoch)); } }
/** * Constructor of FastLeaderElection. It takes two parameters, one * is the QuorumPeer object that instantiated this object, and the other * is the connection manager. Such an object should be created only once * by each peer during an instance of the ZooKeeper service. * * @param self QuorumPeer that created this object * @param manager Connection manager */ public FastLeaderElection(QuorumPeer self, QuorumCnxManager manager){ this.stop = false; this.manager = manager; starter(self, manager); }
/** * Update the peer state based on the given proposedLeader. Also update * the leadingVoteSet if it becomes the leader. */ private void setPeerState(long proposedLeader, SyncedLearnerTracker voteSet) { ServerState ss = (proposedLeader == self.getId()) ? ServerState.LEADING: learningState(); self.setPeerState(ss); if (ss == ServerState.LEADING) { leadingVoteSet = voteSet; } }
protected Election createElectionAlgorithm(int electionAlgorithm){ Election le=null; //TODO: use a factory rather than a switch switch (electionAlgorithm) { case 0: le = new LeaderElection(this); break; case 1: le = new AuthFastLeaderElection(this); break; case 2: le = new AuthFastLeaderElection(this, true); break; case 3: qcm = createCnxnManager(); QuorumCnxManager.Listener listener = qcm.listener; if(listener != null){ listener.start(); le = new FastLeaderElection(this, qcm); } else { LOG.error("Null listener when initializing cnx manager"); } break; default: assert false; } return le; }
Vote v = getVote(); ToSend notmsg = new ToSend(ToSend.mType.notification, v.getId(),
/** * Constructor of FastLeaderElection. It takes two parameters, one * is the QuorumPeer object that instantiated this object, and the other * is the connection manager. Such an object should be created only once * by each peer during an instance of the ZooKeeper service. * * @param self QuorumPeer that created this object * @param manager Connection manager */ public FastLeaderElection(QuorumPeer self, QuorumCnxManager manager){ this.stop = false; this.manager = manager; starter(self, manager); }
protected Election createElectionAlgorithm(int electionAlgorithm){ Election le=null; //TODO: use a factory rather than a switch switch (electionAlgorithm) { case 0: le = new LeaderElection(this); break; case 1: le = new AuthFastLeaderElection(this); break; case 2: le = new AuthFastLeaderElection(this, true); break; case 3: QuorumCnxManager mng = new QuorumCnxManager(this); QuorumCnxManager.Listener listener = mng.listener; if(listener != null){ listener.start(); le = new FastLeaderElection(this,mng); } else { LOG.error("Null listener when initializing cnx manager"); } break; default: assert false; } return le; }
Vote v = getVote(); ToSend notmsg = new ToSend(ToSend.mType.notification, v.id,
/** * Constructor of FastLeaderElection. It takes two parameters, one * is the QuorumPeer object that instantiated this object, and the other * is the connection manager. Such an object should be created only once * by each peer during an instance of the ZooKeeper service. * * @param self QuorumPeer that created this object * @param manager Connection manager */ public FastLeaderElection(QuorumPeer self, QuorumCnxManager manager){ this.stop = false; this.manager = manager; starter(self, manager); }
/** * Called by run() once there is a new message to send. * * @param m message to send */ void process(ToSend m) { ByteBuffer requestBuffer = buildMsg(m.state.ordinal(), m.leader, m.zxid, m.electionEpoch, m.peerEpoch, m.configData); manager.toSend(m.sid, requestBuffer); } }
protected Election createElectionAlgorithm(int electionAlgorithm){ Election le=null; //TODO: use a factory rather than a switch switch (electionAlgorithm) { case 0: le = new LeaderElection(this); break; case 1: le = new AuthFastLeaderElection(this); break; case 2: le = new AuthFastLeaderElection(this, true); break; case 3: qcm = createCnxnManager(); QuorumCnxManager.Listener listener = qcm.listener; if(listener != null){ listener.start(); le = new FastLeaderElection(this, qcm); } else { LOG.error("Null listener when initializing cnx manager"); } break; default: assert false; } return le; }