Esempio n. 1
1
  @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));
  }
  /** Verify the ability to start a standalone server instance. */
  @Test
  public void testStandalone() throws Exception {
    LOG.info("STARTING " + getName());
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = 3181;

    MainThread main = new MainThread(CLIENT_PORT);
    main.start();

    assertTrue(
        "waiting for server being up",
        ClientBase.waitForServerUp("localhost:" + CLIENT_PORT, CONNECTION_TIMEOUT));

    ZooKeeper zk = new ZooKeeper("localhost:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    assertEquals(new String(zk.getData("/foo", null, null)), "foobar");
    zk.close();

    main.shutdown();

    assertTrue(
        "waiting for server down",
        ClientBase.waitForServerDown("localhost:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT));
  }
Esempio n. 3
0
 /** Starting the given server instance */
 public static void startServerInstance(File dataDir, ServerCnxnFactory factory, String hostPort)
     throws IOException, InterruptedException {
   final int port = getPort(hostPort);
   LOG.info("STARTING server instance 127.0.0.1:{}", port);
   ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
   factory.startup(zks);
   Assert.assertTrue(
       "waiting for server up",
       ClientBase.waitForServerUp("127.0.0.1:" + port, CONNECTION_TIMEOUT));
 }
  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);
  }
  void startServers() throws Exception {
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    JMXEnv.setUp();

    HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
    peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", 3181)));
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", 3182)));
    peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", 3183)));
    peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress("127.0.0.1", 3184)));
    peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress("127.0.0.1", 3185)));

    LOG.info("creating QuorumPeer 1");
    s1 = new QuorumPeer(peers, s1dir, s1dir, 2181, 0, 1, tickTime, initLimit, syncLimit);
    assertEquals(2181, s1.getClientPort());
    LOG.info("creating QuorumPeer 2");
    s2 = new QuorumPeer(peers, s2dir, s2dir, 2182, 0, 2, tickTime, initLimit, syncLimit);
    assertEquals(2182, s2.getClientPort());
    LOG.info("creating QuorumPeer 3");
    s3 = new QuorumPeer(peers, s3dir, s3dir, 2183, 0, 3, tickTime, initLimit, syncLimit);
    assertEquals(2183, s3.getClientPort());
    LOG.info("creating QuorumPeer 4");
    s4 = new QuorumPeer(peers, s4dir, s4dir, 2184, 0, 4, tickTime, initLimit, syncLimit);
    assertEquals(2184, s4.getClientPort());
    LOG.info("creating QuorumPeer 5");
    s5 = new QuorumPeer(peers, s5dir, s5dir, 2185, 0, 5, tickTime, initLimit, syncLimit);
    assertEquals(2185, s5.getClientPort());
    LOG.info("start QuorumPeer 1");
    s1.start();
    LOG.info("start QuorumPeer 2");
    s2.start();
    LOG.info("start QuorumPeer 3");
    s3.start();
    LOG.info("start QuorumPeer 4");
    s4.start();
    LOG.info("start QuorumPeer 5");
    s5.start();
    LOG.info("started QuorumPeer 5");
    for (String hp : hostPort.split(",")) {
      assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT));
      LOG.info(hp + " is accepting client connections");
    }

    // interesting to see what's there...
    JMXEnv.dump();
    // make sure we have these 5 servers listed
    Set<String> ensureNames = new LinkedHashSet<String>();
    for (int i = 1; i <= 5; i++) {
      ensureNames.add("InMemoryDataTree");
    }
    for (int i = 1; i <= 5; i++) {
      ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2=");
    }
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j);
      }
    }
    for (int i = 1; i <= 5; i++) {
      ensureNames.add("name0=ReplicatedServer_id" + i);
    }
    JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()]));
  }