zookeeper source code analysis - event monitoring Watcher

Posted by centenial on Tue, 10 Dec 2019 21:28:13 +0100

Watcher is the event monitoring mechanism of zookeeper. Today, let's see what the code of watcher class contains?

Watcher

Watcher is an interface that defines the process method, which needs to be implemented by a subclass. It represents the method that must be implemented when implementing the watcher interface, that is, defining for processing, and WatchedEvent represents the observed event.

abstract public void process(WatchedEvent event);

Inner class

1. Event interface

Represents the state represented by the event, which contains two internal enumeration classes: KeeperState and EventType.

KeeperState

KeeperState is an enumeration class, which defines various states of Zookeeper when an event occurs, and also defines a method fromInt to return the corresponding state from an integer value.

@InterfaceAudience.Public
    public interface Event {
        /**
         * Enumeration of states the ZooKeeper may be at the event
         */
        @InterfaceAudience.Public
        public enum KeeperState {
            /** Unused, this state is never generated by the server */
            //Unknown state, server no longer generates this state
            @Deprecated
            Unknown (-1),

            /** The client is in the disconnected state - it is not connected
             * to any server in the ensemble. */
            //To break off
            Disconnected (0),

            /** Unused, this state is never generated by the server */
            //The connection is not synchronized and is no longer used. The server will not generate this state
            @Deprecated
            NoSyncConnected (1),

            /** The client is in the connected state - it is connected
             * to a server in the ensemble (one of the servers specified
             * in the host connection parameter during ZooKeeper client
             * creation). */
            //Sync connection status
            SyncConnected (3),

            /**
             * Auth failed state
             */
            //Authentication failure status
            AuthFailed (4),

            /**
             * The client is connected to a read-only server, that is the
             * server which is not currently connected to the majority.
             * The only operations allowed after receiving this state is
             * read operations.
             * This state is generated for read-only clients only since
             * read/write clients aren't allowed to connect to r/o servers.
             */
            //Read only connection status
            ConnectedReadOnly (5),

            /**
              * SaslAuthenticated: used to notify clients that they are SASL-authenticated,
              * so that they can perform Zookeeper actions with their SASL-authorized permissions.
              */
            //SASL certification status
            SaslAuthenticated(6),

            /** The serving cluster has expired this session. The ZooKeeper
             * client connection (the session) is no longer valid. You must
             * create a new client connection (instantiate a new ZooKeeper
             * instance) if you with to access the ensemble. */
            //past due
            Expired (-112),

            /** 
             * The client has been closed. This state is never generated by
             * the server, but is generated locally when a client calls
             * {@link ZooKeeper#close()} or {@link ZooKeeper#close(int)}
             */
            //Close
            Closed (7);

            //Integer value representing the state
            private final int intValue;     // Integer representation of value
                                            // for sending over wire

            KeeperState(int intValue) {
                this.intValue = intValue;
            }

            public int getIntValue() {
                return intValue;
            }
            //Constructing corresponding states from integers
            public static KeeperState fromInt(int intValue) {
                switch(intValue) {
                    case   -1: return KeeperState.Unknown;
                    case    0: return KeeperState.Disconnected;
                    case    1: return KeeperState.NoSyncConnected;
                    case    3: return KeeperState.SyncConnected;
                    case    4: return KeeperState.AuthFailed;
                    case    5: return KeeperState.ConnectedReadOnly;
                    case    6: return KeeperState.SaslAuthenticated;
                    case -112: return KeeperState.Expired;
                    case   7: return KeeperState.Closed;

                    default:
                        throw new RuntimeException("Invalid integer value for conversion to KeeperState");
                }
            }
        }

EventType

EventType is an enumeration class, which defines the type of events (such as creating nodes, deleting nodes, etc.), and also defines a method fromInt that returns the corresponding event type from an integer value.

 @InterfaceAudience.Public
        public enum EventType {
            //nothing
            None (-1),
            //Node creation
            NodeCreated (1),
            //Node deletion
            NodeDeleted (2),
            //Change of node data
            NodeDataChanged (3),
            //Change of child node
            NodeChildrenChanged (4),
            //Listener removal
            DataWatchRemoved (5),
            //Removal of sub node listening
            ChildWatchRemoved (6);

            private final int intValue;     // Integer representation of value
                                            // for sending over wire

            EventType(int intValue) {
                this.intValue = intValue;
            }

            public int getIntValue() {
                return intValue;
            }
            //Constructing events from integers
            public static EventType fromInt(int intValue) {
                switch(intValue) {
                    case -1: return EventType.None;
                    case  1: return EventType.NodeCreated;
                    case  2: return EventType.NodeDeleted;
                    case  3: return EventType.NodeDataChanged;
                    case  4: return EventType.NodeChildrenChanged;
                    case  5: return EventType.DataWatchRemoved;
                    case  6: return EventType.ChildWatchRemoved;

                    default:
                        throw new RuntimeException("Invalid integer value for conversion to EventType");
                }
            }           
        }

2. Enumeration WatcherType

Listener type enumeration

@InterfaceAudience.Public
    public enum WatcherType {
        //Child listener
        Children(1), 
        //Data monitoring
        Data(2), 
        //Arbitrarily
        Any(3);

        // Integer representation of value
        private final int intValue;

        private WatcherType(int intValue) {
            this.intValue = intValue;
        }

        public int getIntValue() {
            return intValue;
        }
                //Integer to type conversion
        public static WatcherType fromInt(int intValue) {
            switch (intValue) {
            case 1:
                return WatcherType.Children;
            case 2:
                return WatcherType.Data;
            case 3:
                return WatcherType.Any;

            default:
                throw new RuntimeException(
                        "Invalid integer value for conversion to WatcherType");
            }
        }
    }

WatchedEvent

/**
     * Create a WatchedEvent with specified type, state and path
     */
    public WatchedEvent(EventType eventType, KeeperState keeperState, String path) {
        this.keeperState = keeperState;
        this.eventType = eventType;
        this.path = path;
    }

The WatchedEvent class contains three properties, which respectively represent the state of Zookeeper when the event occurs, the event type and the node path involved in the event.

Topics: Java Zookeeper Session