Exemplo n.º 1
5
  @Test
  public void testCnxManagerTimeout() throws Exception {
    Random rand = new Random();
    byte b = (byte) rand.nextInt();
    int deadPort = PortAssignment.unique();
    String deadAddress = new String("10.1.1." + b);

    LOG.info("This is the dead address I'm trying: " + deadAddress);

    peers.put(
        Long.valueOf(2),
        new QuorumServer(
            2,
            new InetSocketAddress(deadAddress, deadPort),
            new InetSocketAddress(deadAddress, PortAssignment.unique())));
    peerTmpdir[2] = ClientBase.createTmpDir();

    QuorumPeer peer =
        new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
      listener.start();
    } else {
      LOG.error("Null listener when initializing cnx manager");
    }

    long begin = System.currentTimeMillis();
    cnxManager.toSend(new Long(2), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    long end = System.currentTimeMillis();

    if ((end - begin) > 6000) fail("Waited more than necessary");
  }
  protected void setUp() throws Exception {
    LOG.info("STARTING " + getName());
    setupTestEnv();

    hostPort = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184,127.0.0.1:2185";

    s1dir = ClientBase.createTmpDir();
    s2dir = ClientBase.createTmpDir();
    s3dir = ClientBase.createTmpDir();
    s4dir = ClientBase.createTmpDir();
    s5dir = ClientBase.createTmpDir();

    startServers();

    LOG.info("Setup finished");
  }
Exemplo n.º 3
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));
  }
Exemplo n.º 4
0
  /** 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));
  }
Exemplo n.º 5
0
  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);
  }
Exemplo n.º 6
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));
 }
Exemplo n.º 7
0
  public void killServer() throws Exception {
    if (zkc != null) {
      zkc.close();
    }

    // shutdown ZK server
    if (serverFactory != null) {
      serverFactory.shutdown();
      assertTrue(
          ClientBase.waitForServerDown(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT),
          "waiting for server down");
    }
    // ServerStats.unregister();
    FileUtils.deleteDirectory(ZkTmpDir);
  }
  @After
  @Override
  protected void tearDown() throws Exception {
    LOG.info("TearDown started");
    shutdownServers();

    for (String hp : hostPort.split(",")) {
      assertTrue(
          "waiting for server down",
          ClientBase.waitForServerDown(hp, ClientBase.CONNECTION_TIMEOUT));
      LOG.info(hp + " is no longer accepting client connections");
    }

    JMXEnv.tearDown();

    LOG.info("FINISHED " + getName());
  }
Exemplo n.º 9
0
 public void testLE() throws Exception {
   int count = 30;
   HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>(count);
   ArrayList<LEThread> threads = new ArrayList<LEThread>(count);
   File tmpdir[] = new File[count];
   int port[] = new int[count];
   votes = new Vote[count];
   for (int i = 0; i < count; i++) {
     peers.put(
         Long.valueOf(i),
         new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
     tmpdir[i] = ClientBase.createTmpDir();
     port[i] = PortAssignment.unique();
   }
   LeaderElection le[] = new LeaderElection[count];
   leaderDies = true;
   boolean allowOneBadLeader = leaderDies;
   for (int i = 0; i < le.length; i++) {
     QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 0, i, 1000, 2, 2);
     peer.startLeaderElection();
     le[i] = new LeaderElection(peer);
     LEThread thread = new LEThread(le[i], peer, i);
     thread.start();
     threads.add(thread);
   }
   for (int i = 0; i < threads.size(); i++) {
     threads.get(i).join(15000);
     if (threads.get(i).isAlive()) {
       fail("Threads didn't join");
     }
   }
   long id = votes[0].id;
   for (int i = 1; i < votes.length; i++) {
     if (votes[i] == null) {
       fail("Thread " + i + " had a null vote");
     }
     if (votes[i].id != id) {
       if (allowOneBadLeader && votes[i].id == i) {
         allowOneBadLeader = false;
       } else {
         fail("Thread " + i + " got " + votes[i].id + " expected " + id);
       }
     }
   }
 }
Exemplo n.º 10
0
  public void setUp() throws Exception {

    this.count = 3;
    this.peers = new HashMap<Long, QuorumServer>(count);
    peerTmpdir = new File[count];
    peerQuorumPort = new int[count];
    peerClientPort = new int[count];

    for (int i = 0; i < count; i++) {
      peerQuorumPort[i] = PortAssignment.unique();
      peerClientPort[i] = PortAssignment.unique();
      peers.put(
          Long.valueOf(i),
          new QuorumServer(
              i,
              new InetSocketAddress(peerQuorumPort[i]),
              new InetSocketAddress(PortAssignment.unique())));
      peerTmpdir[i] = ClientBase.createTmpDir();
    }
  }
Exemplo n.º 11
0
    public MainThread(int clientPort) throws IOException {
      super("Standalone server with clientPort:" + clientPort);
      File tmpDir = ClientBase.createTmpDir();
      confFile = new File(tmpDir, "zoo.cfg");

      FileWriter fwriter = new FileWriter(confFile);
      fwriter.write("tickTime=2000\n");
      fwriter.write("initLimit=10\n");
      fwriter.write("syncLimit=5\n");

      File dataDir = new File(tmpDir, "data");
      if (!dataDir.mkdir()) {
        throw new IOException("unable to mkdir " + dataDir);
      }
      fwriter.write("dataDir=" + dataDir.toString() + "\n");

      fwriter.write("clientPort=" + clientPort + "\n");
      fwriter.flush();
      fwriter.close();

      main = new TestZKSMain();
    }
Exemplo n.º 12
0
  @Override
  protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException {
    File tmpDir = ClientBase.createTmpDir();
    File saslConfFile = new File(tmpDir, "jaas.conf");
    FileWriter fwriter = new FileWriter(saslConfFile);

    fwriter.write(
        ""
            + "Server {\n"
            + "          org.apache.zookeeper.server.auth.DigestLoginModule required\n"
            + "          user_super=\"test\";\n"
            + "};\n"
            + "Client {\n"
            + "       org.apache.zookeeper.server.auth.DigestLoginModule required\n"
            + "       username=\"super\"\n"
            + "       password=\"test\";\n"
            + "};"
            + "\n");
    fwriter.close();
    System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath());
    MyWatcher watcher = new MyWatcher();
    return createClient(watcher, hp);
  }
Exemplo n.º 13
0
  static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) {
    if (factory != null) {
      ZKDatabase zkDb = null;
      {
        ZooKeeperServer zs = getServer(factory);
        if (zs != null) {
          zkDb = zs.getZKDatabase();
        }
      }
      factory.shutdown();
      try {
        if (zkDb != null) {
          zkDb.close();
        }
      } catch (IOException ie) {
        LOG.warn("Error closing logs ", ie);
      }
      final int PORT = getPort(hostPort);

      Assert.assertTrue(
          "waiting for server down",
          ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT));
    }
  }
Exemplo n.º 14
0
 @After
 public void tearDown() throws Exception {
   serverFactory.shutdown();
   Assert.assertTrue(
       "waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
 }
  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()]));
  }