1. ZooKeeper Watches->ZooKeeper中的所有读取操作:

(All of the read operations in ZooKeeper):

getData(), getChildren(), and exists() - have the option of setting a watch as a side effect.

getData(), getChildren(), and exists() - 可以有选择的设置一个watch作为一个副作用

Here is ZooKeeper's definition of a watch: 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.

以下是ZooKeeper对 watch 的定义:watch事件是一次性触发的,发送给设置watch的客户端,当watch所设置的数据发生变化时,就会发生watch事件。

1.2 在这个watch的定义中,有三个关键点需要考虑:

There are three key points to consider in this definition of a watch:

1. One-time trigger(一次性触发)

One watch event will be sent to the client when the data has changed.


For example, if a client does a getData("/znode1", true) and later the data for /znode1 is changed or deleted, the client will get a watch event for /znode1.

例如,如果客户端执行了getData("/znode1", true),之后/znode1的数据被更改或删除,客户端将获得/znode1的一个监视事件。

If /znode1 changes again, no watch event will be sent unless the client has done another read that sets a new watch.


2. Sent to the client (发送给client):

This implies that an event is on the way to the client, but may not reach the client before the successful return code to the change operation reaches the client that initiated the change.


Watches are sent asynchronously to watchers.


ZooKeeper provides an ordering guarantee: a client will never see a change for which it has set a watch until it first sees the watch event.


Network delays or other factors may cause different clients to see watches and return codes from updates at different times.


The key point is that everything seen by the different clients will have a consistent order.


3. The data for which the watch was set (为watch设置的数据):

This refers to the different ways a node can change.


It helps to think of ZooKeeper as maintaining two lists of watches: data watches and child watches.

我们可以将ZooKeeper看作维护两个watch列表:数据watch和child watch。

getData() and exists() set data watches. getChildren() sets child watches.

getData()和exists()设置数据监视。getChildren()设置child watch。

Alternatively, it may help to think of watches being set according to the kind of data returned.


getData() and exists() return information about the data of the node, whereas getChildren() returns a list of children.

getData()和 exists()返回关于节点数据的信息,而getChildren()返回子节点列表。

Thus, setData() will trigger data watches for the znode being set (assuming the set is successful).


A successful create() will trigger a data watch for the znode being created and a child watch for the parent znode.


A successful delete() will trigger both a data watch and a child watch (since there can be no more children) for a znode being deleted as well as a child watch for the parent znode.


2. watches的管理

Watches are maintained locally at the ZooKeeper server to which the client is connected.

watches 在客户机连接的ZooKeeper服务器上本地维护。

This allows watches to be lightweight to set, maintain, and dispatch.

这使得 watches 在设置、维护和分派方面都是轻量级的。

When a client connects to a new server, the watch will be triggered for any session events.


Watches will not be received while disconnected from a server.

当从服务器断开连接时,将不会收到 Watches。

When a client reconnects, any previously registered watches will be reregistered and triggered if needed.

当客户端重新连接时,任何之前注册的 watches 都将重新注册并在需要时触发。

In general this all occurs transparently.


There is one case where a watch may be missed: a watch for the existence of a znode not yet created will be missed if the znode is created and deleted while disconnected.

有一种情况可能会漏掉一个 watch : watch 因为znode还没有创建, 此时断开连接,就会漏掉。

3. Watches的语义

We can set watches with the three calls that read the state of ZooKeeper: exists, getData, and getChildren.


The following list details the events that a watch can trigger and the calls that enable them:


Created event: Enabled with a call to exists.


Deleted event: Enabled with a call to exists, getData, and getChildren.


Changed event: Enabled with a call to exists and getData.


Child event: Enabled with a call to getChildren.


4. 持久、递归的watch: Persistent, Recursive Watches

New in 3.6.0: There is now a variation on the standard watch described above whereby you can set a watch that does not get removed when triggered.


Additionally, these watches trigger the event types NodeCreated, NodeDeleted, and NodeDataChanged and, optionally, recursively for all znodes starting at the znode that the watch is registered for.


Note that NodeChildrenChanged events are not triggered for persistent recursive watches as it would be redundant.


Persistent watches are set using the method addWatch().


The triggering semantics and guarantees (other than one-time triggering) are the same as standard watches.


The only exception regarding events is that recursive persistent watchers never trigger child changed events as they are redundant.


Persistent watches are removed using removeWatches() with watcher type WatcherType.Any.


5. 移除watches:(Remove Watches):

We can remove the watches registered on a znode with a call to removeWatches.

我们可以通过调用 removeWatches 来删除在znode上注册的watch。

Also, a ZooKeeper client can remove watches locally even if there is no server connection by setting the local flag to true.


The following list details the events which will be triggered after the successful watch removal.

  • Child Remove event(子移除事件):
    Watcher which was added with a call to getChildren. 监视器,它是通过调用getChildren添加的。
  • Data Remove event(数据删除事件):
    Watcher which was added with a call to exists or getData.通过调用exists或getData添加的监视
  • Persistent Remove event(持久删除事件):
    Watcher which was added with a call to add a persistent watch.监视器,它是通过调用添加一个持久的watch而添加的。

6. 关于Watcher需要记住的事情(Things to Remember about Watches)

  • Standard watches are one time triggers; if you get a watch event and you want to get notified of future changes, you must set another watch
  • Because standard watches are one time triggers and there is latency between getting the event and sending a new request to get a watch you cannot reliably see every change that happens to a node in ZooKeeper. Be prepared to handle the case where the znode changes multiple times between getting the event and setting the watch again. (You may not care, but at least realize it may happen.)
  • A watch object, or function/context pair, will only be triggered once for a given notification. For example, if the same watch object is registered for an exists and a getData call for the same file and that file is then deleted, the watch object would only be invoked once with the deletion notification for the file.
  • When you disconnect from a server (for example, when the server fails), you will not get any watches until the connection is reestablished. For this reason session events are sent to all outstanding watch handlers. Use session events to go into a safe mode: you will not be receiving events while disconnected, so your process should act conservatively in that mode.
阅读 172


259 声望
6 粉丝
0 条评论


259 声望
6 粉丝