zookeeper(10)源码分析-事件监听Watcher(3)

今天继续源码分析,分析一下org.apache.zookeeper.server下的WatchManager类。

WatcherManager类用于管理watchers和相应的触发器。

类的属性

//watchTable表示从节点路径到watcher集合的映射
    private final HashMap<String, HashSet<Watcher>> watchTable =
        new HashMap<String, HashSet<Watcher>>();
    //watch2Paths则表示从watcher到所有节点路径集合的映射
    private final HashMap<Watcher, HashSet<String>> watch2Paths =
        new HashMap<Watcher, HashSet<String>>();

核心方法

1. size方法

size方法是同步的,因此在多线程环境下是安全的,其主要作用是获取watchTable的大小,即遍历watchTable的值集合,每个集合大小累加。

synchronized int size(){
        int result = 0;
        for(Set<Watcher> watches : watchTable.values()) {
            result += watches.size();
        }
        return result;
    }

2、addWatch方法

addWatch方法同样是同步的,主要用来更新类里面的上面提到的两个集合属性。

synchronized void addWatch(String path, Watcher watcher) {
        //通过传入的path(节点路径)从watchTable获取相应的watcher集合
        HashSet<Watcher> list = watchTable.get(path);
        if (list == null) { //watcher是否为空,若为空
            // don‘t waste memory if there are few watches on a node
            // rehash when the 4th entry is added, doubling size thereafter
            // seems like a good compromise
            //新生成watcher集合,并将路径path和此集合添加至watchTable中
            list = new HashSet<Watcher>(4);
            watchTable.put(path, list);
        }
        //将传入的watcher添加至watcher集合,即完成了path和watcher添加至watchTable的步骤
        list.add(watcher);
        //通过传入的watcher从watch2Paths中获取相应的path集合
        HashSet<String> paths = watch2Paths.get(watcher);
        if (paths == null) {// 判断path集合是否为空,若为空
            // cnxns typically have many watches, so use default cap here
            //新生成path集合,并将watcher和paths添加至watch2Paths中
            paths = new HashSet<String>();
            watch2Paths.put(watcher, paths);
        }
        // 将传入的path(节点路径)添加至path集合,即完成了path和watcher添加至watch2Paths的步骤
        paths.add(path);
    }

3、removeWatcher

removeWatcher用作从watch2Paths和watchTable中中移除该watcher

synchronized void removeWatcher(Watcher watcher) {
        //从wach2Paths中移除watcher,并返回watcher对应的path集合
        HashSet<String> paths = watch2Paths.remove(watcher);
        if (paths == null) {
            return;
        }
        for (String p : paths) {
            //从watcherTable中根据路径取出相应的watcher集合
            HashSet<Watcher> list = watchTable.get(p);
            if (list != null) {
                // 从list中移除该watcher
                list.remove(watcher);
                // 移除后list为空,则从watchTable中移出path
                if (list.size() == 0) {
                    watchTable.remove(p);
                }
            }
        }
    }

4、triggerWatch

该方法主要用于触发watch事件,并对事件进行处理。

Set<Watcher> triggerWatch(String path, EventType type) {
        return triggerWatch(path, type, null);
    }

    Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
        // 根据事件类型、连接状态、节点路径创建WatchedEvent
        WatchedEvent e = new WatchedEvent(type,
                KeeperState.SyncConnected, path);
        HashSet<Watcher> watchers;
        synchronized (this) {
            // 从watcher表中移除path,并返回其对应的watcher集合
            watchers = watchTable.remove(path);
            if (watchers == null || watchers.isEmpty()) {
                if (LOG.isTraceEnabled()) {
                    ZooTrace.logTraceMessage(LOG,
                            ZooTrace.EVENT_DELIVERY_TRACE_MASK,
                            "No watchers for " + path);
                }
                return null;
            }
            // 遍历watcher集合
            for (Watcher w : watchers) {
                // 根据watcher从watcher表中取出路径集合
                HashSet<String> paths = watch2Paths.get(w);
                if (paths != null) {
                                // 如果paths不为空,则移除传入路径path
                    paths.remove(path);
                }
            }
        }
        // 遍历watcher集合
        for (Watcher w : watchers) {
            if (supress != null && supress.contains(w)) {
                continue;
            }
            // watcher进行处理
            w.process(e);
        }
        return watchers;
    }

原文地址:https://blog.51cto.com/janephp/2456616

时间: 2024-08-04 01:39:31

