Java Code Examples for org.apache.zookeeper.Watcher.Event.EventType#NodeCreated

The following examples show how to use org.apache.zookeeper.Watcher.Event.EventType#NodeCreated . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZkClient.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void processDataOrChildChange(WatchedEvent event) {
    final String path = event.getPath();

    if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) {
        Set<IZkChildListener> childListeners = _childListener.get(path);
        if (childListeners != null && !childListeners.isEmpty()) {
            fireChildChangedEvents(path, childListeners);
        }
    }

    if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) {
        Set<IZkDataListener> listeners = _dataListener.get(path);
        if (listeners != null && !listeners.isEmpty()) {
            fireDataChangedEvents(event.getPath(), listeners);
        }
    }
}
 
Example 2
Source File: ZkClient.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void processDataOrChildChange(WatchedEvent event) {
    final String path = event.getPath();

    if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) {
        Set<IZkChildListener> childListeners = _childListener.get(path);
        if (childListeners != null && !childListeners.isEmpty()) {
            fireChildChangedEvents(path, childListeners);
        }
    }

    if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) {
        Set<IZkDataListener> listeners = _dataListener.get(path);
        if (listeners != null && !listeners.isEmpty()) {
            fireDataChangedEvents(event.getPath(), listeners);
        }
    }
}
 
Example 3
Source File: ZkClient.java    From TakinRPC with Apache License 2.0 6 votes vote down vote up
private void processDataOrChildChange(WatchedEvent event) {
    final String path = event.getPath();

    if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) {
        Set<IZkChildListener> childListeners = _childListener.get(path);
        if (childListeners != null && !childListeners.isEmpty()) {
            fireChildChangedEvents(path, childListeners);
        }
    }

    if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) {
        Set<IZkDataListener> listeners = _dataListener.get(path);
        if (listeners != null && !listeners.isEmpty()) {
            fireDataChangedEvents(event.getPath(), listeners);
        }
    }
}
 
Example 4
Source File: ZKChildDataListener.java    From zkclient with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(String path, EventType eventType, Object data) throws Exception {
    //子节点个数变化
    if (eventType == EventType.NodeChildrenChanged 
            || eventType == EventType.NodeCreated 
            || eventType == EventType.NodeDeleted) {
        handleChildCountChanged(path,(List<String>)data);
    }
    //子节点数据变化
    if(eventType == EventType.NodeDataChanged){
        handleChildDataChanged(path,data);
    }
    //Session失效
    if(eventType == eventType.None){
        handleSessionExpired(path,data);
    }
}
 
Example 5
Source File: ZKNodeListener.java    From zkclient with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(String path, EventType eventType, Object data) throws Exception {
    
    if (eventType == EventType.NodeCreated) {
        handleDataCreated(path,data);
    }
    
    if (eventType == EventType.NodeDataChanged) {
        handleDataChanged(path,data);
    }
    
    if(eventType == EventType.NodeDeleted ){
        handleDataDeleted(path);
    }
    
    if(eventType == eventType.None){
        handleSessionExpired(path);
    }
}
 
Example 6
Source File: ZkClient.java    From helix with Apache License 2.0 6 votes vote down vote up
private void processDataOrChildChange(WatchedEvent event, long notificationTime) {
  final String path = event.getPath();
  final boolean pathExists = event.getType() != EventType.NodeDeleted;
  if (EventType.NodeDeleted == event.getType()) {
    LOG.debug("Event NodeDeleted: {}", event.getPath());
  }

  if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated
      || event.getType() == EventType.NodeDeleted) {
    Set<IZkChildListener> childListeners = _childListener.get(path);
    if (childListeners != null && !childListeners.isEmpty()) {
      // TODO recording child changed event propagation latency as well. Note this change will
      // introduce additional ZK access.
      fireChildChangedEvents(path, childListeners, pathExists);
    }
  }

  if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted
      || event.getType() == EventType.NodeCreated) {
    Set<IZkDataListenerEntry> listeners = _dataListener.get(path);
    if (listeners != null && !listeners.isEmpty()) {
      fireDataChangedEvents(event.getPath(), listeners, OptionalLong.of(notificationTime),
          pathExists);
    }
  }
}
 
