Talk about ideas with Zookeeper distributed lock is achieved

When the interview was a deeply chiefs hammer, I "** talk about ideas to achieve distributed lock with Zookeeper **"?

If you are unfamiliar with the zookeeper students, will certainly be startled mind, increased blood pressure, stuttering speech, the amount of the amount of ...... ....... ...... Amount

Interviewer: Do you know what I mean? Then the interview basically cool.

We assume that A and B at the same time to compete a critical resource, then in order to ensure data consistency or avoid the problems caused by competition for resources, it is necessary to distributed lock.

Zookeeper distributed lock is implemented as follows:

> 1.A resources needed R, it will apply a lock to ZK, this lock build lock master node underneath (** ** temporary order node, the node is unique). A judge will lock before the lock queue is not there another lock, so if he is the first to add at this time zk may be a name called the 00001-xxx.

> R 2.B also want resources , but also to apply the lock, but found in front of a xxx-00001, and only added a watcher monitor the first lock, while zk to B named called xxx-00002, etc. after xxx-00001 lock and then use it to end .
>
> 3. Later the same CDEF, lined up behind.

However, the problem is over? not at all!

In the process described above, the process is repeated a large number of competing processes running distributed lock, and the vast majority of operating results are judged he was not the smallest node number, which continues to wait for a notice - this is obviously not very scientific way .

The distributed lock contention procedure, its core logic comprising: * all nodes to determine if they are the smallest number **. So, it is easy to think of is the creator of each node only need to focus on that node smaller than their number.

So when the zookeeper cluster is large enough, the above process, it may lead to herding ** **.

> Solution:
> (1) the process of locking in the competition, you will find that they are not all child nodes of the smallest, indicating that he still did not get the lock.

> (2) In this case the client needs to find the node is smaller than its own, then calls the exist () method, and register event listeners.

> After (3) When this node is of interest is removed, the client will receive a corresponding notification. This time the client needs to call again getChildren ( "locknode") method to get all the child nodes that have been created to ensure that you are indeed the smallest node,
 

Published 27 original articles · won praise 0 · Views 3243

Guess you like

Origin blog.csdn.net/u011536031/article/details/104781599