zk 创建瞬时、顺序节点的原理

时间:2023-03-09 03:48:00
zk 创建瞬时、顺序节点的原理

命令:

create -s -e /worker/lock xx

zk 创建瞬时、顺序节点的原理

zk 的实现代码在:PrepRequestProcessor.pRequest2Txn 中

//The number of changes to the children of this znode.
int parentCVersion = parentRecord.stat.getCversion();
CreateMode createMode =
CreateMode.fromFlag(createRequest.getFlags());
if (createMode.isSequential()) {
path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
}

根据 parenetCVersion 生成 序号,注意不是上图中的 cversion。

上图中的 cversion 是 Stat 中的属性,而这里的 cversion 是 StatPersisted 中的属性,持久化到磁盘中的属性。

public class DataNode implements Record {
// 省略其他代码
/**
* the stat for this node that is persisted to disk.
*/
public StatPersisted stat;
}