java类org.apache.zookeeper.ZooKeeper.States的实例源码

QuorumPeerMainTest.java 文件源码 项目:fuck_zookeeper 阅读 26 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
        throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
QuorumPeerMainTest.java 文件源码 项目:fuck_zookeeper 阅读 37 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
ReadOnlyModeTest.java 文件源码 项目:fuck_zookeeper 阅读 37 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
QuorumPeerMainTest.java 文件源码 项目:https-github.com-apache-zookeeper 阅读 35 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = ClientBase.CONNECTION_TIMEOUT / 1000;
    boolean someoneNotConnected = true;
    while (someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
            throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for (ZooKeeper zk : zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
                break;
            }
        }
        Thread.sleep(1000);
    }
}
QuorumPeerMainTest.java 文件源码 项目:https-github.com-apache-zookeeper 阅读 35 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+";"+clientPorts[i]+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
ClientRetryTest.java 文件源码 项目:https-github.com-apache-zookeeper 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            zk.close();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
QuorumPeerMainTest.java 文件源码 项目:ZooKeeper 阅读 34 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
        throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
QuorumPeerMainTest.java 文件源码 项目:ZooKeeper 阅读 33 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
ClientRetry.java 文件源码 项目:ZooKeeper 阅读 33 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
ReadOnlyModeTest.java 文件源码 项目:ZooKeeper 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
QuorumPeerMainTest.java 文件源码 项目:StreamProcessingInfrastructure 阅读 30 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
ClientRetry.java 文件源码 项目:StreamProcessingInfrastructure 阅读 36 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
QuorumPeerMainTest.java 文件源码 项目:SecureKeeper 阅读 33 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = ClientBase.CONNECTION_TIMEOUT / 1000;
    boolean someoneNotConnected = true;
    while (someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
            throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for (ZooKeeper zk : zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
                break;
            }
        }
        Thread.sleep(1000);
    }
}
ReadOnlyModeTest.java 文件源码 项目:SecureKeeper 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
QuorumPeerMainTest.java 文件源码 项目:bigstreams 阅读 35 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
        throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
ZookeeperConnectionManagerTest.java 文件源码 项目:Camel 阅读 25 收藏 0 点赞 0 评论 0
@Test
public void shouldWaitForConnection() {
    ZooKeeperConfiguration config = new ZooKeeperConfiguration();
    config.addZookeeperServer("localhost:" + getServerPort());

    ZooKeeperComponent component = new ZooKeeperComponent(config);
    component.setConfiguration(config);
    component.setCamelContext(context);

    ZooKeeperEndpoint zep = new ZooKeeperEndpoint("zookeeper:someserver/this/is/a/path", component, config);

    ZooKeeperConnectionManager zkcm = new ZooKeeperConnectionManager(zep);
    ZooKeeper zk = zkcm.getConnection();
    zk.getState();
    assertEquals(States.CONNECTED, zk.getState());
}
TruncateCorruptionTest.java 文件源码 项目:bigstreams 阅读 33 收藏 0 点赞 0 评论 0
/**
 * @param zk
 * @throws InterruptedException
 */
private void waitForConnection(final ZooKeeper zk)
        throws InterruptedException {
    Assert.assertTrue(await(new Check() {

        public boolean doCheck() {
            if (zk.getState() == States.CONNECTED) {
                List<String> children;
                try {
                    children = zk.getChildren("/", false);

                    return children.size() != 0;
                } catch (Exception e) {
                    // silently fail
                }
            }
            return false;
        }
    }, TimeUnit.MINUTES.toMillis(2)));
}
ClientRetry.java 文件源码 项目:bigstreams 阅读 35 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
ReadOnlyModeTest.java 文件源码 项目:bigstreams 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
ClientCnxn.java 文件源码 项目:bigstreams 阅读 38 收藏 0 点赞 0 评论 0
private void startConnect() throws IOException {
    if(!isFirstConnect){
        try {
            Thread.sleep(r.nextInt(1000));
        } catch (InterruptedException e) {
            LOG.warn("Unexpected exception", e);
        }
    }
    state = States.CONNECTING;

    InetSocketAddress addr;
    if (rwServerAddress != null) {
        addr = rwServerAddress;
        rwServerAddress = null;
    } else {
        addr = hostProvider.next(1000);
    }

    LOG.info("Opening socket connection to server " + addr);

    setName(getName().replaceAll("\\(.*\\)",
            "(" + addr.getHostName() + ":" + addr.getPort() + ")"));

    clientCnxnSocket.connect(addr);
}
QuorumPeerMainTest.java 文件源码 项目:bigstreams 阅读 26 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
ClientRetry.java 文件源码 项目:bigstreams 阅读 28 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
ReadOnlyModeTest.java 文件源码 项目:bigstreams 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
QuorumPeerMainTest.java 文件源码 项目:zookeeper 阅读 40 收藏 0 点赞 0 评论 0
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
        throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
