Search in sources :

Example 1 with LeaderElection

use of org.apache.zookeeper.server.quorum.LeaderElection in project zookeeper by apache.

the class LETest method testLE.

@Test
public void testLE() throws Exception {
    int count = 30;
    HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>(count);
    ArrayList<LEThread> threads = new ArrayList<LEThread>(count);
    File[] tmpdir = new File[count];
    int[] port = new int[count];
    votes = new Vote[count];
    for (int i = 0; i < count; i++) {
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = PortAssignment.unique();
    }
    LeaderElection[] le = new LeaderElection[count];
    leaderDies = true;
    boolean allowOneBadLeader = leaderDies;
    for (int i = 0; i < le.length; i++) {
        QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 0, i, 1000, 2, 2);
        peer.startLeaderElection();
        le[i] = new LeaderElection(peer);
        LEThread thread = new LEThread(le[i], peer, i);
        thread.start();
        threads.add(thread);
    }
    for (int i = 0; i < threads.size(); i++) {
        threads.get(i).join(15000);
        if (threads.get(i).isAlive()) {
            Assert.fail("Threads didn't join");
        }
    }
    long id = votes[0].getId();
    for (int i = 1; i < votes.length; i++) {
        if (votes[i] == null) {
            Assert.fail("Thread " + i + " had a null vote");
        }
        if (votes[i].getId() != id) {
            if (allowOneBadLeader && votes[i].getId() == i) {
                allowOneBadLeader = false;
            } else {
                Assert.fail("Thread " + i + " got " + votes[i].getId() + " expected " + id);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) LeaderElection(org.apache.zookeeper.server.quorum.LeaderElection) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LeaderElection (org.apache.zookeeper.server.quorum.LeaderElection)1 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)1 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)1 Test (org.junit.Test)1