Example usage for org.apache.zookeeper WatchedEvent getType

List of usage examples for org.apache.zookeeper WatchedEvent getType

Introduction

In this page you can find the example usage for org.apache.zookeeper WatchedEvent getType.

Prototype

public EventType getType() 

Source Link

Usage

From source file:org.apache.giraph.graph.BspServiceWorker.java

License:Apache License

@Override
protected boolean processEvent(WatchedEvent event) {
    boolean foundEvent = false;
    if (event.getPath().startsWith(masterJobStatePath) && (event.getType() == EventType.NodeChildrenChanged)) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent: Job state changed, checking " + "to see if it needs to restart");
        }//from   w w  w.  j a  v a  2s.  co  m
        JSONObject jsonObj = getJobState();
        try {
            if ((ApplicationState
                    .valueOf(jsonObj.getString(JSONOBJ_STATE_KEY)) == ApplicationState.START_SUPERSTEP)
                    && jsonObj.getLong(JSONOBJ_APPLICATION_ATTEMPT_KEY) != getApplicationAttempt()) {
                LOG.fatal("processEvent: Worker will restart " + "from command - " + jsonObj.toString());
                System.exit(-1);
            }
        } catch (JSONException e) {
            throw new RuntimeException(
                    "processEvent: Couldn't properly get job state from " + jsonObj.toString());
        }
        foundEvent = true;
    } else if (event.getPath().contains(PARTITION_EXCHANGE_DIR)
            && event.getType() == EventType.NodeChildrenChanged) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent : partitionExchangeChildrenChanged "
                    + "(at least one worker is done sending partitions)");
        }
        partitionExchangeChildrenChanged.signal();
        foundEvent = true;
    }

    return foundEvent;
}

From source file:org.apache.giraph.master.BspServiceMaster.java

License:Apache License

@Override
public boolean processEvent(WatchedEvent event) {
    boolean foundEvent = false;
    if (event.getPath().contains(WORKER_HEALTHY_DIR) && (event.getType() == EventType.NodeDeleted)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("processEvent: Healthy worker died (node deleted) " + "in " + event.getPath());
        }//from www. j  a va2s.co  m
        checkHealthyWorkerFailure(event.getPath());
        superstepStateChanged.signal();
        foundEvent = true;
    } else if (event.getPath().contains(WORKER_FINISHED_DIR)
            && event.getType() == EventType.NodeChildrenChanged) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(
                    "processEvent: Worker finished (node change) " + "event - superstepStateChanged signaled");
        }
        superstepStateChanged.signal();
        foundEvent = true;
    } else if (event.getPath().contains(WORKER_WROTE_CHECKPOINT_DIR)
            && event.getType() == EventType.NodeChildrenChanged) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("processEvent: Worker wrote checkpoint (node change) "
                    + "event - workerWroteCheckpoint signaled");
        }
        workerWroteCheckpoint.signal();
        foundEvent = true;
    }

    return foundEvent;
}

From source file:org.apache.giraph.worker.BspServiceSource.java

License:Apache License

/**
 * Get event when the state of a partition exchange has changed.
 *
 * @return Event to check.//from w w  w . j  a v a 2 s . com
 */
//    public final BspEvent getPartitionExchangeChildrenChangedEvent() {
//        return partitionExchangeChildrenChanged;
//    }

@Override
protected boolean processEvent(WatchedEvent event) {
    boolean foundEvent = false;
    if (event.getPath().startsWith(masterJobStatePath) && (event.getType() == EventType.NodeChildrenChanged)) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent: Job state changed, checking " + "to see if it needs to restart");
        }
        JSONObject jsonObj = getJobState();
        // in YARN, we have to manually commit our own output in 2 stages that we
        // do not have to do in Hadoop-based Giraph. So jsonObj can be null.
        if (getConfiguration().isPureYarnJob() && null == jsonObj) {
            LOG.error("BspServiceWorker#getJobState() came back NULL.");
            return false; // the event has been processed.
        }
        try {
            if ((ApplicationState
                    .valueOf(jsonObj.getString(JSONOBJ_STATE_KEY)) == ApplicationState.START_SUPERSTEP)
                    && jsonObj.getLong(JSONOBJ_APPLICATION_ATTEMPT_KEY) != getApplicationAttempt()) {
                LOG.fatal("processEvent: Worker will restart " + "from command - " + jsonObj.toString());
                System.exit(-1);
            }
        } catch (JSONException e) {
            throw new RuntimeException(
                    "processEvent: Couldn't properly get job state from " + jsonObj.toString());
        }
        foundEvent = true;
    } else if (event.getPath().contains(PARTITION_EXCHANGE_DIR)
            && event.getType() == EventType.NodeChildrenChanged) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent : partitionExchangeChildrenChanged "
                    + "(at least one worker is done sending partitions)");
        }
        partitionExchangeChildrenChanged.signal();
        foundEvent = true;
    }

    return foundEvent;
}

