private boolean termPredicate(HashMap<InetSocketAddress, Vote> votes, long l, long zxid) { Collection<Vote> votesCast = votes.values(); int count = 0; /* * First make the views consistent. Sometimes peers will have different * zxids for a server depending on timing. */ for (Vote v : votesCast) { if ((v.getId() == l) && (v.getZxid() == zxid)) count++; } if (count > (self.getVotingView().size() / 2)) return true; else return false; }
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"); }
/** * MockServer plays the role of peer C. Respond to two requests for votes * with vote for self and then Assert.fail. */ void mockServer() throws InterruptedException, IOException { byte b[] = new byte[36]; ByteBuffer responseBuffer = ByteBuffer.wrap(b); DatagramPacket packet = new DatagramPacket(b, b.length); QuorumServer server = peers.get(Long.valueOf(2)); DatagramSocket udpSocket = new DatagramSocket(server.addr.getPort()); LOG.info("In MockServer"); mockLatch.countDown(); Vote current = new Vote(2, 1); for (int i=0;i<2;++i) { udpSocket.receive(packet); responseBuffer.rewind(); LOG.info("Received " + responseBuffer.getInt() + " " + responseBuffer.getLong() + " " + responseBuffer.getLong()); LOG.info("From " + packet.getSocketAddress()); responseBuffer.clear(); responseBuffer.getInt(); // Skip the xid responseBuffer.putLong(2); responseBuffer.putLong(current.getId()); responseBuffer.putLong(current.getZxid()); packet.setData(b); udpSocket.send(packet); } }
private boolean termPredicate(Map<InetSocketAddress, Vote> votes, long l, long zxid) { Collection<Vote> votesCast = votes.values(); int count = 0; /* * First make the views consistent. Sometimes peers will have different * zxids for a server depending on timing. */ for (Vote v : votesCast) { if ((v.getId() == l) && (v.getZxid() == zxid)) count++; } if (count > (self.getVotingView().size() / 2)) return true; else return false; }
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(){ boolean flag = true; 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"); }
@Test public void testDontCare() { MockFLE fle = new MockFLE(peer, new QuorumCnxManager(peer)); HashMap<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.termPredicate(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING))); }
@Test public void testDontCareVersion() { MockFLE fle = new MockFLE(peer, new QuorumCnxManager(peer)); HashMap<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.termPredicate(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING))); }
@Test public void testLookingNormal() { MockFLE fle = new MockFLE(peer, new QuorumCnxManager(peer)); HashMap<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.termPredicate(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING))); }
@Test public void testLookingDiffRounds() { MockFLE fle = new MockFLE(peer, new QuorumCnxManager(peer)); 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.termPredicate(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING))); }
/** * Helper method to build notifications and populate outofelection. * * * @param version * @param leader * @param zxid * @param electionEpoch * @param state * @param sid * @param peerEpoch * @param outofelection * @return */ FastLeaderElection.Notification genNotification(int version, long leader, long zxid, long electionEpoch, ServerState state, long sid, long peerEpoch, HashMap<Long,Vote> outofelection) { FastLeaderElection.Notification n = new FastLeaderElection.Notification(); n.version = version; n.leader = leader; n.zxid = zxid; n.electionEpoch = electionEpoch; n.state = state; n.sid = sid; n.peerEpoch = peerEpoch; outofelection.put(n.sid, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); return n; }