dfritsi
dfritsi

Reputation: 1232

Zookeeper reset sequential number

If I have a persistent node which have sequential ephemeral children, is it possible to reset the sequence, when all child nodes went away.

What I would like to achieve:

  1. You create the first sequential ephemeral node: [node-00..01]
  2. You create a 2nd one: [node-00..01, node-00..02]
  3. You create the 3rd one: [node-00..01, node-00..02, node-00.003]
  4. The 2nd node dies: [node-00..01, node-00.003]
  5. You create another sequential child: [node-00..01, node-00..03, node-00.004]

So if one dies, the next one should not fill the gap, but when all child nodes die, I would like the sequence to be reset, and if I create a child node again it should start from the beginning.

Is this possible? From Java.

Upvotes: 1

Views: 1890

Answers (1)

Imesha Sudasingha
Imesha Sudasingha

Reputation: 3570

You can achieve this with create mode CONTAINER. The containers will be automatically removed when there are no child nodes under it.

When you create a sequential ephemeral node, use the following method.

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

What happens here is, curator will create parent ZNode <parentNode> as a container if it does not exist. And will create ephemeral sequential nodes under it, which is your expected behavior described from 1 to 5 in your question. When all the children are dead, the parent ZNode will also die. When you attempt to create a child again, creatingParentContainersIfNeeded() clause will make sure the parent container is created again.

For further reading, this is the documentation for 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.

NOTE : I have used Apache Curator Framework to answer your question rather than using ZooKeeper Client directly.

Upvotes: 1

Related Questions