|
@@ -43,8 +43,6 @@ import org.apache.zookeeper.cli.MalformedCommandException;
|
|
|
import org.apache.zookeeper.cli.VersionCommand;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.apache.zookeeper.ZooDefs.Ids;
|
|
|
-import org.apache.zookeeper.data.Stat;
|
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -381,214 +379,6 @@ public class ZooKeeperMain {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * trim the quota tree to recover unwanted tree elements
|
|
|
- * in the quota's tree
|
|
|
- * @param zk the zookeeper client
|
|
|
- * @param path the path to start from and go up and see if their
|
|
|
- * is any unwanted parent in the path.
|
|
|
- * @return true if sucessful
|
|
|
- * @throws KeeperException
|
|
|
- * @throws IOException
|
|
|
- * @throws InterruptedException
|
|
|
- */
|
|
|
- private static boolean trimProcQuotas(ZooKeeper zk, String path)
|
|
|
- throws KeeperException, IOException, InterruptedException
|
|
|
- {
|
|
|
- if (Quotas.quotaZookeeper.equals(path)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- List<String> children = zk.getChildren(path, false);
|
|
|
- if (children.size() == 0) {
|
|
|
- zk.delete(path, -1);
|
|
|
- String parent = path.substring(0, path.lastIndexOf('/'));
|
|
|
- return trimProcQuotas(zk, parent);
|
|
|
- } else {
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- private static void checkIfParentQuota(ZooKeeper zk, String path)
|
|
|
- throws InterruptedException, KeeperException
|
|
|
- {
|
|
|
- final String[] splits = path.split("/");
|
|
|
- String quotaPath = Quotas.quotaZookeeper;
|
|
|
- for (String str: splits) {
|
|
|
- if (str.length() == 0) {
|
|
|
- // this should only be for the beginning of the path
|
|
|
- // i.e. "/..." - split(path)[0] is empty string before first '/'
|
|
|
- continue;
|
|
|
- }
|
|
|
- quotaPath += "/" + str;
|
|
|
- List<String> children = null;
|
|
|
- try {
|
|
|
- children = zk.getChildren(quotaPath, false);
|
|
|
- } catch(KeeperException.NoNodeException ne) {
|
|
|
- LOG.debug("child removed during quota check", ne);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (children.size() == 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (String child: children) {
|
|
|
- if (Quotas.limitNode.equals(child)) {
|
|
|
- throw new IllegalArgumentException(path + " has a parent "
|
|
|
- + quotaPath + " which has a quota");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * this method creates a quota node for the path
|
|
|
- * @param zk the ZooKeeper client
|
|
|
- * @param path the path for which quota needs to be created
|
|
|
- * @param bytes the limit of bytes on this path
|
|
|
- * @param numNodes the limit of number of nodes on this path
|
|
|
- * @return true if its successful and false if not.
|
|
|
- */
|
|
|
- public static boolean createQuota(ZooKeeper zk, String path,
|
|
|
- long bytes, int numNodes)
|
|
|
- throws KeeperException, IOException, InterruptedException
|
|
|
- {
|
|
|
- // check if the path exists. We cannot create
|
|
|
- // quota for a path that already exists in zookeeper
|
|
|
- // for now.
|
|
|
- Stat initStat = zk.exists(path, false);
|
|
|
- if (initStat == null) {
|
|
|
- throw new IllegalArgumentException(path + " does not exist.");
|
|
|
- }
|
|
|
- // now check if their is already existing
|
|
|
- // parent or child that has quota
|
|
|
-
|
|
|
- String quotaPath = Quotas.quotaZookeeper;
|
|
|
- // check for more than 2 children --
|
|
|
- // if zookeeper_stats and zookeeper_qutoas
|
|
|
- // are not the children then this path
|
|
|
- // is an ancestor of some path that
|
|
|
- // already has quota
|
|
|
- String realPath = Quotas.quotaZookeeper + path;
|
|
|
- try {
|
|
|
- List<String> children = zk.getChildren(realPath, false);
|
|
|
- for (String child: children) {
|
|
|
- if (!child.startsWith("zookeeper_")) {
|
|
|
- throw new IllegalArgumentException(path + " has child " +
|
|
|
- child + " which has a quota");
|
|
|
- }
|
|
|
- }
|
|
|
- } catch(KeeperException.NoNodeException ne) {
|
|
|
- // this is fine
|
|
|
- }
|
|
|
-
|
|
|
- //check for any parent that has been quota
|
|
|
- checkIfParentQuota(zk, path);
|
|
|
-
|
|
|
- // this is valid node for quota
|
|
|
- // start creating all the parents
|
|
|
- if (zk.exists(quotaPath, false) == null) {
|
|
|
- try {
|
|
|
- zk.create(Quotas.procZookeeper, null, Ids.OPEN_ACL_UNSAFE,
|
|
|
- CreateMode.PERSISTENT);
|
|
|
- zk.create(Quotas.quotaZookeeper, null, Ids.OPEN_ACL_UNSAFE,
|
|
|
- CreateMode.PERSISTENT);
|
|
|
- } catch(KeeperException.NodeExistsException ne) {
|
|
|
- // do nothing
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // now create the direct children
|
|
|
- // and the stat and quota nodes
|
|
|
- String[] splits = path.split("/");
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- sb.append(quotaPath);
|
|
|
- for (int i=1; i<splits.length; i++) {
|
|
|
- sb.append("/" + splits[i]);
|
|
|
- quotaPath = sb.toString();
|
|
|
- try {
|
|
|
- zk.create(quotaPath, null, Ids.OPEN_ACL_UNSAFE ,
|
|
|
- CreateMode.PERSISTENT);
|
|
|
- } catch(KeeperException.NodeExistsException ne) {
|
|
|
- //do nothing
|
|
|
- }
|
|
|
- }
|
|
|
- String statPath = quotaPath + "/" + Quotas.statNode;
|
|
|
- quotaPath = quotaPath + "/" + Quotas.limitNode;
|
|
|
- StatsTrack strack = new StatsTrack(null);
|
|
|
- strack.setBytes(bytes);
|
|
|
- strack.setCount(numNodes);
|
|
|
- try {
|
|
|
- zk.create(quotaPath, strack.toString().getBytes(),
|
|
|
- Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
|
|
|
- StatsTrack stats = new StatsTrack(null);
|
|
|
- stats.setBytes(0L);
|
|
|
- stats.setCount(0);
|
|
|
- zk.create(statPath, stats.toString().getBytes(),
|
|
|
- Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
|
|
|
- } catch(KeeperException.NodeExistsException ne) {
|
|
|
- byte[] data = zk.getData(quotaPath, false , new Stat());
|
|
|
- StatsTrack strackC = new StatsTrack(new String(data));
|
|
|
- if (bytes != -1L) {
|
|
|
- strackC.setBytes(bytes);
|
|
|
- }
|
|
|
- if (numNodes != -1) {
|
|
|
- strackC.setCount(numNodes);
|
|
|
- }
|
|
|
- zk.setData(quotaPath, strackC.toString().getBytes(), -1);
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
protected boolean processCmd(MyCommandOptions co) throws CliException, IOException, InterruptedException {
|
|
|
boolean watch = false;
|
|
|
try {
|