/** @throws java.lang.Exception */
  @Before
  public void setUp() throws Exception {
    Properties startupProperties = new Properties();
    startupProperties.setProperty("dataDir", (new File("./target").getAbsolutePath()));
    startupProperties.setProperty("dataLogDir", (new File("./target").getAbsolutePath()));
    freePort = findFreePort();
    startupProperties.setProperty("clientPort", "" + freePort);
    QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
    try {
      quorumConfiguration.parseProperties(startupProperties);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    final ZooKeeperServerMain zooKeeperServer = new ZooKeeperServerMain();
    final ServerConfig configuration = new ServerConfig();
    configuration.readFrom(quorumConfiguration);
    new Thread() {
      public void run() {
        try {
          zooKeeperServer.runFromConfig(configuration);
        } catch (IOException e) {
          System.out.println("ZooKeeper failure");
        }
      }
    }.start();
  }
  @Override
  public void start() throws Exception {
    ServerConfig config = new ServerConfig();
    config.parse(new String[] {"2181", "zk"});

    zkServer = new ZooKeeperServer();
    zkServer.setTxnLogFactory(
        new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
    zkServer.setTickTime(config.getTickTime());
    zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
    cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
    cnxnFactory.startup(zkServer);
  }
Example #3
0
  private static ServerConfig createZooKeeperConf() throws IOException, ConfigException {

    // create conf file
    File zkConfDir = new File(TEST_DIR);
    zkConfDir.mkdirs();
    File zkConfFile = new File(ZK_CONF_FILE);
    zkConfFile.delete();
    zkConfFile.createNewFile();

    Properties zkConfProps = new Properties();
    zkConfProps.setProperty("tickTime", "2000");
    zkConfProps.setProperty("dataDir", ZK_DATA_DIR);
    zkConfProps.setProperty("clientPort", new Integer(zkClientPort).toString());
    zkConfProps.setProperty("maxClientCnxns", "30");
    zkConfProps.store(new FileOutputStream(zkConfFile), "");

    // create config object
    ServerConfig zkConf = new ServerConfig();
    zkConf.parse(ZK_CONF_FILE);

    return zkConf;
  }
Example #4
0
  public static void createAndStartZooKeeper()
      throws IOException, ConfigException, InterruptedException {
    ServerConfig zkConf = createZooKeeperConf();

    zooKeeper = new ZooKeeperServer();
    FileTxnSnapLog ftxn =
        new FileTxnSnapLog(new File(zkConf.getDataLogDir()), new File(zkConf.getDataDir()));
    zooKeeper.setTxnLogFactory(ftxn);
    zooKeeper.setTickTime(zkConf.getTickTime());
    zooKeeper.setMinSessionTimeout(zkConf.getMinSessionTimeout());
    zooKeeper.setMaxSessionTimeout(zkConf.getMaxSessionTimeout());

    cnxnFactory =
        new NIOServerCnxn.Factory(zkConf.getClientPortAddress(), zkConf.getMaxClientCnxns());
    cnxnFactory.startup(zooKeeper);
  }