In this article: HelloGitHub- Old xun

I haven’t updated ZK for a long time. I miss you so much. Since the end of the HelloZooKeeper series, the project has harvested nearly 600 stars. This is much more than I expected. Thank you for your support

We will continue the topic of ZooKeeper, through the form of a single ZK topic to continue to talk, today let’s first look at the node types of ZK. Without further ado, let’s move on to today’s topic

1. Node type of ZK

If you brush ZK related interview questions, you will brush to “ZK has several types of nodes?” , the answer that everybody usually endorses is: 4 kinds! In fact, the ZK (3.6.2) server supports seven node types, which are:

  • persistent
  • Last order
  • temporary
  • The temporary order
  • The container
  • Persistent TTL
  • Persistent sequential TTL

These seven types have been mentioned in previous articles, but have not been expanded upon. This update of the single article wants to these 7 types of nodes, seriously talk about again! Let’s GO

Two, a brief introduction

2.1 Persistent and temporary

Persistent is one of the most commonly used types and is the default node type. Temporary nodes, as opposed to persistent nodes, are deleted when the client session ends. This can be used in specific scenarios such as distributed lock release, health check, etc.

2.2 Persistent order and temporary order

I will introduce these two types together, because their characteristics relative to the above two types are that ZK will automatically add a numeric suffix after these two types of nodes, and the path + numeric suffix can guarantee the unique, the application scenarios of the numeric suffix can realize such as distributed queue, distributed fair lock, etc.

2.3 container

A CONTAINER node is a new type of node created after 3.5. You can create a CONTAINER node by specifying CreateMode as CONTAINER in the create method. A CONTAINER node is the same as a persistent node. A separate thread will scan all the container nodes and automatically delete the container node if the number of children of the container node is zero, otherwise it is the same as the persistent node. Container Nodes are special purpose Nodes Useful for recipes such as leader, lock, etc. This can be used in a leader or lock scenario.

2.4 Durable TTL and durable sequential TTL

The TTL is an abbreviation for “time to live”, which means that the TTL is automatically removed after the specified time. Features to keep up with the container node, just no timeout container node, but TTL enabled is in need of extra configuration (also have mentioned this before) configuration is zookeeper extendedTypesEnabled need to be configured to true, Otherwise, Unimplemented will be sent when the TTL is created

Iii. Principle introduction

I won’t go into the simple persistent and temporary nodes, which were covered in the previous series

3.1 Sequential Keywords

When the client creates a sequential node, the server automatically adds a suffix to the path when it knows that the current node is a sequential node. The suffix is the cversion of the parent node, which represents the number of child nodes to be created

if (createMode.isSequential()) {
  	path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
}
Copy the code

It’s that simple

3.2 Container and TTL keywords

When the server starts, it starts a scheduled task thread that periodically scans all containers and TTL nodes to determine the number of child nodes and their configurations before deleting them. The entire logic is in ContainerManager. Timed tasks are implemented by TimeTask

Configuration items The default value instructions
znode.container.checkIntervalMs 60000 (ms) Interval for checking scheduled tasks
znode.container.maxPerMinute 10000 Combined with the above parameter to make the minimum check interval, each node interval must be at least (60,000/10000) milliseconds (6 milliseconds by default)
znode.container.maxNeverUsedIntervalMs 0 If the value is not set to 0, yesThe containerandTTLIf the difference between the last update time and the current timestamp exceeds this value, the node will also be deleted

Four, summary

  • Persistent keyword: If the client does not actively delete the node data, the node data will always exist
  • Temporary keyword: When the client connection is disconnected, node data is deleted
  • Sequential keyword: The server automatically adds a numeric suffix to this node
  • Container: The server will periodically scan these nodes and automatically delete them if there are no children under them (or other conditions)
  • TTL: Requires additional configuration to enable it. It is basically the same as the container. If no child nodes are created after the TTL is exceeded, the node will be deleted

ZKr ~ long time no more, this time pick an easy topic. Behind you want to learn what ZK knowledge can leave a message to tell me.


Follow the HelloGitHub public account to receive the first updates.

There are many more open source projects and treasure trove projects to be discovered.