zookeeper(8)源碼分析-事件監聽Watcher(1)

Watcher是zookeeper的事件監聽機制,今天我們來看看Watcher類的代碼都包含了什麼內容?

Watcher

Watcher是一個接口,定義了process方法,需要子類實現。其代表了實現Watcher接口時必須實現的的方法,即定義進行處理,WatchedEvent表示觀察的事件。

abstract public void process(WatchedEvent event);

內部類

1、Event接口

表示事件代表的狀態,其包含了KeeperState和EventType兩個內部枚舉類。

KeeperState

KeeperState是一個枚舉類,其定義了在事件發生時Zookeeper所處的各種狀態,其還定義了一個從整型值返回對應狀態的方法fromInt。

@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 */
            //未知狀態,服務器不再產生此狀態
            @Deprecated
            Unknown (-1),

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

            /** Unused, this state is never generated by the server */
            //未同步連接,不再使用,服務器不會產生此狀態
            @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). */
            //同步連接狀態
            SyncConnected (3),

            /**
             * Auth failed state
             */
            //認證失敗狀態
            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.
             */
            //只讀連接狀態
            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認證通過狀態
            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. */
            //過期狀態
            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)}
             */
            //關閉
            Closed (7);

            //代表狀態的整型值
            private final int intValue;     // Integer representation of value
                                            // for sending over wire

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

            public int getIntValue() {
                return intValue;
            }
            //從整型構造相應的狀態
            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是一個枚舉類,其定義了事件的類型(如創建節點、刪除節點等事件),同時,其還定義了一個從整型值返回對應事件類型的方法fromInt。

 @InterfaceAudience.Public
        public enum EventType {
            //無
            None (-1),
            //結點創建
            NodeCreated (1),
            //結點刪除
            NodeDeleted (2),
            //結點數據變化
            NodeDataChanged (3),
            //子結點變化
            NodeChildrenChanged (4),
            //監聽移除
            DataWatchRemoved (5),
            //子結點監聽移除
            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;
            }
            //從整型構造相應的事件
            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、枚舉類WatcherType

監聽器類型枚舉

@InterfaceAudience.Public
    public enum WatcherType {
        //子監聽器
        Children(1), 
        //數據監聽
        Data(2), 
        //任意
        Any(3);

        // Integer representation of value
        private final int intValue;

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

        public int getIntValue() {
            return intValue;
        }
                //整數到類型的轉換
        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;
    }

WatchedEvent類包含了三個屬性,分別代表事件發生時Zookeeper的狀態、事件類型和發生事件所涉及的節點路徑。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章