Java Code Examples for org.apache.zookeeper.server.ServerCnxnFactory#createFactory()

The following examples show how to use org.apache.zookeeper.server.ServerCnxnFactory#createFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientBaseWithFixes.java    From hadoop with Apache License 2.0 6 votes vote down vote up
static ServerCnxnFactory createNewServerInstance(File dataDir,
        ServerCnxnFactory factory, String hostPort, int maxCnxns)
    throws IOException, InterruptedException
{
    ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
    final int PORT = getPort(hostPort);
    if (factory == null) {
        factory = ServerCnxnFactory.createFactory(PORT, maxCnxns);
    }
    factory.startup(zks);
    Assert.assertTrue("waiting for server up",
               ClientBaseWithFixes.waitForServerUp("127.0.0.1:" + PORT,
                                          CONNECTION_TIMEOUT));

    return factory;
}
 
Example 2
Source File: ClientBaseWithFixes.java    From big-c with Apache License 2.0 6 votes vote down vote up
static ServerCnxnFactory createNewServerInstance(File dataDir,
        ServerCnxnFactory factory, String hostPort, int maxCnxns)
    throws IOException, InterruptedException
{
    ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
    final int PORT = getPort(hostPort);
    if (factory == null) {
        factory = ServerCnxnFactory.createFactory(PORT, maxCnxns);
    }
    factory.startup(zks);
    Assert.assertTrue("waiting for server up",
               ClientBaseWithFixes.waitForServerUp("127.0.0.1:" + PORT,
                                          CONNECTION_TIMEOUT));

    return factory;
}
 
Example 3
Source File: ZooKeeperServerRunner.java    From waltz with Apache License 2.0 5 votes vote down vote up
public ZooKeeperServerRunner(int port, Path zkDir) throws IOException {
    int zkPort = port == 0 ? new PortFinder().getPort() : port;
    InetAddress inetAddress = InetAddress.getLocalHost();
    this.connectString = inetAddress.getCanonicalHostName() + ":" + zkPort;

    try {
        InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, zkPort);
        this.standaloneServerFactory = ServerCnxnFactory.createFactory(socketAddress, NUM_CONNECTIONS);

    } catch (IOException ex) {
        throw new IOException("failed to create zookeeper ServerCnxnFactory: port=" + zkPort, ex);
    }
    this.zkDir = zkDir.toFile();
}
 
Example 4
Source File: ZooKeeperStateServer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void startDistributed() throws IOException {
    logger.info("Starting Embedded ZooKeeper Peer");

    try {
        transactionLog = new FileTxnSnapLog(new File(quorumPeerConfig.getDataLogDir()), new File(quorumPeerConfig.getDataDir()));

        connectionFactory = ServerCnxnFactory.createFactory();
        connectionFactory.configure(quorumPeerConfig.getClientPortAddress(), quorumPeerConfig.getMaxClientCnxns());

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

        quorumPeer.start();
    } catch (final IOException ioe) {
        throw new IOException("Failed to start embedded ZooKeeper Peer", ioe);
    } catch (final Exception e) {
        throw new RuntimeException("Failed to start embedded ZooKeeper Peer", e);
    }
}
 
Example 5
Source File: EmbeddedZooKeeper.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
private static ServerCnxnFactory createCnxnFactory(QuorumPeerConfig zkCfg) throws IOException {
    final InetSocketAddress bindAddr = zkCfg.getClientPortAddress();
    final ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    // Listen only on 127.0.0.1 because we do not want to expose ZooKeeper to others.
    cnxnFactory.configure(new InetSocketAddress("127.0.0.1", bindAddr != null ? bindAddr.getPort() : 0),
                          zkCfg.getMaxClientCnxns());
    return cnxnFactory;
}
 
Example 6
Source File: MicroZookeeperService.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Startup: start ZK. It is only after this that
 * the binding information is valid.
 * @throws Exception
 */
@Override
protected void serviceStart() throws Exception {

  setupSecurity();

  ZooKeeperServer zkServer = new ZooKeeperServer();
  FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, dataDir);
  zkServer.setTxnLogFactory(ftxn);
  zkServer.setTickTime(tickTime);

  LOG.info("Starting Local Zookeeper service");
  factory = ServerCnxnFactory.createFactory();
  factory.configure(getAddress(port), -1);
  factory.startup(zkServer);

  String connectString = getConnectionString();
  LOG.info("In memory ZK started at {}\n", connectString);

  if (LOG.isDebugEnabled()) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    zkServer.dumpConf(pw);
    pw.flush();
    LOG.debug(sw.toString());
  }
  binding = new BindingInformation();
  binding.ensembleProvider = new FixedEnsembleProvider(connectString);
  binding.description =
      getName() + " reachable at \"" + connectString + "\"";

  addDiagnostics(binding.description);
  // finally: set the binding information in the config
  getConfig().set(KEY_REGISTRY_ZK_QUORUM, connectString);
}
 
