Example usage for org.apache.zookeeper.server ServerConfig ServerConfig

List of usage examples for org.apache.zookeeper.server ServerConfig ServerConfig

Introduction

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

Prototype

ServerConfig

Source Link

Usage

From source file:blazingcache.TestingZookeeperServerEmbedded.java

License:Apache License

public void start() throws Exception {

    mainsingle = new ZooKeeperServerMain();

    thread = new Thread("zkservermainrunner") {
        @Override/* w  w w . j a va2s . c o m*/
        public void run() {
            try {
                ServerConfig cc = new ServerConfig();
                cc.readFrom(config);
                mainsingle.runFromConfig(cc);
                System.out.println("ZK server died");
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    };
    thread.start();

    this.cnxnFactory = getServerConnectionFactory();
    if (cnxnFactory != null) {
        final ZooKeeperServer zkServer = getZooKeeperServer(cnxnFactory);
        if (zkServer != null) {
            synchronized (zkServer) {
                if (!zkServer.isRunning()) {
                    zkServer.wait();
                }
            }
        }
    }

}

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  a  v  a 2 s  .  co m*/
    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.alibaba.wasp.zookeeper.FQuorumPeer.java

License:Apache License

private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException {
    if (zkConfig.isDistributed()) {
        QuorumPeerMain qp = new QuorumPeerMain();
        qp.runFromConfig(zkConfig);//www .  j a  v  a2 s . c  o  m
    } else {
        ZooKeeperServerMain zk = new ZooKeeperServerMain();
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.readFrom(zkConfig);
        zk.runFromConfig(serverConfig);
    }
}

From source file:com.anhth12.zk.util.LocalZKServer.java

/**
 * Starts Zookeeper./*  w w w  . j  ava  2s .c o  m*/
 *
 * @throws IOException if an error occurs during initialization
 * @throws InterruptedException if an error occurs during initialization
 */
public synchronized void start() throws IOException, InterruptedException {
    log.info("Starting Zookeeper on port {}", port);

    dataDir = Files.createTempDirectory(LocalZKServer.class.getSimpleName());
    dataDir.toFile().deleteOnExit();

    Properties properties = new Properties();
    properties.setProperty("dataDir", dataDir.toAbsolutePath().toString());
    properties.setProperty("clientPort", Integer.toString(port));
    log.info("ZK config: {}", properties);

    QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
    try {
        quorumConfig.parseProperties(properties);
    } catch (QuorumPeerConfig.ConfigException e) {
        throw new IllegalArgumentException(e);
    }

    purgeManager = new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(),
            quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval());
    purgeManager.start();

    ServerConfig serverConfig = new ServerConfig();
    serverConfig.readFrom(quorumConfig);

    zkServer = new ZooKeeperServer();
    zkServer.setTickTime(serverConfig.getTickTime());
    zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());

    // These two ServerConfig methods returned String in 3.4.x and File in 3.5.x
    transactionLog = new FileTxnSnapLog(new File(serverConfig.getDataLogDir().toString()),
            new File(serverConfig.getDataDir().toString()));
    zkServer.setTxnLogFactory(transactionLog);

    connectionFactory = ServerCnxnFactory.createFactory();
    connectionFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());
    connectionFactory.startup(zkServer);
}

From source file:com.cloudera.flume.master.ZKInProcessServer.java

License:Apache License

/**
 * If block is set, wait until the server comes up
 *//*from w  w w .j  a v a2  s .c  om*/
protected void createInstanceFromConfig(boolean block) throws IOException, InterruptedException {
    final ServerConfig serverConfig = new ServerConfig();
    if (standalone) {
        zkServerMain = new FlumeZKServerMain();
    } else {
        quorumPeer = new FlumeZKQuorumPeerMain();
    }
    new Thread() {
        public void run() {
            if (standalone) {
                this.setName("ZooKeeper standalone thread");

                LOG.info("Starting ZooKeeper server");
                serverConfig.readFrom(config);
                try {
                    zkServerMain.runFromConfig(serverConfig);
                } catch (IOException e) {
                    LOG.error("Couldn't start ZooKeeper server!", e);
                }
            } else {
                this.setName("ZooKeeper thread");
                try {
                    LOG.info("Starting ZooKeeper server");
                    quorumPeer.runFromConfig(config);
                } catch (IOException e) {
                    LOG.error("Couldn't start ZooKeeper server!", e);
                }
            }
        }
    }.start();

    if (block && !waitForServerUp("0.0.0.0", config.getClientPortAddress().getPort(), 15000)) {
        throw new IOException("ZooKeeper server did not come up within 15 seconds");
    }
}

From source file:com.cloudera.oryx.kafka.util.LocalZKServer.java

License:Open Source License

/**
 * Starts Zookeeper.//w w  w.  jav a 2  s.  com
 *
 * @throws IOException if an error occurs during initialization
 * @throws InterruptedException if an error occurs during initialization
 */
