static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { if (factory != null) { ZKDatabase zkDb; { ZooKeeperServer zs = getServer(factory); zkDb = zs.getZKDatabase(); } factory.shutdown(); try { zkDb.close(); } catch (IOException ie) { LOG.warn("Error closing logs ", ie); } final int PORT = getPort(hostPort); Assert.assertTrue("waiting for server down", ClientBaseWithFixes.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
public MiniZKFCCluster(Configuration conf, ZooKeeperServer zks) { this.conf = conf; // Fast check interval so tests run faster conf.setInt(CommonConfigurationKeys.HA_HM_CHECK_INTERVAL_KEY, 50); conf.setInt(CommonConfigurationKeys.HA_HM_CONNECT_RETRY_INTERVAL_KEY, 50); conf.setInt(CommonConfigurationKeys.HA_HM_SLEEP_AFTER_DISCONNECT_KEY, 50); svcs = new ArrayList<DummyHAService>(2); // remove any existing instances we are keeping track of DummyHAService.instances.clear(); for (int i = 0; i < 2; i++) { addSvcs(svcs, i); } this.ctx = new TestContext(); this.zks = zks; }
/** * 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)); }
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)); } }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { CommandResponse response = initializeResponse(); long traceMask; if (!kwargs.containsKey("traceMask")) { response.put("error", "setTraceMask requires long traceMask argument"); return response; } try { traceMask = Long.parseLong(kwargs.get("traceMask")); } catch (NumberFormatException e) { response.put("error", "setTraceMask requires long traceMask argument, got " + kwargs.get("traceMask")); return response; } ZooTrace.setTextTraceLevel(traceMask); response.put("tracemask", traceMask); return response; }
@BeforeClass public static void setupZooKeeper() throws Exception { LOG.info("Starting ZK server"); zkTmpDir = File.createTempFile("zookeeper", "test"); zkTmpDir.delete(); zkTmpDir.mkdir(); try { zks = new ZooKeeperServer(zkTmpDir, zkTmpDir, ZooKeeperDefaultPort); serverFactory = new NIOServerCnxnFactory(); serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), 10); serverFactory.startup(zks); } catch (Exception e) { LOG.error("Exception while instantiating ZooKeeper", e); } boolean b = LocalBookKeeper.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT); LOG.debug("ZooKeeper server up: " + b); }
@BeforeClass public static void setupZooKeeper() throws Exception { // create a ZooKeeper server(dataDir, dataLogDir, port) LOG.info("Starting ZK server"); ZkTmpDir = File.createTempFile("zookeeper", "test"); ZkTmpDir.delete(); ZkTmpDir.mkdir(); try { zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort); serverFactory = new NIOServerCnxnFactory(); serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), 10); serverFactory.startup(zks); } catch (Exception e) { LOG.error("Exception while instantiating ZooKeeper", e); } boolean b = LocalBookKeeper.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT); LOG.debug("ZooKeeper server up: " + b); }
public MiniZooKeeperCluster(Configuration configuration) { this.started = false; this.configuration = configuration; activeZKServerIndex = -1; zooKeeperServers = new ArrayList<ZooKeeperServer>(); clientPortList = new ArrayList<Integer>(); standaloneServerFactoryList = new ArrayList<NIOServerCnxnFactory>(); }
public static void waitForActiveLockData(TestContext ctx, ZooKeeperServer zks, String parentDir, byte[] activeData) throws Exception { long st = Time.now(); long lastPrint = st; while (true) { if (ctx != null) { ctx.checkException(); } try { Stat stat = new Stat(); byte[] data = zks.getZKDatabase().getData( parentDir + "/" + ActiveStandbyElector.LOCK_FILENAME, stat, null); if (activeData != null && Arrays.equals(activeData, data)) { return; } if (Time.now() > lastPrint + LOG_INTERVAL_MS) { LOG.info("Cur data: " + StringUtils.byteToHexString(data)); lastPrint = Time.now(); } } catch (NoNodeException nne) { if (activeData == null) { return; } if (Time.now() > lastPrint + LOG_INTERVAL_MS) { LOG.info("Cur data: no node"); lastPrint = Time.now(); } } Thread.sleep(50); } }
EmbeddedZooKeeper() throws IOException, InterruptedException { this.tmpDir = Files.createTempDirectory(null).toFile(); this.factory = new NIOServerCnxnFactory(); this.zookeeper = new ZooKeeperServer(new File(tmpDir, "data"), new File(tmpDir, "log"), TICK_TIME); InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 0); factory.configure(addr, 0); factory.startup(zookeeper); this.port = zookeeper.getClientPort(); }
public ReadOnlyRequestProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { super("ReadOnlyRequestProcessor:" + zks.getServerId(), zks .getZooKeeperServerListener()); this.zks = zks; this.nextProcessor = nextProcessor; }
synchronized public ZooKeeperServer getActiveServer(){ if(leader!=null) return leader.zk; else if(follower!=null) return follower.zk; else if (observer != null) return observer.zk; return null; }
protected void unregisterJMX(ZooKeeperServer zks) { // unregister from JMX try { if (jmxServerBean != null) { MBeanRegistry.getInstance().unregister(jmxServerBean); } } catch (Exception e) { LOG.warn("Failed to unregister with JMX", e); } jmxServerBean = null; }
public static void initialize() { synchronized (ProviderRegistry.class) { if (initialized) return; IPAuthenticationProvider ipp = new IPAuthenticationProvider(); DigestAuthenticationProvider digp = new DigestAuthenticationProvider(); authenticationProviders.put(ipp.getScheme(), ipp); authenticationProviders.put(digp.getScheme(), digp); Enumeration<Object> en = System.getProperties().keys(); while (en.hasMoreElements()) { String k = (String) en.nextElement(); if (k.startsWith("zookeeper.authProvider.")) { String className = System.getProperty(k); try { Class<?> c = ZooKeeperServer.class.getClassLoader() .loadClass(className); AuthenticationProvider ap = (AuthenticationProvider) c .newInstance(); authenticationProviders.put(ap.getScheme(), ap); } catch (Exception e) { LOG.warn("Problems loading " + className,e); } } } initialized = true; } }
private ConversableFollower createFollower(File tmpDir, QuorumPeer peer) throws IOException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); ZKDatabase zkDb = new ZKDatabase(logFactory); FollowerZooKeeperServer zk = new FollowerZooKeeperServer(logFactory, peer, new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); peer.setZKDatabase(zkDb); return new ConversableFollower(peer, zk); }
/** 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]); ServerCnxnFactory factory = ServerCnxnFactory.createFactory(PORT, -1); factory.startup(zks); System.out.println("Comment: starting factory"); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); factory.shutdown(); zks.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); System.out.println("Comment: shutting down standalone"); }
/** * 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]); ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); f.startup(zks); LOG.info("starting up the zookeeper server .. waiting"); Assert.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 Assert.assertTrue(zk.exists("/9/9/8", false) != null); } finally { zk.close(); } f.shutdown(); zks.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test 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]); ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); f.startup(zks); try { LOG.info("starting up the zookeeper server .. waiting"); Assert.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(); } } finally { f.shutdown(); zks.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); } }
/** * test the upgrade * @throws Exception */ @Test 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]); ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); f.startup(zks); LOG.info("starting up the zookeeper server .. waiting"); Assert.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++) { Assert.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) { Assert.assertTrue(false); } zk.close(); // bring down the server f.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
public MiniZooKeeperCluster() { this.started = false; // this.configuration = configuration; activeZKServerIndex = -1; zooKeeperServers = new ArrayList<ZooKeeperServer>(); clientPortList = new ArrayList<Integer>(); standaloneServerFactoryList = new ArrayList<NIOServerCnxnFactory>(); }
public void setZooKeeperServer(ZooKeeperServer zks) { if (cnxnFactory != null) { cnxnFactory.setZooKeeperServer(zks); } if (secureCnxnFactory != null) { secureCnxnFactory.setZooKeeperServer(zks); } }
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; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { CommandResponse response = initializeResponse(); zkServer.getServerCnxnFactory().resetAllConnectionStats(); return response; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { CommandResponse response = initializeResponse(); response.put("datadir_size", zkServer.getDataDirSize()); response.put("logdir_size", zkServer.getLogDirSize()); return response; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { CommandResponse response = initializeResponse(); for (Entry e : Environment.list()) { response.put(e.getKey(), e.getValue()); } return response; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { CommandResponse response = initializeResponse(); LOG.info("running stat"); response.put("version", Version.getFullVersion()); response.put("read_only", zkServer instanceof ReadOnlyZooKeeperServer); response.put("server_stats", zkServer.serverStats()); response.put("node_count", zkServer.getZKDatabase().getNodeCount()); return response; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { DataTree dt = zkServer.getZKDatabase().getDataTree(); CommandResponse response = initializeResponse(); response.put("path_to_session_ids", dt.getWatchesByPath().toMap()); return response; }
@Override public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) { DataTree dt = zkServer.getZKDatabase().getDataTree(); CommandResponse response = initializeResponse(); response.putAll(dt.getWatchesSummary().toMap()); return response; }
private byte[] getKey(ZooKeeperServer zks) { ZKDatabase db = zks.getZKDatabase(); if (db != null) { try { Stat stat = new Stat(); return db.getData("/key", stat, null); } catch (NoNodeException e) { LOG.error("getData failed", e); } } return null; }
public static void initialize() { synchronized (ProviderRegistry.class) { if (initialized) return; IPAuthenticationProvider ipp = new IPAuthenticationProvider(); DigestAuthenticationProvider digp = new DigestAuthenticationProvider(); authenticationProviders.put(ipp.getScheme(), ipp); authenticationProviders.put(digp.getScheme(), digp); Enumeration<Object> en = System.getProperties().keys(); while (en.hasMoreElements()) { String k = (String) en.nextElement(); if (k.startsWith("zookeeper.authProvider.")) { String className = System.getProperty(k); try { Class<?> c = ZooKeeperServer.class.getClassLoader() .loadClass(className); AuthenticationProvider ap = (AuthenticationProvider) c.getDeclaredConstructor() .newInstance(); authenticationProviders.put(ap.getScheme(), ap); } catch (Exception e) { LOG.warn("Problems loading " + className,e); } } } initialized = true; } }
/** * Checks that running a given Command returns the expected Map. Asserts * that all specified keys are present with values of the specified types * and that there are no extra entries. * * @param cmdName * - the primary name of the command * @param kwargs * - keyword arguments to the command * @param keys * - the keys that are expected in the returned Map * @param types * - the classes of the values in the returned Map. types[i] is * the type of the value for keys[i]. * @throws IOException * @throws InterruptedException */ public void testCommand(String cmdName, Map<String, String> kwargs, Field... fields) throws IOException, InterruptedException { ZooKeeperServer zks = serverFactory.getZooKeeperServer(); Map<String, Object> result = Commands.runCommand(cmdName, zks, kwargs).toMap(); assertTrue(result.containsKey("command")); // This is only true because we're setting cmdName to the primary name assertEquals(cmdName, result.remove("command")); assertTrue(result.containsKey("error")); assertNull("error: " + result.get("error"), result.remove("error")); for (Field field : fields) { String k = field.key; assertTrue("Result from command " + cmdName + " missing field \"" + k + "\"" + "\n" + result, result.containsKey(k)); Class<?> t = field.type; Object v = result.remove(k); assertTrue("\"" + k + "\" field from command " + cmdName + " should be of type " + t + ", is actually of type " + v.getClass(), t.isAssignableFrom(v.getClass())); } assertTrue("Result from command " + cmdName + " contains extra fields: " + result, result.isEmpty()); }