zookeeper(9)源碼分析-事件監聽Watcher(2)

接着上一篇文章,繼續分析和Watcher相關的類的源碼。

ClientWatchManager

public Set<Watcher> materialize(Watcher.Event.KeeperState state,
        Watcher.Event.EventType type, String path);

該接口,只有一個方法,需要實現。該方法表示事件發生時,返回需要被通知的Watcher集合,可能爲空集合。

ZKWatchManager

1、ZKWatchManager是ZooKeeper的內部類,實現了ClientWatchManager。
2、ZKWatchManager定義了三個Map鍵值對,鍵爲節點路徑,值爲Watcher。分別對應數據變化的Watcher、節點是否存在的Watcher、子節點變化的Watcher。

static class ZKWatchManager implements ClientWatchManager {
        //數據變化的watchers
        private final Map<String, Set<Watcher>> dataWatches =
            new HashMap<String, Set<Watcher>>();
        //節點存在與否的watchers
        private final Map<String, Set<Watcher>> existWatches =
            new HashMap<String, Set<Watcher>>();
        //子節點變化的watchers
        private final Map<String, Set<Watcher>> childWatches =
            new HashMap<String, Set<Watcher>>();

3、materialize方法

該方法在事件發生後,返回需要被通知的Watcher集合。在該方法中,首先會根據EventType類型確定相應的事件類型,然後根據事件類型的不同做出相應的操作,如針對None類型,即無任何事件,則首先會從三個鍵值對中刪除clientPath對應的Watcher,然後將剩餘的Watcher集合添加至結果集合;針對NodeDataChanged和NodeCreated事件而言,其會從dataWatches和existWatches中刪除clientPath對應的Watcher,然後將剩餘的Watcher集合添加至結果集合。

@Override
        public Set<Watcher> materialize(Watcher.Event.KeeperState state,
                                        Watcher.Event.EventType type,
                                        String clientPath)
        {
            //返回結果集合
            Set<Watcher> result = new HashSet<Watcher>();

            switch (type) {//事件類型
            case None://無類型
                //添加默認watcher
                result.add(defaultWatcher);
                //根據disableAutoWatchReset和Zookeeper的狀態是否爲同步連接判斷是否需要清空
                boolean clear = disableAutoWatchReset && state != Watcher.Event.KeeperState.SyncConnected;
                //針對3個不同的watcherMap進行操作
                synchronized(dataWatches) {
                    for(Set<Watcher> ws: dataWatches.values()) {
                        // 添加至結果集合
                        result.addAll(ws);
                    }
                    if (clear) { // 是否需要清空
                        dataWatches.clear();
                    }
                }

                synchronized(existWatches) {
                    for(Set<Watcher> ws: existWatches.values()) {
                        result.addAll(ws);
                    }
                    if (clear) {
                        existWatches.clear();
                    }
                }

                synchronized(childWatches) {
                    for(Set<Watcher> ws: childWatches.values()) {
                        result.addAll(ws);
                    }
                    if (clear) {
                        childWatches.clear();
                    }
                }

                return result;
            case NodeDataChanged:// 節點數據變化
            case NodeCreated:// 創建節點
                synchronized (dataWatches) {
                    //移除clientPath對應的Watcher後全部添加至結果集合
                    addTo(dataWatches.remove(clientPath), result);
                }
                synchronized (existWatches) {
                    //移除clientPath對應的Watcher後全部添加至結果集合
                    addTo(existWatches.remove(clientPath), result);
                }
                break;
            case NodeChildrenChanged: // 節點子節點變化
                synchronized (childWatches) {
                    // 移除clientPath對應的Watcher後全部添加至結果集合
                    addTo(childWatches.remove(clientPath), result);
                }
                break;
            case NodeDeleted:// 刪除節點
                synchronized (dataWatches) {
                    // 移除clientPath對應的Watcher後全部添加至結果集合
                    addTo(dataWatches.remove(clientPath), result);
                }
                // XXX This shouldn't be needed, but just in case
                synchronized (existWatches) {
                    Set<Watcher> list = existWatches.remove(clientPath);
                    if (list != null) {
                        addTo(list, result);
                        LOG.warn("We are triggering an exists watch for delete! Shouldn't happen!");
                    }
                }
                synchronized (childWatches) {
                    //移除clientPath對應的Watcher後全部添加至結果集合
                    addTo(childWatches.remove(clientPath), result);
                }
                break;
            default:
                String msg = "Unhandled watch event type " + type
                    + " with state " + state + " on path " + clientPath;
                LOG.error(msg);
                throw new RuntimeException(msg);
            }

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