zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvfangmin <...@git.apache.org>
Subject [GitHub] zookeeper pull request #659: ZOOKEEPER-3161. Refactor QuorumPeerMainTest.jav...
Date Tue, 09 Oct 2018 05:39:25 GMT
Github user lvfangmin commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/659#discussion_r223564880
  
    --- Diff: zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java
---
    @@ -402,4 +421,129 @@ public File getConfFile() {
             }
     
         }
    +
    +    // This class holds the servers and clients for those servers
    +    protected static class Servers {
    +        MainThread mt[];
    +        ZooKeeper zk[];
    +        int[] clientPorts;
    +
    +        public void shutDownAllServers() throws InterruptedException {
    +            for (MainThread t: mt) {
    +                t.shutdown();
    +            }
    +        }
    +
    +        public void restartAllServersAndClients(Watcher watcher) throws IOException,
InterruptedException {
    +            for (MainThread t : mt) {
    +                if (!t.isAlive()) {
    +                    t.start();
    +                }
    +            }
    +            for (int i = 0; i < zk.length; i++) {
    +                restartClient(i, watcher);
    +            }
    +        }
    +
    +        public void restartClient(int clientIndex, Watcher watcher) throws IOException,
InterruptedException {
    +            if (zk[clientIndex] != null) {
    +                zk[clientIndex].close();
    +            }
    +            zk[clientIndex] = new ZooKeeper("127.0.0.1:" + clientPorts[clientIndex],
ClientBase.CONNECTION_TIMEOUT, watcher);
    +        }
    +
    +        public int findLeader() {
    +            for (int i = 0; i < mt.length; i++) {
    +                if (mt[i].main.quorumPeer.leader != null) {
    +                    return i;
    +                }
    +            }
    +            return -1;
    +        }
    +    }
    +
    +    protected Servers LaunchServers(int numServers) throws IOException, InterruptedException
{
    +        return LaunchServers(numServers, null);
    +    }
    +
    +    /** * This is a helper function for launching a set of servers
    +     *
    +     * @param numServers the number of servers
    +     * @param tickTime A ticktime to pass to MainThread
    +     * @return
    +     * @throws IOException
    +     * @throws InterruptedException
    +     */
    +    protected Servers LaunchServers(int numServers, Integer tickTime) throws IOException,
InterruptedException {
    +        int SERVER_COUNT = numServers;
    +        QuorumPeerMainTest.Servers svrs = new QuorumPeerMainTest.Servers();
    +        svrs.clientPorts = new int[SERVER_COUNT];
    +        StringBuilder sb = new StringBuilder();
    +        for(int i = 0; i < SERVER_COUNT; i++) {
    +            svrs.clientPorts[i] = PortAssignment.unique();
    +            sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+";"+svrs.clientPorts[i]+"\n");
    +        }
    +        String quorumCfgSection = sb.toString();
    +
    +        svrs.mt = new MainThread[SERVER_COUNT];
    +        svrs.zk = new ZooKeeper[SERVER_COUNT];
    +        for(int i = 0; i < SERVER_COUNT; i++) {
    +            if (tickTime != null) {
    +                svrs.mt[i] = new MainThread(i, svrs.clientPorts[i], quorumCfgSection,
new HashMap<String, String>(), tickTime);
    +            } else {
    +                svrs.mt[i] = new MainThread(i, svrs.clientPorts[i], quorumCfgSection);
    +            }
    +            svrs.mt[i].start();
    +            svrs.restartClient(i, this);
    +        }
    +
    +        waitForAll(svrs, ZooKeeper.States.CONNECTED);
    +
    +        return svrs;
    +    }
    +
    +    public static void waitForOne(ZooKeeper zk, ZooKeeper.States state) throws InterruptedException
{
    +        int iterations = ClientBase.CONNECTION_TIMEOUT / 500;
    +        while (zk.getState() != state) {
    +            if (iterations-- == 0) {
    +                throw new RuntimeException("Waiting too long " + zk.getState() + " !=
" + state);
    +            }
    +            Thread.sleep(500);
    +        }
    +    }
    +
    +    protected void waitForAll(Servers servers, ZooKeeper.States state) throws InterruptedException
{
    --- End diff --
    
    Maybe change this to public as well?


---

Mime
View raw message