Example usage for org.apache.zookeeper.server ServerCnxnFactory configure

List of usage examples for org.apache.zookeeper.server ServerCnxnFactory configure

Introduction

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

Prototype

public void configure(InetSocketAddress addr, int maxcc) throws IOException 

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 a v  a2  s  . c o 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.chiralBehaviors.autoconfigure.ZookeeperLauncher.java

License:Open Source License

/**
 * Copied from QuorumPeerMain/*w  w w  .  j av a  2s . com*/
 */
protected void runFromConfig(QuorumPeerConfig config) throws IOException {
    LOG.info("Starting quorum peer");
    ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());

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

    quorumPeer.start();
}

From source file:com.glaf.cluster.catalina.session.QuorumPeerMain.java

License:Apache License

public void runFromConfig(QuorumPeerConfig config) throws IOException {
    try {//  w  ww .  j a v a 2  s. co m
        ManagedUtil.registerLog4jMBeans();
    } catch (JMException e) {
        LOG.warn("Unable to register log4j JMX control", e);
    }

    LOG.info("Starting quorum peer");
    try {
        ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());

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

        quorumPeer.start();
        quorumPeer.join();
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Quorum Peer interrupted", e);
    }
}

From source file:com.palantir.curatortestrule.DefaultZooKeeperRuleConfig.java

License:Open Source License

