Search in sources :

Example 11 with QuorumServer

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

the class ReconfigTest method testUnspecifiedClientAddress.

@Test
public void testUnspecifiedClientAddress() throws Exception {
    int[] ports = new int[3];
    for (int port : ports) {
        port = PortAssignment.unique();
    }
    String server = "server.0=localhost:" + ports[0] + ":" + ports[1] + ";" + ports[2];
    QuorumServer qs = new QuorumServer(0, server);
    Assert.assertEquals(qs.clientAddr.getHostString(), "0.0.0.0");
    Assert.assertEquals(qs.clientAddr.getPort(), ports[2]);
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) Test(org.junit.Test)

Example 12 with QuorumServer

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

the class ZKQuorum method createNewPeerData.

private void createNewPeerData(int id) {
    int clientPort = ZKTestUtil.getRandomPort();
    int quorumPort = ZKTestUtil.getRandomPort() + 1000;
    int electionPort = ZKTestUtil.getRandomPort() + 1001;
    ZKPeer zkpeer = new ZKPeer(id, ZKTestUtil.createTempDir("zkdata" + id), ZKTestUtil.createTempDir("zklog" + id), HOST, clientPort, quorumPort, electionPort);
    _peers.put(id, zkpeer);
    _peersView.put(Long.valueOf(id), new QuorumServer(id, new InetSocketAddress(HOST, quorumPort), new InetSocketAddress(HOST, electionPort), LearnerType.PARTICIPANT));
    _log.info("Created peer #" + id + " with ports:" + clientPort + "/" + quorumPort + "/" + electionPort + "  peer server addr:" + _peersView.get(Long.valueOf(id)).addr + "  peer server electionAddr:" + _peersView.get(Long.valueOf(id)).electionAddr);
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress)

Example 13 with QuorumServer

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

the class Follower method followLeader.

/**
 * the main method called by the follower to follow the leader
 *
 * @throws InterruptedException
 */
void followLeader() throws InterruptedException {
    self.end_fle = Time.currentElapsedTime();
    long electionTimeTaken = self.end_fle - self.start_fle;
    self.setElectionTimeTaken(electionTimeTaken);
    LOG.info("FOLLOWING - LEADER ELECTION TOOK - {} {}", electionTimeTaken, QuorumPeer.FLE_TIME_UNIT);
    self.start_fle = 0;
    self.end_fle = 0;
    fzk.registerJMX(new FollowerBean(this, zk), self.jmxLocalPeerBean);
    try {
        QuorumServer leaderServer = findLeader();
        try {
            connectToLeader(leaderServer.addr, leaderServer.hostname);
            long newEpochZxid = registerWithLeader(Leader.FOLLOWERINFO);
            if (self.isReconfigStateChange())
                throw new Exception("learned about role change");
            // check to see if the leader zxid is lower than ours
            // this should never happen but is just a safety check
            long newEpoch = ZxidUtils.getEpochFromZxid(newEpochZxid);
            if (newEpoch < self.getAcceptedEpoch()) {
                LOG.error("Proposed leader epoch " + ZxidUtils.zxidToString(newEpochZxid) + " is less than our accepted epoch " + ZxidUtils.zxidToString(self.getAcceptedEpoch()));
                throw new IOException("Error: Epoch of leader is lower");
            }
            syncWithLeader(newEpochZxid);
            QuorumPacket qp = new QuorumPacket();
            while (this.isRunning()) {
                readPacket(qp);
                processPacket(qp);
            }
        } catch (Exception e) {
            LOG.warn("Exception when following the leader", e);
            try {
                sock.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            // clear pending revalidations
            pendingRevalidations.clear();
        }
    } finally {
        zk.unregisterJMX((Learner) this);
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) IOException(java.io.IOException) IOException(java.io.IOException)

Example 14 with QuorumServer

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

the class Learner method findLeader.

/**
 * Returns the address of the node we think is the leader.
 */
protected QuorumServer findLeader() {
    QuorumServer leaderServer = null;
    // Find the leader by id
    Vote current = self.getCurrentVote();
    for (QuorumServer s : self.getView().values()) {
        if (s.id == current.getId()) {
            leaderServer = s;
            break;
        }
    }
    if (leaderServer == null) {
        LOG.warn("Couldn't find the leader with id = " + current.getId());
    }
    return leaderServer;
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)

Example 15 with QuorumServer

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

the class QuorumHierarchical method parse.

/**
 * Parse properties if configuration given in a separate file.
 * Assumes that allMembers has been already assigned
 * @throws ConfigException
 */
private void parse(Properties quorumProp) throws ConfigException {
    for (Entry<Object, Object> entry : quorumProp.entrySet()) {
        String key = entry.getKey().toString();
        String value = entry.getValue().toString();
        if (key.startsWith("server.")) {
            int dot = key.indexOf('.');
            long sid = Long.parseLong(key.substring(dot + 1));
            QuorumServer qs = new QuorumServer(sid, value);
            allMembers.put(Long.valueOf(sid), qs);
            if (qs.type == LearnerType.PARTICIPANT)
                participatingMembers.put(Long.valueOf(sid), qs);
            else {
                observingMembers.put(Long.valueOf(sid), qs);
            }
        } else if (key.startsWith("group")) {
            int dot = key.indexOf('.');
            long gid = Long.parseLong(key.substring(dot + 1));
            numGroups++;
            String[] parts = value.split(":");
            for (String s : parts) {
                long sid = Long.parseLong(s);
                if (serverGroup.containsKey(sid))
                    throw new ConfigException("Server " + sid + "is in multiple groups");
                else
                    serverGroup.put(sid, gid);
            }
        } else if (key.startsWith("weight")) {
            int dot = key.indexOf('.');
            long sid = Long.parseLong(key.substring(dot + 1));
            serverWeight.put(sid, Long.parseLong(value));
        } else if (key.equals("version")) {
            version = Long.parseLong(value, 16);
        }
    }
    for (QuorumServer qs : allMembers.values()) {
        Long id = qs.id;
        if (qs.type == LearnerType.PARTICIPANT) {
            if (!serverGroup.containsKey(id))
                throw new ConfigException("Server " + id + "is not in a group");
            if (!serverWeight.containsKey(id))
                serverWeight.put(id, (long) 1);
        }
    }
    computeGroupWeight();
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)

Aggregations

QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)45 InetSocketAddress (java.net.InetSocketAddress)24 Test (org.junit.Test)20 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)19 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 File (java.io.File)7 ArrayList (java.util.ArrayList)5 Before (org.junit.Before)4 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 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 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 SocketException (java.net.SocketException)2