示例#1
0
  /**
   * remove the path from the datatree
   *
   * @param path the path to of the node to be deleted
   * @param zxid the current zxid
   * @throws KeeperException.NoNodeException
   */
  public void deleteNode(String path, long zxid) throws KeeperException.NoNodeException {
    int lastSlash = path.lastIndexOf('/');
    String parentName = path.substring(0, lastSlash);
    String childName = path.substring(lastSlash + 1);
    DataNode node = nodes.get(path);
    if (node == null) {
      throw new KeeperException.NoNodeException();
    }
    nodes.remove(path);
    DataNode parent = nodes.get(parentName);
    if (parent == null) {
      throw new KeeperException.NoNodeException();
    }
    synchronized (parent) {
      parent.children.remove(childName);
      parent.stat.setCversion(parent.stat.getCversion() + 1);
      parent.stat.setPzxid(zxid);
      long eowner = node.stat.getEphemeralOwner();
      if (eowner != 0) {
        HashSet<String> nodes = ephemerals.get(eowner);
        if (nodes != null) {
          synchronized (nodes) {
            nodes.remove(path);
          }
        }
      }
      node.parent = null;
    }
    if (parentName.startsWith(procZookeeper)) {
      // delete the node in the trie.
      if (Quotas.limitNode.equals(childName)) {
        // we need to update the trie
        // as well
        pTrie.deletePath(parentName.substring(quotaZookeeper.length()));
      }
    }

    // also check to update the quotas for this node
    String lastPrefix = pTrie.findMaxPrefix(path);
    if (!rootZookeeper.equals(lastPrefix) && !("".equals(lastPrefix))) {
      // ok we have some match and need to update
      updateCount(lastPrefix, -1);
      updateBytes(lastPrefix, node.data == null ? 0 : -(node.data.length));
    }
    ZooTrace.logTraceMessage(
        LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "dataWatches.triggerWatch " + path);
    ZooTrace.logTraceMessage(
        LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "childWatches.triggerWatch " + parentName);
    Set<Watcher> processed = dataWatches.triggerWatch(path, EventType.NodeDeleted);
    childWatches.triggerWatch(path, EventType.NodeDeleted, processed);
    childWatches.triggerWatch(
        parentName.equals("") ? "/" : parentName, EventType.NodeChildrenChanged);
  }
示例#2
0
 /**
  * @param path
  * @param data
  * @param acl
  * @param ephemeralOwner the session id that owns this node. -1 indicates this is not an ephemeral
  *     node.
  * @param zxid
  * @param time
  * @return the patch of the created node
  * @throws KeeperException
  */
 public String createNode(
     String path, byte data[], List<ACL> acl, long ephemeralOwner, long zxid, long time)
     throws KeeperException.NoNodeException, KeeperException.NodeExistsException {
   int lastSlash = path.lastIndexOf('/');
   String parentName = path.substring(0, lastSlash);
   String childName = path.substring(lastSlash + 1);
   StatPersisted stat = new StatPersisted();
   stat.setCtime(time);
   stat.setMtime(time);
   stat.setCzxid(zxid);
   stat.setMzxid(zxid);
   stat.setPzxid(zxid);
   stat.setVersion(0);
   stat.setAversion(0);
   stat.setEphemeralOwner(ephemeralOwner);
   DataNode parent = nodes.get(parentName);
   if (parent == null) {
     throw new KeeperException.NoNodeException();
   }
   synchronized (parent) {
     if (parent.children.contains(childName)) {
       throw new KeeperException.NodeExistsException();
     }
     int cver = parent.stat.getCversion();
     cver++;
     parent.stat.setCversion(cver);
     parent.stat.setPzxid(zxid);
     Long longval = convertAcls(acl);
     DataNode child = new DataNode(parent, data, longval, stat);
     parent.children.add(childName);
     nodes.put(path, child);
     if (ephemeralOwner != 0) {
       HashSet<String> list = ephemerals.get(ephemeralOwner);
       if (list == null) {
         list = new HashSet<String>();
         ephemerals.put(ephemeralOwner, list);
       }
       synchronized (list) {
         list.add(path);
       }
     }
   }
   // now check if its one of the zookeeper node child
   if (parentName.startsWith(quotaZookeeper)) {
     // now check if its the limit node
     if (Quotas.limitNode.equals(childName)) {
       // this is the limit node
       // get the parent and add it to the trie
       pTrie.addPath(parentName.substring(quotaZookeeper.length()));
     }
     if (Quotas.statNode.equals(childName)) {
       updateQuotaForPath(parentName.substring(quotaZookeeper.length()));
     }
   }
   // also check to update the quotas for this node
   String lastPrefix = pTrie.findMaxPrefix(path);
   if (!rootZookeeper.equals(lastPrefix) && !("".equals(lastPrefix))) {
     // ok we have some match and need to update
     updateCount(lastPrefix, 1);
     updateBytes(lastPrefix, data == null ? 0 : data.length);
   }
   dataWatches.triggerWatch(path, Event.EventType.NodeCreated);
   childWatches.triggerWatch(
       parentName.equals("") ? "/" : parentName, Event.EventType.NodeChildrenChanged);
   return path;
 }