Example 7
Source File: ZooKeeperSessionWatcherTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess3() {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, KeeperState.Expired, null);
    sessionWatcher.process(event);
    assertFalse(sessionWatcher.isShutdownStarted());
    assertEquals(shutdownService.getExitCode(), 0);
}
 
Example 8
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
void handleNewSessionAsController() {
  if (_leaderElectionHandler != null) {
    _leaderElectionHandler.init();
  } else {
    _leaderElectionHandler =
        new CallbackHandler(this, _zkclient, _keyBuilder.controller(),
            new DistributedLeaderElection(this, _controller, _controllerTimerTasks),
            new EventType[] {
                EventType.NodeChildrenChanged, EventType.NodeDeleted, EventType.NodeCreated
            }, ChangeType.CONTROLLER, _callbackMonitors.get(ChangeType.CONTROLLER));
  }
}
 
Example 9
Source File: ZkClient.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    LOG.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
            || event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();
    try {

        // We might have to install child change event listener if a new node was created
        if (getShutdownTrigger()) {
            LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath() + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions, because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        LOG.debug("Leaving process event");
    }
}
 
Example 10
Source File: ZkClient.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    LOG.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
            || event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();
    try {

        // We might have to install child change event listener if a new node was created
        if (getShutdownTrigger()) {
            LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath() + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions, because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        LOG.debug("Leaving process event");
    }
}
 