Example 7
Source File: ZookeeperInstance.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
public ZookeeperInstance(File commonWorkDir, boolean useCrusher, int instance) throws Exception {
    File workDir = new File(commonWorkDir, "instance-" + instance);
    FileUtils.forceMkdir(workDir);

    File logDir = new File(commonWorkDir, "log-" + instance);
    FileUtils.forceMkdir(logDir);

    File markerFile = new File(workDir, "myid");
    FileUtils.write(markerFile, Integer.toString(instance), "UTF-8");

    this.instance = instance;
    this.clientPort = PORT_BASE_CLIENT + instance;

    Properties properties = new Properties();
    properties.put("tickTime", "100");
    properties.put("dataDir", workDir.getAbsolutePath());
    properties.put("dataLogDir", logDir.getAbsolutePath());
    properties.put("clientPort", Integer.toString(clientPort));
    properties.put("clientPortAddress", "127.0.0.1");
    properties.put("initLimit", "5");
    properties.put("syncLimit", "5");

    for (int i = 1; i <= CLUSTER_SIZE; i++) {
        int leaderPort = (i != instance && useCrusher) ?
                PORT_BASE_LEADER + PORT_CRUSHER_BASE + i : PORT_BASE_LEADER + i;
        int electionPort = (i != instance && useCrusher) ?
                PORT_BASE_ELECTION + PORT_CRUSHER_BASE + i : PORT_BASE_ELECTION + i;

        properties.put("server." + i, "127.0.0.1:" + leaderPort + ":" + electionPort);
    }

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

    this.factory = ServerCnxnFactory.createFactory();
    this.peer = createPeer(factory, config);

    LOGGER.info("Zookeeper #{} is started", instance);
}
 
Example 8
Source File: InMemoryZKServer.java    From twill with Apache License 2.0 5 votes vote down vote up
@Override
protected void startUp() throws Exception {
  ZooKeeperServer zkServer = new ZooKeeperServer();
  FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, dataDir);
  zkServer.setTxnLogFactory(ftxn);
  zkServer.setTickTime(tickTime);

  factory = ServerCnxnFactory.createFactory();
  factory.configure(getAddress(port), -1);
  factory.startup(zkServer);

  LOG.info("In memory ZK started: " + getConnectionStr());
}
 
Example 9
Source File: MicroZookeeperService.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Startup: start ZK. It is only after this that
 * the binding information is valid.
 * @throws Exception
 */
@Override
protected void serviceStart() throws Exception {

  setupSecurity();

  ZooKeeperServer zkServer = new ZooKeeperServer();
  FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDir, dataDir);
  zkServer.setTxnLogFactory(ftxn);
  zkServer.setTickTime(tickTime);

  LOG.info("Starting Local Zookeeper service");
  factory = ServerCnxnFactory.createFactory();
  factory.configure(getAddress(port), -1);
  factory.startup(zkServer);

  String connectString = getConnectionString();
  LOG.info("In memory ZK started at {}\n", connectString);

  if (LOG.isDebugEnabled()) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    zkServer.dumpConf(pw);
    pw.flush();
    LOG.debug(sw.toString());
  }
  binding = new BindingInformation();
  binding.ensembleProvider = new FixedEnsembleProvider(connectString);
  binding.description =
      getName() + " reachable at \"" + connectString + "\"";

  addDiagnostics(binding.description);
  // finally: set the binding information in the config
  getConfig().set(KEY_REGISTRY_ZK_QUORUM, connectString);
}
 
Example 10
Source File: ZookeeperDiscoverySpiSaslAuthAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** */
private ServerCnxnFactory createNewServerInstance(
    ServerCnxnFactory factory, String hostPort, int maxCnxns)
    throws IOException {
    final int port = getPort(hostPort);

    if (factory == null)
        factory = ServerCnxnFactory.createFactory(port, maxCnxns);

    return factory;
}
 
