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。因而,会话事件被发送到所有未实现的监督处理程序。应用会话事件进入平安模式:断开连接时将不会接管事件,因而流程在该模式下应审慎行事。