synchronized public Vote getVote(){ return new Vote(proposedLeader, proposedZxid, proposedEpoch); }
synchronized Vote getVote(){ return new Vote(proposedLeader, proposedZxid, proposedEpoch); }
synchronized public void startLeaderElection() { try { if (getPeerState() == ServerState.LOOKING) { 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); }
private boolean updateVote(long designatedLeader, long zxid){ Vote currentVote = getCurrentVote(); if (currentVote!=null && designatedLeader != currentVote.getId()) { setCurrentVote(new Vote(designatedLeader, zxid)); reconfigFlagSet(); LOG.warn("Suggested leader: " + designatedLeader); return true; } return false; }
@Test public void testLookingDiffRounds() { HashMap<Long, Vote> votes = new HashMap<Long, Vote>(); votes.put(0L, new Vote(4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.LOOKING)); votes.put(1L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING)); votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 3, 2, ServerState.LOOKING)); votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 3, 2, ServerState.LEADING)); Assert.assertFalse(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING)).hasAllQuorums()); }
@Test public void testIgnoringZxidElectionEpoch() { Map<Long, Vote> votes = new HashMap<Long, Vote>(); votes.put(0L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 2, ServerState.FOLLOWING)); votes.put(1L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 2), 1, 2, ServerState.FOLLOWING)); votes.put(3L, new Vote(0x1, 4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)); votes.put(4L, new Vote(0x1, 4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LEADING)); Assert.assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)).hasAllQuorums()); }
@Test public void testLookingNormal() { Map<Long, Vote> votes = new HashMap<Long, Vote>(); votes.put(0L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); votes.put(1L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LEADING)); Assert.assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)).hasAllQuorums()); }
@Test public void testElectionWIthDifferentVersion() { Map<Long, Vote> votes = new HashMap<Long, Vote>(); votes.put(0L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.FOLLOWING)); votes.put(1L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.FOLLOWING)); votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)); votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LEADING)); Assert.assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)).hasAllQuorums()); }
/** * 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)); }
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; } for (QuorumServer p : getView().values()) { if (p.id == myid) { myQuorumAddr = p.addr; break; } } if (myQuorumAddr == null) { throw new RuntimeException("My id " + myid + " not in the peer list"); } 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); }
protected ElectionResult countVotes(HashMap<InetSocketAddress, Vote> votes, HashSet<Long> heardFrom) { final ElectionResult result = new ElectionResult(); result.vote = new Vote(Long.MIN_VALUE, Long.MIN_VALUE); result.winner = new Vote(Long.MIN_VALUE, Long.MIN_VALUE); e.setValue(new Vote(v.getId(), zxid, v.getElectionEpoch(), v.getPeerEpoch(), v.getState()));
recvset.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(proposedLeader, proposedZxid); return new Vote(proposedLeader, proposedZxid); outofelection.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(n.leader, n.zxid); outofelection.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(n.leader, n.zxid);
recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch)); recvset, new Vote(proposedLeader, proposedZxid, logicalclock.get(), proposedEpoch)); Vote endVote = new Vote(proposedLeader, proposedZxid, logicalclock.get(), proposedEpoch); recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch)); voteSet = getVoteTracker(recvset, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); if (voteSet.hasAllQuorums() && checkLeader(outofelection, n.leader, n.electionEpoch)) { setPeerState(n.leader, voteSet); Vote endVote = new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch); leaveInstance(endVote); outofelection.put(n.sid, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); voteSet = getVoteTracker(outofelection, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); Vote endVote = new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch); leaveInstance(endVote);
@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)); } }
recvset.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(proposedLeader, proposedZxid); return new Vote(proposedLeader, proposedZxid); outofelection.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(n.leader, n.zxid); outofelection.put(n.addr, new Vote(n.leader, n.zxid)); return new Vote(n.leader, n.zxid);
Vote currentVote = getCurrentVote(); if (currentVote != null) { setCurrentVote(new Vote(currentVote.getId(), currentVote.getZxid(), currentVote.getElectionEpoch(),
/** * Updates leader election info to avoid inconsistencies when * a new server tries to join the ensemble. * See ZOOKEEPER-1732 for more info. */ protected void updateElectionVote(long newEpoch) { Vote currentVote = getCurrentVote(); setBCVote(currentVote); if (currentVote != null) { setCurrentVote(new Vote(currentVote.getId(), currentVote.getZxid(), currentVote.getElectionEpoch(), newEpoch, currentVote.getState())); } }
@Test public void testLocalPeerIsLeader() throws Exception { long localPeerId = 7; QuorumPeer peer = new QuorumPeer(); peer.setId(localPeerId); Vote voteLocalPeerIsLeader = new Vote(localPeerId, 0); peer.setCurrentVote(voteLocalPeerIsLeader); assertTrue(peer.isLeader(localPeerId)); }
@Test public void testLocalPeerIsNotLeader() throws Exception { long localPeerId = 7; long otherPeerId = 17; QuorumPeer peer = new QuorumPeer(); peer.setId(localPeerId); Vote voteLocalPeerIsNotLeader = new Vote(otherPeerId, 0); peer.setCurrentVote(voteLocalPeerIsNotLeader); assertFalse(peer.isLeader(localPeerId)); }
ServerState state = peer.getCurrentVote().getState(); long peerEpoch = peer.getCurrentVote().getPeerEpoch(); Vote newVote = new Vote(leaderSid, zxid+100, electionEpoch+100, peerEpoch, state); peer.setCurrentVote(newVote);