zookeeper源码之配置存储

Posted zhangwanhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper源码之配置存储相关的知识,希望对你有一定的参考价值。

  配置存储主要维护了一个树结构,实现了类似文件系统的读写修改操作。 

类图结构

技术分享图片

  DataTree内部维护了一个树结构,并且也维护了一个hastable结构,key为路径,value为节点,方便根据路径查询。 

技术分享图片
public class DataTree {
...
private final ConcurrentHashMap<String, DataNode> nodes =
        new ConcurrentHashMap<String, DataNode>();
...
public void addDataNode(String path, DataNode node) {
        nodes.put(path, node);
}
public DataNode getNode(String path) {
     return nodes.get(path);
}
public int getNodeCount() {
    return nodes.size();
}
public String createNode(String path, byte data[], List<ACL> acl,
            long ephemeralOwner, long zxid, long time)
            throws KeeperException.NoNodeException,
            KeeperException.NodeExistsException {
        ...
        String parentName = path.substring(0, lastSlash);
        ...
        DataNode parent = nodes.get(parentName);
        ...
        synchronized (parent) {
            Set<String> children = parent.getChildren();
            ...
            DataNode child = new DataNode(parent, data, longval, stat);
            parent.addChild(childName);
            nodes.put(path, child);
            ...
        }
       ...
        return path;
    }
public void deleteNode(String path, long zxid)
            throws KeeperException.NoNodeException {
        ...
        String parentName = path.substring(0, lastSlash);
        String childName = path.substring(lastSlash + 1);
        DataNode node = nodes.get(path);
        ...
        nodes.remove(path);
        DataNode parent = nodes.get(parentName);
        ...
        synchronized (parent) {
            parent.removeChild(childName);
            ....
            node.parent = null;
        }
        ...
    }
public Stat setData(String path, byte data[], int version, long zxid,
            long time) throws KeeperException.NoNodeException {
        Stat s = new Stat();
        DataNode n = nodes.get(path);
        ...
        synchronized (n) {
            lastdata = n.data;
            n.data = data;
            ...
            n.copyStat(s);
        }
        ...
        return s;
    }
public byte[] getData(String path, Stat stat, Watcher watcher)
            throws KeeperException.NoNodeException {
        DataNode n = nodes.get(path);
        ...
        return n.data;
    }
}
技术分享图片

   DataNode为树结构的一个节点,内部存储了父节点、子节点、节点数据、节点权限、持久化信息。

技术分享图片
public class DataNode{
    DataNode parent;
    byte data[];
    Long acl;
    public StatPersisted stat;
    private Set<String> children = null;
}
技术分享图片

 

以上是关于zookeeper源码之配置存储的主要内容,如果未能解决你的问题,请参考以下文章

zookeeper源码之配置监听

zookeeper源码之权限控制

zookeeper源码之临时节点管理

zookeeper源码之服务端数据管理

zookeeper源码之服务端启动模块

刚看完zookeeper源码,各位随便问!