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

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

Introduction

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

Prototype

public File getDataDir() 

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);// w  ww .  j  a  v  a  2 s . 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.anhth12.zk.util.LocalZKServer.java

/**
 * Starts Zookeeper./*from w w w . ja  v  a2s.  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.oryx.kafka.util.LocalZKServer.java

License:Open Source License

/**
 * Starts Zookeeper.//from  w  w w . ja va 2  s. 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.comcast.viper.flume2storm.zookeeper.ZkTestServer.java

License:Apache License

/**
 * Starts the test ZooKeeper server/*from   w w  w  .  j  av  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.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 w  w. 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.heliosapm.streams.TestZooKeeperServer.java

License:Apache License

/**
 * Run from a ServerConfig./*from   ww  w  .ja va2s .  c  o m*/
 * @param config ServerConfig to use.
 * @throws IOException on any error
 */
public void runFromConfig(ServerConfig config) throws IOException {
    LOG.info(">>>>> Starting Test ZooKeep Server...");
    FileTxnSnapLog txnLog = null;
    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
        zkServer = new ZooKeeperServer();

        txnLog = new FileTxnSnapLog(new File(config.getDataLogDir()), 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);
        LOG.info("<<<<< Test ZooKeep Server Started.");
        //            cnxnFactory.join();
        //            if (zkServer.isRunning()) {
        //                zkServer.shutdown();
        //            }            
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Server interrupted", e);
    } finally {
        //            if (txnLog != null) {
        //                txnLog.close();
        //            }
    }
}

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

License:Apache License

private void runFromConfig(ServerConfig config) throws IOException {
    zkServer = new ZooKeeperServer();
    try {//from   w ww . j a  va2 s . c o  m

        txnLog = new FileTxnSnapLog(new File(config.getDataLogDir()), 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);
    } catch (InterruptedException e) {
        if (zkServer.isRunning()) {
            zkServer.shutdown();
        }
    }
}

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

License:Apache License

/**
 * Starts ZooKeeper server instance./*w w w  . 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./*  ww  w .j a va2 s  . c  o  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());
    }/* w  w  w .j  av a  2s  . com*/

    //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;
    }
}