In the Zookeeper application scenario there is a description of the distributed cluster configuration file synchronization problem, imagine if there are 100 machines at the same time a file on the same machine to modify, how to ensure that the text will not be confused, this is the simplest distributed lock, this article describes the use of ZK implementation of distributed locks. Here is the implementation step of the write lock
Distributed write lock
Create a persistent type of znode,/locks/write_lock
- Client Creation sequence| The Znode of the ephemeral type, whose name is Lockid, creates the Znode is/locks/write_lock/lockid0000000001
- Call GetChildren () do not set watcher get/locks/write_lock under Znode list
- Judge yourself Step 2 create Znode is not the smallest one in the Znode list, if it is on behalf of the lock, if not go down
- Call exists () to determine the node number that you created for step 2 is small 1 znode node (that is, the smallest znode in the Znode node list), and set Watcher, if exists () returns FALSE, perform step 3
- if exists () returns True, wait for the ZK notification to return to step 3 in the return function
Releasing a lock is either deleting the Znode node or disconnecting it.
* Note: GetChildren () in step 2 above does not set watcher because the herd effect is prevented, and if the GetChildren () is set to Watcher, then a jitter of the cluster will be notified. In the entire distributed lock competition process, a large number of repeated operations, and most of the results of the operation is to determine that they are not the smallest number of nodes, so as to continue to wait for the next notification-This obviously does not seem very scientific. The client is unnecessarily accepting excessive and self-unrelated event notifications, which can have a significant performance impact on the server if the cluster is large, and if a client with multiple nodes at the same time disconnects, the server will send a large number of event notifications like the rest of the clients- This is called the herd effect.
Zookeeper applicable scenario: distributed lock implementation