I. Notification of WATCHER---change------> Client Registration

Source: Internet
Author: User
Tags zookeeper client

Zookeeper provides a distributed data publishing/subscription feature that enables this distributed notification function through the Watch mechanism.
Zookeeper allows the client to register a watch listener with the server, and when the watch is triggered by some specified events on the service side, an event notification is sent to the specified client to implement distributed notifications. The whole watch registration and notification process Zookeeper watcher mechanism mainly includes the client out-of-the-box, client Watchmanager, zookeeper server three parts, the specific process is: Client to zookeeper registration at the same time, The Watcher object is stored in the client's Watchmanager, and when the zookeeper server triggers the Watcher event, a notification is sent to the client, and the client thread takes the corresponding Watchmanager object out of the watcher to perform the rollback logic.
Client Registration WatcherWhen you create a client instance, you pass in a default watcher object to the constructor method, or you can register Zookeeper with the watcher server by calling GetData (), GetChildren (), exist () three interfaces, Regardless of the method of registration, the principle is consistent. Here is the GetData interface description:
Public byte[] GetData (final String path, Watcher Watcher, stat stat)
After registering the Watcher with the GetData interface, the client first requests the current client Request for tagging, it is set to "Use Watcher listener", and encapsulates a watcher registered object Watcherregistration object, which is used to temporarily save the data node path and watcher correspondence, the specific logic code:
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 for follow-up here;
In zookeeper, Packet can be seen as a minimal communication protocol unit for network transmission between client and server, and any object that needs to be transmitted is packaged Packet Object for transmission. Because the watcherregistration in CLIENTCNXN will be encapsulated in the Packet, and then put in the send queue waiting 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); }....}
The Zookeeper client then sends the request to the server and waits for the server to return. After the request is sent, the client Sendthread thread's Readresponse method accepts the response from the server, and the Finishpacket method takes the corresponding watcher from Packet and registers it with Zkmanager, the code logic: P rivate void Finishpacket (Packet p) {if (p.watcherregistration! = null) {P.watcherregistration.register (p.reply     Header.geterr ()); }.......}
Now remove the Watcher object 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 = GETW Atchers (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 Register method above will be the temporary storage of the Watcher object to Zkwatchermanager, and finally saved to Datawatchers, Datawatchers, Zkwatchermanager are map<s The data structure of the tring,set<watcher>> type is used to map the path of the data node and the Watcher object one by one to save it.
In the above process, Watcherregistration is serialized to the underlying byte array in the underlying network.















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

I. Notification of WATCHER---change------> Client Registration

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.