Example 11
Source File: ZkClient.java    From TakinRPC with Apache License 2.0 4 votes vote down vote up
public void process(WatchedEvent event) {
    logger.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged || //
                    event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated || //
                    event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();
    try {

        // We might have to install child change event listener if a new node was created
        if (getShutdownTrigger()) {
            logger.debug("ignoring event '{" + event.getType() + " | " + event.getPath() + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions, because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        logger.debug("Leaving process event");
    }
}
 
Example 12
Source File: ZKWatcher.java    From zkclient with Apache License 2.0 4 votes vote down vote up
/**
 * 事件处理
 * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
 */
@Override
public void process(WatchedEvent event) {
    LOG.debug("ZooKeeper event is arrived [" + event+" ]...");
    EventType eventType = event.getType();
    //状态更新
    boolean stateChanged = event.getPath() == null;
    //节点相关的所有事件
    boolean znodeChanged = event.getPath() != null;
    
    //节点创建、删除和数据改变的事件
    boolean nodeChanged = eventType == EventType.NodeDataChanged 
            || eventType == EventType.NodeDeleted 
            || eventType == EventType.NodeCreated;
    
    //子节点数量改变相关的事件,包括节点创建和删除(都会影响子节点数量的变化),以及子节点数量的改变
    boolean childChanged = eventType == EventType.NodeDeleted 
            || eventType == EventType.NodeCreated
            || eventType == EventType.NodeChildrenChanged;
    
    client.acquireEventLock();
    try {
        if (client.getShutdownTrigger()) {
            LOG.debug("client will shutdown,ignore the event [" + eventType + " | " + event.getPath() + "]");
            return;
        }
        if (stateChanged) {//ZooKeeper状态改变的处理
            process.processStateChanged(event);
        }
        if (nodeChanged) {//节点改变事件处理,包括节点的创建、删除、数据改变
            process.processNodeChanged(event);
        }
        if (childChanged) {//造成子节点数量改变的事件的处理,包括节点的创建、删除、子节点数量改变
            process.processChildChanged(event);
        }
    } finally {
        if (stateChanged) {
            client.getEventLock().getStateChangedCondition().signalAll();
            // 在会话失效后,服务端会取消watch.
            // 如果在会话失效后与重连这段时间内有数据发生变化,监听器是无法监听到的,
            // 所以要唤醒等待的监听,并触发所有的监听事件
            if (event.getState() == KeeperState.Expired) {
                client.getEventLock().getNodeEventCondition().signalAll();
                client.getEventLock().getNodeOrChildChangedCondition().signalAll();
                
                // 通知所有的监听器,可能存在数据变化
                process.processAllNodeAndChildListeners(event);
            }
        }
        if (znodeChanged) {
            client.getEventLock().getNodeEventCondition().signalAll();
        }
        if (nodeChanged || childChanged) {
            client.getEventLock().getNodeOrChildChangedCondition().signalAll();
        }
        client.releaseEventLock();
    }
}
 
Example 13
Source File: ZKWatcherProcess.java    From zkclient with Apache License 2.0 4 votes vote down vote up
/**
 * 提交节点改变相关的事件进行处理
 * @param listeners
 * @param childDataChangeListners
 * @param path
 * @param eventType 
 * @return void
 */
private void submitNodeEvent(final Set<ZKListener> listeners,final Set<ZKListener> childDataChangeListners,final String path,final EventType eventType ){
    if (listeners != null && !listeners.isEmpty()) {
        for (final ZKListener listener : listeners) {
            ZKEvent zkEvent = new ZKEvent("Node of " + path + " changed sent to " + listener) {
                @Override
                public void run() throws Exception {
                    //原生的zookeeper 的监听只生效一次,重新注册监听
                    LOG.debug("Rewatch the path ["+path+"] by exists method");
                    boolean flag = client.exists(path, true);
                    LOG.debug("Rewatched the path ["+path+"] by exists method");
                    try {
                        LOG.debug("Rewatch and get changed data [path:"+path+" | EventType:"+eventType+"] by getData method");
                        Object data = client.getData(path, null);
                        LOG.debug("Rewatched and return data   ["+path+" | "+data+" | EventType:"+eventType+"] by getData method");
                        listener.handle(path, eventType, data);
                        
                        //响应了删除事件,但是在再次注册监听之前节点又被创建了,这样是无法重新监听到节点创建的
                        //这里主动触发节点创建事件。
                        if (eventType == EventType.NodeDeleted && flag) {
                            listener.handle(path, EventType.NodeCreated, data);
                        }
                    } catch (ZKNoNodeException e) {
                        //如果是节点不存在了,则只移除,ZKChildDataListener监听器
                        client.unlistenNodeChanges(path, childDataChangeListners);
                        //如果路径不存在,在调用client.getData(path,null)会抛出异常
                        listener.handle(path, eventType, null);
                        
                        //如果是创建节点事件,并且在创建事件收到后,监听还没来得及重新注册,刚创建的节点已经被删除了。
                        //对于这种情况,客户端就无法重新监听到节点的删除事件的,这里做特殊处理
                        //主动触发删除的监听事件
                        if (eventType == EventType.NodeCreated && !flag) {
                            listener.handle(path, EventType.NodeDeleted, null);
                        }
                    }
                }
            };
            eventThreadPool.submit(zkEvent);
        }
    }
    
}
 
Example 14
Source File: ZkClient.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public void process(WatchedEvent event) {
  long notificationTime = System.currentTimeMillis();
  if (LOG.isDebugEnabled()) {
    LOG.debug("Received event: " + event);
  }
  _zookeeperEventThread = Thread.currentThread();

  boolean stateChanged = event.getPath() == null;
  boolean sessionExpired = stateChanged && event.getState() == KeeperState.Expired;
  boolean znodeChanged = event.getPath() != null;
  boolean dataChanged =
      event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted
          || event.getType() == EventType.NodeCreated
          || event.getType() == EventType.NodeChildrenChanged;
  if (event.getType() == EventType.NodeDeleted) {
    LOG.debug("Path {} is deleted", event.getPath());
  }

  getEventLock().lock();
  try {
    // We might have to install child change event listener if a new node was created
    if (getShutdownTrigger()) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath()
            + "}' since shutdown triggered");
      }
      return;
    }
    if (stateChanged) {
      processStateChanged(event);
    }
    if (dataChanged) {
      processDataOrChildChange(event, notificationTime);
    }
  } finally {
    if (stateChanged) {
      getEventLock().getStateChangedCondition().signalAll();

      // If the session expired we have to signal all conditions, because watches might have been
      // removed and
      // there is no guarantee that those
      // conditions will be signaled at all after an Expired event
      // TODO PVo write a test for this
      if (event.getState() == KeeperState.Expired) {
        getEventLock().getZNodeEventCondition().signalAll();
        getEventLock().getDataChangedCondition().signalAll();
      }
    }
    if (znodeChanged) {
      getEventLock().getZNodeEventCondition().signalAll();
    }
    if (dataChanged) {
      getEventLock().getDataChangedCondition().signalAll();
    }
    getEventLock().unlock();

    // update state change counter.
    recordStateChange(stateChanged, dataChanged, sessionExpired);

    if (LOG.isDebugEnabled()) {
      LOG.debug("Leaving process event");
    }
  }
}