Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Zookeeper (3): Znod in Zookeeper

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

data model

ZK has a namespace like a lean file system, except that each node in its namespace has its own data associated with it or its children. Absolute paths must be used in ZK, i.e. they start with a "/".

Znode:

Each node in the ZK directory tree corresponds to a Znode. Each Znode maintains this attribute, current version, data version, build time and modification time, etc., as shown in the following figure:

ZK uses these attributes to implement special functions. When a client wants to modify a node, it must provide the version number of the data, and when the node data changes, its version number will increase. As shown below:

Znode has the following characteristics:

Watches: Clients can set Watches (which can be called monitors) on nodes. When the node state changes, the corresponding operation of the monitor will be triggered. When the monitor is triggered, the ZK server will send only one notification to the client.

Data access: Data stored on ZK needs to be atomically manipulated (either modified successfully or returned to its original state), read operations will read all data associated with the node, write operations will modify all data associated with the node, and each node has its own ACL.

Node type: There are several node types in ZK. The node type is determined when the node is created and cannot be changed.

Temporary node (EPHEMERAL): created temporarily. Session end node is deleted automatically or manually. Temporary node cannot have child node.

Temporary sequential node (EPHEMERAL_SEQUENTIAL): has the characteristics of a temporary node, but it will have a sequence number, and this type of node will be used in distributed locks

PERSISTENT: Permanently exists after creation unless actively deleted.

PERSISTENT_SEQUENTIAL: This node persists after creation. It automatically adds a 10-digit sequence number to the end of the node name relative to persistent nodes. This count is unique to the parent node of this node. If this sequence number is greater than 2^32-1, it will overflow.

Create Order Node

create -s /NODE_NAME DATA # -e parameter is used to create temporary node. If no parameter is used, persistent node is created.

Time and version number in ZK:

ZXID: A ZK node state change causes the node to receive a timestamp in the zxid format, which is globally ordered and generates a new timestamp every time it is updated. If zxid1 is less than zxid2, then zxid2 changed after zxid1. zxid is a unique transaction ID, which is incremental. The establishment or update of a znode will generate a new zxid value. The specific time is 3 cZxid (node creation time), mZxid (node modification time, independent of child nodes), pZxid (last creation or modification time of child nodes of this node, independent of grandchild nodes)

version: Each operation on a node increments the version number of the node. There are three version numbers: dataversion (data version number), cversion (child node version number), aclversion (ACL version number owned by the node).

cZxid

Transaction ID at node creation

ctime

Time at which node was created

mZxid

The transaction ID at the time the node was last modified

mtime

Time when node was last modified

pZxid

Indicates the transaction ID of the last modification of the child node list of this node. Adding or deleting child nodes affects the child node list, but modifying the data content of child nodes does not affect this ID.

cversion

Version number of child node. The version number of child node is increased by 1 every time it is modified.

dataversion

Data version number, which is incremented by 1 every time data is modified

aclversion

Permission version number, each time the permission is modified, the version number increases by 1

dataLength

The data length of this node

numChildren

Number of child nodes this node has

The role of version numbers

The version number in Zookeeper is different from the version number we understand. It represents the number of modifications to the content of the data node, the list of child nodes, or ACL information. When the node is created, dataversion, aclversion, and cversion are all 0, and the corresponding version number is incremented by 1 every time the response content is modified.

The purpose of this version number is related to the concept of a lock in distributed scenarios. For example, one seat in the ticket sales of a show, obviously there is only one seat in each show, and it is impossible to sell it twice. If A placed an order that was available for sale and he wanted to buy it, then in order to ensure that he could place the order successfully, no one else could buy it. There needs to be a mechanism to ensure that only one person can modify the inventory of that seat at a time. That's where the lock comes in. There are pessimistic locks and optimistic locks.

Pessimal locking: It assumes that all different transactions must interfere, the most stringent concurrency control policy in the database, if a transaction A is processing data, then during the entire transaction, no other transactions can update this data until transaction A releases the lock.

Optimistic locking: It assumes that all transactions will not necessarily interfere, so locks are not allowed in most operations, but since there is a possibility of interference since it is concurrent, how to resolve conflicts is a problem. In optimistic locking, before you submit an update request, you check whether the data has changed since you read it. If so, you must abandon the commit. If not, you can submit it.

The version number in Zookeeper is an optimistic lock. Before you modify the node data, you will read the data and record the version number of the data. When you need to update, you will carry this version number to submit. If the version number you carry at this time (that is, the version number you read last time) is the same as the version number of the current node, it means that the data has not been modified, then your submission will succeed. If the submission fails, it means that the data has been modified during the period between reading and submission.

Here, submit the update with the set command and the version number, and the update with the same version number will succeed.

If you update again and use the previous version number then it will fail.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report