java类org.apache.zookeeper.Watcher.Event的实例源码

ClientCnxn.java 文件源码 项目:SecureKeeper 阅读 22 收藏 0 点赞 0 评论 0
void queueEvent(String clientPath, int err,
        Set<Watcher> materializedWatchers, EventType eventType) {
    KeeperState sessionState = KeeperState.SyncConnected;
    if (KeeperException.Code.SESSIONEXPIRED.intValue() == err
            || KeeperException.Code.CONNECTIONLOSS.intValue() == err) {
        sessionState = Event.KeeperState.Disconnected;
    }
    WatchedEvent event = new WatchedEvent(eventType, sessionState,
            clientPath);
    eventThread.queueEvent(event, materializedWatchers);
}
ClientCnxn.java 文件源码 项目:SecureKeeper 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Callback invoked by the ClientCnxnSocket once a connection has been
 * established.
 * 
 * @param _negotiatedSessionTimeout
 * @param _sessionId
 * @param _sessionPasswd
 * @param isRO
 * @throws IOException
 */
void onConnected(int _negotiatedSessionTimeout, long _sessionId,
        byte[] _sessionPasswd, boolean isRO) throws IOException {
    negotiatedSessionTimeout = _negotiatedSessionTimeout;
    if (negotiatedSessionTimeout <= 0) {
        state = States.CLOSED;

        eventThread.queueEvent(new WatchedEvent(
                Watcher.Event.EventType.None,
                Watcher.Event.KeeperState.Expired, null));
        eventThread.queueEventOfDeath();
        throw new SessionExpiredException(
                "Unable to reconnect to ZooKeeper service, session 0x"
                        + Long.toHexString(sessionId) + " has expired");
    }
    if (!readOnly && isRO) {
        LOG.error("Read/write client got connected to read-only server");
    }
    readTimeout = negotiatedSessionTimeout * 2 / 3;
    connectTimeout = negotiatedSessionTimeout / hostProvider.size();
    hostProvider.onConnected();
    sessionId = _sessionId;
    sessionPasswd = _sessionPasswd;
    state = (isRO) ?
            States.CONNECTEDREADONLY : States.CONNECTED;
    seenRwServerBefore |= !isRO;
    LOG.info("Session establishment complete on server "
            + clientCnxnSocket.getRemoteSocketAddress()
            + ", sessionid = 0x" + Long.toHexString(sessionId)
            + ", negotiated timeout = " + negotiatedSessionTimeout
            + (isRO ? " (READ-ONLY mode)" : ""));
    KeeperState eventState = (isRO) ?
            KeeperState.ConnectedReadOnly : KeeperState.SyncConnected;
    eventThread.queueEvent(new WatchedEvent(
            Watcher.Event.EventType.None,
            eventState, null));
}
WatcherTest.java 文件源码 项目:SecureKeeper 阅读 31 收藏 0 点赞 0 评论 0
public void process(WatchedEvent event) {
    super.process(event);
    if (event.getType() != Event.EventType.None) {
        try {
            events.put(event);
        } catch (InterruptedException e) {
            LOG.warn("ignoring interrupt during event.put");
        }
    }
}
ClientCnxn.java 文件源码 项目:SecureKeeper 阅读 24 收藏 0 点赞 0 评论 0
void queueEvent(String clientPath, int err,
        Set<Watcher> materializedWatchers, EventType eventType) {
    KeeperState sessionState = KeeperState.SyncConnected;
    if (KeeperException.Code.SESSIONEXPIRED.intValue() == err
            || KeeperException.Code.CONNECTIONLOSS.intValue() == err) {
        sessionState = Event.KeeperState.Disconnected;
    }
    WatchedEvent event = new WatchedEvent(eventType, sessionState,
            clientPath);
    eventThread.queueEvent(event, materializedWatchers);
}
ClientCnxn.java 文件源码 项目:SecureKeeper 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Callback invoked by the ClientCnxnSocket once a connection has been
 * established.
 * 
 * @param _negotiatedSessionTimeout
 * @param _sessionId
 * @param _sessionPasswd
 * @param isRO
 * @throws IOException
 */
