Site icon DataFlair

Zookeeper Locks – Shared & Recoverable Shared Locks

ZooKeeper Locks

ZooKeeper Locks

In our last ZooKeeper tutorial, we discussed the ZooKeeper Data Model. Today, we will see Zookeeper Locks. Moreover, we will discuss shared locks and recoverable shared locks in ZooKeeper. Also, we will see how to implement shared locks in ZooKeeper. 

So, let’s start ZooKeeper Locks.

What is Zookeeper Locks?

Zookeeper Locks are fully distributed locks in ZooKeeper which are globally synchronous. However, globally synchronous means at any snapshot in time no two clients think they hold the same lock.

Though these we can implement these locks by using ZooKeeper. Moreover, we can say first define a lock node, as with priority ZooKeeper Queues.

Now do the following, to obtain a lock in ZooKeeper:

However, clients wishing to release a lock in ZooKeeper simply delete the node they created in step 1, to unlock.

Also, notice these few things in ZooKeeper Locks:

Shared Locks

By making some changes to the ZooKeeper Lock protocol, it is possible to implement shared locks in ZooKeeper:

ZooKeeper Locks –

a. Obtaining a Read lock

  1. At first, to create a node with pathname “_locknode_/read-“, Call create( ). Although, don’t forget to set both the sequence and ephemeral flags.
  2. Then without setting the watch flag, call getChildren( ) on the lock node. Basically, that helps to avoid the herd effect.
  3. However, make sure the client has the lock and can exit the protocol if somehow there are no children with a pathname starting with “write-” and having a lower sequence number than the node created in step 1.
  4. Else, call exists( ), with “write-” having the next lowest sequence number set on the node in lock directory with pathname starting, also with watch flag.
  5. Go to step 2, if exists( ) returns false.
  6. Else, before going to step 2, wait for a notification for the pathname from the previous step.

b. Obtaining a Write Lock

  1. Again, with pathname “_locknode_/write-“, call create( ) to create a node. And, remember to set both sequence and ephemeral flags.
  2. Further, call getChildren( ) on the lock node – because it helps to avoid the herd effect.
  3. However, the client has the lock and the client exits the protocol if there are no children with a lower sequence number than the node created in step 1.
  4. Further, with watch flag set, call exists( ), on the node with the pathname which has the next lowest sequence number.
  5. Goto step 2, if exists( ) returns false. Else, before going to step 2, wait for a notification for the pathname from the previous step.

Recoverable Shared Locks

Further, we can make ZooKeeper Shared Locks revocable by modifying the shared lock protocol, with minor modifications to the Shared Lock Protocol in ZooKeeper:

Just call getData( ) with watch set, in step 1, of both obtain reader and writer lock protocols, just after the call to create( ).

So, it created in step 1, if the client subsequently receives notification for the node, moreover, with watch set and looks for the string “unlock”, it does another getData( ) on that node, that signifies the client that now it must release the lock.

The reason behind this is, as per this shared lock protocol, writing “unlock” to that node, we can request the client with the lock give up the lock by calling setData() on the lock node.

So, this was all in ZooKeeper Locks. Hope you like our explanation.

Conclusion

Hence, we have seen the whole about Zookeeper Locks along with Shared Locks and Recoverable Shared Locks in detail. So, we hope this article will resolve all your doubts regarding Locks in ZooKeeper. Still, if any query, ask in the comment tab.

Exit mobile version