Exemple #1
0
  private static void startZooKeeperServer() throws IOException, InterruptedException {
    String zooLocation = System.getProperty("java.io.tmpdir");
    File zooFile = new File(zooLocation, "zookeeper-malhartest");
    ZooKeeperServer zooKeeper = new ZooKeeperServer(zooFile, zooFile, 2000);

    NIOServerCnxnFactory serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(new InetSocketAddress(2181), 10);
    serverFactory.startup(zooKeeper);
  }
  /** @return ClientPort server bound to. */
  public int startup(File baseDir, int numZooKeeperServers)
      throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) return -1;

    setupTestEnv();
    shutdown();

    int tentativePort = selectClientPort();

    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
      File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
      recreateDir(dir);
      int tickTimeToUse;
      if (this.tickTime > 0) {
        tickTimeToUse = this.tickTime;
      } else {
        tickTimeToUse = TICK_TIME;
      }
      ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
      NIOServerCnxnFactory standaloneServerFactory;
      while (true) {
        try {
          standaloneServerFactory = new NIOServerCnxnFactory();
          standaloneServerFactory.configure(
              new InetSocketAddress(tentativePort),
              configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, 1000));
        } catch (BindException e) {
          LOG.debug("Failed binding ZK Server to client port: " + tentativePort);
          // This port is already in use, try to use another.
          tentativePort++;
          continue;
        }
        break;
      }

      // Start up this ZK server
      standaloneServerFactory.startup(server);
      if (!waitForServerUp(tentativePort, CONNECTION_TIMEOUT)) {
        throw new IOException("Waiting for startup of standalone server");
      }

      // We have selected this port as a client port.
      clientPortList.add(tentativePort);
      standaloneServerFactoryList.add(standaloneServerFactory);
      zooKeeperServers.add(server);
    }

    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Started MiniZK Cluster and connect 1 ZK server " + "on client port: " + clientPort);
    return clientPort;
  }
  public void startServer() throws Exception {
    // 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();

    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);

    boolean b =
        ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);

    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    final CountDownLatch latch = new CountDownLatch(1);
    zkc =
        new ZooKeeper(
            getZooKeeperConnectString(),
            10000,
            new Watcher() {
              @Override
              public void process(WatchedEvent event) {
                // handle session disconnects and expires
                if (event.getState().equals(Watcher.Event.KeeperState.SyncConnected)) {
                  latch.countDown();
                }
              }
            });
    if (!latch.await(10000, TimeUnit.MILLISECONDS)) {
      zkc.close();
      fail("Could not connect to zookeeper server");
    }

    // initialize the zk client with values
    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);
  }
  public void startZookeeper() {
    if (!useZookeeper) { // Do not use zookeeper for simpleconsumer
      return;
    }

    try {
      int clientPort = 2182;
      int numConnections = 5000;
      int tickTime = 2000;
      File dir = new File(zklogdir);

      ZooKeeperServer zserver = new ZooKeeperServer(dir, dir, tickTime);
      standaloneServerFactory = new NIOServerCnxnFactory();
      standaloneServerFactory.configure(new InetSocketAddress(clientPort), numConnections);
      standaloneServerFactory.startup(zserver); // start the zookeeper server.
    } catch (InterruptedException ex) {
      logger.debug(ex.getLocalizedMessage());
    } catch (IOException ex) {
      logger.debug(ex.getLocalizedMessage());
    }
  }