From source file:org.apache.giraph.worker.BspServiceWorker.java

License:Apache License

@Override
protected boolean processEvent(WatchedEvent event) {
    boolean foundEvent = false;
    if (event.getPath().startsWith(masterJobStatePath) && (event.getType() == EventType.NodeChildrenChanged)) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent: Job state changed, checking " + "to see if it needs to restart");
        }/*from   www .j a v a 2  s  .  c  om*/
        JSONObject jsonObj = getJobState();
        // in YARN, we have to manually commit our own output in 2 stages that we
        // do not have to do in Hadoop-based Giraph. So jsonObj can be null.
        if (getConfiguration().isPureYarnJob() && null == jsonObj) {
            LOG.error("BspServiceWorker#getJobState() came back NULL.");
            return false; // the event has been processed.
        }
        try {
            if ((ApplicationState
                    .valueOf(jsonObj.getString(JSONOBJ_STATE_KEY)) == ApplicationState.START_SUPERSTEP)
                    && jsonObj.getLong(JSONOBJ_APPLICATION_ATTEMPT_KEY) != getApplicationAttempt()) {
                LOG.fatal("processEvent: Worker will restart " + "from command - " + jsonObj.toString());
                System.exit(-1);
            }
        } catch (JSONException e) {
            throw new RuntimeException(
                    "processEvent: Couldn't properly get job state from " + jsonObj.toString());
        }
        foundEvent = true;
    } else if (event.getPath().contains(PARTITION_EXCHANGE_DIR)
            && event.getType() == EventType.NodeChildrenChanged) {
        if (LOG.isInfoEnabled()) {
            LOG.info("processEvent : partitionExchangeChildrenChanged "
                    + "(at least one worker is done sending partitions)");
        }
        partitionExchangeChildrenChanged.signal();
        foundEvent = true;
    }

    return foundEvent;
}

From source file:org.apache.giraph.worker.InputSplitsHandler.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getPath() == null) {
        LOG.warn("process: Problem with zookeeper, got event with path null, " + "state " + event.getState()
                + ", event type " + event.getType());
        return;/*w w  w . j  av a 2 s  .  c  o m*/
    }
    // Check if the reservation for the input split was lost
    // (some worker died)
    if (event.getPath().endsWith(inputSplitReservedNode)
            && event.getType() == Watcher.Event.EventType.NodeDeleted) {
        synchronized (pathList) {
            String split = event.getPath();
            split = split.substring(0, split.indexOf(inputSplitReservedNode));
            pathList.add(split);
            if (LOG.isInfoEnabled()) {
                LOG.info("process: Input split " + split + " lost reservation");
            }
        }
    }
}

From source file:org.apache.hadoop.ha.ActiveStandbyElector.java

License:Apache License

/**
 * interface implementation of Zookeeper watch events (connection and node),
 * proxied by {@link WatcherWithClientRef}.
 *//*from   w w w.  ja  v a2  s.c  o m*/
synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) {
    Event.EventType eventType = event.getType();
    if (isStaleClient(zk))
        return;
    LOG.debug("Watcher event type: " + eventType + " with state:" + event.getState() + " for path:"
            + event.getPath() + " connectionState: " + zkConnectionState + " for " + this);

    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 to
            // be 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;
    }

    // a watch on lock path in zookeeper has fired. so something has changed on
    // the lock. ideally we should check that the path is the same as the lock
    // path but trusting zookeeper for now
    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");
}

From source file:org.apache.hadoop.ha.TestActiveStandbyElector.java

License:Apache License

/**
 * verify behavior of watcher.process callback with non-node event
 *///w w  w . j a v a2s.c om
