zookeeper watcher使用注意点

背景

 项目中使用了zookeeper进行的类似工作流引擎的工作流转,将一次工作请求拆分了4个节点(S/E/T/L)。S阶段做完后,通过zk的watcher触发下一个E节点进行处理,S和E可能为不同的jvm上,所以需要走一个分布式的消息进行通知。

思路

基于zookeeper做持久化watcher,项目中直接使用zookeeper官方api,大致的工作模型:

private synchronized void initNodes(List<String> nodes) {
    // 根据zk节点,判断是否需要处理
}

private void syncNodes() {
        try {
            List<String> nodes = zookeeper.getChildren(ArbitrateConstants.NODE_NID_ROOT, new AsyncWatcher() {

                public void asyncProcess(WatchedEvent event) {
                    syncNodes();// 继续关注node节点变化
                }
            });

            initNodes(nodes);
        } catch (KeeperException e) {
            syncNodes();
        } catch (InterruptedException e) {
            // ignore
        }
    }
  • 有两个方法initNodes 和 syncNodes,  syncNodes主要是监听zookeeper的节点变化
  • syncNodes会通过级联方式,在每次watcher被触发后,就会再挂一次watcher。完成了一个类似链式触发的功能

遇到的问题

系统上线运行后,跑了几天时间,跑出了一个OutOfMemory的问题,jmap dump了下对应的内存数据文件,发现了一个zk使用上的问题.

a. 通过mat分析查看了下jvm中占用内存最大的对象,居然是zookeeper中的一个waitingEvents.: 

b. waitingEvents中的WatcherSetEventPair对象中,包含了一个待响应的watchers和对应的响应event事件对象,对应的watchers数量居然有300W个

问题分析:

分析了下WatcherSetEventPair中的处理机制。 

Event响应中对应EventType的枚举类型:(存在一个特殊的None类型)

  • None (-1),
  • NodeCreated (1),
  • NodeDeleted (2),
  • NodeDataChanged (3),
  • NodeChildrenChanged (4);

查了下代码,None类型会在Session expired / connection loss/  auth failed得到对应的触发,对应的触发path为null
代码:
eventThread.queueEvent(new WatchedEvent(
                        Watcher.Event.EventType.None,
                        Watcher.Event.KeeperState.Expired, null));
针对None类型,在获取对应的watcher响应时:
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
                                        Watcher.Event.EventType type,
                                        String clientPath)
        {
            Set<Watcher> result = new HashSet<Watcher>();

            switch (type) {
            case None:
                result.add(defaultWatcher);
                for(Set<Watcher> ws: dataWatches.values()) {
                    result.addAll(ws);
                }
                for(Set<Watcher> ws: existWatches.values()) {
                    result.addAll(ws);
                }
                for(Set<Watcher> ws: childWatches.values()) {
                    result.addAll(ws);
                }

                // clear the watches if auto watch reset is not enabled
                if (ClientCnxn.getDisableAutoResetWatch() &&
                        state != Watcher.Event.KeeperState.SyncConnected)
                {
                    synchronized(dataWatches) {
                        dataWatches.clear();
                    }
                    synchronized(existWatches) {
                        existWatches.clear();
                    }
                    synchronized(childWatches) {
                        childWatches.clear();
                    }
                }

                return result;
针对出现None的类型,会将所有的watcher进行触发, 同时并不会移除watcher,所以,watcher会在下一次reconnect成功后再次触发,除非设置DisableAutoResetWatch

总结

a.  需要明确watcher的触发条件和触发case场景。特别注意,None类型可能会引起触发2次watcher调用

(截取了淘宝同学的blog : http://rdc.taobao.com/team/jm/archives/1047)

event For “/path” defaultWatcher exists
(“/path”)
getData
(“/path”)
getChildren
(“/path”)
EventType.None
EventType.NodeCreated    
EventType.NodeDeleted    
EventType.NodeDataChanged    
EventType.NodeChildrenChanged      

b.  出现session expired,需要重建zookeeper connector,对应的watcher会失效。因为watcher在client的存储是和对应的zookeeper client绑定,不同的client有不同的watcher列表。

猜你喜欢

转载自agapple.iteye.com/blog/1685957