public synchronized void start() throws IOException, InterruptedException {
    log.info("Starting Zookeeper on port {}", port);

    dataDir = Files.createTempDirectory(LocalZKServer.class.getSimpleName());
    dataDir.toFile().deleteOnExit();

    QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
    try {
        quorumConfig.parseProperties(
                ConfigUtils.keyValueToProperties("dataDir", dataDir.toAbsolutePath(), "clientPort", port));
    } catch (QuorumPeerConfig.ConfigException e) {
        throw new IllegalArgumentException(e);
    }

    purgeManager = new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(),
            quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval());
    purgeManager.start();

    ServerConfig serverConfig = new ServerConfig();
    serverConfig.readFrom(quorumConfig);

    zkServer = new ZooKeeperServer();
    zkServer.setTickTime(serverConfig.getTickTime());
    zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());

    // These two ServerConfig methods returned String in 3.4.x and File in 3.5.x
    transactionLog = new FileTxnSnapLog(new File(serverConfig.getDataLogDir().toString()),
            new File(serverConfig.getDataDir().toString()));
    zkServer.setTxnLogFactory(transactionLog);

    connectionFactory = ServerCnxnFactory.createFactory();
    connectionFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());
    connectionFactory.startup(zkServer);
}

From source file:com.comcast.viper.flume2storm.zookeeper.ZkTestServer.java

License:Apache License

/**
 * Starts the test ZooKeeper server/*from  w w w.j  a  v  a  2 s.c om*/
 */
public void start() {
    if (started) {
        LOG.debug("Already started");
        return;
    }
    try {
        LOG.debug("Starting...");
        ServerConfig config = new ServerConfig();
        config.parse(new String[] { port.toString(), ZK_DIR.getCanonicalPath(), ticktime.toString() });

        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);
        started = true;
        LOG.info("Started, {}", getConnectString());
    } catch (Exception e) {
        LOG.error("Failed to start: " + e.getMessage(), e);
    }
}

From source file:com.googlecode.jmxtrans.model.output.kafka.EmbeddedZookeeper.java

License:Open Source License

@Override
public void before() throws Exception {
    LOGGER.info("Starting Zookeeper");
    Properties properties = getResourceAsProperties("zookeeper.properties");
    dataDir = temporaryFolder.newFolder("zookeeper");
    properties.setProperty("dataDir", dataDir.getAbsolutePath());
    QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
    try {/*from w w w  . j  a v a2  s. co m*/
        quorumConfiguration.parseProperties(properties);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    final ServerConfig configuration = new ServerConfig();
    configuration.readFrom(quorumConfiguration);
    // Start Zookeeper in separate thread
    executor.execute(new Runnable() {
        @Override
        public void run() {
            runServer(configuration);
        }
    });
    // Wait for Zookeeper to be started
    await().atMost(5, TimeUnit.SECONDS).until(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            return getServerCnxnFactory() != null;
        }
    });
}

From source file:com.greplin.zookeeper.EmbeddedZookeeperServer.java

License:Apache License

public EmbeddedZookeeperServer(Integer clientPort, File dataDir, Long tickTime)
        throws QuorumPeerConfig.ConfigException, IOException {
    Preconditions.checkNotNull(dataDir);
    Preconditions.checkNotNull(clientPort);
    Preconditions.checkNotNull(tickTime);

    Preconditions.checkArgument(clientPort > 0);
    Preconditions.checkArgument(clientPort < 65536);
    Preconditions.checkArgument(tickTime > 0);

    this.shutdown = new AtomicBoolean(false);
    this.clientPort = clientPort;
    this.dataDir = dataDir;
    this.tickTime = tickTime;

    Properties properties = new Properties();
    properties.setProperty("tickTime", tickTime.toString());
    properties.setProperty("clientPort", clientPort.toString());
    properties.setProperty("dataDir", dataDir.getAbsolutePath());

    QuorumPeerConfig qpc = new QuorumPeerConfig();
    try {// w  ww  .  j a  va  2  s. c  om
        qpc.parseProperties(properties);
    } catch (IOException e) {
        throw new RuntimeException(
                "This is impossible - no I/O to configure a quorumpeer from a properties object", e);
    }

    // don't ask me why ...
    ServerConfig config = new ServerConfig();
    config.readFrom(qpc);

    log.info("Starting embedded zookeeper server on port " + clientPort);
    this.zooKeeperServer = new ZooKeeperServer();
    this.zooKeeperServer.setTxnLogFactory(
            new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
    this.zooKeeperServer.setTickTime(config.getTickTime());
    this.zooKeeperServer.setMinSessionTimeout(config.getMinSessionTimeout());
    this.zooKeeperServer.setMaxSessionTimeout(config.getMaxSessionTimeout());

    this.connectionFactory = new NIOServerCnxn.Factory(config.getClientPortAddress(),
            config.getMaxClientCnxns());
    try {
        connectionFactory.startup(zooKeeperServer);
    } catch (InterruptedException e) {
        throw new RuntimeException("Server Interrupted", e);
    }

    serverThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                connectionFactory.join();
            } catch (InterruptedException e) {
                log.error("Zookeeper Connection Factory Interrupted", e);
            }
        }
    });

    serverThread.start();
}

From source file:com.griddynamics.jagger.ZooKeeperServer.java

License:Open Source License

@Override
public void run() {
    log.info("Starting ZooKeeper...");
    QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
    try {/*w  ww.jav  a 2  s  .co m*/
        quorumConfiguration.parseProperties(startupProperties);
    } catch (Exception e) {
        throw new TechnicalException(e);
    }

    zooKeeperServer = new ZooKeeperServerWrapper();
    final ServerConfig configuration = new ServerConfig();
    configuration.readFrom(quorumConfiguration);

    new Thread() {
        public void run() {
            try {
                zooKeeperServer.runFromConfig(configuration);
            } catch (IOException e) {
                log.error("ZooKeeper Failed", e);
            }
        }
    }.start();
}