java - Zookeeper重置序列号

标签 java apache-zookeeper apache-curator

如果我有一个具有连续的临时子节点的持久节点,当所有子节点消失时是否可以重置序列。

我想要实现的目标:

  1. 您创建第一个连续的临时节点:[node-00..01]
  2. 您创建了第二个:[node-00..01, node-00..02]
  3. 您创建了第三个:[node-00..01、node-00..02、node-00.003]
  4. 第二个节点死亡:[node-00..01,node-00.003]
  5. 您创建另一个连续子节点:[node-00..01、node-00..03、node-00.004]

因此,如果一个节点死亡,下一个应该不会填补空白,但是当所有子节点都死亡时,我希望重置序列,如果我再次创建一个子节点,它应该从头开始。

这可能吗?来自 Java。

最佳答案

您可以通过创建模式 CONTAINER 来实现此目的。当容器下没有子节点时,容器将被自动移除。

当您创建顺序临时节点时,请使用以下方法。

curatorFrameworkInstance.create()
.creatingParentContainersIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath("/<some_path>/<parentNode>/<sequential_path_prefix>");

这里发生的是,curator 将创建父 ZNode <parentNode>如果不存在则作为容器。并将在其下创建临时顺序节点,这是问题中 1 到 5 描述的预期行为。当所有子节点都死亡时,父 ZNode 也会死亡。当您尝试再次创建子项时,creatingParentContainersIfNeeded()子句将确保再次创建父容器。

为了进一步阅读,这是 CreateMode.CONTAINER 的文档.

The znode will be a container node. Container nodes are special purpose nodes useful for recipes such as leader, lock, etc. When the last child of a container is deleted, the container becomes a candidate to be deleted by the server at some point in the future. Given this property, you should be prepared to get KeeperException.NoNodeException when creating children inside of this container node.

注意:我使用过Apache Curator Framework回答你的问题而不是使用 ZooKeeper Client直接。

关于java - Zookeeper重置序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794563/

相关文章:

hadoop - HBase主站立即崩溃

java - Zookeeper curator缓存如何等待同步?

apache-zookeeper - Zookeeper 连接超时问题

java - 如何使软键盘不直接推到编辑文本,而是推到编辑文本下方的特定位置? (安卓)

Java 字符串到字节的转换是不同的

hadoop - 动物园管理员如何确定Hadoop工作的 'java.library.path'?

java - CuratorTransaction KeeperErrorCode = NoNode

java - 如何从对象中包含的对象列表中每个元素的字段中检索字符串集?

java - 错误: missing return statement 3

hadoop - 如果 namenode 和 ZooKeeper 一起失败会发生什么