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.
当数据发生变化时,将发送一个watch事件给客户端。
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.
如果/znode1再次更改,将不会发送watch事件,除非客户端已经完成了另一个读取来设置新的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.
watch被异步发送给观察者。
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.
ZooKeeper提供了一个有序保证:客户在第一次看到watch事件之前,不会看到它已经设置了watch的变化。
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.
这指的是node可以更改的不同方式。
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.
另一种方法是根据返回的数据类型来设置watch。
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).
因此,setData()将触发正在设置的znode的数据监视(假设设置成功)。
A successful create() will trigger a data watch for the znode being created and a child watch for the parent znode.
成功的create()将触发正在创建的znode的数据监视和父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.
成功的delete()将同时触发删除znode的数据监视和子监视(因为不可能有更多的子监视),以及父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.
当客户端连接到新服务器时,watch会被任何会话事件触发
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.
我们可以用读取ZooKeeper状态的三个调用来设置watch:exists、getData和getChildren
The following list details the events that a watch can trigger and the calls that enable them:
下面的列表详细说明了watch可以触发的事件,以及启用它们的调用:
Created event: Enabled with a call to exists.
通过调用exists启用
Deleted event: Enabled with a call to exists, getData, and getChildren.
通过调用exists、getData和getChildren启用。
Changed event: Enabled with a call to exists and getData.
通过调用exists和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.
现在有一个变化的标准watch上面描述,你可以设置一个watch,触发时不删除。
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.
此外,这些监视会触发NodeCreated、NodeDeleted和NodeDataChanged这些事件类型,而且对于从监视所注册的znode开始的所有znodes,还可以递归地触发这些事件类型
Note that NodeChildrenChanged events are not triggered for persistent recursive watches as it would be redundant.
注意,对于持久递归监视,不会触发NodeChildrenChanged事件,因为它是冗余的。
Persistent watches are set using the method addWatch().
使用addWatch()方法设置持久监视。
The triggering semantics and guarantees (other than one-time triggering) are the same as standard watches.
触发语义和保证(除了一次性触发)与标准watch相同
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.
使用removeWatches()和观察者类型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.
另外,通过将local标志设置为true,即使没有服务器连接,ZooKeeper客户端也可以在本地删除watch。
The following list details the events which will be triggered after the successful watch removal.
以下列表详细说明了成功移除watch后将触发的事件。
- 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
标准watch为一次性触发器;如果您获得了一个watch事件,并且希望获得关于未来更改的通知,则必须设置另一个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.)
因为标准watch是一次性触发器,在获取事件和发送新请求来获取watch之间存在延迟,你不能可靠地看到ZooKeeper中某个节点发生的每个变化。
准备好处理在获取事件和再次设置watch之间znode多次更改的情况。
(你可能不在乎,但至少要意识到这可能会发生。)
- 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.
一个watch对象,或函数/上下文对,将只触发一次,为一个给定的通知。例如,如果同一个watch对象注册了exists和同一个文件的getData调用,然后该文件被删除,那么这个watch对象只会被调用一次,并带有该文件的删除通知。
- 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.
当您断开与服务器的连接时(例如,当服务器失败时),在重新建立连接之前,您将不会获得任何watch。因此,会话事件被发送到所有未完成的监视处理程序。使用会话事件进入安全模式:断开连接时将不会接收事件,因此流程在该模式下应谨慎行事。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。