Easy Tutorial
❮ Nvm Manager Node Versions Android Tutorial Broadcastreceiver 2 ❯

11.0 Zookeeper Watcher Event Mechanism Analysis

Category Zookeeper Tutorial

The Zookeeper watcher mechanism can be divided into four processes:

There are three ways for the client to register a watcher, by calling the client API which can be achieved through getData, exists, and getChildren. Using the maven project created in the previous chapters, a new WatcherDemo class is created to illustrate the principle using the exists method.

Example

public class WatcherDemo implements Watcher {
    static ZooKeeper zooKeeper;
    static {
        try {
            zooKeeper = new ZooKeeper("192.168.3.39:2181", 4000, new WatcherDemo());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void process(WatchedEvent event) {
        System.out.println("eventType:" + event.getType());
        if (event.getType() == Event.EventType.NodeDataChanged) {
            try {
                zooKeeper.exists(event.getPath(), true);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String path = "/watcher";
        if (zooKeeper.exists(path, false) == null) {
            zooKeeper.create("/watcher", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        Thread.sleep(1000);
        System.out.println("-----------");
        // true indicates using the watcher configured in the zookeeper instance
        Stat stat = zooKeeper.exists(path, true);
        System.in.read();
    }
}

After running the program, the console displays:

At this point, start the Zookeeper command-line terminal and view and delete the watcher node:

The IDE console outputs, triggering the node deletion event:

The client sends a request to the server through a TCP long connection to establish a network channel, with the underlying default being through Java's NIO method, or it can be configured with the netty implementation.

Registration of watcher event flowchart:

1. Client Sends Event Notification Request

When calling the exists method in the Zookeeper class, the event listener creation is encapsulated into the request object, with the watch attribute set to true. After the server returns the response, the event listener is encapsulated into the client's ZKWatchManager class.

2. Server Processes Watcher Event Request

The server's NIOServerCnxn class is used to handle requests sent from the client, eventually calling FinalRequestProcessor, where there is source code to add the watcher event sent by the client:

Then it enters the statNode method, where the watcher event is added in the DataTree class method and saved to WatchManager's watchTable and watch2Paths.

3. Server Triggers Watcher Event Process:

If a transaction request occurs on a watched node on the server, the server processes the request by calling the processRequest method in the FinalRequestProcessor class, as shown in the following code:

The deletion call chain eventually reaches the DataTree class and triggers the code segment for deleting nodes:

Enter the triggerWatch method of the WatchManager class:

Continuing to track into NIOServerCnxn, a ReplyHeader object with xid of -1 and zxid of -1 is constructed, and then the sendResponse method is called.

4. Client Callback Watcher Event

The client's SendThread class receives the server-triggered event notification in the readResponse method, enters the xid of -1 process, and handles the Event event.

Source Code Download for This Chapter

Download

-1.0 Zookeeper Tutorial

-2.0 Zookeeper Installation and Configuration

-3.0 Zookeeper Linux Server Cluster Setup Steps

-4.0 Zookeeper Java Client Setup

-5.0 ZooKeeper Data Model znode Structure Detailed

-6.0 Zookeeper Session Basic Principles

-7.0 Zookeeper Basic Commands Usage

-8.0 Zookeeper Four-Letter Commands

-9.0 Zookeeper Node Features

-10.0 Zookeeper Access Control ACL

-12.0 Zookeeper Data Synchronization Process

-13.0 Zookeeper Leader Election Principles

-14.0 Zookeeper Distributed Lock Implementation Principles

WeChat Subscription

English:

❮ Nvm Manager Node Versions Android Tutorial Broadcastreceiver 2 ❯