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.hadoop.hbase.master.ZKMasterAddressWatcher.java

License:Apache License

/**
 * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
 *///w w  w  .j a  va2s. c  o  m
@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   ww w.j a va 2  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();//w ww .java  2 s  .  co  m
        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);
        }//w w w  .  j a v a 2 s  .  co 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);
        }
    }
}

From source file:org.apache.hadoop.hbase.zookeeper.ZKWatcher.java

License:Apache License

/**
 * Method called from ZooKeeper for events and connection status.
 * <p>/*from  w w  w.  j a v a  2s .  c o m*/
 * Valid events are passed along to listeners.  Connection status changes
 * are dealt with locally.
 */
@Override
public void process(WatchedEvent event) {
    LOG.debug(prefix("Received ZooKeeper Event, " + "type=" + event.getType() + ", " + "state="
            + event.getState() + ", " + "path=" + event.getPath()));

    switch (event.getType()) {

    // If event type is NONE, this is a connection status change
    case None: {
        connectionEvent(event);
        break;
    }

    // Otherwise pass along to the listeners

    case NodeCreated: {
        for (ZKListener listener : listeners) {
            listener.nodeCreated(event.getPath());
        }
        break;
    }

    case NodeDeleted: {
        for (ZKListener listener : listeners) {
            listener.nodeDeleted(event.getPath());
        }
        break;
    }

    case NodeDataChanged: {
        for (ZKListener listener : listeners) {
            listener.nodeDataChanged(event.getPath());
        }
        break;
    }

    case NodeChildrenChanged: {
        for (ZKListener listener : listeners) {
            listener.nodeChildrenChanged(event.getPath());
        }
        break;
    }
    default:
        throw new IllegalStateException("Received event is not valid: " + event.getState());
    }
}

From source file:org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper.java

License:Apache License

/**
 * This is the primary ZK watcher/*w w w  .ja va 2 s. c om*/
 * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
 */
@Override
public synchronized void process(WatchedEvent event) {
    LOG.debug("<" + instanceName + "> Received ZK WatchedEvent: " + "[path=" + event.getPath() + "] "
            + "[state=" + event.getState().toString() + "] " + "[type=" + event.getType().toString() + "]");
    if (event.getType() == EventType.None) {
        if (event.getState() == KeeperState.Expired) {
            this.abort("ZooKeeper Session Expiration, aborting server",
                    new KeeperException.SessionExpiredException());
        } else if (event.getState() == KeeperState.Disconnected) {
            LOG.warn("Disconnected from ZooKeeper");
        } else if (event.getState() == KeeperState.SyncConnected) {
            LOG.info("Reconnected to ZooKeeper");
        }
        return;
    }
    for (Watcher w : listeners) {
        try {
            w.process(event);
        } catch (Throwable t) {
            LOG.error("<" + instanceName + ">" + " Sub-ZK Watcher threw an exception " + "in process()", t);
        }
    }
}

From source file:org.apache.hadoop.hive.common.ZKDeRegisterWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType().equals(Watcher.Event.EventType.NodeDeleted)) {
        zooKeeperHiveHelper.deregisterZnode();
    }//from  w  w  w. j a v a  2  s .  c  o m
}

From source file:org.apache.hadoop.hive.llap.registry.impl.SlotZnode.java

License:Apache License

private void processWatchedEvent(WatchedEvent event) {
    if (event.getType() != EventType.NodeDeleted)
        return;/*ww w . j av  a 2  s.c om*/
    String localPath = nodePath.get();
    if (localPath == null)
        return;
    if (!localPath.equals(event.getPath())) {
        LOG.info("Ignoring the NodeDeleted event for " + event.getPath());
        return;
    }
    LOG.info("Trying to reacquire because of the NodeDeleted event");
    startCreateCurrentNode();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore.java

License:Apache License

@VisibleForTesting
@Private/*from   w  ww .  ja v a 2 s . c  om*/
@Unstable
public synchronized void processWatchEvent(WatchedEvent event) throws Exception {
    Event.EventType eventType = event.getType();
    LOG.info("Watcher event type: " + eventType + " with state:" + event.getState() + " for path:"
            + event.getPath() + " for " + this);

    if (eventType == Event.EventType.None) {

        // the connection state has changed
        switch (event.getState()) {
        case SyncConnected:
            LOG.info("ZKRMStateStore Session connected");
            if (oldZkClient != null) {
                // the SyncConnected must be from the client that sent Disconnected
                zkClient = oldZkClient;
                oldZkClient = null;
                ZKRMStateStore.this.notifyAll();
                LOG.info("ZKRMStateStore Session restored");
            }
            break;
        case Disconnected:
            LOG.info("ZKRMStateStore Session disconnected");
            oldZkClient = zkClient;
            zkClient = null;
            break;
        case Expired:
            // the connection got terminated because of session timeout
            // call listener to reconnect
            LOG.info("ZKRMStateStore Session expired");
            createConnection();
            break;
        default:
            LOG.error("Unexpected Zookeeper" + " watch event state: " + event.getState());
            break;
        }
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore.java

License:Apache License

@VisibleForTesting
@Private/*from  www  .  j a v a2s.com*/
@Unstable
public synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) throws Exception {
    // only process watcher event from current ZooKeeper Client session.
    if (zk != activeZkClient) {
        LOG.info("Ignore watcher event type: " + event.getType() + " with state:" + event.getState()
                + " for path:" + event.getPath() + " from old session");
        return;
    }

    Event.EventType eventType = event.getType();
    LOG.info("Watcher event type: " + eventType + " with state:" + event.getState() + " for path:"
            + event.getPath() + " for " + this);

    if (eventType == Event.EventType.None) {

        // the connection state has changed
        switch (event.getState()) {
        case SyncConnected:
            LOG.info("ZKRMStateStore Session connected");
            if (zkClient == null) {
                // the SyncConnected must be from the client that sent Disconnected
                zkClient = activeZkClient;
                ZKRMStateStore.this.notifyAll();
                LOG.info("ZKRMStateStore Session restored");
            }
            break;
        case Disconnected:
            LOG.info("ZKRMStateStore Session disconnected");
            zkClient = null;
            break;
        case Expired:
            // the connection got terminated because of session timeout
            // call listener to reconnect
            LOG.info("ZKRMStateStore Session expired");
            createConnection();
            break;
        default:
            LOG.error("Unexpected Zookeeper" + " watch event state: " + event.getState());
            break;
        }
    }
}