@Test
public void testProcessCallbackEventNone() throws Exception {
    mockNoPriorActive();
    elector.joinElection(data);

    WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);
    Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.None);

    // first SyncConnected should not do anything
    Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.SyncConnected);
    elector.processWatchEvent(mockZK, mockEvent);
    Mockito.verify(mockZK, Mockito.times(0)).exists(Mockito.anyString(), Mockito.anyBoolean(),
            Mockito.<AsyncCallback.StatCallback>anyObject(), Mockito.<Object>anyObject());

    // disconnection should enter safe mode
    Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Disconnected);
    elector.processWatchEvent(mockZK, mockEvent);
    Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();

    // re-connection should monitor master status
    Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.SyncConnected);
    elector.processWatchEvent(mockZK, mockEvent);
    verifyExistCall(1);

    // session expired should enter safe mode and initiate re-election
    // re-election checked via checking re-creation of new zookeeper and
    // call to create lock znode
    Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Expired);
    elector.processWatchEvent(mockZK, mockEvent);
    // already in safe mode above. should not enter safe mode again
    Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
    // called getNewZooKeeper to create new session. first call was in
    // constructor
    Assert.assertEquals(2, count);
    // once in initial joinElection and one now
    Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data, Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL, elector, mockZK);

    // create znode success. become master and monitor
    elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, ZK_LOCK_NAME);
    Mockito.verify(mockApp, Mockito.times(1)).becomeActive();
    verifyExistCall(2);

    // error event results in fatal error
    Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.AuthFailed);
    elector.processWatchEvent(mockZK, mockEvent);
    Mockito.verify(mockApp, Mockito.times(1))
            .notifyFatalError("Unexpected Zookeeper watch event state: AuthFailed");
    // only 1 state change callback is called at a time
    Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
}

From source file:org.apache.hadoop.ha.TestActiveStandbyElector.java

License:Apache License

/**
 * verify behavior of watcher.process with node event
 *///from   www.j  a  v  a  2  s.  c  o m
@Test
public void testProcessCallbackEventNode() throws Exception {
    mockNoPriorActive();
    elector.joinElection(data);

    // make the object go into the monitoring 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);

    // monitoring should be setup again after event is received
    Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDataChanged);
    elector.processWatchEvent(mockZK, mockEvent);
    verifyExistCall(2);

    // monitoring should be setup again after event is received
    Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeChildrenChanged);
    elector.processWatchEvent(mockZK, mockEvent);
    verifyExistCall(3);

    // lock node deletion when in standby mode should create znode again
    // successful znode creation enters active state and sets monitor
    Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);
    elector.processWatchEvent(mockZK, mockEvent);
    // enterNeutralMode not called when app is standby and leader is lost
    Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();
    // once in initial joinElection() and one now
    Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data, Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL, elector, mockZK);
    elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, ZK_LOCK_NAME);
    Mockito.verify(mockApp, Mockito.times(1)).becomeActive();
    verifyExistCall(4);

    // lock node deletion in active mode should enter neutral mode and create
    // znode again successful znode creation enters active state and sets
    // monitor
    Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);
    elector.processWatchEvent(mockZK, mockEvent);
    Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
    // another joinElection called
    Mockito.verify(mockZK, Mockito.times(3)).create(ZK_LOCK_NAME, data, Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL, elector, mockZK);
    elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, ZK_LOCK_NAME);
    Mockito.verify(mockApp, Mockito.times(2)).becomeActive();
    verifyExistCall(5);

    // bad path name results in fatal error
    Mockito.when(mockEvent.getPath()).thenReturn(null);
    elector.processWatchEvent(mockZK, mockEvent);
    Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError("Unexpected watch error from Zookeeper");
    // fatal error means no new connection other than one from constructor
    Assert.assertEquals(1, count);
    // no new watches after fatal error
    verifyExistCall(5);

}

From source file:org.apache.hadoop.ha.TestActiveStandbyElector.java

License:Apache License

/**
 * verify becomeStandby is not called if already in standby
 *///from  w  w w . j av  a  2s  . com
@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);
}

From source file:org.apache.hadoop.ha.TestActiveStandbyElector.java

License:Apache License

/**
 * joinElection(..) should happen only after SERVICE_HEALTHY.
 *///ww w.j  a  v  a2  s. c o  m
@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);
}