Class DataTree


  • public class DataTree
    extends Object
    This class maintains the tree data structure. It doesn't have any networking or client connection code in it so that it can be tested in a stand alone way.

    The tree maintains two parallel data structures: a hashtable that maps from full paths to DataNodes and a tree of DataNodes. All accesses to a path is through the hashtable. The tree is traversed only when serializing to disk.

    • Field Detail

      • STAT_OVERHEAD_BYTES

        public static final int STAT_OVERHEAD_BYTES
        over-the-wire size of znode's stat. Counting the fields of Stat class
        See Also:
        Constant Field Values
      • lastProcessedZxid

        public volatile long lastProcessedZxid
    • Constructor Detail

      • DataTree

        public DataTree()
    • Method Detail

      • getEphemerals

        public Set<String> getEphemerals​(long sessionId)
      • getContainers

        public Set<String> getContainers()
      • getNodeCount

        public int getNodeCount()
      • getWatchCount

        public int getWatchCount()
      • getEphemeralsCount

        public int getEphemeralsCount()
      • approximateDataSize

        public long approximateDataSize()
        Get the size of the nodes based on path and data length.
        Returns:
        size of the data
      • cachedApproximateDataSize

        public long cachedApproximateDataSize()
      • addConfigNode

        public void addConfigNode()
        create a /zookeeper/config node for maintaining the configuration (membership and quorum system) info for zookeeper
      • copyStat

        public static void copyStat​(Stat from,
                                    Stat to)
      • updateQuotaStat

        public void updateQuotaStat​(String lastPrefix,
                                    long bytesDiff,
                                    int countDiff)
        update the count/bytes of this stat data node
        Parameters:
        lastPrefix - the path of the node that has a quota.
        bytesDiff - the diff to be added to number of bytes
        countDiff - the diff to be added to the count
      • getMaxPrefixWithQuota

        public String getMaxPrefixWithQuota​(String path)
        If there is a quota set, return the appropriate prefix for that quota Else return null
        Parameters:
        path - The ZK path to check for quota
        Returns:
        Max quota prefix, or null if none
      • addWatch

        public void addWatch​(String basePath,
                             Watcher watcher,
                             int mode)
      • getAllChildrenNumber

        public int getAllChildrenNumber​(String path)
      • aclCacheSize

        public int aclCacheSize()
      • dumpWatchesSummary

        public void dumpWatchesSummary​(PrintWriter pwriter)
        Summary of the watches on the datatree.
        Parameters:
        pwriter - the output to write to
      • dumpWatches

        public void dumpWatches​(PrintWriter pwriter,
                                boolean byPath)
        Write a text dump of all the watches on the datatree. Warning, this is expensive, use sparingly!
        Parameters:
        pwriter - the output to write to
      • dumpEphemerals

        public void dumpEphemerals​(PrintWriter pwriter)
        Write a text dump of all the ephemerals in the datatree.
        Parameters:
        pwriter - the output to write to
      • shutdownWatcher

        public void shutdownWatcher()
      • getEphemerals

        public Map<Long,​Set<String>> getEphemerals()
        Returns a mapping of session ID to ephemeral znodes.
        Returns:
        map of session ID to sets of ephemeral znodes
      • removeCnxn

        public void removeCnxn​(Watcher watcher)
      • setCversionPzxid

        public void setCversionPzxid​(String path,
                                     int newCversion,
                                     long zxid)
                              throws KeeperException.NoNodeException
        This method sets the Cversion and Pzxid for the specified node to the values passed as arguments. The values are modified only if newCversion is greater than the current Cversion. A NoNodeException is thrown if a znode for the specified path is not found.
        Parameters:
        path - Full path to the znode whose Cversion needs to be modified. A "/" at the end of the path is ignored.
        newCversion - Value to be assigned to Cversion
        zxid - Value to be assigned to Pzxid
        Throws:
        KeeperException.NoNodeException - If znode not found.
      • serializeZxidDigest

        public boolean serializeZxidDigest​(OutputArchive oa)
                                    throws IOException
        Serializing the digest to snapshot, this is done after the data tree is being serialized, so when we replay the txns and it hits this zxid we know we should be in a non-fuzzy state, and have the same digest.
        Parameters:
        oa - the output stream to write to
        Returns:
        true if the digest is serialized successfully
        Throws:
        IOException
      • deserializeZxidDigest

        public boolean deserializeZxidDigest​(InputArchive ia,
                                             long startZxidOfSnapshot)
                                      throws IOException
        Deserializing the zxid digest from the input stream and update the digestFromLoadedSnapshot.
        Parameters:
        ia - the input stream to read from
        startZxidOfSnapshot - the zxid of snapshot file
        Returns:
        the true if it deserialized successfully
        Throws:
        IOException
      • compareSnapshotDigests

        public void compareSnapshotDigests​(long zxid)
        Compares the actual tree's digest with that in the snapshot. Resets digestFromLoadedSnapshot after comparision.
        Parameters:
        zxid - zxid
      • compareDigest

        public boolean compareDigest​(TxnHeader header,
                                     Record txn,
                                     TxnDigest digest)
        Compares the digest of the tree with the digest present in transaction digest. If there is any error, logs and alerts the watchers.
        Parameters:
        header - transaction header being applied
        txn - transaction
        digest - transaction digest
        Returns:
        false if digest in the txn doesn't match what we have now in the data tree
      • reportDigestMismatch

        public void reportDigestMismatch​(long zxid)
        Reports any mismatch in the transaction digest.
        Parameters:
        zxid - zxid for which the error is being reported.
      • getTreeDigest

        public long getTreeDigest()
      • addDigestWatcher

        public void addDigestWatcher​(DigestWatcher digestWatcher)
        Add digest mismatch event handler.
        Parameters:
        digestWatcher - the handler to add
      • getDigestLog

        public List<DataTree.ZxidDigest> getDigestLog()
        Return all the digests in the historical digest list.
      • createStat

        public static StatPersisted createStat​(long zxid,
                                               long time,
                                               long ephemeralOwner)
        Create a node stat from the given params.
        Parameters:
        zxid - the zxid associated with the txn
        time - the time when the txn is created
        ephemeralOwner - the owner if the node is an ephemeral
        Returns:
        the stat