Search in sources :

Example 16 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.

the class FLETest method runElection.

/**
     * Test leader election for a number of rounds. In all rounds but the last one
     * we kill the leader.
     * 
     * @param rounds
     * @throws Exception
     */
private void runElection(int rounds) throws Exception {
    ConcurrentHashMap<Long, HashSet<Integer>> quora = new ConcurrentHashMap<Long, HashSet<Integer>>();
    LOG.info("TestLE: " + getTestName() + ", " + count);
    /*
         * Creates list of peers.
         */
    for (int i = 0; i < count; i++) {
        port[i] = PortAssignment.unique();
        peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port[i])));
        tmpdir[i] = ClientBase.createTmpDir();
    }
    /*
         * Start one LEThread for each peer we want to run.
         */
    for (int i = 0; i < count; i++) {
        QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2);
        peer.startLeaderElection();
        LEThread thread = new LEThread(this, peer, i, rounds, quora);
        thread.start();
        threads.add(thread);
    }
    LOG.info("Started threads " + getTestName());
    int waitCounter = 0;
    synchronized (this) {
        while (((successCount <= count / 2) || (leader == -1)) && (waitCounter < MAX_LOOP_COUNTER)) {
            this.wait(200);
            waitCounter++;
        }
    }
    LOG.info("Success count: " + successCount);
    /*
        * Lists what threads haven't joined. A thread doesn't join if
        * it hasn't decided upon a leader yet. It can happen that a
        * peer is slow or disconnected, and it can take longer to
        * nominate and connect to the current leader.
        */
    for (int i = 0; i < threads.size(); i++) {
        if (threads.get(i).isAlive()) {
            LOG.info("Threads didn't join: " + i);
        }
    }
    /*
        * If we have a majority, then we are good to go.
        */
    if (successCount <= count / 2) {
        Assert.fail("Fewer than a a majority has joined");
    }
    /*
        * I'm done so joining.
        */
    if (!joinedThreads.contains(leader)) {
        Assert.fail("Leader hasn't joined: " + leader);
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Example 17 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.

the class FLETest method testJoinInconsistentEnsemble.

/*
     * For ZOOKEEPER-1732 verify that it is possible to join an ensemble with
     * inconsistent election round information.
     */
@Test
public void testJoinInconsistentEnsemble() throws Exception {
    int sid;
    QuorumPeer peer;
    int waitTime = 10 * 1000;
    ArrayList<QuorumPeer> peerList = new ArrayList<QuorumPeer>();
    for (sid = 0; sid < 3; sid++) {
        peers.put(Long.valueOf(sid), new QuorumServer(sid, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        tmpdir[sid] = ClientBase.createTmpDir();
        port[sid] = PortAssignment.unique();
    }
    // start 2 peers and verify if they form the cluster
    for (sid = 0; sid < 2; sid++) {
        peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2);
        LOG.info("Starting peer " + peer.getId());
        peer.start();
        peerList.add(sid, peer);
    }
    peer = peerList.get(0);
    VerifyState v1 = new VerifyState(peerList.get(0));
    v1.start();
    v1.join(waitTime);
    Assert.assertFalse("Unable to form cluster in " + waitTime + " ms", !v1.isSuccess());
    // Change the election round for one of the members of the ensemble
    long leaderSid = peer.getCurrentVote().getId();
    long zxid = peer.getCurrentVote().getZxid();
    long electionEpoch = peer.getCurrentVote().getElectionEpoch();
    ServerState state = peer.getCurrentVote().getState();
    long peerEpoch = peer.getCurrentVote().getPeerEpoch();
    Vote newVote = new Vote(leaderSid, zxid + 100, electionEpoch + 100, peerEpoch, state);
    peer.setCurrentVote(newVote);
    // Start 3rd peer and check if it joins the quorum
    peer = new QuorumPeer(peers, tmpdir[2], tmpdir[2], port[2], 3, 2, 2000, 2, 2);
    LOG.info("Starting peer " + peer.getId());
    peer.start();
    peerList.add(sid, peer);
    v1 = new VerifyState(peer);
    v1.start();
    v1.join(waitTime);
    if (v1.isAlive()) {
        Assert.fail("Peer " + peer.getId() + " failed to join the cluster " + "within " + waitTime + " ms");
    }
    // cleanup
    for (int id = 0; id < 3; id++) {
        peer = peerList.get(id);
        if (peer != null) {
            peer.shutdown();
        }
    }
}
Also used : Vote(org.apache.zookeeper.server.quorum.Vote) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) ServerState(org.apache.zookeeper.server.quorum.QuorumPeer.ServerState) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 18 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.

the class CnxManagerTest method testCnxManagerTimeout.

@Test
public void testCnxManagerTimeout() throws Exception {
    Random rand = new Random();
    byte b = (byte) rand.nextInt();
    int deadPort = PortAssignment.unique();
    String deadAddress = "10.1.1." + b;
    LOG.info("This is the dead address I'm trying: " + deadAddress);
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(deadAddress, deadPort), new InetSocketAddress(deadAddress, PortAssignment.unique()), new InetSocketAddress(deadAddress, PortAssignment.unique())));
    peerTmpdir[2] = ClientBase.createTmpDir();
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
        listener.start();
    } else {
        LOG.error("Null listener when initializing cnx manager");
    }
    long begin = Time.currentElapsedTime();
    cnxManager.toSend(2L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    long end = Time.currentElapsedTime();
    if ((end - begin) > 6000)
        Assert.fail("Waited more than necessary");
    cnxManager.halt();
    Assert.assertFalse(cnxManager.listener.isAlive());
}
Also used : Random(java.util.Random) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Test(org.junit.Test)

