[Zookeeper学习笔记之六]Zookeeper源代码分析之Zookeeper.WatchRegistration

编程技术  /  houtizong 发布于 3年前   57

Zookeeper类是Zookeeper提供给用户访问Zookeeper service的主要API,它包含了如下几个内部类

 

 

首先分析它的内部类,从WatchRegistration开始,为指定的znode path注册一个Watcher,

 

    /**     * Register a watcher for a particular path.     */    abstract class WatchRegistration {        //Watcher和clientPath组织在一起,一个Watcher对应一个具体的path,一个具体        //的path可以绑定多个Watcher?        private Watcher watcher;        private String clientPath;        public WatchRegistration(Watcher watcher, String clientPath)        {            this.watcher = watcher;            this.clientPath = clientPath;        }                //获取Watcher集合,Map的key是clientPath        //抽象类,需要实现类进行实现        abstract protected Map<String, Set<Watcher>> getWatches(int rc);        /**         * Register the watcher with the set of watches on path.         * @param rc the result code of the operation that attempted to         * add the watch on the path.         */        public void register(int rc) {            if (shouldAddWatch(rc)) { //抽象累默认rc为0,则添加,实现类可以改写                Map<String, Set<Watcher>> watches = getWatches(rc);                synchronized(watches) {//实现类返回的watches必须为非null                   //获取clientPath对应的Watcher集合                    Set<Watcher> watchers = watches.get(clientPath);                    if (watchers == null) {//Set唯恐,构造Set,并设置到Map中                        watchers = new HashSet<Watcher>();                        watches.put(clientPath, watchers);                    }                    //将watcher添加到Set中                    watchers.add(watcher);                }            }        }        /**         * Determine whether the watch should be added based on return code.         * @param rc the result code of the operation that attempted to add the         * watch on the node         * @return true if the watch should be added, otw false         */        protected boolean shouldAddWatch(int rc) {            return rc == 0;        }    }

 

接下来简单下分析WatcherRegistration的实现类,

 

    /** Handle the special case of exists watches - they add a watcher     * even in the case where NONODE result code is returned.     */    //一个zookeeper客户端调用Zookeeper.exists方法,即使znode不存在,也会添加监听这个znode的创建    //如果另外一个zookeeper客户端创建这个znode,上面的watcher也会收到这个事件    class ExistsWatchRegistration extends WatchRegistration {        public ExistsWatchRegistration(Watcher watcher, String clientPath) {            super(watcher, clientPath);        }        @Override        protected Map<String, Set<Watcher>> getWatches(int rc) {            //如果znode存在,那么返回Zookeeper的实例的watchManager实例的dataWatches,否则返回watchManager实例的existWatches            //zookeeper的实例的watchManager实例的dataWatches和existWatches表示什么含义??zookeeper应该把watcher进行了分类            //dataWatch表示数据变化类(NodeDataChangeEvent)的watch,existWatch表示存在类的watch            return rc == 0 ?  watchManager.dataWatches : watchManager.existWatches;        }        @Override        protected boolean shouldAddWatch(int rc) {            return rc == 0 || rc == KeeperException.Code.NONODE.intValue(); //znode不存在,调用zookeeper.exists也可以添加这个watcher,        }    }

 

    DataWatchRegistration类

    class DataWatchRegistration extends WatchRegistration {        public DataWatchRegistration(Watcher watcher, String clientPath) {            super(watcher, clientPath);        }        @Override        protected Map<String, Set<Watcher>> getWatches(int rc) {        //只返回Zookeeper对象的watchManager的dataWatches,dataWatches表示数据变化类(NodeDataChangeEvent)的watch            return watchManager.dataWatches;        }    }

 

    ChildWatchRegistration类

    class ChildWatchRegistration extends WatchRegistration {        public ChildWatchRegistration(Watcher watcher, String clientPath) {            super(watcher, clientPath);        }        @Override        protected Map<String, Set<Watcher>> getWatches(int rc) {        //返回的是child变化事件,该Watcher设置在父节点上,子节点的添加和删除触发此事件            return watchManager.childWatches;        }    }

 

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客