`
bit1129
  • 浏览: 1052208 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多

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;
        }
    }

 

 

 

分享到:
评论

相关推荐

    zookeeper学习笔记

    zookeeper学习笔记

    Zookeeper学习笔记.docx

    Zookeeper学习笔记

    Zookeeper学习笔记.pdf

    ZooKeeper是一种为分布式应用所设计的高可用、高性能且一致的开源协调服务,它提供了一项基本服务:**分布式锁服务**。由于ZooKeeper的开源特性,后来我们的开发者在分布式锁的基础上,摸索了出了其他的使用方法:**...

    zookeeper学习笔记.pptx

    本文适合但不限于软件开发人员阅读。本文档能够使阅读者对zookeeper有一个宏观且全面的了解,内容主要包含zookeeper架构、数据模型、读写及工作原理、典型应用场景、指令汇总等,

    org.apache.zookeeper/zookeeper的jar包

    这个只是其中一个版本的包,详细的可以在http://maven.outofmemory.cn/org.apache.zookeeper/zookeeper/里面找到更多的

    zookeeper笔记.pdf

    zookeeper笔记.pdf

    第6章 Zookeeper 2 6.1. Zookeeper入门 2 6.1.1. 概述 2 6.1.2. 特点 3 6.1.3

    第6章 Zookeeper 2 6.1. Zookeeper入门 2 6.1.1. 概述 2 6.1.2. 特点 3 6.1.3. 数据结构 4 6.1.4. 应用场景 4 6.1.5. 下载地址 6 6.2. Zookeeper安装 7 6.2.1. 本地模式安装部署 7 6.2.2. 配置参数解读 9 6.3. ...

    zookeeper-3.4.9.tar.gz

    zookeeper-3.4.9.tar.gz

    zookeeper面试专题.pdf

    1.ZooKeeper 是什么? 2.ZooKeeper 提供了什么? 3.Zookeeper 文件系统 4.四种类型的 znode 5.Zookeeper 通知机制 6.Zookeeper 做了什么? 7.zk 的命名服务(文件系统) 8.zk 的配置管理(文件系统、通知机制) 9....

    Zookeeper学习笔记

    自己整理的ZooKeeper学习笔记,适合刚刚接触ZooKeeper的人学习

    ZooKeeper学习笔记

    java ZooKeeper学习笔记\ZooKeeper原理、运用

    org.apache.zookeeper 3.4.6

    org.apache.zookeeper 3.4.6下载 org.apache.zookeeper 3.4.6 安全稳定的版本

    ZooKeeper笔记.pdf

    zookeeper笔记

    zookeeper-3.5.7.zip文件(分享给需要的同学)

    zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 zookeeper-3.5.7.zip文件 ...

    zookeeper-3.4.9.tar.gz+安装过程笔记.doc

    zookeeper.tar.gz包,及安装使用步骤:1、下载zookeeper.x.x.x.tar.gz,放到虚拟机上 2、解压 3、单机模式 a.在conf目录下,复制zoo_sample.cfg到当前文件夹下命名为zoo.cfg。zookeeper默认使用zoo.cfg配置文件 b....

    zookeeper-3.4.14.zip

    zookeeper-3.4.14,包含添加系统服务插件及添加bat. 1. zookeeper-3.4.14源包 2. commons-daemon-1.1.0-bin-windows.zip 插件 3. 配置好插件的zookeeper-3.4.14包,右键管理员权限执行zk-server-install.bat

    zookeeper-3.4.11.tar.gz

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调...ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.3\src\recipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。

    zookeeper-3.4.6.tar

    zookeeper-3.4.6.tar,

    Zookeeper源码分析.epub

    Zookeeper源码分析.epub

    zookeeper-3.4.10.tar.gz.zip

    zookeeper-3.4.10.tar.gz 安装包

Global site tag (gtag.js) - Google Analytics