public static boolean createQuota()

in kyuubi-relocated-zookeeper-parent/kyuubi-relocated-zookeeper-34/src/main/java/org/apache/zookeeper/ZooKeeperMain.java [485:567]


  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;
  }