zookeeper(10)源码分析-事件监听Watcher(3)的相关文章

zookeeper(9)源码分析-事件监听Watcher(2)

接着上一篇文章,继续分析和Watcher相关的类的源码. ClientWatchManager public Set<Watcher> materialize(Watcher.Event.KeeperState state, Watcher.Event.EventType type, String path); 该接口,只有一个方法,需要实现.该方法表示事件发生时,返回需要被通知的Watcher集合,可能为空集合. ZKWatchManager 1.ZKWatchManager是ZooKeep

zookeeper(8)源码分析-事件监听Watcher(1)

Watcher是zookeeper的事件监听机制,今天我们来看看Watcher类的代码都包含了什么内容? Watcher Watcher是一个接口,定义了process方法,需要子类实现.其代表了实现Watcher接口时必须实现的的方法,即定义进行处理,WatchedEvent表示观察的事件. abstract public void process(WatchedEvent event); 内部类 1.Event接口 表示事件代表的状态,其包含了KeeperState和EventType两个内

【Zookeeper】源码分析目录

Zookeeper源码分析目录如下 1. [Zookeeper]源码分析之序列化 2. [Zookeeper]源码分析之持久化(一)之FileTxnLog 3. [Zookeeper]源码分析之持久化(二)之FileSnap 4. [Zookeeper]源码分析之持久化(三)之FileTxnSnapLog 5. [Zookeeper]源码分析之Watcher机制(一) 6. [Zookeeper]源码分析之Watcher机制(二)之WatchManager 7. [Zookeeper]源码分析之

Spark2.1.0之源码分析——事件总线

阅读提示:阅读本文前,最好先阅读<Spark2.1.0之源码分析--事件总线>.<Spark2.1.0事件总线分析--ListenerBus的继承体系>及<Spark2.1.0事件总线分析--SparkListenerBus详解>几篇文章的内容. LiveListenerBus继承了SparkListenerBus,并实现了将事件异步投递给监听器,达到实时刷新UI界面数据的效果.LiveListenerBus主要由以下部分组成: eventQueue:是SparkLis

【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象

前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑][下文简称(五),请先阅读完(五)再阅读本文],我们通过示例和log来分析了Android的事件分发机制.这些,我们只是看到了现象,如果要进一步了解事件分发机制,这是不够的,我们还需要透过现象看本质,去研究研究源码.本文将从源码(基

jQuery 2.0.3 源码分析 事件绑定 - bind/live/delegate/on

转:http://www.cnblogs.com/aaronjs/p/3440647.html?winzoom=1 事件(Event)是JavaScript应用跳动的心脏,通过使用JavaScript ,你可以监听特定事件的发生,并规定让某些事件发生以对这些事件做出响应 事件的基础就不重复讲解了,本来是定位源码分析实现的, 所以需要有一定的基础才行 为了下一步更好的理解内部的实现,所以首先得清楚的认识到事件接口的划分 网上资料遍地都是,但是作为一个jQuery系列的源码分析,我还是很有必要在重新

【Zookeeper】源码分析之Watcher机制(一)

一.前言 前面已经分析了Zookeeper持久话相关的类,下面接着分析Zookeeper中的Watcher机制所涉及到的类. 二.总体框图 对于Watcher机制而言,主要涉及的类主要如下. 说明: Watcher,接口类型,其定义了process方法,需子类实现. Event,接口类型,Watcher的内部类,无任何方法. KeeperState,枚举类型,Event的内部类,表示Zookeeper所处的状态. EventType,枚举类型,Event的内部类,表示Zookeeper中发生的事

【Zookeeper】源码分析之持久化--FileTxnSnapLog

一.前言 前面分析了FileSnap,接着继续分析FileTxnSnapLog源码,其封装了TxnLog和SnapShot,其在持久化过程中是一个帮助类. 二.FileTxnSnapLog源码分析 2.1 类的属性 public class FileTxnSnapLog { //the direcotry containing the //the transaction logs // 日志文件目录 private final File dataDir; //the directory cont

【Zookeeper】源码分析之服务器(二)

一.前言 前面阐述了服务器的总体框架,下面来分析服务器的所有父类ZooKeeperServer. 二.ZooKeeperServer源码分析 2.1 类的继承关系 public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {} 说明:ZooKeeperServer是ZooKeeper中所有服务器的父类,其实现了Session.Expirer和ServerStats.Provider接口,Session