共计 7312 个字符,预计需要花费 19 分钟才能阅读完成。
zookeeper 官网文档摘抄
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。因而,会话事件被发送到所有未实现的监督处理程序。应用会话事件进入平安模式: 断开连接时将不会接管事件,因而流程在该模式下应审慎行事。