Example usage for org.apache.zookeeper.server ZooKeeperServer setMaxSessionTimeout

List of usage examples for org.apache.zookeeper.server ZooKeeperServer setMaxSessionTimeout

Introduction

In this page you can find the example usage for org.apache.zookeeper.server ZooKeeperServer setMaxSessionTimeout.

Prototype

public void setMaxSessionTimeout(int max) 

Source Link

Usage

From source file:co.paralleluniverse.galaxy.test.GalaxyTestingUtils.java

License:Open Source License

public static ServerCnxnFactory startZookeeper(final String configResource, final String dataDirName)
        throws IOException, QuorumPeerConfig.ConfigException {
    ServerConfig sc = new ServerConfig();
    sc.parse(pathToResource(configResource));
    deleteDir(dataDirName);//from   w  w w  . j av  a 2s .  com
    new File(dataDirName).mkdirs();
    FileTxnSnapLog txnLog = null;
    try {
        ZooKeeperServer zkServer = new ZooKeeperServer();

        txnLog = new FileTxnSnapLog(new File(sc.getDataDir()), new File(sc.getDataDir()));
        zkServer.setTxnLogFactory(txnLog);
        zkServer.setTickTime(sc.getTickTime());
        zkServer.setMinSessionTimeout(sc.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(sc.getMaxSessionTimeout());
        ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(sc.getClientPortAddress(), sc.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);
        return cnxnFactory;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        if (txnLog != null) {
            txnLog.close();
        }
    }
}

From source file:com.huawei.streaming.cql.LocalTaskCommons.java

License:Apache License

/**
 * Run from a ServerConfig./*  w w  w. j  a  v a 2s  .c o  m*/
 *
 */
private static void runFromConfig(ServerConfig config) throws IOException {
    LOG.info("Starting server");
    String newDataDir = LocalTaskCommons.setDir(ZKDATA);
    String newLogDir = LocalTaskCommons.setDir(ZKLOG);

    try {
        removeZKTmpDir(newDataDir, newLogDir);
        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(newLogDir), new File(newDataDir));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(config.getTickTime());
        zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
        cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Server interrupted", e);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.inputs.KafkaStream.java

License:Apache License

/**
 * Starts ZooKeeper server instance.//from   w  ww . j av a 2  s.co m
 *
 * @throws Exception
 *             if an error occurred wile starting ZooKeeper server
 */
protected void startZooKeeper() throws Exception {
    logger().log(OpLevel.DEBUG, StreamsResources.getString(KafkaStreamConstants.RESOURCE_BUNDLE_NAME,
            "KafkaStream.zookeeper.server.starting"));

    // ZooKeeperServerMain.main();

    ServerConfig sc = new ServerConfig();
    sc.parse(System.getProperty(ZK_PROP_FILE_KEY));

    ZooKeeperServer zkServer = new ZooKeeperServer();
    zLog = new FileTxnSnapLog(new File(sc.getDataLogDir()), new File(sc.getDataDir()));
    zkServer.setTxnLogFactory(zLog);
    zkServer.setTickTime(sc.getTickTime());
    zkServer.setMinSessionTimeout(sc.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(sc.getMaxSessionTimeout());
    zkCnxnFactory = ServerCnxnFactory.createFactory(sc.getClientPortAddress(), sc.getMaxClientCnxns());
    zkCnxnFactory.startup(zkServer);

    logger().log(OpLevel.DEBUG, StreamsResources.getString(KafkaStreamConstants.RESOURCE_BUNDLE_NAME,
            "KafkaStream.zookeeper.server.started"));
}

From source file:com.sodec.embeddedZk.ZooKeeperServerMain.java

License:Apache License

/**
 * Run from a ServerConfig.//from w w w  . jav a2s  .co  m
 * @param config ServerConfig to use.
 * @throws IOException
 */
public void runFromConfig(ServerConfig config) throws IOException {
    LOG.info("Starting server");
    try {
        // Note that this thread isn't going to be doing anything else,
        // so rather than spawning another thread, we will just call
        // run() in this thread.
        // create a file logger url from the command line args
        ZooKeeperServer zkServer = new ZooKeeperServer();

        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config.getDataLogDir()),
                new File(config.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(config.getTickTime());
        zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
        cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);

        //         cnxnFactory.join();

        //            if (zkServer.isRunning()) {
        //                zkServer.shutdown();
        //            }
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Server interrupted", e);
    }
}

From source file:io.fabric8.core.zookeeper.FabricZooKeeperServer.java

License:Apache License

private Destroyable activateInternal(Map<String, ?> configuration) throws Exception {
    LOGGER.info("Creating zookeeper server with: {}", configuration);

    Properties props = new Properties();
    for (Map.Entry<String, ?> entry : configuration.entrySet()) {
        props.put(entry.getKey(), entry.getValue());
    }/*from ww  w. j  a  v a  2  s  .  c  om*/

    //Check required directories exist or create them.
    if (!dataDir.exists() && !dataDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataDir at: " + dataDir.getAbsolutePath());
    }

    if (!dataLogDir.exists() && !dataLogDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataLogDir at: " + dataLogDir.getAbsolutePath());
    }

    // Create myid file
    String serverId = (String) props.get(SERVER_ID);
    if (serverId != null) {
        props.remove(SERVER_ID);
        File myId = new File(dataDir, MY_ID);
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null
                || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }

    QuorumPeerConfig peerConfig = getPeerConfig(props);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        // Register stats provider
        ClusteredServer server = new ClusteredServer(quorumPeer);
        return server;
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        cnxnFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);

        return server;
    }
}

