Zookeeper源代码阅读分析之watcher机制

来源:互联网 发布:nginx 禁止目录浏览 编辑:程序博客网 时间:2024/05/03 18:13


    客户端ClientWatchManager,管理由ClientXncn产生的watchers和handle events
在zookeeper的exists、getChildren、getData等这些API中可以注册watcher对象到ClientWatchManager中,
create、setData、delete等这些引起zookeeper节点变化的API会触发watcher process的执行。

    服务端WatchManager,服务端的watcher对象管理器;
    注册watcher时候,会在服务端调用FinalRequestProcessor.processRequest,注册client对应的服务端连接对象ServerCnxn(实现了watcher接口)到DataTree中
这样在触发server端的watcher时,其实就是触发ServerCnxn的process方法,在ServerCnxn的process这个实现里会向对应的注册watcher对象的client发送notify消息,
而客户端会调用对应path注册的watcher对象的process方法
服务端注册watcher对象
FinalRequestProcessor.processRequest
             case OpCode.getData: {
        ...............................
                Stat stat = new Stat();
                byte b[] = zks.getZKDatabase().getData(getDataRequest.getPath(), stat,
                        getDataRequest.getWatch() ? cnxn : null);//cnxn为ServerCnxn
                rsp = new GetDataResponse(b, stat);
     watcher的触发,WatchManager中的trigerWatch(String path,EvenType type),当server接受到例如createNode/deleteNode/setData等操作时,
将会操作ZKDatabase来操作DataTree中的数据,当然dataTree的数据改动,将会触发相应patch(节点)上的watch(有可能一个操作会导致多种watch被触发),
trigerWatch就是在这些时机下被调用。此操作中就是从watchManager中将相应path下注册的watch移除,并依次调用watch.process()。
此process()做了一件事情,就是向client发送一个nofication消息,此消息中包含一个WatchEvent对象,此对象封装了事件的类型/path等,

发送到EventThread中的waitingEvents队列中,EventThread后台线程从队列中拉取消息执行watcher中的process逻辑。

     在ServerCnxn处理请求时出现异常或者client关闭,将会导致ServerCnxn调用close()方法,此方法中有个分支操作就是从DataTree中的两种watches列表中删除其关联的watch。


 

原创粉丝点击