Search in sources :

Example 21 with QuorumServer

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

the class ReconfigTest method testJMXBeanAfterRoleChange.

/**
     * Tests verifies the jmx attributes of local and remote peer bean - change
     * participant to observer role
     */
@Test
public void testJMXBeanAfterRoleChange() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    ZooKeeper[] zkArr = createHandles(qu);
    ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
    // changing a server's role / port is done by "adding" it with the same
    // id but different role / port
    List<String> joiningServers = new ArrayList<String>();
    // assert remotePeerBean.1 of ReplicatedServer_2
    int changingIndex = 1;
    int replica2 = 2;
    QuorumPeer peer2 = qu.getPeer(replica2).peer;
    QuorumServer changingQS2 = peer2.getView().get(new Long(changingIndex));
    String remotePeerBean2 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + changingIndex;
    assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    int replica3 = 3;
    QuorumPeer peer3 = qu.getPeer(replica3).peer;
    QuorumServer changingQS3 = peer3.getView().get(new Long(changingIndex));
    String remotePeerBean3 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + changingIndex;
    assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3);
    String newRole = "observer";
    ZooKeeper zk = zkArr[changingIndex];
    ZooKeeperAdmin zkAdmin = zkAdminArr[changingIndex];
    // exactly as it is now, except for role change
    joiningServers.add("server." + changingIndex + "=127.0.0.1:" + qu.getPeer(changingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(changingIndex).peer.getElectionAddress().getPort() + ":" + newRole + ";127.0.0.1:" + qu.getPeer(changingIndex).peer.getClientPort());
    reconfig(zkAdmin, joiningServers, null, null, -1);
    testNormalOperation(zkArr[changingIndex], zk);
    Assert.assertTrue(qu.getPeer(changingIndex).peer.observer != null && qu.getPeer(changingIndex).peer.follower == null && qu.getPeer(changingIndex).peer.leader == null);
    Assert.assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING);
    QuorumPeer qp = qu.getPeer(changingIndex).peer;
    String localPeerBeanName = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + changingIndex + ",name1=replica." + changingIndex;
    // localPeerBean.1 of ReplicatedServer_1
    assertLocalPeerMXBeanAttributes(qp, localPeerBeanName, true);
    // assert remotePeerBean.1 of ReplicatedServer_2
    changingQS2 = peer2.getView().get(new Long(changingIndex));
    assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
    // assert remotePeerBean.1 of ReplicatedServer_3
    changingQS3 = peer3.getView().get(new Long(changingIndex));
    assertRemotePeerMXBeanAttributes(changingQS3, 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)

Example 22 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 23 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 24 with QuorumServer

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

the class QuorumPeerInstance method configure.

public void configure(String params) {
    if (clientAddr == null) {
        String[] parts = params.split(" ");
        // The first time we are configured, it is just to tell
        // us which machine we are
        serverId = Integer.parseInt(parts[0]);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Setting up server " + serverId);
        }
        if (parts.length > 1 && parts[1].equals("false")) {
            System.setProperty("zookeeper.leaderServes", "no");
        } else {
            System.setProperty("zookeeper.leaderServes", "yes");
        }
        // Let's grab two ports
        try {
            ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
            clientAddr = (InetSocketAddress) ss.getLocalSocketAddress();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
            quorumLeaderAddr = (InetSocketAddress) ss.getLocalSocketAddress();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
            quorumLeaderElectionAddr = (InetSocketAddress) ss.getLocalSocketAddress();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String report = clientAddr.getHostString() + ':' + clientAddr.getPort() + ',' + quorumLeaderAddr.getHostString() + ':' + quorumLeaderAddr.getPort() + ':' + quorumLeaderElectionAddr.getPort();
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Reporting " + report);
            }
            r.report(report);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    } else {
        int spaceIndex = params.indexOf(' ');
        if (spaceIndex == -1) {
            LOG.warn("looking for host:port,... start|stop, but found " + params);
            return;
        }
        String quorumSpecs = params.substring(0, spaceIndex);
        String cmd = params.substring(spaceIndex + 1);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Running command: " + cmd);
        }
        if (!cmd.equals("start")) {
            if (peer != null) {
                peer.shutdown();
            }
            peer = null;
            try {
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(500);
                    try {
                        // Wait until we can't connect
                        new Socket("127.0.0.1", clientAddr.getPort()).close();
                    } catch (IOException e) {
                        break;
                    }
                }
                r.report("stopped");
            } catch (Exception e) {
                LOG.error("Unhandled error", e);
            }
            return;
        }
        String[] parts = quorumSpecs.split(",");
        peers = new HashMap<Long, QuorumServer>();
        for (int i = 0; i < parts.length; i++) {
            // parts[i] == "host:leaderPort:leaderElectionPort;clientPort"
            String[] subparts = ((parts[i].split(";"))[0]).split(":");
            String clientPort = (parts[i].split(";"))[1];
            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(subparts[0], Integer.parseInt(subparts[1])), new InetSocketAddress(subparts[0], Integer.parseInt(subparts[2])), new InetSocketAddress(subparts[0], Integer.parseInt(clientPort))));
        }
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Starting quorumPeer " + serverId + " on port " + clientAddr.getPort());
            }
            if (peer != null) {
                LOG.warn("Peer " + serverId + " already started");
                return;
            }
            System.err.println("SnapDir = " + snapDir + " LogDir = " + logDir);
            peer = new QuorumPeer(peers, snapDir, logDir, clientAddr.getPort(), 0, serverId, tickTime, initLimit, syncLimit);
            peer.start();
            for (int i = 0; i < 5; i++) {
                Thread.sleep(500);
                try {
                    // Wait until we can connect
                    new Socket("127.0.0.1", clientAddr.getPort()).close();
                    break;
                } catch (IOException e) {
                }
            }
            r.report("started");
        } catch (Exception e) {
            LOG.error("Unhandled exception", e);
        }
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 25 with QuorumServer

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

the class QuorumMaj method toString.

public String toString() {
    StringBuilder sw = new StringBuilder();
    for (QuorumServer member : getAllMembers().values()) {
        String key = "server." + member.id;
        String value = member.toString();
        sw.append(key);
        sw.append('=');
        sw.append(value);
        sw.append('\n');
    }
    String hexVersion = Long.toHexString(version);
    sw.append("version=");
    sw.append(hexVersion);
    return sw.toString();
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)

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