shawnmei 发表于 2017-4-19 07:01:48

zookeeper学习(四)

         首先来了解一下ZooKeeper的数据模型,源代码中会有很多诸如zxid, czxid等这样的变量,那这些到底是什么呢?我在网上查了一些资料,分享一下:
         以上举的那两个例子,叫做Zk的节点(znode)的状态信息,可以通过get命令获取,成为Stat:
 

[*]czxid
The zxid of the change that caused this znode to be created.
[*]mzxid
The zxid of the change that last modified this znode.
[*]ctime
The time in milliseconds from epoch when this znode was created.
[*]mtime
The time in milliseconds from epoch when this znode was last modified.
[*]version
The number of changes to the data of this znode.
[*]cversion
The number of changes to the children of this znode.
[*]aversion
The number of changes to the ACL of this znode.
[*]ephemeralOwner
The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
[*]dataLength
The length of the data field of this znode.
[*]numChildren
The number of children of this znode.
        如果感觉英文看不太清楚的话,我分享一张表格说明一下:


     其实还有一个ephemeralOwner的状态:如果节点为临时节点,那么这个值为此节点拥有者的session ID,否则值为0.
      那么,什么是zxid呢?答:ZooKeeper状态的每一次改变, 都对应着一个递增的Transaction id, 该id称为zxid. 由于zxid的递增性质, 如果zxid1小于zxid2, 那么zxid1肯定先于zxid2发生. 创建任意节点, 或者更新任意节点的数据, 或者删除任意节点, 都会导致Zookeeper状态发生改变, 从而导致zxid的值增加.
 
    我们知道,Zookeeper中对znode状态进行监听并及时的反馈给客户端,这个有点像观察者模式。事实上,watcher也是有自己的一套规则的:
 
    1.Znode状态发生改变时(增删改等操作),watch (监视器)机制可以让客户端得到通知,并且仅仅只会触发一次watch。
 
    2.在读操作exists、getChildren和getData上可以设置监视器,这些监视器可以被create、delete和setData触发。
 
    3.当所监视的znode被创建子节点、删除或其他数据更新时,设置在exists操作上的监视器将会被触发。
 
    4.当所监视的znode被删除或其更新时,设置在getData上的监视器将会被触发,创建znode不会触发getData上的监视器,因为getData操作成功执行的前提是znode必须已经在。
 
    5。当所监视的znode的一个子节点被创建或删除时,或监视的znode自己被删时,设置在getChildren操作上的监视器将会被触发。
我在word中画一张图展示一下:


 ps: 
     1.每个znode创建时都会有一个ACL列表,用于决定谁可以执行那些操作。
 
     2.临时节点不允许有子节点。
 
页: [1]
查看完整版本: zookeeper学习(四)