Example 11
Source File: EmbeddedZookeeper.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public void startup() throws IOException {
    if (this.port == -1) {
        this.port = TestUtils.getAvailablePort();
    }
    this.factory = ServerCnxnFactory.createFactory(new InetSocketAddress("localhost", port), 1024);
    this.snapshotDir = TestUtils.constructTempDir("embeeded-zk/snapshot");
    this.logDir = TestUtils.constructTempDir("embeeded-zk/log");

    try {
        factory.startup(new ZooKeeperServer(snapshotDir, logDir, tickTime));
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
 
Example 12
Source File: ZooKeeperStateServer.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void startDistributed() throws IOException {
    logger.info("Starting Embedded ZooKeeper Peer");

    try {
        transactionLog = new FileTxnSnapLog(quorumPeerConfig.getDataLogDir(), quorumPeerConfig.getDataDir());

        connectionFactory = ServerCnxnFactory.createFactory();
        connectionFactory.configure(quorumPeerConfig.getClientPortAddress(), quorumPeerConfig.getMaxClientCnxns());

        quorumPeer = new QuorumPeer();
        quorumPeer.setTxnFactory(new FileTxnSnapLog(quorumPeerConfig.getDataLogDir(), quorumPeerConfig.getDataDir()));
        quorumPeer.setElectionType(quorumPeerConfig.getElectionAlg());
        quorumPeer.setMyid(quorumPeerConfig.getServerId());
        quorumPeer.setTickTime(quorumPeerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(quorumPeerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(quorumPeerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(quorumPeerConfig.getInitLimit());
        quorumPeer.setSyncLimit(quorumPeerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(quorumPeerConfig.getQuorumVerifier(), false);
        quorumPeer.setCnxnFactory(connectionFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(quorumPeerConfig.getPeerType());
        quorumPeer.setSyncEnabled(quorumPeerConfig.getSyncEnabled());
        quorumPeer.setQuorumListenOnAllIPs(quorumPeerConfig.getQuorumListenOnAllIPs());

        quorumPeer.start();
    } catch (final IOException ioe) {
        throw new IOException("Failed to start embedded ZooKeeper Peer", ioe);
    } catch (final Exception e) {
        throw new RuntimeException("Failed to start embedded ZooKeeper Peer", e);
    }
}
 
Example 13
Source File: SpliceZoo.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public SpliceZoo(QuorumPeerConfig config, int number) throws IOException {
	this.config = config;
	try {
		if (QuorumPeer.class.getMethod("testingQuorumPeer", null) != null)
			this.peer = (QuorumPeer) QuorumPeer.class.getMethod("testingQuorumPeer", null).invoke(null,null);
		else
			this.peer = QuorumPeer.class.newInstance();
	} catch (Exception e) {
		throw new RuntimeException("Quorum Peer Signature Issue for Unit Tests");
	}
	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());
	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);
}
 
Example 14
Source File: HBaseServerTestInstance.java    From Halyard with Apache License 2.0 4 votes vote down vote up
public static synchronized Configuration getInstanceConfig() throws Exception {
    if (conf == null) {
        File zooRoot = File.createTempFile("hbase-zookeeper", "");
        zooRoot.delete();
        ZooKeeperServer zookeper = new ZooKeeperServer(zooRoot, zooRoot, 2000);
        ServerCnxnFactory factory = ServerCnxnFactory.createFactory(new InetSocketAddress("localhost", 0), 5000);
        factory.startup(zookeper);

        YarnConfiguration yconf = new YarnConfiguration();
        String argLine = System.getProperty("argLine");
        if (argLine != null) {
            yconf.set("yarn.app.mapreduce.am.command-opts", argLine.replace("jacoco.exec", "jacocoMR.exec"));
        }
        yconf.setBoolean(MRConfig.MAPREDUCE_MINICLUSTER_CONTROL_RESOURCE_MONITORING, false);
        yconf.setClass(YarnConfiguration.RM_SCHEDULER, FifoScheduler.class, ResourceScheduler.class);
        MiniMRYarnCluster miniCluster = new MiniMRYarnCluster("testCluster");
        miniCluster.init(yconf);
        String resourceManagerLink = yconf.get(YarnConfiguration.RM_ADDRESS);
        yconf.setBoolean(MRJobConfig.JOB_UBERTASK_ENABLE, true);
        miniCluster.start();
        miniCluster.waitForNodeManagersToConnect(10000);
        // following condition set in MiniYarnCluster:273
        while (resourceManagerLink.endsWith(":0")) {
            Thread.sleep(100);
            resourceManagerLink = yconf.get(YarnConfiguration.RM_ADDRESS);
        }

        File hbaseRoot = File.createTempFile("hbase-root", "");
        hbaseRoot.delete();
        conf = HBaseConfiguration.create(miniCluster.getConfig());
        conf.set(HConstants.HBASE_DIR, hbaseRoot.toURI().toURL().toString());
        conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, factory.getLocalPort());
        conf.set("hbase.master.hostname", "localhost");
        conf.set("hbase.regionserver.hostname", "localhost");
        conf.setInt("hbase.master.info.port", -1);
        conf.set("hbase.fs.tmp.dir", new File(System.getProperty("java.io.tmpdir")).toURI().toURL().toString());
        LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
        cluster.startup();
    }
    return new Configuration(conf);
}
 
Example 15
Source File: LocalZKServer.java    From oryx with Apache License 2.0 4 votes vote down vote up
/**
 * Starts Zookeeper.
 *
 * @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);
}