Search in sources :

Example 11 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project wildfly-camel by wildfly-extras.

the class ZKServerFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    if (purge) {
        deleteFilesInDir(getDataLogDir());
        deleteFilesInDir(getDataDir());
    }
    FileTxnSnapLog ftxn = new FileTxnSnapLog(getDataLogDir(), getDataDir());
    zooKeeperServer.setTxnLogFactory(ftxn);
    zooKeeperServer.setTickTime(getTickTime());
    zooKeeperServer.setMinSessionTimeout(getMinSessionTimeout());
    zooKeeperServer.setMaxSessionTimeout(getMaxSessionTimeout());
    connectionFactory = new NIOServerCnxnFactory() {

        @Override
        protected void configureSaslLogin() throws IOException {
        // do nothing
        }
    };
    connectionFactory.configure(getClientPortAddress(), getMaxClientConnections());
    connectionFactory.startup(zooKeeperServer);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 12 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project incubator-pulsar by apache.

the class ZooKeeperUtil method startServer.

public void startServer() throws Exception {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();
    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);
    boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);
    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    zkc = ZooKeeperClient.newBuilder().connectString(getZooKeeperConnectString()).build();
    // initialize the zk client with values
    zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 13 with NIOServerCnxnFactory

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

the class MiniZooKeeperCluster method shutdown.

/**
 * @throws IOException
 */
public void shutdown() throws IOException {
    if (!started) {
        return;
    }
    // shut down all the zk servers
    for (int i = 0; i < standaloneServerFactoryList.size(); i++) {
        NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(i);
        int clientPort = clientPortList.get(i);
        standaloneServerFactory.shutdown();
        if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
            throw new IOException("Waiting for shutdown of standalone server");
        }
    }
    // clear everything
    started = false;
    activeZKServerIndex = 0;
    standaloneServerFactoryList.clear();
    clientPortList.clear();
    zooKeeperServers.clear();
    LOG.info("Shutdown MiniZK cluster with all ZK servers");
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException)

Example 14 with NIOServerCnxnFactory

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

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) {
                LOG.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) {
                LOG.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);
    LOG.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 15 with NIOServerCnxnFactory

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

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);
    LOG.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);
    LOG.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)

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