1. Watcher --- Notification of Change ------ & gt; client registration, watcher ---------

Source: Internet
Author: User
Tags zookeeper client

1. Watcher --- Notification of Change ------> client registration, watcher ---------
Zookeeper provides the distributed data publishing/subscription function, which is implemented through the Watch mechanism.
Zookeeper allows the client to register a Watch listener to the server. When some specified events on the server trigger this Watch, it will send an event notification to the specified client for Distributed notification. The Watcher mechanism of Zookeeper mainly includes three parts: client ready-to-use, client WatchManager, and Zookeeper server. The specific process is: when the client registers with Zookeeper, the Watcher object is stored in the WatchManager of the client. When the Zookeeper server triggers the Watcher event, a notification is sent to the client. The client thread extracts the corresponding Watcher object from the WatchManager and executes the return logic.
The client registers Watcher.You can pass in a default Watcher object to the constructor when creating a client instance. You can also call getData (), getChildren (), exist () the three interfaces register Watcher with the Zookeeper server, regardless of the method used for registration, the principle is consistent. The getData interface has been described here:
Public byte [] getData (final String path, Watcher watcher, Stat stat)
After registering Watcher with the getData interface, the client first marks the request of the current client and sets it to "use Watcher listener". At the same time, it encapsulates a Watcher registration object WatcherRegistration, it is used to temporarily Save the correspondence between the Data Node path and Watcher. The specific logic code is as follows:
Public Stat getData (final String path, Watcher watcher, Stat stat) {...... WatcherRegistration wrt = null; if (watcher! = Null) {wrt = new DataWatchRegistration (watcher, path) ;}...... request. setWatche (watcher! = Null); // ------ note that ReplyHeader r = cnxn. submitRequest (h, request, response, wrt) will be used later );
In Zookeeper, Packet can be considered as the smallest Communication Protocol Unit for network transmission between the client and the server, any object to be transmitted must be packaged as a Packet object for transmission. Because the WatcherRegistration in ClientCnxn will be encapsulated in Packet, and then placed in the sending queue to wait for the client to send.
Packet queuePacket (RequestHeader h, ReplyHeader r, Record reqest, Record response, AsyncCallback cb, String clientPath, String serverPath, Object ctx, WatcherRegistration wrt ){....... synchronized (outgoingqueue) {packet = new Packet (h, r, request, response, wrt );......... outgoingqueue. add (packet );}....}
Then, the Zookeeper client sends this request to the server and waits for the server to return. After the request is sent, the readResponse method of the client SendThread thread receives the response from the server. The finishPacket method extracts the corresponding Watcher from Packet and registers it to ZkManager. The Code logic is as follows: private void finishPacket (Packet p) {if (p. watcherRegistration! = Null) {p. watcherRegistration. register (p. replyHeader. getErr ());}.......}
Now, the Watcher object is retrieved from the WatchRegistration object: protected Map <String, HashSet <Watcher> getWatchers (int rc) {return watchManager. dataWatches;
}
Public void register (int rc) {if (shouldAddWatch (rc) {Map <String, HashSet <Watcher> watches = getWatchers (rc );

Synchronized (watches) {Set <Watcher> watchers = watches. get (clientPath); if (watchers = null) {watchers = new HashSet <Watcher> (); watches. put (clientPath, watchers );}
Watchers. add (watcher );.........} in the above register method, we will transfer the watcher object temporarily stored to ZKWatcherManager and save it to dataWatchers. Both dataWatchers and ZKWatcherManager are Map <String, set <Watcher> is a data structure that maps the path of a data node to a Watcher object one by one and saves it.
In the above process, WatcherRegistration serializes the underlying network to the underlying byte array.















Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.