@Before
  public void setUp() throws Exception {
    if (tmpDir == null) {
      tmpDir = ClientBase.createTmpDir();
    }

    ClientBase.setupTestEnv();
    ZooKeeperServer zs = new ZooKeeperServer(tmpDir, tmpDir, TICK_TIME);

    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    serverFactory = new NIOServerCnxn.Factory(new InetSocketAddress(PORT));
    serverFactory.startup(zs);

    Assert.assertTrue(
        "waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
  }
    /**
     * Constructs an embedded Zookeeper instance.
     *
     * @throws IOException if an error occurs during Zookeeper initialization.
     */
    public EmbeddedZookeeper() throws IOException {
      this.port = getAvailablePort();
      this.snapshotDir = getTempDir();
      this.logDir = getTempDir();
      this.factory = new NIOServerCnxn.Factory(new InetSocketAddress("localhost", port), 1024);

      try {
        int tickTime = 500;
        factory.startup(new ZooKeeperServer(snapshotDir, logDir, tickTime));
      } catch (InterruptedException e) {
        throw new IOException(e);
      }
    }
Beispiel #3
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);
  }
  protected void setUp() throws IOException {
    LOG.addAppender(ca);
    LOG.setLevel((Level) Level.DEBUG);

    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();

    try {
      zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort);
      serverFactory = new NIOServerCnxn.Factory(ZooKeeperDefaultPort);
      serverFactory.startup(zks);
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    boolean b = ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT);

    LOG.debug("Server up: " + b);

    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    zkc = new ZooKeeper("127.0.0.1", ZooKeeperDefaultPort, new emptyWatcher());

    // initialize the zk client with values
    try {
      zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      zkc.create(
          "/ledgers/available/127.0.0.1:" + Integer.toString(initialPort),
          new byte[0],
          Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
      zkc.create(
          "/ledgers/available/127.0.0.1:" + Integer.toString(initialPort + 1),
          new byte[0],
          Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
      zkc.create(
          "/ledgers/available/127.0.0.1:" + Integer.toString(initialPort + 2),
          new byte[0],
          Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    } catch (KeeperException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // Create Bookie Servers (B1, B2, B3)
    tmpDirB1 = File.createTempFile("bookie1", "test");
    tmpDirB1.delete();
    tmpDirB1.mkdir();

    bs1 = new BookieServer(initialPort, tmpDirB1, new File[] {tmpDirB1});
    bs1.start();

    tmpDirB2 = File.createTempFile("bookie2", "test");
    tmpDirB2.delete();
    tmpDirB2.mkdir();

    bs2 = new BookieServer(initialPort + 1, tmpDirB2, new File[] {tmpDirB2});
    bs2.start();

    tmpDirB3 = File.createTempFile("bookie3", "test");
    tmpDirB3.delete();
    tmpDirB3.mkdir();

    bs3 = new BookieServer(initialPort + 2, tmpDirB3, new File[] {tmpDirB3});
    bs3.start();

    rng = new Random(System.currentTimeMillis()); // Initialize the Random Number Generator
    entries = new ArrayList<byte[]>(); // initialize the  entries list
    entriesSize = new ArrayList<Integer>();
    sync = new SyncObj(); // initialize the synchronization data structure
  }