How to achieve Zookeeper distributed lock
Tags: Zookeeper distributed
Important issues to consider implementation of distributed lock
1. The three core elements
Lock, unlock, lock timeout
2. The three questions
- To ensure atomicity operations, and lock out the locking operation to be performed is finished disposable
- Lock to prevent accidental deletion
- On the basis of accidentally deleted on the addition of a daemon thread, continued life for the lock.
What is a temporary order of nodes
Zookeeper data storage structure is like a tree, the tree consists of nodes, this node is called Znode. Znode divided into four types.
Persistent node (Persistent)
The default node type, create a node client and Zookeeper after disconnected, the node still exists.
2. Persistence order of nodes (Persistent Sequential)
The so-called order of nodes, that is, when creating a node, Zookeeper numbered according to the name of the node node creation time of order.
3. Temporary node (Ephemeral)
And lasting node On the contrary, when creating a node client and Zookeeper disconnected temporarily node will be deleted.
4. The temporal order node (Ephemeral Sequential)
When you create a node, Zookeeper numbered according to the time order of creation to the node name; a node is created when the client and Zookeeper disconnected temporarily node will be deleted.
Zookeeper distributed lock principle to achieve the
It has been said above four types of Znode, which last type temporary order of nodes is the most conducive to achieving Zookeeper distributed lock.
1. Obtain lock
- First, create a lasting node ParentLock in Zookeeper them. When the first client wants to acquire a lock of an operational data, you need to resume a temporary order of nodes under the persistent node
Lock1
.
- After that,
客户端1
look forParentLock
all the interim order of the nodes below and in accordance with the size of the sort, they created their own judgment nodeLock1
is not a highest order. If it is the first node, the successful lock.
- 此时,
客户端2
前来获取该项数据的锁, 则在ParentLock
下再创建一个临时顺序节点Lock2
.
客户端2
查找ParentLock下面所有的临时顺序节点并排序,发现自己的Lock2
节点并不是最靠前的. 于是客户端2
向排序仅仅比它靠前的Lock1
注册Watcher
, 用于监听Lock1
动态. 这意味着Lock2
抢锁失败, 进入等待状态.
- 此时, 如果有一个
客户端3
前来获取锁, 则在ParentLock
下在创建一个临时顺序节点Lock3
.
客户端3
查找ParentLock
下面所有的临时顺序节点并排序, 判断自己所创建的节点Lock3
是不是顺序最靠前的一个, 结果发现Lock3
不是最靠前的. 于是客户端3
同样抢锁失败, 进入了等待状态.
- 这个时候
客户端1
得到了锁,客户端2
监听了客户端1
,客户端3
监听了客户端2
. 这样刚好形成一个等待的队列.
2. 释放锁
释放锁有两种情况
2.1 任务完成, 客户端显示释放
- 当任务完成时,
客户端1
会显示的调用删除节点Lock1
的指令.
2.2 任务执行过程中, 客户端崩溃
- 获得锁的
客户端1
在执行任务的过程中, 如果崩溃, 则会断开和Zookeeper
服务器的链接, 根据临时节点的特性, 相关联的Lock1
会随之自动删除.
3. 获得锁
- Since
客户端2
've been listeningLock1
state, this time foundLock1
written off,客户端2
will immediately receive notification. This time客户端2
will query againParentLock
all the nodes under the confirmed node created yourself is not the smallest node, if it is the smallest of the successful lock.
The same can be pushed to
客户端3
to sum up
Compare Zookeeper distributed lock and Redis
Distributed Lock | advantage | Shortcoming |
---|---|---|
Zookeeper | 1. There encapsulated frame, easy to implement. 2. Wait lock mechanism ( Watcher ), can increase the efficiency of the grab locks, many benefits |
Performance adding and removing nodes is relatively low |
Redis | Set And Del performance is relatively high (after all, the key database, Hash) |
1. To achieve complex and require consideration of the atom, etc. mistakenly deleted. Mechanism 2 without waiting for the lock, the lock can only be through the client and the like spin, inefficient. |