@Override
public ServerCnxnFactory getServer(int port) {
    ZooKeeperServer zkServer = new NoJMXZooKeeperServer();

    FileTxnSnapLog ftxn;//  w w  w .j a v  a2 s.  c om
    try {
        File dataDir = Files.createTempDir();
        File snapDir = Files.createTempDir();
        if (cleanupOnExit) {
            directoriesToCleanup.add(dataDir);
            directoriesToCleanup.add(snapDir);
        }
        ftxn = new FileTxnSnapLog(dataDir, snapDir);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    zkServer.setTxnLogFactory(ftxn);

    try {
        ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(new InetSocketAddress(port), cnxnFactory.getMaxClientCnxnsPerHost());
        cnxnFactory.startup(zkServer);

        return cnxnFactory;
    } catch (IOException | InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.splicemachine.test.SpliceZoo.java

License:Apache License

public SpliceZoo(QuorumPeerConfig config, int number) throws IOException {
    this.config = config;
    this.peer = new QuorumPeer();
    ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
    peer.setClientPortAddress(config.getClientPortAddress());
    peer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
    peer.setQuorumPeers(config.getServers());
    peer.setElectionType(config.getElectionAlg());
    peer.setMyid(config.getServerId());//www.  ja va  2 s  .  c om
    peer.setTickTime(config.getTickTime());
    peer.setMinSessionTimeout(config.getMinSessionTimeout());
    peer.setMaxSessionTimeout(config.getMaxSessionTimeout());
    peer.setInitLimit(config.getInitLimit());
    peer.setSyncLimit(config.getSyncLimit());
    peer.setQuorumVerifier(config.getQuorumVerifier());
    peer.setCnxnFactory(cnxnFactory);
    peer.setZKDatabase(new ZKDatabase(peer.getTxnFactory()));
    peer.setLearnerType(config.getPeerType());
    peer.setMyid(number);
}

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   w  ww. ja v  a 2 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.nifi.atlas.emulator.EmbeddedKafka.java

License:Apache License

/**
 * Will start Zookeeper server via {@link ServerCnxnFactory}
 *//*  w w w  .  j  ava 2s. c  o  m*/
private void startZookeeper() {
    QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
    try {
        quorumConfiguration.parseProperties(this.zookeeperConfig);

        ServerConfig configuration = new ServerConfig();
        configuration.readFrom(quorumConfiguration);

        FileTxnSnapLog txnLog = new FileTxnSnapLog(new File(configuration.getDataLogDir()),
                new File(configuration.getDataDir()));

        zkServer.setTxnLogFactory(txnLog);
        zkServer.setTickTime(configuration.getTickTime());
        zkServer.setMinSessionTimeout(configuration.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(configuration.getMaxSessionTimeout());
        ServerCnxnFactory zookeeperConnectionFactory = ServerCnxnFactory.createFactory();
        zookeeperConnectionFactory.configure(configuration.getClientPortAddress(),
                configuration.getMaxClientCnxns());
        zookeeperConnectionFactory.startup(zkServer);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to start Zookeeper server", e);
    }
}

From source file:org.dcache.zookeeper.service.ZooKeeperCell.java

License:Open Source License

@Override
protected void starting() throws Exception {
    super.starting();

    InetSocketAddress socketAddress = Strings.isNullOrEmpty(address) ? new InetSocketAddress(port)
            : new InetSocketAddress(address, port);

    checkArgument(autoPurgeInterval > 0, "zookeeper.auto-purge.purge-interval must be non-negative.");
    zkServer = new PatchedZooKeeperServer();

    txnLog = new FileTxnSnapLog(dataLogDir, dataDir);
    zkServer.setTxnLogFactory(txnLog);/*from  w  w w . j  a  va  2s  .  c  om*/
    zkServer.setTickTime((int) tickTimeUnit.toMillis(tickTime));
    zkServer.setMinSessionTimeout(
            minSessionTimeout == -1 ? -1 : (int) minSessionTimeoutUnit.toMillis(minSessionTimeout));
    zkServer.setMaxSessionTimeout(
            maxSessionTimeout == -1 ? -1 : (int) maxSessionTimeoutUnit.toMillis(maxSessionTimeout));

    zkServer.setZKDatabase(new ZKDatabase(txnLog)); // Work-around https://issues.apache.org/jira/browse/ZOOKEEPER-2810
    zkServer.createSessionTracker(); // Work around https://issues.apache.org/jira/browse/ZOOKEEPER-2812

    ServerCnxnFactory cnxnFactory;
    cnxnFactory = new NIOServerCnxnFactory() {
        @Override
        protected void configureSaslLogin() throws IOException {
            // ZooKeeper gets confused by dCache configuring a JAAS configuration without a section for ZooKeeper, so
            // we disable the whole thing. Use a non-embedded ZooKeeper if you want security.
        }
    };
    cnxnFactory.configure(socketAddress, maxClientConnections);
    this.cnxnFactory = cnxnFactory;
    this.cnxnFactory.startup(zkServer);

    // FileTxnSnapLog constructor creates dataDir and dataLogDir if they
    // don't already exist, but in a non-thread safe fashion.
    // Unfortunately, DatadirCleanupManager#start launches an asynchronous
    // task that runs immediately and creates a FileTxnSnapLog object.  This
    // can creating a race between the constructor above.  To avoid this,
    // we must call DatadirCleanupManager#start after the FileTxnSnapLog
    // object has been created.
    int purgeIntervalHours = (int) TimeUnit.HOURS.convert(autoPurgeInterval, autoPurgeIntervalUnit);
    DatadirCleanupManager purgeMgr = new DatadirCleanupManager(dataDir.getAbsolutePath(),
            dataLogDir.getAbsolutePath(), autoPurgeRetainCount, purgeIntervalHours);
    purgeMgr.start();
}

From source file:zookeeperjunit.ZKInstanceImpl.java

License:Apache License

@Override
public Future<Unit> start() {
    return Future(() -> {
        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog log = new FileTxnSnapLog(new File(rootZooDir, "dataDir"),
                new File(rootZooDir, "snapDir"));
        zkServer.setTxnLogFactory(log);/*from w  ww.ja  va  2  s . co m*/
        zkServer.setTickTime(2000);
        zkServer.setMinSessionTimeout(10000);
        zkServer.setMaxSessionTimeout(10000);
        ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(new InetSocketAddress(cfgPort), maxClientConnections);
        cnxnFactory.startup(zkServer);
        fileTxnSnapLog = Some(log);
        serverCnxnFactory = Some(cnxnFactory);
        //remember the port. if 0 was provided then ZK will pick a free port
        //it must be remembered for the scenario of restarting this instance
        //in such case we want to get the same port again
        cfgPort = cnxnFactory.getLocalPort();
    });
}