Example 19 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.

the class HierarchicalQuorumTest method startServers.

/**
     * Starts 5 Learners. When withObservers == false, all 5 are Followers.
     * When withObservers == true, 3 are Followers and 2 Observers.
     * @param withObservers
     * @throws Exception
     */
void startServers(boolean withObservers) throws Exception {
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
    peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", port1), new InetSocketAddress("127.0.0.1", leport1), new InetSocketAddress("127.0.0.1", clientport1)));
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", port2), new InetSocketAddress("127.0.0.1", leport2), new InetSocketAddress("127.0.0.1", clientport2)));
    peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", port3), new InetSocketAddress("127.0.0.1", leport3), new InetSocketAddress("127.0.0.1", clientport3)));
    peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress("127.0.0.1", port4), new InetSocketAddress("127.0.0.1", leport4), new InetSocketAddress("127.0.0.1", clientport4), withObservers ? QuorumPeer.LearnerType.OBSERVER : QuorumPeer.LearnerType.PARTICIPANT));
    peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress("127.0.0.1", port5), new InetSocketAddress("127.0.0.1", leport5), new InetSocketAddress("127.0.0.1", clientport5), withObservers ? QuorumPeer.LearnerType.OBSERVER : QuorumPeer.LearnerType.PARTICIPANT));
    LOG.info("creating QuorumPeer 1 port " + clientport1);
    if (withObservers) {
        qp.setProperty("server.4", "127.0.0.1:" + port4 + ":" + leport4 + ":observer" + ";" + clientport4);
        qp.setProperty("server.5", "127.0.0.1:" + port5 + ":" + leport5 + ":observer" + ";" + clientport5);
    }
    QuorumHierarchical hq1 = new QuorumHierarchical(qp);
    s1 = new QuorumPeer(peers, s1dir, s1dir, clientport1, 3, 1, tickTime, initLimit, syncLimit, hq1);
    Assert.assertEquals(clientport1, s1.getClientPort());
    LOG.info("creating QuorumPeer 2 port " + clientport2);
    QuorumHierarchical hq2 = new QuorumHierarchical(qp);
    s2 = new QuorumPeer(peers, s2dir, s2dir, clientport2, 3, 2, tickTime, initLimit, syncLimit, hq2);
    Assert.assertEquals(clientport2, s2.getClientPort());
    LOG.info("creating QuorumPeer 3 port " + clientport3);
    QuorumHierarchical hq3 = new QuorumHierarchical(qp);
    s3 = new QuorumPeer(peers, s3dir, s3dir, clientport3, 3, 3, tickTime, initLimit, syncLimit, hq3);
    Assert.assertEquals(clientport3, s3.getClientPort());
    LOG.info("creating QuorumPeer 4 port " + clientport4);
    QuorumHierarchical hq4 = new QuorumHierarchical(qp);
    s4 = new QuorumPeer(peers, s4dir, s4dir, clientport4, 3, 4, tickTime, initLimit, syncLimit, hq4);
    if (withObservers) {
        s4.setLearnerType(QuorumPeer.LearnerType.OBSERVER);
    }
    Assert.assertEquals(clientport4, s4.getClientPort());
    LOG.info("creating QuorumPeer 5 port " + clientport5);
    QuorumHierarchical hq5 = new QuorumHierarchical(qp);
    s5 = new QuorumPeer(peers, s5dir, s5dir, clientport5, 3, 5, tickTime, initLimit, syncLimit, hq5);
    if (withObservers) {
        s5.setLearnerType(QuorumPeer.LearnerType.OBSERVER);
    }
    Assert.assertEquals(clientport5, s5.getClientPort());
    // Observers are currently only compatible with LeaderElection
    if (withObservers) {
        s1.setElectionType(0);
        s2.setElectionType(0);
        s3.setElectionType(0);
        s4.setElectionType(0);
        s5.setElectionType(0);
    }
    LOG.info("start QuorumPeer 1");
    s1.start();
    LOG.info("start QuorumPeer 2");
    s2.start();
    LOG.info("start QuorumPeer 3");
    s3.start();
    LOG.info("start QuorumPeer 4" + (withObservers ? "(observer)" : ""));
    s4.start();
    LOG.info("start QuorumPeer 5" + (withObservers ? "(observer)" : ""));
    s5.start();
    LOG.info("started QuorumPeer 5");
    LOG.info("Closing ports " + hostPort);
    for (String hp : hostPort.split(",")) {
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT));
        LOG.info(hp + " is accepting client connections");
    }
    // interesting to see what's there...
    JMXEnv.dump();
    // make sure we have these 5 servers listed
    Set<String> ensureNames = new LinkedHashSet<String>();
    for (int i = 1; i <= 5; i++) {
        ensureNames.add("InMemoryDataTree");
    }
    for (int i = 1; i <= 5; i++) {
        ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2=");
    }
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5; j++) {
            ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j);
        }
    }
    for (int i = 1; i <= 5; i++) {
        ensureNames.add("name0=ReplicatedServer_id" + i);
    }
    JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()]));
    for (int i = 1; i <= 5; i++) {
        String bean = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + i + ",name1=replica." + i;
        JMXEnv.ensureBeanAttribute(bean, "ConfigVersion");
        JMXEnv.ensureBeanAttribute(bean, "LearnerType");
        JMXEnv.ensureBeanAttribute(bean, "ClientAddress");
        JMXEnv.ensureBeanAttribute(bean, "ElectionAddress");
        JMXEnv.ensureBeanAttribute(bean, "QuorumSystemInfo");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumHierarchical(org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer)

Example 20 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.

the class ReconfigTest method testJMXBeanAfterRemoveAddOne.

/**
     * Tests verifies the jmx attributes of local and remote peer bean - remove
     * one quorum peer and again adding it back
     */
@Test
public void testJMXBeanAfterRemoveAddOne() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    ZooKeeper[] zkArr = createHandles(qu);
    ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
    List<String> leavingServers = new ArrayList<String>();
    List<String> joiningServers = new ArrayList<String>();
    // assert remotePeerBean.1 of ReplicatedServer_2
    int leavingIndex = 1;
    int replica2 = 2;
    QuorumPeer peer2 = qu.getPeer(replica2).peer;
    QuorumServer leavingQS2 = peer2.getView().get(new Long(leavingIndex));
    String remotePeerBean2 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + leavingIndex;
    assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    int replica3 = 3;
    QuorumPeer peer3 = qu.getPeer(replica3).peer;
    QuorumServer leavingQS3 = peer3.getView().get(new Long(leavingIndex));
    String remotePeerBean3 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + leavingIndex;
    assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
    ZooKeeper zk = zkArr[leavingIndex];
    ZooKeeperAdmin zkAdmin = zkAdminArr[leavingIndex];
    leavingServers.add(Integer.toString(leavingIndex));
    // remember this server so we can add it back later
    joiningServers.add("server." + leavingIndex + "=127.0.0.1:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getPort() + ":participant;127.0.0.1:" + qu.getPeer(leavingIndex).peer.getClientPort());
    // Remove ReplicatedServer_1 from the ensemble
    reconfig(zkAdmin, null, leavingServers, null, -1);
    // localPeerBean.1 of ReplicatedServer_1
    QuorumPeer removedPeer = qu.getPeer(leavingIndex).peer;
    String localPeerBean = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + leavingIndex + ",name1=replica." + leavingIndex;
    assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, false);
    // remotePeerBean.1 shouldn't exists in ReplicatedServer_2
    JMXEnv.ensureNone(remotePeerBean2);
    // remotePeerBean.1 shouldn't exists in ReplicatedServer_3
    JMXEnv.ensureNone(remotePeerBean3);
    // Add ReplicatedServer_1 back to the ensemble
    reconfig(zkAdmin, joiningServers, null, null, -1);
    // localPeerBean.1 of ReplicatedServer_1
    assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, true);
    // assert remotePeerBean.1 of ReplicatedServer_2
    leavingQS2 = peer2.getView().get(new Long(leavingIndex));
    assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    leavingQS3 = peer3.getView().get(new Long(leavingIndex));
    assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
    closeAllHandles(zkArr, zkAdminArr);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) ArrayList(java.util.ArrayList) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) Test(org.junit.Test)

Aggregations

QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)41 InetSocketAddress (java.net.InetSocketAddress)25 Test (org.junit.Test)20 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)19 HashMap (java.util.HashMap)9 IOException (java.io.IOException)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 Before (org.junit.Before)4 ByteBuffer (java.nio.ByteBuffer)3 KeeperException (org.apache.zookeeper.KeeperException)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Vote (org.apache.zookeeper.server.quorum.Vote)3 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 SocketException (java.net.SocketException)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Properties (java.util.Properties)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2