Search in sources :

Example 16 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project strimzi by strimzi.

the class EmbeddedZooKeeper method start.

private void start(InetSocketAddress addr) throws IOException, InterruptedException {
    factory = new NIOServerCnxnFactory();
    factory.configure(addr, 10);
    factory.startup(zk);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory)

Example 17 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project drill by apache.

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir
 * @param numZooKeeperServers
 * @return ClientPort server bound to.
 * @throws IOException
 * @throws InterruptedException
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv();
    shutdown();
    int tentativePort = selectClientPort();
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        recreateDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(tentativePort), 1000);
            } catch (BindException e) {
                logger.debug("Failed binding ZK Server to client port: {}", tentativePort);
                // This port is already in use, try to use another.
                tentativePort++;
                continue;
            }
            try {
                standaloneServerFactory.startup(server);
            } catch (IOException e) {
                logger.error("Zookeeper startup error", e);
                tentativePort++;
                continue;
            }
            if (!waitForServerUp(server, CONNECTION_TIMEOUT)) {
                server.shutdown();
                server = new ZooKeeperServer(dir, dir, tickTimeToUse);
                tentativePort++;
                continue;
            }
            break;
        }
        // We have selected this port as a client port.
        clientPortList.add(tentativePort);
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    clientPort = clientPortList.get(activeZKServerIndex);
    logger.info("Started MiniZK Cluster and connect 1 ZK server " + "on client port: {}", clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 18 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project drill by apache.

the class MiniZooKeeperCluster method killOneBackupZooKeeperServer.

/**
 * Kill one back up ZK servers
 *
 * @throws IOException
 * @throws InterruptedException
 */
public void killOneBackupZooKeeperServer() throws IOException, InterruptedException {
    if (!started || activeZKServerIndex < 0 || standaloneServerFactoryList.size() <= 1) {
        return;
    }
    int backupZKServerIndex = activeZKServerIndex + 1;
    // Shutdown the current active one
    NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(backupZKServerIndex);
    int clientPort = clientPortList.get(backupZKServerIndex);
    standaloneServerFactory.shutdown();
    if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
        throw new IOException("Waiting for shutdown of standalone server");
    }
    // remove this backup zk server
    standaloneServerFactoryList.remove(backupZKServerIndex);
    clientPortList.remove(backupZKServerIndex);
    zooKeeperServers.remove(backupZKServerIndex);
    logger.info("Kill one backup ZK servers in the cluster " + "on client port: {}", clientPort);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException)

Example 19 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project drill by apache.

the class MiniZooKeeperCluster method killCurrentActiveZooKeeperServer.

/**
 * @return clientPort return clientPort if there is another ZK backup can run
 *         when killing the current active; return -1, if there is no backups.
 * @throws IOException
 * @throws InterruptedException
 */
public int killCurrentActiveZooKeeperServer() throws IOException, InterruptedException {
    if (!started || activeZKServerIndex < 0) {
        return -1;
    }
    // Shutdown the current active one
    NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(activeZKServerIndex);
    int clientPort = clientPortList.get(activeZKServerIndex);
    standaloneServerFactory.shutdown();
    if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
        throw new IOException("Waiting for shutdown of standalone server");
    }
    // remove the current active zk server
    standaloneServerFactoryList.remove(activeZKServerIndex);
    clientPortList.remove(activeZKServerIndex);
    zooKeeperServers.remove(activeZKServerIndex);
    logger.info("Kill the current active ZK servers in the cluster " + "on client port: {}", clientPort);
    if (standaloneServerFactoryList.size() == 0) {
        // there is no backup servers;
        return -1;
    }
    clientPort = clientPortList.get(activeZKServerIndex);
    logger.info("Activate a backup zk server in the cluster " + "on client port: {}", clientPort);
    // return the next back zk server's port
    return clientPort;
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException)

Example 20 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project hbase by apache.

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir             the base directory to use
 * @param numZooKeeperServers the number of ZooKeeper servers
 * @return ClientPort server bound to, -1 if there was a binding problem and we couldn't pick
 *   another port.
 * @throws IOException          if an operation fails during the startup
 * @throws InterruptedException if the startup fails
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv();
    shutdown();
    // the seed port
    int tentativePort = -1;
    int currentClientPort;
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        createDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        // Set up client port - if we have already had a list of valid ports, use it.
        if (hasValidClientPortInList(i)) {
            currentClientPort = clientPortList.get(i);
        } else {
            // update the seed
            tentativePort = selectClientPort(tentativePort);
            currentClientPort = tentativePort;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        // Setting {min,max}SessionTimeout defaults to be the same as in Zookeeper
        server.setMinSessionTimeout(configuration.getInt("hbase.zookeeper.property.minSessionTimeout", -1));
        server.setMaxSessionTimeout(configuration.getInt("hbase.zookeeper.property.maxSessionTimeout", -1));
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(LOOPBACK_HOST, currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, HConstants.DEFAULT_ZOOKEEPER_MAX_CLIENT_CNXNS));
            } catch (BindException e) {
                LOG.debug("Failed binding ZK Server to client port: " + currentClientPort, e);
                // We're told to use some port but it's occupied, fail
                if (hasValidClientPortInList(i)) {
                    return -1;
                }
                // This port is already in use, try to use another.
                tentativePort = selectClientPort(tentativePort);
                currentClientPort = tentativePort;
                continue;
            }
            break;
        }
        // Start up this ZK server. Dump its stats.
        standaloneServerFactory.startup(server);
        LOG.info("Started connectionTimeout={}, dir={}, {}", connectionTimeout, dir, getServerConfigurationOnOneLine(server));
        // Runs a 'stat' against the servers.
        if (!waitForServerUp(currentClientPort, connectionTimeout)) {
            Threads.printThreadInfo(System.out, "Why is zk standalone server not coming up?");
            throw new IOException("Waiting for startup of standalone server; " + "server isRunning=" + server.isRunning());
        }
        // We have selected a port as a client port.  Update clientPortList if necessary.
        if (clientPortList.size() <= i) {
            // it is not in the list, add the port
            clientPortList.add(currentClientPort);
        } else if (clientPortList.get(i) <= 0) {
            // the list has invalid port, update with valid port
            clientPortList.remove(i);
            clientPortList.add(i, currentClientPort);
        }
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    int clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Started MiniZooKeeperCluster and ran 'stat' on client port={}", clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Aggregations

NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)55 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)24 IOException (java.io.IOException)22 InetSocketAddress (java.net.InetSocketAddress)19 File (java.io.File)16 Test (org.junit.Test)13 CuratorFramework (org.apache.curator.framework.CuratorFramework)12 RetryNTimes (org.apache.curator.retry.RetryNTimes)12 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)6 ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)5 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)3 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)3 Field (java.lang.reflect.Field)2 SelectionKey (java.nio.channels.SelectionKey)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 ArrayList (java.util.ArrayList)2