/** * update the quota for the given path * * @param path * the path to be used */ private void updateQuotaForPath(String path) { Counts c = new Counts(); getCounts(path, c); StatsTrack strack = new StatsTrack(); strack.setBytes(c.bytes); strack.setCount(c.count); String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; DataNode node = getNode(statPath); // it should exist if (node == null) { LOG.warn("Missing quota stat node " + statPath); return; } synchronized (node) { node.data = strack.toString().getBytes(); } }
@Override public boolean exec() throws KeeperException, InterruptedException { String path = args[1]; String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; try { err.println("absolute path is " + absolutePath); Stat stat = new Stat(); byte[] data = zk.getData(absolutePath, false, stat); StatsTrack st = new StatsTrack(new String(data)); out.println("Output quota for " + path + " " + st.toString()); data = zk.getData(Quotas.quotaZookeeper + path + "/" + Quotas.statNode, false, stat); out.println("Output stat for " + path + " " + new StatsTrack(new String(data)).toString()); } catch (KeeperException.NoNodeException ne) { err.println("quota for " + path + " does not exist."); } return false; }
/** * this method deletes quota for a node. * * @param zk the zookeeper client * @param path the path to delete quota for * @param bytes true if number of bytes needs to be unset * @param numNodes true if number of nodes needs to be unset * @return true if quota deletion is successful * @throws KeeperException * @throws IOException * @throws InterruptedException */ public static boolean delQuota(ZooKeeper zk, String path, boolean bytes, boolean numNodes) throws KeeperException, IOException, InterruptedException, MalformedPathException { String parentPath = Quotas.quotaZookeeper + path; String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; if (zk.exists(quotaPath, false) == null) { System.out.println("Quota does not exist for " + path); return true; } byte[] data = null; try { data = zk.getData(quotaPath, false, new Stat()); } catch (IllegalArgumentException ex) { throw new MalformedPathException(ex.getMessage()); } catch (KeeperException.NoNodeException ne) { System.err.println("quota does not exist for " + path); return true; } StatsTrack strack = new StatsTrack(new String(data)); if (bytes && !numNodes) { strack.setBytes(-1L); zk.setData(quotaPath, strack.toString().getBytes(), -1); } else if (!bytes && numNodes) { strack.setCount(-1); zk.setData(quotaPath, strack.toString().getBytes(), -1); } else if (bytes && numNodes) { // delete till you can find a node with more than // one child List<String> children = zk.getChildren(parentPath, false); /// delete the direct children first for (String child : children) { zk.delete(parentPath + "/" + child, -1); } // cut the tree till their is more than one child trimProcQuotas(zk, parentPath); } return true; }
/** * this method deletes quota for a node. * * @param zk the zookeeper client * @param path the path to delete quota for * @param bytes true if number of bytes needs to be unset * @param numNodes true if number of nodes needs to be unset * @return true if quota deletion is successful * @throws KeeperException * @throws IOException * @throws InterruptedException */ public static boolean delQuota(ZooKeeper zk, String path, boolean bytes, boolean numNodes) throws KeeperException, IOException, InterruptedException { String parentPath = Quotas.quotaZookeeper + path; String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; if (zk.exists(quotaPath, false) == null) { System.out.println("Quota does not exist for " + path); return true; } byte[] data = null; try { data = zk.getData(quotaPath, false, new Stat()); } catch (KeeperException.NoNodeException ne) { System.err.println("quota does not exist for " + path); return true; } StatsTrack strack = new StatsTrack(new String(data)); if (bytes && !numNodes) { strack.setBytes(-1L); zk.setData(quotaPath, strack.toString().getBytes(), -1); } else if (!bytes && numNodes) { strack.setCount(-1); zk.setData(quotaPath, strack.toString().getBytes(), -1); } else if (bytes && numNodes) { // delete till you can find a node with more than // one child List<String> children = zk.getChildren(parentPath, false); /// delete the direct children first for (String child : children) { zk.delete(parentPath + "/" + child, -1); } // cut the tree till their is more than one child trimProcQuotas(zk, parentPath); } return true; }
public void testQuota() throws IOException, InterruptedException, KeeperException, Exception { final ZooKeeper zk = createClient(); final String path = "/a/b/v"; // making sure setdata works on / zk.setData("/", "some".getBytes(), -1); zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); ZooKeeperMain.createQuota(zk, path, 1000L, 1000); // see if its set String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; byte[] data = zk.getData(absolutePath, false, new Stat()); StatsTrack st = new StatsTrack(new String(data)); assertTrue("bytes are set", st.getBytes() == 1000L); assertTrue("num count is set", st.getCount() == 1000); String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; byte[] qdata = zk.getData(statPath, false, new Stat()); StatsTrack qst = new StatsTrack(new String(qdata)); assertTrue("bytes are set", qst.getBytes() == 8L); assertTrue("count is set", qst.getCount() == 2); stopServer(); startServer(); stopServer(); startServer(); ZooKeeperServer server = serverFactory.getZooKeeperServer(); assertNotNull("Quota is still set", server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null); }
@Test public void testQuota() throws IOException, InterruptedException, KeeperException, Exception { final ZooKeeper zk = createClient(); final String path = "/a/b/v"; // making sure setdata works on / zk.setData("/", "some".getBytes(), -1); zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); ZooKeeperMain.createQuota(zk, path, 5L, 10); // see if its set String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; byte[] data = zk.getData(absolutePath, false, new Stat()); StatsTrack st = new StatsTrack(new String(data)); Assert.assertTrue("bytes are set", st.getBytes() == 5L); Assert.assertTrue("num count is set", st.getCount() == 10); String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; byte[] qdata = zk.getData(statPath, false, new Stat()); StatsTrack qst = new StatsTrack(new String(qdata)); Assert.assertTrue("bytes are set", qst.getBytes() == 8L); Assert.assertTrue("count is set", qst.getCount() == 2); //force server to restart and load from snapshot, not txn log stopServer(); startServer(); stopServer(); startServer(); ZooKeeperServer server = getServer(serverFactory); Assert.assertNotNull("Quota is still set", server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null); }