/** * In the case there is a leader elected, and a quorum supporting this * leader, we have to check if the leader has voted and acked that it is * leading. We need this check to avoid that peers keep electing over and * over a peer that has crashed and it is no longer leading. * * @param votes * set of votes * @param leader * leader id * @param electionEpoch * epoch id */ protected boolean checkLeader(HashMap<Long, Vote> votes, long leader, long electionEpoch) { boolean predicate = true; /* * If everyone else thinks I'm the leader, I must be the leader. The * other two checks are just for the case in which I'm not the leader. * If I'm not the leader and I haven't received a message from leader * stating that it is leading, then predicate is false. */ if (leader != self.getId()) { if (votes.get(leader) == null) predicate = false; else if (votes.get(leader).getState() != ServerState.LEADING) predicate = false; } else if (logicalclock != electionEpoch) { predicate = false; } return predicate; }
public void run(){ try{ Vote v = null; peer.setPeerState(ServerState.LOOKING); LOG.info("Going to call leader election: " + i); v = peer.getElectionAlg().lookForLeader(); if (v == null){ Assert.fail("Thread " + i + " got a null vote"); } /* * A real zookeeper would take care of setting the current vote. Here * we do it manually. */ peer.setCurrentVote(v); LOG.info("Finished election: " + i + ", " + v.getId()); Assert.assertTrue("State is not leading.", peer.getPeerState() == ServerState.LEADING); } catch (Exception e) { e.printStackTrace(); } LOG.info("Joining"); }
public void run(){ try{ Vote v = null; peer.setPeerState(ServerState.LOOKING); LOG.info("Going to call leader election: " + i); v = peer.getElectionAlg().lookForLeader(); if (v == null){ Assert.fail("Thread " + i + " got a null vote"); } /* * A real zookeeper would take care of setting the current vote. Here * we do it manually. */ peer.setCurrentVote(v); LOG.info("Finished election: " + i + ", " + v.getId()); } catch (Exception e) { e.printStackTrace(); } LOG.info("Joining"); }
public void run() { setName("VerifyState-" + peer.getId()); while (true) { if(peer.getPeerState() == ServerState.FOLLOWING) { LOG.info("I am following"); success = true; break; } else if (peer.getPeerState() == ServerState.LEADING) { LOG.info("I am leading"); success = false; break; } try { Thread.sleep(250); } catch (Exception e) { LOG.warn("Sleep failed ", e); } } }
ToSend(mType type, long leader, long zxid, long electionEpoch, ServerState state, long sid, long peerEpoch, byte[] configData) { this.leader = leader; this.zxid = zxid; this.electionEpoch = electionEpoch; this.state = state; this.sid = sid; this.peerEpoch = peerEpoch; this.configData = configData; }
/** * Send notifications to all peers upon a change in our vote */ private void sendNotifications() { for (long sid : self.getCurrentAndNextConfigVoters()) { QuorumVerifier qv = self.getQuorumVerifier(); ToSend notmsg = new ToSend(ToSend.mType.notification, proposedLeader, proposedZxid, logicalclock.get(), QuorumPeer.ServerState.LOOKING, sid, proposedEpoch, qv.toString().getBytes()); if(LOG.isDebugEnabled()){ LOG.debug("Sending Notification: " + proposedLeader + " (n.leader), 0x" + Long.toHexString(proposedZxid) + " (n.zxid), 0x" + Long.toHexString(logicalclock.get()) + " (n.round), " + sid + " (recipient), " + self.getId() + " (myid), 0x" + Long.toHexString(proposedEpoch) + " (n.peerEpoch)"); } sendqueue.offer(notmsg); } }
/** * In the case there is a leader elected, and a quorum supporting * this leader, we have to check if the leader has voted and acked * that it is leading. We need this check to avoid that peers keep * electing over and over a peer that has crashed and it is no * longer leading. * * @param votes set of votes * @param leader leader id * @param electionEpoch epoch id */ private boolean checkLeader( Map<Long, Vote> votes, long leader, long electionEpoch){ boolean predicate = true; /* * If everyone else thinks I'm the leader, I must be the leader. * The other two checks are just for the case in which I'm not the * leader. If I'm not the leader and I haven't received a message * from leader stating that it is leading, then predicate is false. */ if(leader != self.getId()){ if(votes.get(leader) == null) predicate = false; else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false; } else if(logicalclock.get() != electionEpoch) { predicate = false; } return predicate; }
public void run() { try { Vote v = null; peer.setPeerState(ServerState.LOOKING); LOG.info("Going to call leader election: {}", i); v = peer.getElectionAlg().lookForLeader(); if (v == null) { Assert.fail("Thread " + i + " got a null vote"); } /* * A real zookeeper would take care of setting the current vote. Here * we do it manually. */ peer.setCurrentVote(v); LOG.info("Finished election: {}, {}", i, v.getId()); Assert.assertTrue("State is not leading.", peer.getPeerState() == ServerState.LEADING); } catch (Exception e) { e.printStackTrace(); } LOG.info("Joining"); }
/** * Send notifications to all peers upon a change in our vote */ private void sendNotifications() { for (QuorumServer server : self.getVotingView().values()) { long sid = server.id; ToSend notmsg = new ToSend(ToSend.mType.notification, proposedLeader, proposedZxid, logicalclock, QuorumPeer.ServerState.LOOKING, sid, proposedEpoch); if(LOG.isDebugEnabled()){ LOG.debug("Sending Notification: " + proposedLeader + " (n.leader), 0x" + Long.toHexString(proposedZxid) + " (n.zxid), 0x" + Long.toHexString(logicalclock) + " (n.round), " + sid + " (recipient), " + self.getId() + " (myid), 0x" + Long.toHexString(proposedEpoch) + " (n.peerEpoch)"); } sendqueue.offer(notmsg); } }
/** * In the case there is a leader elected, and a quorum supporting * this leader, we have to check if the leader has voted and acked * that it is leading. We need this check to avoid that peers keep * electing over and over a peer that has crashed and it is no * longer leading. * * @param votes set of votes * @param leader leader id * @param electionEpoch epoch id */ protected boolean checkLeader( HashMap<Long, Vote> votes, long leader, long electionEpoch){ boolean predicate = true; /* * If everyone else thinks I'm the leader, I must be the leader. * The other two checks are just for the case in which I'm not the * leader. If I'm not the leader and I haven't received a message * from leader stating that it is leading, then predicate is false. */ if(leader != self.getId()){ if(votes.get(leader) == null) predicate = false; else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false; } else if(logicalclock != electionEpoch) { predicate = false; } return predicate; }
private void shutdownQP(QuorumPeer qp) throws InterruptedException { assertNotNull("QuorumPeer doesn't exist!", qp); qp.shutdown(); int retryCnt = 30; while (retryCnt > 0) { if (qp.getPeerState() == ServerState.LOOKING) { LOG.info("Number of retries:{} to change the server state to {}", retryCnt, ServerState.LOOKING); break; } Thread.sleep(500); retryCnt--; } Assert.assertEquals( "After shutdown, QuorumPeer should change its state to LOOKING", ServerState.LOOKING, qp.getPeerState()); }
private void verifyElectionTimeTakenJMXAttribute(List<QuorumPeer> peers) throws Exception { LOG.info("Verify QuorumPeer#electionTimeTaken jmx bean attribute"); for (int i = 1; i <= peers.size(); i++) { QuorumPeer qp = peers.get(i - 1); if (qp.getLearnerType() == LearnerType.OBSERVER) { continue; // Observer don't have electionTimeTaken attribute. } Long electionTimeTaken = -1L; String bean = ""; if (qp.getPeerState() == ServerState.FOLLOWING) { bean = String.format( "%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Follower", CommonNames.DOMAIN, i, i); } else if (qp.getPeerState() == ServerState.LEADING) { bean = String.format( "%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Leader", CommonNames.DOMAIN, i, i); } electionTimeTaken = (Long) JMXEnv.ensureBeanAttribute(bean, "ElectionTimeTaken"); Assert.assertTrue("Wrong electionTimeTaken value!", electionTimeTaken >= 0); } }
/** * In the case there is a leader elected, and a quorum supporting * this leader, we have to check if the leader has voted and acked * that it is leading. We need this check to avoid that peers keep * electing over and over a peer that has crashed and it is no * longer leading. * * @param votes set of votes * @param leader leader id * @param electionEpoch epoch id */ private boolean checkLeader( HashMap<Long, Vote> votes, long leader, long electionEpoch){ boolean predicate = true; /* * If everyone else thinks I'm the leader, I must be the leader. * The other two checks are just for the case in which I'm not the * leader. If I'm not the leader and I haven't received a message * from leader stating that it is leading, then predicate is false. */ if(leader != self.getId()){ if(votes.get(leader) == null) predicate = false; else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false; } else if(logicalclock != electionEpoch) { predicate = false; } return predicate; }
/** * Send notifications to all peers upon a change in our vote */ private void sendNotifications() { for (QuorumServer server : self.getVotingView().values()) { long sid = server.id; ToSend notmsg = new ToSend(ToSend.mType.notification, proposedLeader, proposedZxid, logicalclock, QuorumPeer.ServerState.LOOKING, sid, proposedEpoch); if(LOG.isDebugEnabled()){ LOG.debug("Sending Notification: " + proposedLeader + " (n.leader), " + proposedZxid + " (n.zxid), " + logicalclock + " (n.round), " + sid + " (recipient), " + self.getId() + " (myid), " + proposedEpoch + " (n.peerEpoch)"); } sendqueue.offer(notmsg); } }
/** * In the case there is a leader elected, and a quorum supporting * this leader, we have to check if the leader has voted and acked * that it is leading. We need this check to avoid that peers keep * electing over and over a peer that has crashed and it is no * longer leading. * * @param votes set of votes * @param leader leader id * @param electionEpoch epoch id */ private boolean checkLeader( HashMap<Long, Vote> votes, long leader, long electionEpoch){ boolean predicate = true; /* * If everyone else thinks I'm the leader, I must be the leader. * The other two checks are just for the case in which I'm not the * leader. If I'm not the leader and I haven't received a message * from leader stating that it is leading, then predicate is false. */ if(leader != self.getId()){ if(votes.get(leader) == null) predicate = false; else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false; } else if(logicalclock.get() != electionEpoch) { predicate = false; } return predicate; }
public Vote(long id, long zxid) { this.version = 0x0; this.id = id; this.zxid = zxid; this.electionEpoch = -1; this.peerEpoch = -1; this.state = ServerState.LOOKING; }
public Vote(long id, long zxid, long peerEpoch) { this.version = 0x0; this.id = id; this.zxid = zxid; this.electionEpoch = -1; this.peerEpoch = peerEpoch; this.state = ServerState.LOOKING; }
public Vote(long id, long zxid, long electionEpoch, long peerEpoch) { this.version = 0x0; this.id = id; this.zxid = zxid; this.electionEpoch = electionEpoch; this.peerEpoch = peerEpoch; this.state = ServerState.LOOKING; }
public Vote(int version, long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) { this.version = version; this.id = id; this.zxid = zxid; this.electionEpoch = electionEpoch; this.state = state; this.peerEpoch = peerEpoch; }
public Vote(long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) { this.id = id; this.zxid = zxid; this.electionEpoch = electionEpoch; this.state = state; this.peerEpoch = peerEpoch; this.version = 0x0; }
@Override public boolean equals(Object o) { if (!(o instanceof Vote)) { return false; } Vote other = (Vote) o; /* * There are two things going on in the logic below. * First, we compare votes of servers out of election * using only id and peer epoch. Second, if one version * is 0x0 and the other isn't, then we only use the * leader id. This case is here to enable rolling upgrades. * * {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1805} */ if ((state == ServerState.LOOKING) || (other.state == ServerState.LOOKING)) { return (id == other.id && zxid == other.zxid && electionEpoch == other.electionEpoch && peerEpoch == other.peerEpoch); } else { if ((version > 0x0) ^ (other.version > 0x0)) { return id == other.id; } else { return (id == other.id && peerEpoch == other.peerEpoch); } } }