Zookeeper class

Zookeeper Java code primarily use org. Apache. Zookeeper. They are the class using the Zookeeper service. ZooKeeper(connectString, sessionTimeout, watcher) connectString: a comma-separated list. Each ZooKeeper node is a host:port pair. Host is the machine name or IP address. Port is the port number used by the ZooKeeper node to provide services to clients. The client selects any node in the connectString to establish a connection. SessionTimeout: indicates the session timeout time. Watcher: used to receive events from the ZooKeeper cluster.

Main methods of Zookeeper
  • Create (path, data, flags): creates a znode for a given path and stores data[] in zNode. Flags specifies the type of znode.
  • Delete (path, version) : Deletes the ZNode if the version of the zNode on the given path matches that of the given version.
  • Exists (path, watch) : checks whether a Znode in a given path exists and sets a watch in the znode.
  • GetData (path, watch) : Returns zNode data on the given path and sets a watch on zNode.
  • SetData (path, data, version) : Sets zNode data if the version of the zNode on the given path matches the given version.
Method statement
  • Any API that reads ZNode data can set a watch to monitor zNode changes.
  • All apis that update ZNode data come in two versions: unconditional update and conditional update. If the version is -1, the update is unconditional. Otherwise, the update is conditional only if the given version is the same as the current version of ZNode.
  • All methods are available in both synchronous and asynchronous versions. The synchronized version method sends a request to ZooKeeper and waits for a response from the server. The asynchronous version puts the request into the client’s request queue and returns it immediately. The asynchronous version uses callback to receive the response from the server.
Sample data reading API

There are three ways to get zNode data:

  1. Byte [] getData(String path, Boolean watch, Stat Stat) Synchronization method. If watch is true, the zNode state changes are sent to the watcher that built ZooKeeper.
  2. Void getData(String Path, Boolean Watch, DataCallback CB, Object CTX) Asynchronous method. Cb is a callback that receives a response from the server. CTX is the context provided to CB. The watch parameter has the same meaning as method 1.
  3. Void getData(String path, Watcher Watcher, DataCallback CB, Object CTX) Asynchronous method. Watcher is used to receive state changes of the ZNode.
Example data write API
  1. Stat setData(String path, byte[] data, int version) Synchronization version. If version is -1, do an unconditional update. If version is a non-negative integer, do a conditional update.
  2. Void setData(String path, byte[] data, int version, StatCallback CB, Object CTX) Asynchronous version.
Watch mechanism

Watch provides a mechanism for clients to get up-to-date data. Without the Watch mechanism, the client needs to constantly poll ZooKeeper to see if there is any data update, which is very time-consuming in a distributed environment. Clients can set up a Watcher while reading data so that they are notified when the data is updated.

Conditional updates

Imagine implementing a counter with zNode/C and incrementing by 1 with the set command. Conditional update scenario:

  1. Client 1 updates /c to version 1, incrementing /c by 1.
  2. Client 2 updates /c to version 2, incrementing /c by 1.
  3. Client 1 does not know that /c has been updated by client 2, but uses the outdated version 1 to update /c, and the update fails. If client 1 was using an unconditional update, /c would be updated to 2 without incrementing by 1.

Conditional updates can be used to avoid data update operations based on expired data.