This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9508|Reply: 2

Zookeeper distributed lock implementation

[Copy link]
Posted on 3/20/2018 4:23:52 PM | | | |
Lock design

  Get the lock implementation idea:
1. Start by creating a directory as a lock (znode), which is usually used to describe the locked entity, called :/lock_node
2. The client that wants to obtain the lock creates a znode in the lock directory as a child node of the lock/lock_node, and the node type is Ordered Temporary Node (EPHEMERAL_SEQUENTIAL);
For example, two clients create znodes, /lock_node/lock-1 and /lock_node/lock-2
3. The current client calls getChildren(/lock_node) to get all the child nodes of the lock directory, without setting watch, and then obtaining the brother nodes that are smaller than itself (created in step 2).
4. The node that is smaller than itself in step 3 does not exist && the smallest node is the same as the one created in step 2, indicating that the current client sequence number is the smallest, get the lock, and end.
5. The client monitors the state of the orderly temporary node that is the next smallest to itself
6. If the status of the monitored sub-node changes, jump to step 3 and continue the follow-up operations until the lock race is exited.     

The author will not introduce the distribution lock here, let's take a look at the flow chart of the entire code design as follows





Previous:Detailed explanation of AutoResetEvent in .net/c#
Next:Regarding the issue that Android 7.0 cannot perform https packet capture
 Landlord| Posted on 3/20/2018 4:49:35 PM |
Zookeeper distributed lock steps:
1. Zookeeper is a node with nodes, similar to a file directory, so we abstract the lock into a directory, zookeeper has a node of EPHEMERAL_SEQUENTIAL types, multiple threads and when the node created by zookeeper, it will help us arrange the order to create, so the directories under this node are sequential.
2. Get the smallest node of the current directory, determine whether the smallest node is the current node, if it means that the lock is successful, if it is not the lock acquisition failure.
3. When the lock fails, in order to avoid the swarm effect, what you have to do is to obtain the previous node of the current node, and then listen to the node.
4. When you do this, when you release the lock, you will also notify the next node.
 Landlord| Posted on 3/20/2018 4:54:46 PM |
Learn about zookeeper status and event types in advance.


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com