Example usage for org.apache.zookeeper WatchedEvent getPath

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

Introduction

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

Prototype

public String getPath() 

Source Link

Usage

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  .jav  a2s  .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   w w w  .j a v a  2 s .c o m*/
        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;//from  w  w w. j  av a 2 s.co  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}.
 *//*  w ww. jav  a2s .  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 with node event
 *///w  ww  .j a  v a2  s. c  om
@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
 *///w w w .  ja  v  a  2s. c  o  m
@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.hbase.master.ZKMasterAddressWatcher.java

License:Apache License

/**
 * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
 */// w  w  w . j a v a2 s. com
@Override
public synchronized void process(WatchedEvent event) {
    EventType type = event.getType();
    if (type.equals(EventType.NodeDeleted)) {
        if (event.getPath().equals(this.zookeeper.clusterStateZNode)) {
            LOG.info("Cluster shutdown while waiting, shutting down" + " this master.");
            this.stoppable.requestClusterShutdown();
        } else if (event.getPath().equals(this.zookeeper.masterElectionZNode)) {
            LOG.info("Master address ZNode deleted, notifying waiting masters");
            notifyAll();
        }
    } else if (type.equals(EventType.NodeCreated)) {
        if (event.getPath().equals(this.zookeeper.clusterStateZNode)) {
            LOG.info("Resetting watch on cluster state node.");
            this.zookeeper.setClusterStateWatch();
        } else if (event.getPath().equals(this.zookeeper.masterElectionZNode)) {
            LOG.info("Master address ZNode created, check exists and reset watch");
            if (!zookeeper.exists(zookeeper.masterElectionZNode, true)) {
                LOG.debug("Got NodeCreated for master node but it does not exist now" + ", notifying");
                notifyAll();
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.master.ZKUnassignedWatcher.java

License:Apache License

/**
 * This is the processing loop that gets triggered from the ZooKeeperWrapper.
 * This zookeeper events process function dies the following:
 *   - WATCHES the following events: NodeCreated, NodeDataChanged, NodeChildrenChanged
 *   - IGNORES the following events: None, NodeDeleted
 *///from w  ww .j a  va2  s  .  c o  m
@Override
public synchronized void process(WatchedEvent event) {
    EventType eventType = event.getType();
    // Handle the ignored events
    if (eventType.equals(EventType.None) || eventType.equals(EventType.NodeDeleted)) {
        return;
    }

    // check if the path is for the UNASSIGNED directory we care about
    if (event.getPath() == null || !event.getPath().startsWith(unassignedZNode)) {
        return;
    }

    LOG.debug("ZK-EVENT-PROCESS: Got zkEvent " + eventType + " path:" + event.getPath());

    try {
        /*
         * If a node is created in the UNASSIGNED directory in zookeeper, then:
         *   1. watch its updates (this is an unassigned region).
         *   2. read to see what its state is and handle as needed (state may have
         *      changed before we started watching it)
         */
        if (eventType.equals(EventType.NodeCreated)) {
            zkWrapper.watchZNode(event.getPath());
            handleRegionStateInZK(eventType, event.getPath());
        }
        /*
         * Data on some node has changed. Read to see what the state is and handle
         * as needed.
         */
        else if (eventType.equals(EventType.NodeDataChanged)) {
            handleRegionStateInZK(eventType, event.getPath());
        }
        /*
         * If there were some nodes created then watch those nodes
         */
        else if (eventType.equals(EventType.NodeChildrenChanged)) {
            List<ZNodeEventData> newZNodes = zkWrapper.watchAndGetNewChildren(event.getPath());
            for (ZNodeEventData zNodePathAndData : newZNodes) {
                LOG.debug("Handling updates for znode: " + zNodePathAndData.getzNodePath());
                handleRegionStateInZK(eventType, zNodePathAndData.getzNodePath(), zNodePathAndData.getData(),
                        true);
            }
        }
    } catch (IOException e) {
        LOG.error("Could not process event from ZooKeeper", e);
    }
}

From source file:org.apache.hadoop.hbase.stargate.RESTServlet.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    LOG.debug(("ZooKeeper.Watcher event " + event.getType() + " with path " + event.getPath()));
    // handle disconnection (or manual delete to test disconnection scenario)
    if (event.getState() == KeeperState.Expired
            || (event.getType().equals(EventType.NodeDeleted) && event.getPath().equals(znode))) {
        wrapper.close();//from w  w  w.j a v a2  s .com
        wrapper = null;
        while (!stopping.get())
            try {
                wrapper = initZooKeeperWrapper();
                break;
            } catch (IOException e) {
                LOG.error(StringUtils.stringifyException(e));
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException ex) {
                }
            }
    }
}

From source file:org.apache.hadoop.hbase.zookeeper.lock.DeletionWatcher.java

License:Apache License

@Override
public void process(WatchedEvent watchedEvent) {
    if (watchedEvent.getPath().equals(pathToWatch)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processing event " + watchedEvent + " on " + pathToWatch);
        }//from www. j a va 2  s.  c  o  m
        try {
            if (watchedEvent.getType() == EventType.NodeDeleted
                    || !(zooKeeperWrapper.setWatchIfNodeExists(pathToWatch))) {
                deletedLatch.countDown();
            }
        } catch (Throwable t) {
            exception = t;
            deletedLatch.countDown();
            LOG.error("Error when re-setting the watch on " + pathToWatch, t);
        }
    }
}