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

The following examples show how to use org.apache.zookeeper.Watcher.Event.EventType#NodeChildrenChanged . 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: FlinkServerRegister.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void process(WatchedEvent event) {
    logger.info("Handle Zookeeper Event({}) started.", event);

    KeeperState state = event.getState();
    EventType eventType = event.getType();
    String path = event.getPath();

    if (state == KeeperState.SyncConnected || state == KeeperState.NoSyncConnected) {
        // when this happens, ephemeral node disappears
        // reconnects automatically, and process gets notified for all events
        if (eventType == EventType.NodeChildrenChanged) {
            logger.info("zookeeper Event occurs : NodeChildrenChanged event");
        } else if (eventType == EventType.NodeDeleted) {
            logger.info("zookeeper Event occurs : NodeDeleted");
        } else if (eventType == EventType.NodeDataChanged) {
            logger.info("zookeeper Event occurs : NodeDataChanged");
        }
    }
    logger.info("Handle Zookeeper Event({}) completed.", event);
}
 
Example 3
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 4
Source File: ZookeeperClusterService.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    logger.debug("Process Zookeeper Event({})", event);

    EventType eventType = event.getType();

    if (serviceState.isStarted() && client.isConnected()) {
        // duplicate event possible - but the logic does not change
        if (eventType == EventType.NodeChildrenChanged) {
            String path = event.getPath();

            if (ZookeeperConstants.PINPOINT_WEB_CLUSTER_PATH.equals(path)) {
                webClusterManager.handleAndRegisterWatcher(path);
            } else {
                logger.warn("Unknown Path ChildrenChanged {}.", path);
            }
        }
    }
}
 
Example 5
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 6
Source File: FlinkClusterService.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    logger.debug("Process Zookeeper Event({})", event);

    EventType eventType = event.getType();

    if (serviceState.isStarted() && client.isConnected()) {
        // duplicate event possible - but the logic does not change
        if (eventType == EventType.NodeChildrenChanged) {
            String eventPath = event.getPath();

            if (pinpointFlinkClusterPath.equals(eventPath)) {
                zookeeperClusterManager.handleAndRegisterWatcher(eventPath);
            } else {
                logger.warn("Unknown Path ChildrenChanged {}.", eventPath);
            }
        }
    }
}
 
Example 7
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 8
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 9
Source File: LeaderElection.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    if (stopped) {
        return;
    }

    if (event.getType() == EventType.NodeChildrenChanged && event.getPath().equals(electionPath)) {
        watchLeaders();
    }
}
 
Example 10
Source File: ZookeeperClusterDataManager.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void process(WatchedEvent event) {
    logger.info("Handle Zookeeper Event({}) started.", event);

    KeeperState state = event.getState();
    EventType eventType = event.getType();
    String path = event.getPath();

    // when this happens, ephemeral node disappears
    // reconnects automatically, and process gets notified for all events
    boolean result = false;
    if (state == KeeperState.SyncConnected || state == KeeperState.NoSyncConnected) {
        if (eventType == EventType.NodeChildrenChanged) {
            result = handleNodeChildrenChanged(path);
        } else if (eventType == EventType.NodeDeleted) {
            result = handleNodeDeleted(path);
        } else if (eventType == EventType.NodeDataChanged) {
            result = handleNodeDataChanged(path);
        }
    }

    if (result) {
        logger.info("Handle Zookeeper Event({}) completed.", event);
    } else {
        logger.info("Handle Zookeeper Event({}) failed.", event);
    }
}
 
Example 11
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 12
Source File: Quorum.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    try {
        if (event.getType() == EventType.NodeChildrenChanged) {
            refreshNodes();
        }
    }
    catch (Exception x) {
        _log.error("error from zookeeper", x);
    }
}
 
Example 13
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");
    }
  }
}
 
Example 14
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 15
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 16
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 17
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");
    }
}