1、每个znode都包含一个stat structure,这个stat structure包含data和acl的版本信息,每次数据或acl变更,版本都会增加,stat structure还包含一个时间戳。客户端取数据的时候会同时获得这些版本信息,update或delete的时候必须带上版本信息,如果版本信息和要修改的znode不符,则失败。和乐观锁的机制很像。
2、可以为每个znode设置一个watch,当znode的数据变化时,会触发watch并清除watch.zookeeper会通知设置这个watch的client。
3、zonde的数据读写是原子性的,每次读或写,都会读或写znode中的全部字节。znode存储的数据大小最大为1M,服务器和client会校验数据大小。
4、zookeeper节点类型
- Ephemeral Nodes 临时节点,创建这个节点的session断开的时候,这个节点会被删除,因为这个原因,临时节点不能有子节点。
- PERSISTENT 永久节点,节点会一直存在,直到主动删除它。
-
Sequence Nodes 顺序节点,在某个父节点下创建顺序节点,zookeeper会在path后面自动加上一个十位的递增数字,如/parent/aaa-0000000001。
-
Container Nodes,3.5.3版本添加的节点类型,当这种节点的最后一个子节点被删除的时候,zookeeper会在未来的某个时间点自动删除它。所以在创建子节点时,要处理KeeperException.NoNodeException异常,捕获到这个异常则新建这个节点。
-
TTL nodes,3.5.3版本添加的节点类型,在创建永久节点的时候,可以设置一个TTL,如果在这个时间段内,这个节点没有被修改过并且这个节点没有子节点,则在未来的某个时间点,zookeeper会删除这个节点。
5、zookeeper记录数据变动相关的时间概念
- zxid,zookeeper集群中每次数据修改的操作,都会首先产生一个zxid(ZooKeeper Transaction Id),zxid代表着zookeeper所有的数据变动的顺序,如果zxid1 < zxid2 ,那么zxid1代表的操作一定先发生。
- 版本号,每次对znode中数据的变动都会导致某个版本号加1.znode有三个版本号,version表示数据的版本号,cversion表示子节点修改的版本号,aversion表示acl修改的版本号
- ticks,zookeeper中使用ticks作为时间基准,例如status uploads, session timeouts, connection timeouts between peers等等,session timeout是ticks的2倍。
- 时间,zookeeper只有一个地方用到了时间,就是在znode创建和修改时会设置znode的stat structure的时间戳。
6、zookeeper的stat sturcture由下列字段组成
- czxid The zxid of the change that caused this znode to be created.
- mzxid The zxid of the change that last modified this znode.
- pzxid The zxid of the change that last modified children of 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