void onConnected(int _negotiatedSessionTimeout, long _sessionId,
        byte[] _sessionPasswd, boolean isRO) throws IOException {
    negotiatedSessionTimeout = _negotiatedSessionTimeout;
    if (negotiatedSessionTimeout <= 0) {
        state = States.CLOSED;

        eventThread.queueEvent(new WatchedEvent(
                Watcher.Event.EventType.None,
                Watcher.Event.KeeperState.Expired, null));
        eventThread.queueEventOfDeath();
        throw new SessionExpiredException(
                "Unable to reconnect to ZooKeeper service, session 0x"
                        + Long.toHexString(sessionId) + " has expired");
    }
    if (!readOnly && isRO) {
        LOG.error("Read/write client got connected to read-only server");
    }
    readTimeout = negotiatedSessionTimeout * 2 / 3;
    connectTimeout = negotiatedSessionTimeout / hostProvider.size();
    hostProvider.onConnected();
    sessionId = _sessionId;
    sessionPasswd = _sessionPasswd;
    state = (isRO) ?
            States.CONNECTEDREADONLY : States.CONNECTED;
    seenRwServerBefore |= !isRO;
    LOG.info("Session establishment complete on server "
            + clientCnxnSocket.getRemoteSocketAddress()
            + ", sessionid = 0x" + Long.toHexString(sessionId)
            + ", negotiated timeout = " + negotiatedSessionTimeout
            + (isRO ? " (READ-ONLY mode)" : ""));
    KeeperState eventState = (isRO) ?
            KeeperState.ConnectedReadOnly : KeeperState.SyncConnected;
    eventThread.queueEvent(new WatchedEvent(
            Watcher.Event.EventType.None,
            eventState, null));
}
WatcherTest.java 文件源码 项目:SecureKeeper 阅读 27 收藏 0 点赞 0 评论 0
public void process(WatchedEvent event) {
    super.process(event);
    if (event.getType() != Event.EventType.None) {
        try {
            events.put(event);
        } catch (InterruptedException e) {
            LOG.warn("ignoring interrupt during event.put");
        }
    }
}
TestActiveStandbyElector.java 文件源码 项目:hadoop-2.6.0-cdh5.4.3 阅读 28 收藏 0 点赞 0 评论 0
/**
 * verify becomeStandby is not called if already in standby
 */
@Test
public void testSuccessiveStandbyCalls() {
  elector.joinElection(data);

  // make the object go into the monitoring standby state
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();
  verifyExistCall(1);

  WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);
  Mockito.when(mockEvent.getPath()).thenReturn(ZK_LOCK_NAME);

  // notify node deletion
  // monitoring should be setup again after event is received
  Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);
  elector.processWatchEvent(mockZK, mockEvent);
  // is standby. no need to notify anything now
  Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();
  // another joinElection called.
  Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
  // lost election
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  // still standby. so no need to notify again
  Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();
  // monitor is set again
  verifyExistCall(2);
}
TestActiveStandbyElector.java 文件源码 项目:hadoop-2.6.0-cdh5.4.3 阅读 30 收藏 0 点赞 0 评论 0
/**
 * joinElection(..) should happen only after SERVICE_HEALTHY.
 */
@Test
public void testBecomeActiveBeforeServiceHealthy() throws Exception {
  mockNoPriorActive();
  WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);
  Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.None);
  // session expired should enter safe mode
  // But for first time, before the SERVICE_HEALTY i.e. appData is set,
  // should not enter the election.
  Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Expired);
  elector.processWatchEvent(mockZK, mockEvent);
  // joinElection should not be called.
  Mockito.verify(mockZK, Mockito.times(0)).create(ZK_LOCK_NAME, null,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
}
TestActiveStandbyElector.java 文件源码 项目:hadoop-plus 阅读 32 收藏 0 点赞 0 评论 0
/**
 * verify becomeStandby is not called if already in standby
 */
@Test
public void testSuccessiveStandbyCalls() {
  elector.joinElection(data);

  // make the object go into the monitoring standby state
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();
  verifyExistCall(1);

  WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);
  Mockito.when(mockEvent.getPath()).thenReturn(ZK_LOCK_NAME);

  // notify node deletion
  // monitoring should be setup again after event is received
  Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);
  elector.processWatchEvent(mockZK, mockEvent);
  // is standby. no need to notify anything now
  Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();
  // another joinElection called.
  Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
  // lost election
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  // still standby. so no need to notify again
  Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();
  // monitor is set again
  verifyExistCall(2);
}
TestActiveStandbyElector.java 文件源码 项目:hadoop-plus 阅读 32 收藏 0 点赞 0 评论 0
/**
 * joinElection(..) should happen only after SERVICE_HEALTHY.
 */
@Test
public void testBecomeActiveBeforeServiceHealthy() throws Exception {
  mockNoPriorActive();
  WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);
  Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.None);
  // session expired should enter safe mode
  // But for first time, before the SERVICE_HEALTY i.e. appData is set,
  // should not enter the election.
  Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Expired);
  elector.processWatchEvent(mockZK, mockEvent);
  // joinElection should not be called.
  Mockito.verify(mockZK, Mockito.times(0)).create(ZK_LOCK_NAME, null,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
}


问题


面经


文章

微信
公众号

扫码关注公众号