/** * @param args */ private void runZookeeper() throws IOException{ // create a ZooKeeper server(dataDir, dataLogDir, port) LOG.info("Starting 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(new InetSocketAddress(ZooKeeperDefaultPort)); serverFactory.startup(zks); } catch (Exception e) { // TODO Auto-generated catch block LOG.fatal("Exception while instantiating ZooKeeper", e); } boolean b = waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT); LOG.debug("ZooKeeper server up: " + b); }
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); }
public QuorumPeer(Map<Long, QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, NIOServerCnxn.Factory cnxnFactory, QuorumVerifier quorumConfig) throws IOException { this(); this.cnxnFactory = cnxnFactory; this.quorumPeers = quorumPeers; this.electionType = electionType; this.myid = myid; this.tickTime = tickTime; this.initLimit = initLimit; this.syncLimit = syncLimit; this.logFactory = new FileTxnSnapLog(dataLogDir, dataDir); this.zkDb = new ZKDatabase(this.logFactory); if(quorumConfig == null) this.quorumConfig = new QuorumMaj(countParticipants(quorumPeers)); else this.quorumConfig = quorumConfig; }
public void testDisconnectedAddAuth() throws Exception { File tmpDir = ClientBase.createTmpDir(); ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); SyncRequestProcessor.setSnapCount(1000); final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); f.startup(zks); LOG.info("starting up the zookeeper server .. waiting"); assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); try { zk.addAuthInfo("digest", "pat:test".getBytes()); zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); } finally { zk.close(); } f.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
@Override protected void setUp() throws Exception { LOG.info("STARTING " + getName()); 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); assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); }
public static NIOServerCnxn.Factory createNewServerInstance(File dataDir, NIOServerCnxn.Factory factory, String hostPort, int maxCnxns) throws IOException, InterruptedException { ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); final int PORT = getPort(hostPort); if (factory == null) { factory = new NIOServerCnxn.Factory(new InetSocketAddress(PORT),maxCnxns); } factory.startup(zks); assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); return factory; }
static void shutdownServerInstance(NIOServerCnxn.Factory factory, String hostPort) { if (factory != null) { ZKDatabase zkDb = factory.getZooKeeperServer().getZKDatabase(); factory.shutdown(); try { zkDb.close(); } catch (IOException ie) { LOG.warn("Error closing logs ", ie); } final int PORT = getPort(hostPort); assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
public EmbeddedZookeeper(int port) throws IOException { this.port = port; zkDataDir = Files.createTempDir(); zkServer = new ZooKeeperServer(); FileTxnSnapLog ftxn = new FileTxnSnapLog(zkDataDir, zkDataDir); zkServer.setTxnLogFactory(ftxn); cnxnFactory = new NIOServerCnxn.Factory(new InetSocketAddress(this.port), 0); }
public EmbeddedZookeeper(final int port) throws IOException { this.port = port; zkDataDir = Files.createTempDir(); zkServer = new ZooKeeperServer(); final FileTxnSnapLog ftxn = new FileTxnSnapLog(zkDataDir, zkDataDir); zkServer.setTxnLogFactory(ftxn); cnxnFactory = new NIOServerCnxn.Factory(new InetSocketAddress(this.port), 0); }
/** * For backward compatibility purposes, we instantiate QuorumMaj by default. */ public QuorumPeer(Map<Long, QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, NIOServerCnxn.Factory cnxnFactory) throws IOException { this(quorumPeers, dataDir, dataLogDir, electionType, myid, tickTime, initLimit, syncLimit, cnxnFactory, new QuorumMaj(countParticipants(quorumPeers))); }
/** * This constructor is only used by the existing unit test code. * It defaults to FileLogProvider persistence provider. */ public QuorumPeer(Map<Long,QuorumServer> quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit) throws IOException { this(quorumPeers, snapDir, logDir, electionAlg, myid,tickTime, initLimit,syncLimit, new NIOServerCnxn.Factory( new InetSocketAddress(clientPort)), new QuorumMaj(countParticipants(quorumPeers))); }
/** * This constructor is only used by the existing unit test code. * It defaults to FileLogProvider persistence provider. */ public QuorumPeer(Map<Long,QuorumServer> quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit, QuorumVerifier quorumConfig) throws IOException { this(quorumPeers, snapDir, logDir, electionAlg, myid,tickTime, initLimit,syncLimit, new NIOServerCnxn.Factory(new InetSocketAddress(clientPort)), quorumConfig); }
public int getMaxClientCnxnsPerHost() { NIOServerCnxn.Factory fac = peer.getCnxnFactory(); if (fac == null) { return -1; } return fac.getMaxClientCnxns(); }
public MockQuorumPeer(Map<Long,QuorumServer> quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit) throws IOException { super(quorumPeers, snapDir, logDir, electionAlg, myid,tickTime, initLimit,syncLimit, new NIOServerCnxn.Factory(new InetSocketAddress(clientPort)), new QuorumMaj(countParticipants(quorumPeers))); }
/** * test the snapshot * @throws Exception an exception could be expected */ @Test public void testSnapshot() throws Exception { File snapDir = new File(testData, "invalidsnap"); ZooKeeperServer zks = new ZooKeeperServer(snapDir, snapDir, 3000); SyncRequestProcessor.setSnapCount(1000); final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); f.startup(zks); LOG.info("starting up the zookeeper server .. waiting"); assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper(HOSTPORT, 20000, this); try { // we know this from the data files // this node is the last node in the snapshot assertTrue(zk.exists("/9/9/8", false) != null); } finally { zk.close(); } f.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
/** * test the upgrade * @throws Exception */ public void testUpgrade() throws Exception { File upgradeDir = new File(testData, "upgrade"); UpgradeMain upgrade = new UpgradeMain(upgradeDir, upgradeDir); upgrade.runUpgrade(); ZooKeeperServer zks = new ZooKeeperServer(upgradeDir, upgradeDir, 3000); SyncRequestProcessor.setSnapCount(1000); final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); f.startup(zks); LOG.info("starting up the zookeeper server .. waiting"); assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); Stat stat = zk.exists("/", false); List<String> children = zk.getChildren("/", false); Collections.sort(children); for (int i = 0; i < 10; i++) { assertTrue("data tree sanity check", ("test-" + i).equals(children.get(i))); } //try creating one node zk.create("/upgrade", "upgrade".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // check if its there if (zk.exists("/upgrade", false) == null) { assertTrue(false); } zk.close(); // bring down the server f.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
/** bring up 5 quorum peers and then shut them down * and then bring one of the nodes as server * * @throws Exception might be thrown here */ @Test public void testFail() throws Exception { QuorumBase qb = new QuorumBase(); qb.setUp(); System.out.println("Comment: the servers are at " + qb.hostPort); ZooKeeper zk = qb.createClient(); zk.create("/test", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.close(); qb.shutdown(qb.s1); qb.shutdown(qb.s2); qb.shutdown(qb.s3); qb.shutdown(qb.s4); qb.shutdown(qb.s5); String hp = qb.hostPort.split(",")[0]; ZooKeeperServer zks = new ZooKeeperServer(qb.s1.getTxnFactory().getSnapDir(), qb.s1.getTxnFactory().getDataDir(), 3000); final int PORT = Integer.parseInt(hp.split(":")[1]); NIOServerCnxn.Factory factory = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); factory.startup(zks); System.out.println("Comment: starting factory"); assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); factory.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); System.out.println("Comment: shutting down standalone"); }
/** * test the purge * @throws Exception an exception might be thrown here */ public void testPurge() throws Exception { File tmpDir = ClientBase.createTmpDir(); ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); SyncRequestProcessor.setSnapCount(100); final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); f.startup(zks); assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT)); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); try { for (int i = 0; i< 2000; i++) { zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } finally { zk.close(); } f.shutdown(); assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); // now corrupt the snapshot PurgeTxnLog.purge(tmpDir, tmpDir, 3); FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir); List<File> listLogs = snaplog.findNRecentSnapshots(4); int numSnaps = 0; for (File ff: listLogs) { if (ff.getName().startsWith("snapshot")) { numSnaps++; } } assertTrue("exactly 3 snapshots ", (numSnaps == 3)); }
/** * Get the ZooKeeper connection factory using reflection. * @return {@link NIOServerCnxn.Factory} from ZooKeeper */ private NIOServerCnxn.Factory getCnxnFactory() { NIOServerCnxn.Factory factory = null; try { Field field = ZooKeeperServerMain.class.getDeclaredField("cnxnFactory"); field.setAccessible(true); factory = (NIOServerCnxn.Factory) field.get(this); // CHECKSTYLE: stop IllegalCatch } catch (Exception e) { // CHECKSTYLE: resume IllegalCatch LOG.error("Couldn't get cnxn factory", e); } return factory; }
@Before @Override public void setUp() throws Exception { try { // 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 NIOServerCnxn.Factory(new InetSocketAddress(ZooKeeperDefaultPort)); serverFactory.startup(zks); 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 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); // Create Bookie Servers (B1, B2, B3) for (int i = 0; i < numBookies; i++) { File f = File.createTempFile("bookie", "test"); tmpDirs.add(f); f.delete(); f.mkdir(); BookieServer server = new BookieServer(initialPort + i, HOSTPORT, f, new File[] { f }); server.start(); bs.add(server); } zkc.close(); bkc = new BookKeeper("127.0.0.1"); } catch(Exception e) { e.printStackTrace(); throw e; } }
@Override protected NIOServerCnxn createConnection(SocketChannel socket, SelectionKey selection) throws IOException { NIOServerCnxn ret = super.createConnection(socket, selection); validateRemoteOrClose(socket); return ret; }
@Override public void submitRequest(Request si) { long remaining = firstError != 0 ? LOCKOUT_DURATION_MS - (System.currentTimeMillis() - firstError) : 0; if ( si.type != ZooDefs.OpCode.createSession && si.type != ZooDefs.OpCode.sync && si.type != ZooDefs.OpCode.ping && firstError != 0 && remaining > 0 ) { log.debug("Rejected : " + si.toString()); // Still reject request log.debug("Still not ready for " + remaining + "ms"); ((NIOServerCnxn)si.cnxn).close(); return; } // Submit the request to the legacy Zookeeper server log.debug("Applied : " + si.toString()); super.submitRequest(si); // Raise an error if a lock is created if ( (si.type == ZooDefs.OpCode.create) || (si.type == ZooDefs.OpCode.create2) ) { CreateRequest createRequest = new CreateRequest(); try { ByteBuffer duplicate = si.request.duplicate(); duplicate.rewind(); ByteBufferInputStream.byteBuffer2Record(duplicate, createRequest); if ( createRequest.getPath().startsWith(CHAOS_ZNODE_PREFIX) && firstError == 0 ) { firstError = System.currentTimeMillis(); // The znode has been created, close the connection and don't tell it to client log.warn("Closing connection right after " + createRequest.getPath() + " creation"); ((NIOServerCnxn)si.cnxn).close(); } } catch ( Exception e ) { // Should not happen ((NIOServerCnxn)si.cnxn).close(); } } }
public NIOServerCnxn.Factory getCnxnFactory() { return cnxnFactory; }
public void setCnxnFactory(NIOServerCnxn.Factory cnxnFactory) { this.cnxnFactory = cnxnFactory; }
@Test public void testOOM() throws IOException, InterruptedException, KeeperException { // This test takes too long to run! if (true) return; File tmpDir = ClientBase.createTmpDir(); // Grab some memory so that it is easier to cause an // OOM condition; ArrayList<byte[]> hog = new ArrayList<byte[]>(); while (true) { try { hog.add(new byte[1024 * 1024 * 2]); } catch (OutOfMemoryError e) { hog.remove(0); break; } } ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); final int PORT = PortAssignment.unique(); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(PORT)); f.startup(zks); assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); System.err.println("OOM Stage 0"); utestPrep(PORT); System.out.println("Free = " + Runtime.getRuntime().freeMemory() + " total = " + Runtime.getRuntime().totalMemory() + " max = " + Runtime.getRuntime().maxMemory()); System.err.println("OOM Stage 1"); for (int i = 0; i < 1000; i++) { System.out.println(i); utestExists(PORT); } System.out.println("Free = " + Runtime.getRuntime().freeMemory() + " total = " + Runtime.getRuntime().totalMemory() + " max = " + Runtime.getRuntime().maxMemory()); System.err.println("OOM Stage 2"); for (int i = 0; i < 1000; i++) { System.out.println(i); utestGet(PORT); } System.out.println("Free = " + Runtime.getRuntime().freeMemory() + " total = " + Runtime.getRuntime().totalMemory() + " max = " + Runtime.getRuntime().maxMemory()); System.err.println("OOM Stage 3"); for (int i = 0; i < 1000; i++) { System.out.println(i); utestChildren(PORT); } System.out.println("Free = " + Runtime.getRuntime().freeMemory() + " total = " + Runtime.getRuntime().totalMemory() + " max = " + Runtime.getRuntime().maxMemory()); hog.get(0)[0] = (byte) 1; f.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); }
@Test /** * Verify that the server binds to the specified address */ public void testBindByAddress() throws Exception { String bindAddress = null; Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); // if we have a loopback and it has an address use it while(intfs.hasMoreElements()) { NetworkInterface i = intfs.nextElement(); if (i.isLoopback()) { Enumeration<InetAddress> addrs = i.getInetAddresses(); if (addrs.hasMoreElements()) { bindAddress = addrs.nextElement().getHostAddress(); // handle the ipv6 scope_id - ie remove it bindAddress = bindAddress.split("%")[0]; } } } if (bindAddress == null) { LOG.warn("Unable to determine loop back address, skipping test"); return; } final int PORT = PortAssignment.unique(); LOG.info("Using " + bindAddress + " as the bind address"); final String HOSTPORT = bindAddress + ":" + PORT; LOG.info("Using " + HOSTPORT + " as the host/port"); File tmpDir = ClientBase.createTmpDir(); ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); NIOServerCnxn.Factory f = new NIOServerCnxn.Factory( new InetSocketAddress(bindAddress, PORT)); f.startup(zks); LOG.info("starting up the the server, waiting"); assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); startSignal = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); try { startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); assertTrue("count == 0", startSignal.getCount() == 0); zk.close(); } finally { f.shutdown(); assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); } }
public QuorumPeer create(NIOServerCnxn.Factory cnxnFactory) throws IOException;
public NIOServerCnxn.Factory createConnectionFactory() throws IOException;