Search in sources :

Example 6 with StatPersisted

use of org.apache.zookeeper.data.StatPersisted in project zookeeper by apache.

the class DataTree method serializeNode.

/**
 * this method uses a stringbuilder to create a new path for children. This
 * is faster than string appends ( str1 + str2).
 *
 * @param oa
 *            OutputArchive to write to.
 * @param path
 *            a string builder.
 * @throws IOException
 */
void serializeNode(OutputArchive oa, StringBuilder path) throws IOException {
    String pathString = path.toString();
    DataNode node = getNode(pathString);
    if (node == null) {
        return;
    }
    String[] children = null;
    DataNode nodeCopy;
    synchronized (node) {
        StatPersisted statCopy = new StatPersisted();
        copyStatPersisted(node.stat, statCopy);
        // we do not need to make a copy of node.data because the contents
        // are never changed
        nodeCopy = new DataNode(node.data, node.acl, statCopy);
        Set<String> childs = node.getChildren();
        children = childs.toArray(new String[childs.size()]);
    }
    serializeNodeData(oa, pathString, nodeCopy);
    path.append('/');
    int off = path.length();
    for (String child : children) {
        // since this is single buffer being resused
        // we need
        // to truncate the previous bytes of string.
        path.delete(off, Integer.MAX_VALUE);
        path.append(child);
        serializeNode(oa, path);
    }
}
Also used : StatPersisted(org.apache.zookeeper.data.StatPersisted)

Example 7 with StatPersisted

use of org.apache.zookeeper.data.StatPersisted in project zookeeper by apache.

the class DataTree method createNode.

/**
 * Add a new node to the DataTree.
 * @param path
 *            Path for the new node.
 * @param data
 *            Data to store in the node.
 * @param acl
 *            Node acls
 * @param ephemeralOwner
 *            the session id that owns this node. -1 indicates this is not
 *            an ephemeral node.
 * @param zxid
 *            Transaction ID
 * @param time
 * @param outputStat
 *            A Stat object to store Stat output results into.
 * @throws NodeExistsException
 * @throws NoNodeException
 */
public void createNode(final String path, byte[] data, List<ACL> acl, long ephemeralOwner, int parentCVersion, long zxid, long time, Stat outputStat) throws KeeperException.NoNodeException, KeeperException.NodeExistsException {
    int lastSlash = path.lastIndexOf('/');
    String parentName = path.substring(0, lastSlash);
    String childName = path.substring(lastSlash + 1);
    StatPersisted stat = createStat(zxid, time, ephemeralOwner);
    DataNode parent = nodes.get(parentName);
    if (parent == null) {
        throw new KeeperException.NoNodeException();
    }
    synchronized (parent) {
        // Add the ACL to ACL cache first, to avoid the ACL not being
        // created race condition during fuzzy snapshot sync.
        // 
        // This is the simplest fix, which may add ACL reference count
        // again if it's already counted in in the ACL map of fuzzy
        // snapshot, which might also happen for deleteNode txn, but
        // at least it won't cause the ACL not exist issue.
        // 
        // Later we can audit and delete all non-referenced ACLs from
        // ACL map when loading the snapshot/txns from disk, like what
        // we did for the global sessions.
        Long longval = aclCache.convertAcls(acl);
        Set<String> children = parent.getChildren();
        if (children.contains(childName)) {
            throw new KeeperException.NodeExistsException();
        }
        nodes.preChange(parentName, parent);
        if (parentCVersion == -1) {
            parentCVersion = parent.stat.getCversion();
            parentCVersion++;
        }
        // larger.
        if (parentCVersion > parent.stat.getCversion()) {
            parent.stat.setCversion(parentCVersion);
            parent.stat.setPzxid(zxid);
        }
        DataNode child = new DataNode(data, longval, stat);
        parent.addChild(childName);
        nodes.postChange(parentName, parent);
        nodeDataSize.addAndGet(getNodeSize(path, child.data));
        nodes.put(path, child);
        EphemeralType ephemeralType = EphemeralType.get(ephemeralOwner);
        if (ephemeralType == EphemeralType.CONTAINER) {
            containers.add(path);
        } else if (ephemeralType == EphemeralType.TTL) {
            ttls.add(path);
        } else 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);
            }
        }
        if (outputStat != null) {
            child.copyStat(outputStat);
        }
    }
    // 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(Quotas.trimQuotaPath(parentName));
        }
        if (Quotas.statNode.equals(childName)) {
            updateQuotaForPath(Quotas.trimQuotaPath(parentName));
        }
    }
    String lastPrefix = getMaxPrefixWithQuota(path);
    long bytes = data == null ? 0 : data.length;
    // also check to update the quotas for this node
    if (lastPrefix != null) {
        // ok we have some match and need to update
        updateQuotaStat(lastPrefix, bytes, 1);
    }
    updateWriteStat(path, bytes);
    dataWatches.triggerWatch(path, Event.EventType.NodeCreated);
    childWatches.triggerWatch(parentName.equals("") ? "/" : parentName, Event.EventType.NodeChildrenChanged);
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) AtomicLong(java.util.concurrent.atomic.AtomicLong) StatPersisted(org.apache.zookeeper.data.StatPersisted)

Aggregations

StatPersisted (org.apache.zookeeper.data.StatPersisted)7 CreateMode (org.apache.zookeeper.CreateMode)2 KeeperException (org.apache.zookeeper.KeeperException)2 ACL (org.apache.zookeeper.data.ACL)2 CreateRequest (org.apache.zookeeper.proto.CreateRequest)2 ChangeRecord (org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 TxnHeader (org.apache.zookeeper.txn.TxnHeader)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BadArgumentsException (org.apache.zookeeper.KeeperException.BadArgumentsException)1 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)1 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)1 CheckVersionRequest (org.apache.zookeeper.proto.CheckVersionRequest)1 CreateTTLRequest (org.apache.zookeeper.proto.CreateTTLRequest)1 DeleteRequest (org.apache.zookeeper.proto.DeleteRequest)1 SetACLRequest (org.apache.zookeeper.proto.SetACLRequest)1 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)1 CheckVersionTxn (org.apache.zookeeper.txn.CheckVersionTxn)1 CreateContainerTxn (org.apache.zookeeper.txn.CreateContainerTxn)1 CreateSessionTxn (org.apache.zookeeper.txn.CreateSessionTxn)1