zookeeper源码分析三:Znode(DataNode)

zookeeper的数据模型是一个有层次的命名空间,类似于文件系统。zookeeper树里面的每个节点被称为znode。每个节点是用"/"的绝对路径来表示。
在这里插入图片描述

数据结构与数据组织方式:

ZKDatabase
/**
 * This class maintains the in memory database of zookeeper
 * server states that includes the sessions, datatree and the
 * committed logs. It is booted up  after reading the logs
 * and snapshots from the disk.
 */
public class ZKDatabase {
    
    private static final Logger LOG = LoggerFactory.getLogger(ZKDatabase.class);
    
    /**
     * make sure on a clear you take care of 
     * all these members.
     */
    protected DataTree dataTree;
    protected FileTxnSnapLog snapLog;
    protected long minCommittedLog, maxCommittedLog;
    protected LinkedList<Proposal>  committedLog = new LinkedList<Proposal>();
 

ZKDatabase 这个类在内存中维护了zookeeper的一些状态信息,譬如会话、datatree和已提交日志信息。启动的时候读取磁盘上的日志和快照,把相关信息load到内存里面。
ZKDatabase 里面有多个重要元素组成:DataTree

DataTree
/**
 * 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.
 * <p>
 * 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.
 */
public class DataTree {
    private static final Logger LOG = LoggerFactory.getLogger(DataTree.class);

    /**
     * This hashtable provides a fast lookup to the datanodes. The tree is the
     * source of truth and is where all the locking occurs
     */
    private final ConcurrentHashMap<String, DataNode> nodes =
        new ConcurrentHashMap<String, DataNode>();

datatree维护了一个树状的数据结构。用ConcurrentHashMap维护了路径和datanode(znode)的关系:key:路径,value:datanode

datanode(znode)
/**
 * This class contains the data for a node in the data tree.
 * <p>
 * A data node contains a reference to its parent, a byte array as its data, an
 * array of ACLs, a stat object, and a set of its children's paths.
 * 
 */
public class DataNode implements Record {
    /** the parent of this datanode */
    DataNode parent;

    /** the data for this datanode */
    byte data[];

    /**
     * the acl map long for this datanode. the datatree has the map
     */
    Long acl;

    /**
     * the stat for this node that is persisted to disk.
     */
    public StatPersisted stat;

DataNode parent :父亲节点,这样维护了一个数据链的关系
byte data[]:datanode数据
Long acl:用来做访问控制
StatPersisted stat:保存在磁盘的状态

znode的几个概念

  1. Watches
    zookeeper的客户端可以给znode设置Watches,当znode发生改变,会触发并清空watch,并发送一个通知给客户端
  2. 数据访问
    一个client对znode的读取是整个节点的数据,写数据是完全覆盖znode。所以为了安全,每个znode有acl来控制数据访问。
    ZooKeeper不是用来设计成一个大型的或者大对象的存储。一般用来存储与管理一些配置,状态等协同的数据。znode不能存储超过1m的数据。大对象建议存储在redis,hdfs中。
  3. 临时节点
    临时节点的生命周期依赖于session,当客户端与ZooKeeper集群断开连接,临时节点自动被删除,临时节点不允许有子节点。
  4. 顺序节点
    可以在一个路径下面创建顺序自增的节点。自增数据是4bytes,超2147483647会溢出。
  5. Zxid
    zookeeper事务id(ZooKeeper Transaction Id),zk节点状态数据的变更会收到一个时间戳格式的zxid,zxid是唯一而且是有序的,譬如zxid1比zxid2小,那么zxid1发生在zxid2之前。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值