忧郁者 发表于 2018-10-30 09:11:29

Hadoop2.6.0学习笔记(十四)Hadoop Two HA: ActiveStandbyElector

synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) {  
    Event.EventType eventType = event.getType();
  
    if (isStaleClient(zk)) return;
  

  
    if (eventType == Event.EventType.None) {
  
      // the connection state has changed
  
      switch (event.getState()) {
  
      case SyncConnected:
  
      LOG.info("Session connected.");
  
      // if the listener was asked to move to safe state then it needs tobe undone
  
      ConnectionState prevConnectionState = zkConnectionState;
  
      zkConnectionState = ConnectionState.CONNECTED;
  
      if (prevConnectionState == ConnectionState.DISCONNECTED && wantToBeInElection) {
  
          monitorActiveStatus();
  
      }
  
      break;
  
      case Disconnected:
  
      LOG.info("Session disconnected. Entering neutral mode...");
  

  
      // ask the app to move to safe state because zookeeper connection
  
      // is not active and we dont know our state
  
      zkConnectionState = ConnectionState.DISCONNECTED;
  
      enterNeutralMode();
  
      break;
  
      case Expired:
  
      // the connection got terminated because of session timeout
  
      // call listener to reconnect
  
      LOG.info("Session expired. Entering neutral mode and rejoining...");
  
      enterNeutralMode();
  
      reJoinElection(0);
  
      break;
  
      case SaslAuthenticated:
  
      LOG.info("Successfully authenticated to ZooKeeper using SASL.");
  
      break;
  
      default:
  
      fatalError("Unexpected Zookeeper watch event state: " + event.getState());
  
      break;
  
      }
  

  
      return;
  
    }
  

  
    // 另一方面,对于node的变更事件,ActiveStandbyElector也会执行相应的动作。
  
    String path = event.getPath();
  
    if (path != null) {
  
      switch (eventType) {
  
      case NodeDeleted:
  
      if (state == State.ACTIVE) {
  
          enterNeutralMode();
  
      }
  
      joinElectionInternal();
  
      break;
  
      case NodeDataChanged:
  
      monitorActiveStatus();
  
      break;
  
      default:
  
      LOG.debug("Unexpected node event: " + eventType + " for path: " + path);
  
      monitorActiveStatus();
  
      }
  

  
      return;
  
    }
  

  
    // some unexpected error has occurred
  
    fatalError("Unexpected watch error from Zookeeper");
  
}


页: [1]
查看完整版本: Hadoop2.6.0学习笔记(十四)Hadoop Two HA: ActiveStandbyElector