From source file:io.fabric8.groups.GroupTest.java

License:Apache License

private NIOServerCnxnFactory startZooKeeper(int port) throws Exception {
    ServerConfig cfg = new ServerConfig();
    cfg.parse(new String[] { Integer.toString(port), "target/zk/data" });

    ZooKeeperServer zkServer = new ZooKeeperServer();
    FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(cfg.getDataLogDir()), new File(cfg.getDataDir()));
    zkServer.setTxnLogFactory(ftxn);//  ww  w. j a v  a  2 s  .co  m
    zkServer.setTickTime(cfg.getTickTime());
    zkServer.setMinSessionTimeout(cfg.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(cfg.getMaxSessionTimeout());
    NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
    cnxnFactory.configure(cfg.getClientPortAddress(), cfg.getMaxClientCnxns());
    cnxnFactory.startup(zkServer);
    return cnxnFactory;
}

From source file:io.fabric8.process.spring.boot.registry.ZooKeeperProcessRegistries.java

License:Apache License

public static NIOServerCnxnFactory zooKeeperServer(int port) {
    try {/*from  ww  w  .  j  av a 2 s .c  o m*/
        ServerConfig cfg = new ServerConfig();
        String zkData = "target/zk/data/" + randomUUID();
        cfg.parse(new String[] { Integer.toString(port), zkData });

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(cfg.getDataLogDir()), new File(cfg.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(cfg.getTickTime());
        zkServer.setMinSessionTimeout(cfg.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(cfg.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(cfg.getClientPortAddress(), cfg.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);
        return cnxnFactory;
    } catch (InterruptedException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.fabric8.zookeeper.bootstrap.ZooKeeperServerFactory.java

License:Apache License

public ZooKeeperServerFactory(QuorumPeerConfig peerConfig, String serverId)
        throws IOException, InterruptedException {
    this.peerConfig = peerConfig;
    this.serverId = serverId;

    LOGGER.info("Creating zookeeper server with: {}", peerConfig);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(/*w ww .ja v a 2  s  . co m*/
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        // Register stats provider
        this.clusteredServer = new ClusteredServer(quorumPeer);
        /*
                    registration = context.registerService(QuorumStats.Provider.class, server, null);
                
        */
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        InetSocketAddress clientPortAddress = serverConfig.getClientPortAddress();
        cnxnFactory.configure(clientPortAddress, serverConfig.getMaxClientCnxns());
        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        this.simplerServer = new SimpleServer(zkServer, cnxnFactory);
        /*
                    registration = context.registerService(ServerStats.Provider.class, server, null);
        */
    }
}

From source file:org.apache.airavata.common.utils.AiravataZKUtils.java

License:Apache License

public static void runZKFromConfig(ServerConfig config, ServerCnxnFactory cnxnFactory) throws IOException {
    AiravataZKUtils.logger.info("Starting Zookeeper server...");
    FileTxnSnapLog txnLog = null;//from  ww w. j  a  v a2  s.com
    try {
        // Note that this thread isn't going to be doing anything else,
        // so rather than spawning another thread, we will just call
        // run() in this thread.
        // create a file logger url from the command line args
        ZooKeeperServer zkServer = new ZooKeeperServer();

        txnLog = new FileTxnSnapLog(new File(config.getDataDir()), new File(config.getDataDir()));
        zkServer.setTxnLogFactory(txnLog);
        zkServer.setTickTime(config.getTickTime());
        zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
        cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);
        cnxnFactory.join();
        if (zkServer.isRunning()) {
            zkServer.shutdown();
        }
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        AiravataZKUtils.logger.warn("Server interrupted", e);
        System.exit(1);
    } finally {
        if (txnLog != null) {
            txnLog.close();
        }
    }
}

From source file:org.apache.hive.testutils.MiniZooKeeperCluster.java

License:Apache License

/**
 * @param baseDir/*  w ww  .j  a v a  2  s  . co  m*/
 * @param numZooKeeperServers
 * @return ClientPort server bound to, -1 if there was a
 *         binding problem and we couldn't pick another port.
 * @throws IOException
 * @throws InterruptedException
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }

    setupTestEnv();
    shutdown();

    int tentativePort = -1; // the seed port
    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 {
            tentativePort = selectClientPort(tentativePort); // update the seed
            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(currentClientPort),
                        configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS,
                                HConstants.DEFAULT_ZOOKEPER_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
        standaloneServerFactory.startup(server);
        // Runs a 'stat' against the servers.
        if (!waitForServerUp(currentClientPort, connectionTimeout)) {
            throw new IOException("Waiting for startup of standalone server");
        }

        // 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 successful 'stat' " + "on client port=" + clientPort);
    return clientPort;
}