zookeeper筆記之-001-watch

zookeeper官網文檔摘抄html


1. ZooKeeper Watches->ZooKeeper中的全部讀取操做:

(All of the read operations in ZooKeeper):node

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

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.session

例如,若是客戶端執行了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.app

若是/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.less

這意味着一個事件正在到達客戶端,可是在變動操做成功返回的代碼到達發起變動的客戶端以前可能沒法到達客戶端。

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。所以,會話事件被髮送到全部未完成的監視處理程序。使用會話事件進入安全模式:斷開鏈接時將不會接收事件,所以流程在該模式下應謹慎行事。
相關文章
相關標籤/搜索