Search in sources :

Example 46 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project coprhd-controller by CoprHD.

the class SpringQuorumPeerConfig method createQuorumServer.

/**
 * This logic is same as ZK 3.4.6 except splitting values with "," instead of ":"
 */
private void createQuorumServer(String key, String value) throws ConfigException {
    int dot = key.indexOf('.');
    long sid = Long.parseLong(key.substring(dot + 1));
    String[] parts = value.split(",");
    if ((parts.length != 2) && (parts.length != 3) && (parts.length != 4)) {
        log.error(value + " does not have the form host,port or host,port,port " + " or host,port,port,type");
    }
    InetSocketAddress addr = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
    if (parts.length == 2) {
        servers.put(Long.valueOf(sid), new QuorumServer(sid, addr));
    } else if (parts.length == 3) {
        InetSocketAddress electionAddr = new InetSocketAddress(parts[0], Integer.parseInt(parts[2]));
        servers.put(Long.valueOf(sid), new QuorumServer(sid, addr, electionAddr));
    } else if (parts.length == 4) {
        InetSocketAddress electionAddr = new InetSocketAddress(parts[0], Integer.parseInt(parts[2]));
        LearnerType type = LearnerType.PARTICIPANT;
        if (parts[3].toLowerCase().equals("observer")) {
            type = LearnerType.OBSERVER;
            observers.put(Long.valueOf(sid), new QuorumServer(sid, addr, electionAddr, type));
        } else if (parts[3].toLowerCase().equals("participant")) {
            type = LearnerType.PARTICIPANT;
            servers.put(Long.valueOf(sid), new QuorumServer(sid, addr, electionAddr, type));
        } else {
            throw new ConfigException("Unrecognised peertype: " + value);
        }
    }
}
Also used : LearnerType(org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType) InetSocketAddress(java.net.InetSocketAddress) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)

Example 47 with QuorumServer

use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project coprhd-controller by CoprHD.

the class SpringQuorumPeerConfigTest method testInitWithCustomizedSeperator.

@Test
public void testInitWithCustomizedSeperator() throws Exception {
    springQuorumPeerConfig.setProperties(properties);
    springQuorumPeerConfig.init();
    assertTrue(springQuorumPeerConfig.getServers().size() == 3);
    QuorumServer server1 = springQuorumPeerConfig.getServers().get(new Long(1));
    assertTrue(server1.addr.toString().equals("/192.168.1.1:2888"));
    assertTrue(server1.electionAddr.toString().equals("/192.168.1.1:3888"));
    assertTrue(server1.type == LearnerType.PARTICIPANT);
    QuorumServer server2 = springQuorumPeerConfig.getServers().get(new Long(2));
    assertTrue(server2.addr.toString().equals("hostname:2888"));
    assertTrue(server2.electionAddr.toString().equals("hostname:3888"));
    assertTrue(server2.type == LearnerType.PARTICIPANT);
    QuorumServer server3 = springQuorumPeerConfig.getServers().get(new Long(3));
    assertTrue(server3.addr.toString().equals("/fe80:0:0:0:81fe:4fd:95b1:8bbf:2888"));
    assertTrue(server3.electionAddr.toString().equals("/fe80:0:0:0:81fe:4fd:95b1:8bbf:3888"));
    assertTrue(server3.type == LearnerType.PARTICIPANT);
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) Test(org.junit.Test)

Aggregations

QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)47 InetSocketAddress (java.net.InetSocketAddress)24 Test (org.junit.Test)21 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