/** * This is a helper function for launching a set of servers * * @param numServers * @return * @throws IOException * @throws InterruptedException */ private Servers LaunchServers(int numServers) throws IOException, InterruptedException { int SERVER_COUNT = numServers; Servers svrs = new Servers(); final int clientPorts[] = new int[SERVER_COUNT]; StringBuilder sb = new StringBuilder(); for(int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n"); } String quorumCfgSection = sb.toString(); MainThread mt[] = new MainThread[SERVER_COUNT]; ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT]; for(int i = 0; i < SERVER_COUNT; i++) { mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection); mt[i].start(); zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); } waitForAll(zk, States.CONNECTED); svrs.mt = mt; svrs.zk = zk; return svrs; }
@Before 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, "0.0.0.0", peerQuorumPort[i], PortAssignment.unique(), null)); peerTmpdir[i] = ClientBase.createTmpDir(); } }
/** * Ensure that observer only comes up when a proper ensemble is configured. * (and will not come up with standalone server). */ @Test public void testObserverWithStandlone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer\n" + "server.2=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + "\npeerType=observer\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); q1.join(ClientBase.CONNECTION_TIMEOUT); Assert.assertFalse(q1.isAlive()); }
/** * Ensure that a single standalone server comes up when misconfigured * with a single server.# line in the configuration. This handles the * case of HBase, which configures zoo.cfg in this way. Maintain b/w * compatibility. * TODO remove in a future version (4.0.0 hopefully) */ @Test public void testStandaloneQuorum() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + "\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); try { Assert.assertTrue("waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); } finally { q1.shutdown(); } }
/** * This is a helper function for launching a set of servers * * @param numServers * @return * @throws IOException * @throws InterruptedException */ private Servers LaunchServers(int numServers) throws IOException, InterruptedException { int SERVER_COUNT = numServers; Servers svrs = new Servers(); final int clientPorts[] = new int[SERVER_COUNT]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+";"+clientPorts[i]+"\n"); } String quorumCfgSection = sb.toString(); MainThread mt[] = new MainThread[SERVER_COUNT]; ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT]; for (int i = 0; i < SERVER_COUNT; i++) { mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection); mt[i].start(); zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); } waitForAll(zk, States.CONNECTED); svrs.mt = mt; svrs.zk = zk; return svrs; }
/** * Starts a standalone server and tests that we can query its AdminServer. */ @Test public void testStandalone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); ZooKeeperServerMainTest.MainThread main = new ZooKeeperServerMainTest.MainThread(CLIENT_PORT, false, null); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); queryAdminServer(jettyAdminPort); main.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
/** * Test verifies that the server shouldn't allow minsessiontimeout > * maxsessiontimeout */ @Test public void testWithMinSessionTimeoutGreaterThanMaxSessionTimeout() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final int tickTime = 2000; final int minSessionTimeout = 20 * tickTime + 1000; // min is higher final int maxSessionTimeout = tickTime * 2 - 100; // max is lower final String configs = "maxSessionTimeout=" + maxSessionTimeout + "\n" + "minSessionTimeout=" + minSessionTimeout + "\n"; MainThread main = new MainThread(CLIENT_PORT, true, configs); String args[] = new String[1]; args[0] = main.confFile.toString(); try { main.main.initializeAndRun(args); Assert.fail("Must throw exception as " + "minsessiontimeout > maxsessiontimeout"); } catch (ConfigException iae) { // expected } }
@Before 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( "127.0.0.1", peerQuorumPort[i]), new InetSocketAddress( "127.0.0.1", PortAssignment.unique()), new InetSocketAddress( "127.0.0.1", peerClientPort[i]))); peerTmpdir[i] = ClientBase.createTmpDir(); } }
@Before public void setUp() throws Exception { String testDataPath = System.getProperty("test.data.dir", "build/test/data"); System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); System.setProperty(ZKConfig.SSL_AUTHPROVIDER, "x509"); System.setProperty(ZKConfig.SSL_KEYSTORE_LOCATION, testDataPath + "/ssl/testKeyStore.jks"); System.setProperty(ZKConfig.SSL_KEYSTORE_PASSWD, "testpass"); System.setProperty(ZKConfig.SSL_TRUSTSTORE_LOCATION, testDataPath + "/ssl/testTrustStore.jks"); System.setProperty(ZKConfig.SSL_TRUSTSTORE_PASSWD, "testpass"); System.setProperty("javax.net.debug", "ssl"); System.setProperty("zookeeper.authProvider.x509", "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); String host = "localhost"; int port = PortAssignment.unique(); hostPort = host + ":" + port; serverFactory = ServerCnxnFactory.createFactory(); serverFactory.configure(new InetSocketAddress(host, port), maxCnxns, true); super.setUp(); }
@Before public void setUp() throws Exception { this.count = 3; this.peers = new HashMap<Long,QuorumServer>(count); peerQuorumPort = new int[count]; peerClientPort = new int[count]; authzHosts = new HashSet<String>(); for(int i = 0; i < count; i++) { peerQuorumPort[i] = PortAssignment.unique(); peerClientPort[i] = PortAssignment.unique(); QuorumServer qs = new QuorumServer(i, "0.0.0.0", peerQuorumPort[i], PortAssignment.unique(), null); peers.put(Long.valueOf(i), qs); authzHosts.add(qs.hostname); } executor = new ThreadPoolExecutor(3, 10, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
@Test public void testZeroWeightQuorum() throws Exception { FastLeaderElection le[] = new FastLeaderElection[count]; LOG.info("TestZeroWeightQuorum: " + getTestName()+ ", " + count); for(int i = 0; i < count; i++) { peers.put(Long.valueOf(i), new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), PortAssignment.unique(), null)); tmpdir[i] = ClientBase.createTmpDir(); port[i] = PortAssignment.unique(); } for(int i = 0; i < le.length; i++) { QuorumHierarchical hq = new QuorumHierarchical(qp); QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, hq); peer.startLeaderElection(); LEThread thread = new LEThread(peer, i); thread.start(); threads.add(thread); } LOG.info("Started threads " + getTestName()); for(int i = 0; i < threads.size(); i++) { threads.get(i).join(15000); if (threads.get(i).isAlive()) { Assert.fail("Threads didn't join"); } else { if(threads.get(i).fail) Assert.fail("Elected zero-weight server"); } } }
/** * Test verifies the auto creation of data dir and data log dir. */ @Test(timeout = 30000) public void testAutoCreateDataLogDir() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, false); String args[] = new String[1]; args[0] = main.confFile.toString(); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); zk.close(); main.shutdown(); main.join(); main.deleteDirs(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test public void testCnxManagerTimeout() throws Exception { Random rand = new Random(); byte b = (byte) rand.nextInt(); int finalOctet = b & 0xFF; int deadPort = PortAssignment.unique(); String deadAddress = new String("192.0.2." + finalOctet); LOG.info("This is the dead address I'm trying: " + deadAddress); peers.put(Long.valueOf(2), new QuorumServer(2, deadAddress, deadPort, PortAssignment.unique(), null)); 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) Assert.fail("Waited more than necessary"); }
void populate() throws Exception { for (int i = 0; i < count; i++) { peers.put(Long.valueOf(i), new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), PortAssignment.unique(), null)); tmpdir[i] = ClientBase.createTmpDir(); port[i] = PortAssignment.unique(); } }
private LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); Field addrField = peer.getClass().getDeclaredField("myQuorumAddr"); addrField.setAccessible(true); addrField.set(peer, new InetSocketAddress(PortAssignment.unique())); ZKDatabase zkDb = new ZKDatabase(logFactory); LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); return zk; }
@Test public void testLostMessage() throws Exception { FastLeaderElection le[] = new FastLeaderElection[count]; LOG.info("TestLE: " + getTestName()+ ", " + count); for(int i = 0; i < count; i++) { int clientport = PortAssignment.unique(); peers.put(Long.valueOf(i), new QuorumServer(i, "0.0.0.0", clientport, PortAssignment.unique(), null)); tmpdir[i] = ClientBase.createTmpDir(); port[i] = clientport; } /* * Start server 0 */ QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2); peer.startLeaderElection(); FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 1); thread.start(); /* * Start mock server 1 */ mockServer(); thread.join(5000); if (thread.isAlive()) { Assert.fail("Threads didn't join"); } }
/** * Verify the ability to start a standalone server instance. */ @Test public void testStandalone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, true); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); zk.close(); main.shutdown(); main.join(); main.deleteDirs(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
private ServerCnxnFactory startServer(File tmpDir) throws IOException, InterruptedException { final int CLIENT_PORT = PortAssignment.unique(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1); f.startup(zks); Assert.assertNotNull("JMX initialization failed!", zks.jmxServerBean); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); return f; }
/** * This test ensures that an Observer does not elect itself as a leader, or * indeed come up properly, if it is the lone member of an ensemble. * @throws Exception */ @Test public void testObserverOnly() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer\npeerType=observer\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); q1.join(ClientBase.CONNECTION_TIMEOUT); Assert.assertFalse(q1.isAlive()); }
@Test public void testLERestart() throws Exception { FastLeaderElection le[] = new FastLeaderElection[count]; leaderDies = true; boolean allowOneBadLeader = leaderDies; LOG.info("TestLE: " + getTestName()+ ", " + count); for(int i = 0; i < count; i++) { peers.put(Long.valueOf(i), new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), PortAssignment.unique(), null)); tmpdir[i] = ClientBase.createTmpDir(); port[i] = PortAssignment.unique(); } for(int i = 0; i < count; i++) { QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2); peer.startLeaderElection(); FLERestartThread thread = new FLERestartThread(peer, i); thread.start(); restartThreads.add(thread); } LOG.info("Started threads " + getTestName()); for(int i = 0; i < restartThreads.size(); i++) { restartThreads.get(i).join(10000); if (restartThreads.get(i).isAlive()) { Assert.fail("Threads didn't join"); } } }
/** * Test verifies that the server is able to redefine the min/max session * timeouts */ @Test public void testMinMaxSessionTimeOut() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); final int CLIENT_PORT_QP2 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique(); final int minSessionTimeOut = 10000; final int maxSessionTimeOut = 15000; final String configs = "maxSessionTimeout=" + maxSessionTimeOut + "\n" + "minSessionTimeout=" + minSessionTimeOut + "\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, configs); MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, configs); q1.start(); q2.start(); Assert.assertTrue("waiting for server 1 being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); Assert.assertTrue("waiting for server 2 being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); QuorumPeer quorumPeer = q1.main.quorumPeer; Assert.assertEquals("minimumSessionTimeOut is not considered", minSessionTimeOut, quorumPeer.getMinSessionTimeout()); Assert.assertEquals("maximumSessionTimeOut is not considered", maxSessionTimeOut, quorumPeer.getMaxSessionTimeout()); }
/** * Test verifies that the server is able to redefine if user configured only * minSessionTimeout limit */ @Test public void testWithOnlyMinSessionTimeout() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); final int CLIENT_PORT_QP2 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique(); final int minSessionTimeOut = 15000; final String configs = "minSessionTimeout=" + minSessionTimeOut + "\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, configs); MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, configs); q1.start(); q2.start(); Assert.assertTrue("waiting for server 1 being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); Assert.assertTrue("waiting for server 2 being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); QuorumPeer quorumPeer = q1.main.quorumPeer; final int maxSessionTimeOut = quorumPeer.tickTime * 20; Assert.assertEquals("minimumSessionTimeOut is not considered", minSessionTimeOut, quorumPeer.getMinSessionTimeout()); Assert.assertEquals("maximumSessionTimeOut is wrong", maxSessionTimeOut, quorumPeer.getMaxSessionTimeout()); }
private MainThread[] startQuorum() throws IOException { final int clientPorts[] = new int[SERVER_COUNT]; StringBuilder sb = new StringBuilder(); String server; for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i]; sb.append(server + "\n"); } String currentQuorumCfgSection = sb.toString(); MainThread mt[] = new MainThread[SERVER_COUNT]; // start all the servers for (int i = 0; i < SERVER_COUNT; i++) { mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { @Override public TestQPMain getTestQPMain() { return new MockTestQPMain(); } }; mt[i].start(); } // ensure all servers started for (int i = 0; i < SERVER_COUNT; i++) { Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); } return mt; }
/** * Create config strings that will be used for * the test servers. */ private ArrayList<String> buildServerStrings() { ArrayList<String> serverStrings = new ArrayList<String>(); for(int i = 0; i < NUM_SERVERS; i++) { clientPorts[i] = PortAssignment.unique(); String server = "server." + i + "=localhost:" + PortAssignment.unique() +":"+PortAssignment.unique() + ":participant;" + "localhost:" + clientPorts[i]; serverStrings.add(server); } return serverStrings; }
/** * Ensure observer cannot start by itself **/ @Test public void startObserver() throws Exception { int clientPort = PortAssignment.unique(); String config = "server." + observer1 + "=localhost:"+ PortAssignment.unique() + ":" + clientPort + ":observer;" + "localhost:" + PortAssignment.unique(); MainThread observer = new MainThread(observer1, clientPort, config); observer.start(); Assert.assertFalse("Observer was able to start by itself!", ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); }
@Test public void testLostMessage() throws Exception { LOG.info("TestLE: {}, {}", getTestName(), count); for(int i = 0; i < count; i++) { int clientport = PortAssignment.unique(); peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(clientport), new InetSocketAddress(PortAssignment.unique()))); tmpdir[i] = ClientBase.createTmpDir(); port[i] = clientport; } /* * Start server 0 */ QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2); peer.startLeaderElection(); FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 1); thread.start(); /* * Start mock server 1 */ mockServer(); thread.join(5000); if (thread.isAlive()) { Assert.fail("Threads didn't join"); } }
private String generateNewQuorumConfig(int serverCount) { StringBuilder sb = new StringBuilder(); String server; for (int i = 0; i < serverCount; i++) { clientPorts.put(i, PortAssignment.unique()); server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + clientPorts.get(i); serverAddress.put(i, server); sb.append(server + "\n"); } return sb.toString(); }
/** * Tests that the ZooKeeper server will fail to start if the * snapshot directory is read only. * * This test will fail if it is executed as root user. */ @Test(timeout = 30000) public void testReadOnlySnapshotDir() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); // Start up the ZK server to automatically create the necessary directories // and capture the directory where data is stored MainThread main = new MainThread(CLIENT_PORT, true, null); File tmpDir = main.tmpDir; main.start(); Assert.assertTrue("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); // Make the snapshot directory read only File snapDir = new File(main.dataDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); snapDir.setWritable(false); // Restart ZK and observe a failure main = new MainThread(CLIENT_PORT, false, tmpDir, null); main.start(); Assert.assertFalse("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); snapDir.setWritable(true); main.deleteDirs(); }
/** * Tests that the ZooKeeper server will fail to start if the * transaction log directory is read only. * * This test will fail if it is executed as root user. */ @Test(timeout = 30000) public void testReadOnlyTxnLogDir() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); // Start up the ZK server to automatically create the necessary directories // and capture the directory where data is stored MainThread main = new MainThread(CLIENT_PORT, true, null); File tmpDir = main.tmpDir; main.start(); Assert.assertTrue("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); // Make the transaction log directory read only File logDir = new File(main.logDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); logDir.setWritable(false); // Restart ZK and observe a failure main = new MainThread(CLIENT_PORT, false, tmpDir, null); main.start(); Assert.assertFalse("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); logDir.setWritable(true); main.deleteDirs(); }
/** * Verify the ability to start a standalone server instance. */ @Test public void testStandalone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, true, null); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); clientConnected = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); zk.close(); main.shutdown(); main.join(); main.deleteDirs(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
/** * Test verifies that the server is able to redefine if user configured only * minSessionTimeout limit */ @Test public void testWithOnlyMinSessionTimeout() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final int tickTime = 2000; final int minSessionTimeout = tickTime * 2 - 100; int maxSessionTimeout = 20 * tickTime; final String configs = "minSessionTimeout=" + minSessionTimeout + "\n"; MainThread main = new MainThread(CLIENT_PORT, true, configs); main.start(); String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); // create session with min value verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); // create session with max value verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); main.shutdown(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
/** * Test verifies that the server is able to redefine the min/max session * timeouts */ @Test public void testMinMaxSessionTimeOut() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final int tickTime = 2000; final int minSessionTimeout = tickTime * 2 - 100; final int maxSessionTimeout = 20 * tickTime + 1000; final String configs = "maxSessionTimeout=" + maxSessionTimeout + "\n" + "minSessionTimeout=" + minSessionTimeout + "\n"; MainThread main = new MainThread(CLIENT_PORT, true, configs); main.start(); String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); // create session with min value verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); // create session with max value verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); main.shutdown(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
/** * This test checks that SSL works in cluster setup of ZK servers, which includes: * 1. setting "secureClientPort" in "zoo.cfg" file. * 2. setting jvm flags for serverCnxn, keystore, truststore. * Finally, a zookeeper client should be able to connect to the secure port and * communicate with server via secure connection. * <p/> * Note that in this test a ZK server has two ports -- clientPort and secureClientPort. */ @Test public void testSecureQuorumServer() throws Exception { final int SERVER_COUNT = 3; final int clientPorts[] = new int[SERVER_COUNT]; final Integer secureClientPorts[] = new Integer[SERVER_COUNT]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < SERVER_COUNT; i++) { clientPorts[i] = PortAssignment.unique(); secureClientPorts[i] = PortAssignment.unique(); String server = String.format("server.%d=localhost:%d:%d:participant;localhost:%d", i, PortAssignment.unique(), PortAssignment.unique(), clientPorts[i]); sb.append(server + "\n"); } String quorumCfg = sb.toString(); MainThread[] mt = new MainThread[SERVER_COUNT]; for (int i = 0; i < SERVER_COUNT; i++) { mt[i] = new MainThread(i, quorumCfg, secureClientPorts[i], true); mt[i].start(); } // Servers have been set up. Now go test if secure connection is successful. for (int i = 0; i < SERVER_COUNT; i++) { Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], TIMEOUT)); ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + secureClientPorts[i], TIMEOUT); // Do a simple operation to make sure the connection is fine. zk.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.delete("/test", -1); zk.close(); } for (int i = 0; i < mt.length; i++) { mt[i].shutdown(); } }