Zookeeper (five) Zookeeper distributed lock to achieve

znode (node) type

Zookeeper data storage structure like a tree, the tree consists of nodes, this node is called Znode.
Here Insert Picture Description

Znode divided into four types:

1, lasting node (PERSISTENT)

The default node type. After creating the client and Zookeeper node is disconnected, the node still exists.

2, the order of the node (PERSISTENT_SEQUENTIAL)

The so-called order of nodes, that is, when you create a node, Zookeeper numbered according to the node name created in chronological order:Here Insert Picture Description

3, temporary node (EPHEMERAL)

And lasting node On the contrary, when creating a node client and Zookeeper disconnected temporarily node will be deleted;

4, a temporary order of the nodes (EPHEMERAL_SEQUENTIAL)

As the name suggests, the characteristics of the temporal order node, and nodes and temporary binding order of nodes :
When creating the node, Zookeeper numbered according to the order of creation time node name;
when the node is created and the client is disconnected Zookeeper temporary node been deleted.


Zookeeper distributed lock to achieve

Zookeeper distributed lock precisely applied the temporary order of nodes.

1, to obtain a lock

First, create a lasting node ParentLock in Zookeeper them. When the first client wants to acquire a lock, you need to create a temporary order of nodes Lock1 in ParentLock this node.

After, Client1 find all of the temporary order of nodes ParentLock below and sort Lock1 own judgment node is created that is not the highest order. If this is the first node, the successful lock.

At this time, if we have a client Client2 come to acquire the lock, then ParentLock download and then create a temporary order of nodes Lock2.

Find all Client2 temporary order of nodes ParentLock below and sort judge Lock2 own node is not created by the highest order of a found node Lock2 not the most forward.

So, Client2 than its front node Lock1 only registered Watcher to sort, whether listening Lock1 node exists. This means grab Client2 lock fails, enter the wait state.
Here Insert Picture Description

At this time, if there is another client Client3 come to acquire the lock, then ParentLock download and then create a temporary order of nodes Lock3.

Client3 Find all the temporary order of nodes ParentLock below and sort, create their own judgment node Lock3 is not a highest order, the results also found node Lock3 not the least.

So, Client3 than its front node Lock2 only registered Watcher to sort, whether listening Lock2 node exists. This means that the same Client3 grab the lock fails, enter the wait state.

As a result, Client1 get a lock, Client2 monitor the Lock1, Client3 monitor the Lock2. This is precisely the formation of a waiting queue.

3, the lock is released

Release the lock is divided into two situations:
1, when the task is completed, Client1 will show Lock1 command to delete a node calls.
2, task execution, the client crashes

Client1 to acquire a lock in the task execution process, if a crash, it will break the link with the Zookeeper service side. According to the characteristics temporary node, the node associated Lock1 will follow automatically deleted.
Due to the existence of the state and has been monitoring Client2 Lock1 when Lock1 node is deleted, Client2 will be notified immediately. This time Client2 queries all nodes ParentLock below again confirm Lock2 is not the most forward of the current node node create yourself. If the most forward, it is logical to get a lock Client2.
Client3 Similarly to Client2

Published 44 original articles · won praise 5 · Views 906

Guess you like

Origin blog.csdn.net/qq_40634246/article/details/104603902