当前位置:首页 > 开发 > 开源软件 > 正文

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

发表于: 2014-07-29   作者:bit1129   来源:转载   浏览:
摘要: Zookeeper类是Zookeeper提供给用户访问Zookeeper service的主要API,它包含了如下几个内部类     首先分析它的内部类,从WatchRegistration开始,为指定的znode path注册一个Watcher,   /** * Register a watcher for a particular p

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.WatchRegistration

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
ZooKeeper 支持某些特定的四字命令字母与其的交互。它们大多是查询命令,用来获取 ZooKeeper 服务的
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
Zookeeper的会话状态变迁图: Connection Loss: CONNECTION_LOSS意味着客户端和服务器端的连接断开
1. 什么是ZooKeeper? ZooKeeper是一组工具,用来配置和支持分布式调度。以我的理解,就是一个网管
Zookeeper 是Hadoop的高可用高性能的分布式协调服务。 1 Partial Failure 部分失败 即:我们不知道
分布式架构是中心化的设计,就是一个主控机连接多个处理节点,因此保证主控机高可用性十分关键.分布
如果你喜欢命令行式交互型的监控,不妨使用这个小工具。 github地址:https://github.com/phunt/zkt
如果我们要写一个ZooKeeper的client程序,那么有一个相关的配置:connect string,它通常以用逗号分
这是淘宝上的开源项目,地址:http://code.taobao.org/p/zkweb/wiki/index/ 这是一个Java写的web应
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号