Apache Zookepeer Tutorial: Watch Notification on change of Znode
Having effective configuration management system is important and so is to keep track of changes happening in znode. One of the way to track changes is by getting notification for every changes made to znode. A watch can be set on znode. Client can get notification upon changes in znode if client has set watch on znode. Any change to the znode triggers the watch and notifies the client. ZooKeeper's definition of a watch says that “a watch event is one-time trigger, sent to the client that set the watch, which occurs when the data for which the watch was set changes”.
Important things to notice in this definition are:
- One time trigger: On setting watch once, the client will be notified only once for that event. For example if the watch is set for getting notified on any change to data in dezyre znode then on changing data once in that node the client will be notified. But on changing data again to the same node there will be no trigger or no notification for client. To get notified again the client has to set the watch again.
- Sent to client: The client gets notified asynchronously about the trigger or the changes that have happened. But Zookeeper guarantees that client or watcher will not see the changes till it gets the notification.
- Data for which watch was set: Client gets notified only for that particular trigger for which watch was set. For example if the client sets the watch for being notified if any subnode of dezyre is deleted then only when the subnode of dezyre is deleted it will be notified. Client will not be notified for any other changes to the dezyre node like adding another subnode.
Zookeeper 3.4.6 with minimum 3 servers and 1 client.
Example below shows the watch trigger set on ls command of deyzre znode. In case if any new subnode of dezyre is created then the client will be notified.
Basic initial steps:
- Start all the Zookeeper servers with the following command zkServer.sh start
- To confirm that the Zookeeper has started use the command jps and check for QuorumPeerMain.
- To start a client use command zkCli.sh -server Slave1:2181 on any of the server machines
- To understand who is the leader and follower in the Zookeeper use command zkServer.sh status on the console
Creating a directory or “znode”:
Create znode using:
For example here we create new znode with name dezyre and we add data loveZookeeper to it:
create /dezyre loveZookeeper
Watch can be set on any znode while using read operations like ls or get command. Watch is set by setting the second argument of read operations as true:
ls / true
Following code sets watch on znode dezyre and creating any subnode of dezyre will trigger the watch and notify the client:
ls /dezyre true
Update the znode by creating subnode:
To create a subnode it is important to have parent znode. Subnode can be created using:
create / /
On creating subnode of dezyre, a watch is triggered and the client gets notified thus helping the client keep track of the changes made. Although watch is not a very effective way of keeping track of changes as it notifies the client on the event occurring just once. For multiple notification of any change in event the client has to set the watch again and again and this can cause delay in getting notified for changes.