Example usage for org.apache.zookeeper.server.quorum QuorumPeerConfig parseProperties

List of usage examples for org.apache.zookeeper.server.quorum QuorumPeerConfig parseProperties

Introduction

In this page you can find the example usage for org.apache.zookeeper.server.quorum QuorumPeerConfig parseProperties.

Prototype

public void parseProperties(Properties zkProp) throws IOException, ConfigException 

Source Link

Document

Parse config from a Properties.

Usage

From source file:beans.SystemStartable.java

License:Apache License

@Override
public void onStart() throws Throwable {
    // Initialize ZooKeeper.
    Properties properties = new Properties();
    InputStream propertiesStream = getClass().getClassLoader().getResourceAsStream("zoo.cfg");
    properties.load(propertiesStream);/*from   w w w  . jav  a  2  s  .  c o m*/

    QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
    quorumConfig.parseProperties(properties);

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

    zkDirectory = Files.createTempDirectory("zookeeper").toFile();
    LOG.info("Using ZooKeeper directory: " + zkDirectory);
    FileTxnSnapLog txnLog = new FileTxnSnapLog(zkDirectory, zkDirectory);
    zkServer.setTxnLogFactory(txnLog);

    cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(quorumConfig.getClientPortAddress(), quorumConfig.getMaxClientCnxns());
    cnxnFactory.startup(zkServer);
}

From source file:com.alibaba.wasp.zookeeper.FQuorumPeer.java

License:Apache License

/**
 * Parse ZooKeeper configuration from Wasp XML config and run a QuorumPeer.
 * @param args String[] of command line arguments. Not used.
 *///w w  w .j  av  a  2s.  c om
public static void main(String[] args) {
    Configuration conf = WaspConfiguration.create();
    try {
        Properties zkProperties = ZKConfig.makeZKProps(conf);
        writeMyID(conf, zkProperties);
        QuorumPeerConfig zkConfig = new QuorumPeerConfig();
        zkConfig.parseProperties(zkProperties);
        runZKServer(zkConfig);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}

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

/**
 * Starts Zookeeper./*from   w w  w.  j a  v  a 2 s  .c om*/
 *
 * @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.oryx.kafka.util.LocalZKServer.java

License:Open Source License

/**
 * Starts Zookeeper./*from  w w  w  . java2s .  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();

    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.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  ava  2  s.  com
        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 {/*from   w ww .ja v  a  2s .co m*/
        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 {/*from w  w w.j a  v  a2s  . com*/
        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();
}

From source file:com.intropro.prairie.unit.zookeeper.ZookeeperUnit.java

License:Apache License

@Override
protected void init() throws InitUnitException {
    port = PortProvider.nextPort();//from  ww  w.j  a v  a  2 s.c  o m
    LOGGER.info("Port " + port + " reserved for zookeeper");
    zkProperties = new Properties();
    zkProperties.put("clientPort", port);
    zkProperties.put("tickTime", 200);
    zkProperties.setProperty("dataDir", getTmpDir().resolve("data").toString());
    QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
    try {
        quorumConfiguration.parseProperties(zkProperties);
    } catch (IOException | QuorumPeerConfig.ConfigException e) {
        throw new InitUnitException(e);
    }

    final ServerConfig configuration = new ServerConfig();
    configuration.readFrom(quorumConfiguration);
    try {
        runFromConfig(configuration);
    } catch (IOException e) {
        LOGGER.error("Zookeeper failed", e);
    }
    thread = new Thread("zookeeper:" + port) {
        public void run() {
            try {
                cnxnFactory.join();
                if (zkServer.isRunning()) {
                    zkServer.shutdown();
                }
            } catch (InterruptedException e) {
                if (zkServer.isRunning()) {
                    zkServer.shutdown();
                }
            }
        }
    };
    thread.start();
}

From source file:com.nesscomputing.service.discovery.server.zookeeper.ZookeeperModule.java

License:Apache License

private QuorumPeerConfig getQuorumPeerConfig() {
    final Configuration zookeeperConfig = config.getConfiguration("ness.zookeeper");
    final QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
    try {/*from  w  ww.java2s. c o m*/
        quorumPeerConfig.parseProperties(ConfigurationConverter.getProperties(zookeeperConfig));
    } catch (IOException ioe) {
        throw new ProvisionException("while creating the QuorumPeerConfig", ioe);
    } catch (ConfigException ce) {
        throw new ProvisionException("while creating the QuorumPeerConfig", ce);
    }
    return quorumPeerConfig;
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.ZookeeperTestServer.java

License:Apache License

private static TestZookeeperServerIntern startZookeeper(Properties zkConfig) {
    logger.debug("Starting the test zookeeper server on port " + zkConfig.get("clientPort"));

    final TestZookeeperServerIntern server = new TestZookeeperServerIntern();
    try {//w  ww. j a  va 2s .  c  o  m
        QuorumPeerConfig qpConfig = new QuorumPeerConfig();
        qpConfig.parseProperties(zkConfig);
        final ServerConfig sConfig = new ServerConfig();
        sConfig.readFrom(qpConfig);

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    server.runFromConfig(sConfig);
                } catch (IOException ioe) {
                    logger.error(MarkerFactory.getMarker("FATAL"), "", ioe);
                    fail("can't start zookeeper");
                }
            }
        });
        t.start();
        Thread.sleep(2000); // give the server time to start
    } catch (Exception e) {
        logger.error("Can't start zookeeper", e);
        fail("Can't start zookeeper");
    }
    return server;
}