Example usage for org.apache.zookeeper WatchedEvent getState

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

Introduction

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

Prototype

public KeeperState getState() 

Source Link

Usage

From source file:com.twitter.finagle.common.zookeeper.ZooKeeperClient.java

License:Apache License

/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Runnable} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *     removal.//ww w  .j  a  va 2  s .com
 */
public Watcher registerExpirationHandler(final Runnable onExpired) {
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                onExpired.run();
            }
        }
    };
    register(watcher);
    return watcher;
}

From source file:com.yahoo.pulsar.zookeeper.GlobalZooKeeperCache.java

License:Apache License

@Override
public <T> void process(WatchedEvent event, final CacheUpdater<T> updater) {
    synchronized (this) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("[{}] Got Global ZooKeeper WatchdEvent: EventType: {}, KeeperState: {}, path: {}",
                    this.hashCode(), event.getType(), event.getState(), event.getPath());
        }//  www .j a v  a2s.co  m
        if (event.getType() == Event.EventType.None) {
            switch (event.getState()) {
            case Expired:
                // in case of expired, the zkSession is no longer good for sure.
                // We need to restart the session immediately.
                // cancel any timer event since it is already bad
                ZooKeeper oldSession = this.zkSession.getAndSet(null);
                LOG.warn("Global ZK session lost. Triggering reconnection {}", oldSession);
                safeCloseZkSession(oldSession);
                asyncRestartZooKeeperSession();
                return;
            case SyncConnected:
            case ConnectedReadOnly:
                checkNotNull(zkSession.get());
                LOG.info("Global ZK session {} restored connection.", zkSession.get());

                //
                dataCache.synchronous().invalidateAll();
                childrenCache.invalidateAll();
                return;
            default:
                break;
            }
        }
    }

    // Other types of events
    super.process(event, updater);

}

From source file:com.yahoo.pulsar.zookeeper.LocalZooKeeperCache.java

License:Apache License

@Override
public <T> void process(WatchedEvent event, final CacheUpdater<T> updater) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Got Local ZooKeeper WatchedEvent: EventType: {}, KeeperState: {}, Path: {}", event.getType(),
                event.getState(), event.getPath());
    }//from www  .j a  va  2s. c  o  m
    if (event.getType() == Event.EventType.None) {
        switch (event.getState()) {
        case Expired:
            // in case of expired, the zkSession is no longer good
            LOG.warn("Lost connection from local ZK. Invalidating the whole cache.");
            dataCache.synchronous().invalidateAll();
            childrenCache.invalidateAll();
            return;
        default:
            break;
        }
    }
    super.process(event, updater);
}

From source file:com.yahoo.pulsar.zookeeper.ZooKeeperSessionWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    Watcher.Event.EventType eventType = event.getType();
    Watcher.Event.KeeperState eventState = event.getState();

    LOG.info("Received zookeeper notification, eventType={}, eventState={}", eventType, eventState);

    switch (eventType) {
    case None:/* ww  w .j  av  a  2 s. c  om*/
        if (eventState == Watcher.Event.KeeperState.Expired) {
            LOG.error("ZooKeeper session already expired, invoking shutdown");
            close();
            shuttingDown = true;
            shutdownService.shutdown(-1);
        }
        break;
    default:
        break;
    }

}

From source file:com.yihaodian.architecture.zkclient.ZkClient.java

License:Apache License

public void process(WatchedEvent event) {
    LOG.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged
            || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
            || event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();/*from w ww. j a  v  a 2 s  .co  m*/
    try {

        // We might have to install child change event listener if a new
        // node was created
        if (getShutdownTrigger()) {
            LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath()
                    + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions,
            // because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            // TODO PVo write a test for this
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might
                // have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        LOG.debug("Leaving process event");
    }
}

From source file:com.zookeeper.web.inspector.manager.ZooInspectorManagerImpl.java

License:Apache License

@Override
public boolean connect(Properties connectionProps) {
    connected = false;//  w w  w . j  a va2s. co m
    try {
        if (this.zooKeeper == null) {
            String connectString = connectionProps.getProperty(CONNECT_STRING);
            String sessionTimeout = connectionProps.getProperty(SESSION_TIMEOUT);
            String encryptionManager = connectionProps.getProperty(DATA_ENCRYPTION_MANAGER);
            if (connectString == null || sessionTimeout == null) {
                throw new IllegalArgumentException("Both connect string and session timeout are required.");
            }
            if (encryptionManager == null) {
                this.encryptionManager = new BasicDataEncryptionManager();
            } else {
                Class<?> clazz = Class.forName(encryptionManager);

                if (Arrays.asList(clazz.getInterfaces()).contains(DataEncryptionManager.class)) {
                    this.encryptionManager = (DataEncryptionManager) Class.forName(encryptionManager)
                            .newInstance();
                } else {
                    throw new IllegalArgumentException(
                            "Data encryption manager must implement DataEncryptionManager interface");
                }
            }
            this.connectString = connectString;
            this.sessionTimeout = Integer.valueOf(sessionTimeout);

            //        long start = System.currentTimeMillis();
            //        System.out.println("[START] connecting...");
            this.zooKeeper = new ZooKeeperRetry(connectString, Integer.valueOf(sessionTimeout), new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.Expired) {
                        connected = false;
                    }
                }
            });
            ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10);
            //        System.out.println("[START] connected took: " + (System.currentTimeMillis() - start));

            connected = ((ZooKeeperRetry) this.zooKeeper).testConnection();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // connected = false;

    // do initial cache refresh on all childs of "/"
    if (connected == true) {
        cache = new ZooInspectorManagerCache(this);
        try {
            cache.refresh(Arrays.asList("/"), 1);
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            disconnect();
            e.printStackTrace();
        }
    } else {
        disconnect();
    }

    return connected;
}

From source file:common.CountdownWatcher.java

License:Apache License

public synchronized void process(WatchedEvent event) {
    LOG.info("Watcher " + name + " got event " + event);

    state = event.getState();
    if (state == KeeperState.SyncConnected) {
        connected = true;//from w  ww.ja v  a2 s  . c o m
        clientConnected.countDown();
    } else {
        connected = false;
    }
    notifyAll();
}

From source file:crunch.MaxTemperature.java

License:Apache License

@Override
    public void process(WatchedEvent event) {
        if (event.getState() == KeeperState.SyncConnected) {
            connectedSignal.countDown();
        }//from  w  ww . ja  v  a2  s .  co m
    }

From source file:crunch.MaxTemperature.java

License:Apache License

@Override
    public void process(WatchedEvent event) { // Watcher interface
        if (event.getState() == KeeperState.SyncConnected) {
            connectedSignal.countDown();
        }//  ww w  . j a  va  2s . co  m
    }

From source file:dsync.synchronization.impl.zk.SessionManager.java

License:Apache License

public void process(WatchedEvent watchedEvent) {
    if (watchedEvent.getType() == Event.EventType.None) {
        switch (watchedEvent.getState()) {
        case Disconnected:
        case Expired:
            revocate();//from  ww w.j  a  va  2 s  .c  o m
        }
    }

}