Search in sources :

Example 16 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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 17 with QuorumPeer

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

the class ZkDatabaseCorruptionTest method testCorruption.

@Test
public void testCorruption() throws Exception {
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ZooKeeper zk = ClientBase.createZKClient(qb.hostPort, 10000);
    SyncRequestProcessor.setSnapCount(100);
    for (int i = 0; i < 2000; i++) {
        zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null);
    }
    zk.close();
    long leaderSid = 1;
    QuorumPeer leader = null;
    //find out who is the leader and kill it
    for (QuorumPeer quorumPeer : Arrays.asList(qb.s1, qb.s2, qb.s3, qb.s4, qb.s5)) {
        if (quorumPeer.getPeerState() == ServerState.LEADING) {
            leader = quorumPeer;
            break;
        }
        ++leaderSid;
    }
    Assert.assertNotNull("Cannot find the leader.", leader);
    leader.shutdown();
    // now corrupt the leader's database
    FileTxnSnapLog snapLog = leader.getTxnFactory();
    File snapDir = snapLog.getSnapDir();
    //corrupt all the snapshot in the snapshot directory
    corruptAllSnapshots(snapDir);
    qb.shutdownServers();
    qb.setupServers();
    if (leaderSid != 1)
        qb.s1.start();
    else
        leader = qb.s1;
    if (leaderSid != 2)
        qb.s2.start();
    else
        leader = qb.s2;
    if (leaderSid != 3)
        qb.s3.start();
    else
        leader = qb.s3;
    if (leaderSid != 4)
        qb.s4.start();
    else
        leader = qb.s4;
    if (leaderSid != 5)
        qb.s5.start();
    else
        leader = qb.s5;
    try {
        leader.start();
        Assert.assertTrue(false);
    } catch (RuntimeException re) {
        LOG.info("Got an error: expected", re);
    }
    //wait for servers to be up
    String[] list = qb.hostPort.split(",");
    for (int i = 0; i < 5; i++) {
        if (leaderSid != (i + 1)) {
            String hp = list[i];
            Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT));
            LOG.info("{} is accepting client connections", hp);
        } else {
            LOG.info("Skipping the leader");
        }
    }
    zk = qb.createClient();
    SyncRequestProcessor.setSnapCount(100);
    for (int i = 2000; i < 4000; i++) {
        zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null);
    }
    zk.close();
    if (leaderSid != 1)
        QuorumBase.shutdown(qb.s1);
    if (leaderSid != 2)
        QuorumBase.shutdown(qb.s2);
    if (leaderSid != 3)
        QuorumBase.shutdown(qb.s3);
    if (leaderSid != 4)
        QuorumBase.shutdown(qb.s4);
    if (leaderSid != 5)
        QuorumBase.shutdown(qb.s5);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.Test)

Example 18 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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)

Example 19 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer 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 20 with QuorumPeer

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

the class QuorumMajorityTest method testMajQuorums.

/***************************************************************/
/* Test that the majority quorum verifier only counts votes from */
/* followers in its view                                    */
/***************************************************************/
@Test
public void testMajQuorums() throws Throwable {
    LOG.info("Verify QuorumPeer#electionTimeTaken jmx bean attribute");
    ArrayList<QuorumPeer> peers = getPeerList();
    for (int i = 1; i <= peers.size(); i++) {
        QuorumPeer qp = peers.get(i - 1);
        Long electionTimeTaken = -1L;
        String bean = "";
        if (qp.getPeerState() == ServerState.FOLLOWING) {
            bean = String.format("%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Follower", CommonNames.DOMAIN, i, i);
        } else if (qp.getPeerState() == ServerState.LEADING) {
            bean = String.format("%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Leader", CommonNames.DOMAIN, i, i);
        }
        electionTimeTaken = (Long) JMXEnv.ensureBeanAttribute(bean, "ElectionTimeTaken");
        Assert.assertTrue("Wrong electionTimeTaken value!", electionTimeTaken >= 0);
    }
    //setup servers 1-5 to be followers
    setUp(false);
    Proposal p = new Proposal();
    p.addQuorumVerifier(s1.getQuorumVerifier());
    // 2 followers out of 5 is not a majority
    p.addAck(Long.valueOf(1));
    p.addAck(Long.valueOf(2));
    Assert.assertEquals(false, p.hasAllQuorums());
    // 6 is not in the view - its vote shouldn't count
    p.addAck(Long.valueOf(6));
    Assert.assertEquals(false, p.hasAllQuorums());
    // 3 followers out of 5 are a majority of the voting view
    p.addAck(Long.valueOf(3));
    Assert.assertEquals(true, p.hasAllQuorums());
    //setup servers 1-3 to be followers and 4 and 5 to be observers
    setUp(true);
    p = new Proposal();
    p.addQuorumVerifier(s1.getQuorumVerifier());
    // 1 follower out of 3 is not a majority
    p.addAck(Long.valueOf(1));
    Assert.assertEquals(false, p.hasAllQuorums());
    // 4 and 5 are observers, their vote shouldn't count
    p.addAck(Long.valueOf(4));
    p.addAck(Long.valueOf(5));
    Assert.assertEquals(false, p.hasAllQuorums());
    // 6 is not in the view - its vote shouldn't count
    p.addAck(Long.valueOf(6));
    Assert.assertEquals(false, p.hasAllQuorums());
    // 2 followers out of 3 are a majority of the voting view
    p.addAck(Long.valueOf(2));
    Assert.assertEquals(true, p.hasAllQuorums());
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) Test(org.junit.Test)

Aggregations

QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)39 Test (org.junit.Test)24 InetSocketAddress (java.net.InetSocketAddress)19 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)19 QuorumCnxManager (org.apache.zookeeper.server.quorum.QuorumCnxManager)8 ZooKeeper (org.apache.zookeeper.ZooKeeper)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ByteBuffer (java.nio.ByteBuffer)5 File (java.io.File)4 HashMap (java.util.HashMap)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 Stat (org.apache.zookeeper.data.Stat)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Socket (java.net.Socket)2 SocketChannel (java.nio.channels.SocketChannel)2 LinkedHashSet (java.util.LinkedHashSet)2 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)2 KeeperException (org.apache.zookeeper.KeeperException)2 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)2