QuorumPeerMainTest.java 文件源码 项目:zookeeper 阅读 34 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
ClientRetry.java 文件源码 项目:zookeeper 阅读 45 收藏 0 点赞 0 评论 0
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
        cdw1.waitForConnected(CONNECTION_TIMEOUT);
        ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
        try {
            States s1 = zk.getState();
            States s2 = zk2.getState();
            Assert.assertSame(s1,States.CONNECTED);
            Assert.assertSame(s2,States.CONNECTING);
            cdw1.reset();
            cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
            cdw2.waitForConnected(CONNECTION_TIMEOUT);
            Assert.assertSame(zk2.getState(),States.CONNECTED);
        } finally {
            zk2.close();
        }
    } finally {
        zk.close();
    }
}
ReadOnlyModeTest.java 文件源码 项目:zookeeper 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
    qu.shutdown(2);

    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
            watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
            .getState());
    long fakeId = zk.getSessionId();

    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
            "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertFalse("fake session and real session have same id", zk
            .getSessionId() == fakeId);

    zk.close();
}
AdminResource.java 文件源码 项目:incubator-pulsar 阅读 39 收藏 0 点赞 0 评论 0
/**
 * Checks whether the broker is allowed to do read-write operations based on the existence of a node in global
 * zookeeper.
 *
 * @throws WebApplicationException
 *             if broker has a read only access if broker is not connected to the global zookeeper
 */
public void validatePoliciesReadOnlyAccess() {
    boolean arePoliciesReadOnly = true;

    try {
        arePoliciesReadOnly = globalZkCache().exists(POLICIES_READONLY_FLAG_PATH);
    } catch (Exception e) {
        log.warn("Unable to fetch contents of [{}] from global zookeeper", POLICIES_READONLY_FLAG_PATH, e);
        throw new RestException(e);
    }

    if (arePoliciesReadOnly) {
        log.debug("Policies are read-only. Broker cannot do read-write operations");
        throw new RestException(Status.FORBIDDEN, "Broker is forbidden to do read-write operations");
    } else {
        // Make sure the broker is connected to the global zookeeper before writing. If not, throw an exception.
        if (globalZkCache().getZooKeeper().getState() != States.CONNECTED) {
            log.debug("Broker is not connected to the global zookeeper");
            throw new RestException(Status.PRECONDITION_FAILED,
                    "Broker needs to be connected to global zookeeper before making a read-write operation");
        } else {
            // Do nothing, just log the message.
            log.debug("Broker is allowed to make read-write operations");
        }
    }
}
MessagingServiceShutdownHook.java 文件源码 项目:incubator-pulsar 阅读 31 收藏 0 点赞 0 评论 0
@Override
public void shutdown(int exitCode) {
    try {
        // Try to close ZK session to ensure all ephemeral locks gets released immediately
        if (service != null) {
            if (service.getZkClient().getState() != States.CLOSED) {
                service.getZkClient().close();
            }
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage(), e);
    }

    LOG.info("Invoking Runtime.halt({})", exitCode);
    immediateFlushBufferedLogs();
    Runtime.getRuntime().halt(exitCode);

}
QuorumPeerMainTest.java 文件源码 项目:SecureKeeper 阅读 38 收藏 0 点赞 0 评论 0
/**
 * This is a helper function for launching a set of servers
 *
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+";"+clientPorts[i]+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}


问题


面经


文章

微信
公众号

扫码关注公众号