Example usage for org.apache.zookeeper Watcher process

List of usage examples for org.apache.zookeeper Watcher process

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher process.

Prototype

void process(WatchedEvent event);

Source Link

Usage

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

License:Apache License

/**
 * Creates an unconnected client that will lazily attempt to connect on the first call to
 * {@link #get}.  All successful connections will be authenticated with the given
 * {@code credentials}.//from  w ww .  j av a  2  s . c o  m
 *
 * @param sessionTimeout the ZK session timeout
 * @param credentials the credentials to authenticate with
 * @param chrootPath an optional chroot path
 * @param zooKeeperServers the set of servers forming the ZK cluster
 */
public ZooKeeperClient(Amount<Integer, Time> sessionTimeout, Credentials credentials,
        Optional<String> chrootPath, Iterable<InetSocketAddress> zooKeeperServers) {
    this.sessionTimeoutMs = Preconditions.checkNotNull(sessionTimeout).as(Time.MILLISECONDS);
    this.credentials = Preconditions.checkNotNull(credentials);

    if (chrootPath.isPresent()) {
        PathUtils.validatePath(chrootPath.get());
    }

    Preconditions.checkNotNull(zooKeeperServers);
    Preconditions.checkArgument(!Iterables.isEmpty(zooKeeperServers), "Must present at least 1 ZK server");

    Thread watcherProcessor = new Thread("ZookeeperClient-watcherProcessor") {
        @Override
        public void run() {
            while (true) {
                try {
                    WatchedEvent event = eventQueue.take();
                    for (Watcher watcher : watchers) {
                        watcher.process(event);
                    }
                } catch (InterruptedException e) {
                    /* ignore */ }
            }
        }
    };
    watcherProcessor.setDaemon(true);
    watcherProcessor.start();

    Iterable<String> servers = Iterables.transform(ImmutableSet.copyOf(zooKeeperServers),
            InetSocketAddressHelper.INET_TO_STR);
    this.zooKeeperServers = Joiner.on(',').join(servers).concat(chrootPath.or(""));
}

From source file:com.twitter.distributedlog.zk.TestZKWatcherManager.java

License:Apache License

@Test(timeout = 60000)
public void testRegisterUnregisterWatcher() throws Exception {
    ZKWatcherManager watcherManager = ZKWatcherManager.newBuilder().name("test-register-unregister-watcher")
            .statsLogger(NullStatsLogger.INSTANCE).build();
    String path = "/test-register-unregister-watcher";
    final List<WatchedEvent> events = new LinkedList<WatchedEvent>();
    final CountDownLatch latch = new CountDownLatch(2);
    Watcher watcher = new Watcher() {
        @Override//from w  w  w .  ja  v a  2s  . c o m
        public void process(WatchedEvent event) {
            events.add(event);
            latch.countDown();
        }
    };
    watcherManager.registerChildWatcher(path, watcher);

    // fire the event
    WatchedEvent event0 = new WatchedEvent(Watcher.Event.EventType.NodeCreated,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event1 = new WatchedEvent(Watcher.Event.EventType.None,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event2 = new WatchedEvent(Watcher.Event.EventType.NodeChildrenChanged,
            Watcher.Event.KeeperState.SyncConnected, path);
    watcher.process(event1);
    watcher.process(event2);

    latch.await();

    assertEquals(2, events.size());
    assertEquals(event1, events.get(0));
    assertEquals(event2, events.get(1));

    // unregister watcher
    watcherManager.unregisterChildWatcher(path, watcher);

    assertEquals(0, watcherManager.childWatches.size());
}

From source file:com.twitter.distributedlog.zk.ZKWatcherManager.java

License:Apache License

private void handleKeeperStateEvent(WatchedEvent event) {
    Set<Watcher> savedAllWatches = new HashSet<Watcher>(allWatchesGauge.get());
    for (Set<Watcher> watcherSet : childWatches.values()) {
        synchronized (watcherSet) {
            savedAllWatches.addAll(watcherSet);
        }//www .j av a2  s.c  o  m
    }
    for (Watcher watcher : savedAllWatches) {
        watcher.process(event);
    }
}

From source file:com.twitter.distributedlog.zk.ZKWatcherManager.java

License:Apache License

private void handleChildWatchEvent(WatchedEvent event) {
    String path = event.getPath();
    if (null == path) {
        logger.warn("Received zookeeper watch event with null path : {}", event);
        return;//ww w .  j a v  a2 s  . com
    }
    Set<Watcher> watchers = childWatches.get(path);
    if (null == watchers) {
        return;
    }
    Set<Watcher> watchersToFire;
    synchronized (watchers) {
        watchersToFire = new HashSet<Watcher>(watchers.size());
        watchersToFire.addAll(watchers);
    }
    for (Watcher watcher : watchersToFire) {
        watcher.process(event);
    }
}

From source file:com.twitter.distributedlog.ZooKeeperClient.java

License:Apache License

private ZooKeeper buildZooKeeper() throws ZooKeeperConnectionException, InterruptedException {
    Watcher watcher = new Watcher() {
        @Override//  ww w .  j a  v a2 s  .  co m
        public void process(WatchedEvent event) {
            switch (event.getType()) {
            case None:
                switch (event.getState()) {
                case Expired:
                    if (null == retryPolicy) {
                        LOG.info("ZooKeeper {}' session expired. Event: {}", name, event);
                        closeInternal();
                    }
                    authenticated = false;
                    break;
                case Disconnected:
                    if (null == retryPolicy) {
                        LOG.info("ZooKeeper {} is disconnected from zookeeper now,"
                                + " but it is OK unless we received EXPIRED event.", name);
                    }
                    // Mark as not authenticated if expired or disconnected. In both cases
                    // we lose any attached auth info. Relying on Expired/Disconnected is
                    // sufficient since all Expired/Disconnected events are processed before
                    // all SyncConnected events, and the underlying member is not updated until
                    // SyncConnected is received.
                    authenticated = false;
                    break;
                default:
                    break;
                }
            }

            try {
                for (Watcher watcher : watchers) {
                    try {
                        watcher.process(event);
                    } catch (Throwable t) {
                        LOG.warn("Encountered unexpected exception from watcher {} : ", watcher, t);
                    }
                }
            } catch (Throwable t) {
                LOG.warn("Encountered unexpected exception when firing watched event {} : ", event, t);
            }
        }
    };

    Set<Watcher> watchers = new HashSet<Watcher>();
    watchers.add(watcher);

    ZooKeeper zk;
    try {
        RetryPolicy opRetryPolicy = null == retryPolicy
                ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0)
                : retryPolicy;
        RetryPolicy connectRetryPolicy = null == retryPolicy
                ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0)
                : new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, Integer.MAX_VALUE);
        zk = org.apache.bookkeeper.zookeeper.ZooKeeperClient.newBuilder().connectString(zooKeeperServers)
                .sessionTimeoutMs(sessionTimeoutMs).watchers(watchers).operationRetryPolicy(opRetryPolicy)
                .connectRetryPolicy(connectRetryPolicy).statsLogger(statsLogger)
                .retryThreadCount(retryThreadCount).requestRateLimit(requestRateLimit).build();
    } catch (KeeperException e) {
        throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
    } catch (IOException e) {
        throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
    }
    return zk;
}

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

License:Apache License

/**
 * Creates an unconnected client that will lazily attempt to connect on the first call to
 * {@link #get}.  All successful connections will be authenticated with the given
 * {@code credentials}./*  w w w  . j  av  a  2  s  . com*/
 *
 * @param sessionTimeout the ZK session timeout
 * @param credentials the credentials to authenticate with
 * @param chrootPath an optional chroot path
 * @param zooKeeperServers the set of servers forming the ZK cluster
 */
public ZooKeeperClient(Duration sessionTimeout, Credentials credentials, Optional<String> chrootPath,
        Iterable<InetSocketAddress> zooKeeperServers) {
    this.sessionTimeoutMs = (int) Preconditions.checkNotNull(sessionTimeout).inMillis();
    this.credentials = Preconditions.checkNotNull(credentials);

    if (chrootPath.isPresent()) {
        PathUtils.validatePath(chrootPath.get());
    }

    Preconditions.checkNotNull(zooKeeperServers);
    Preconditions.checkArgument(!Iterables.isEmpty(zooKeeperServers), "Must present at least 1 ZK server");

    Thread watcherProcessor = new Thread("ZookeeperClient-watcherProcessor") {
        @Override
        public void run() {
            while (true) {
                try {
                    WatchedEvent event = eventQueue.take();
                    for (Watcher watcher : watchers) {
                        watcher.process(event);
                    }
                } catch (InterruptedException e) {
                    /* ignore */ }
            }
        }
    };
    watcherProcessor.setDaemon(true);
    watcherProcessor.start();

    Iterable<String> servers = Iterables.transform(ImmutableSet.copyOf(zooKeeperServers),
            new Function<InetSocketAddress, String>() {
                @Override
                public String apply(InetSocketAddress addr) {
                    return addr.getHostName() + ":" + addr.getPort();
                }
            });
    this.zooKeeperServers = Joiner.on(',').join(servers);
    this.connectString = this.zooKeeperServers.concat(chrootPath.or(""));
}

From source file:io.reign.Reign.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    // log if TRACE
    if (logger.isTraceEnabled()) {
        logger.trace("***** Received ZooKeeper Event:  {}",
                ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE));

    }//from w w  w.ja v a2s. c o  m

    if (shutdown) {
        logger.warn("Already shutdown:  ignoring event:  type={}; path={}", event.getType(), event.getPath());
        return;
    }

    for (Watcher watcher : watcherList) {
        watcher.process(event);
    }
}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

@Override
public void process(final WatchedEvent event) {
    /***** log event *****/
    // log if TRACE
    if (logger.isTraceEnabled()) {
        logger.trace("***** Received ZooKeeper Event:  {}",
                ReflectionToStringBuilder.toString(event, ToStringStyle.DEFAULT_STYLE));

    }/*from w w  w .j  av  a  2s  .  c  o  m*/

    /***** pass notifications on to registered Watchers *****/
    // if (event.getType() != EventType.None) {
    if (shutdown) {
        logger.warn("Already shutdown:  not passing event to registered watchers:  type={}; path={}",
                event.getType(), event.getPath());
        return;
    } else {
        for (Watcher watcher : watcherSet) {
            watcher.process(event);
        }
    }
    // }

    /***** process events *****/
    switch (event.getType()) {
    case NodeChildrenChanged:
        this.childWatchesMap.remove(event.getPath());
        break;
    case NodeCreated:
    case NodeDataChanged:
    case NodeDeleted:
        this.dataWatchesMap.remove(event.getPath());
        break;
    case None:
        Event.KeeperState eventState = event.getState();
        if (eventState == Event.KeeperState.SyncConnected) {

            // this.connected = true;

            if (currentSessionId == null) {
                if (logger.isInfoEnabled()) {
                    logger.info(
                            "Restoring watches as necessary:  sessionId={}; connectString={}; sessionTimeout={}",
                            new Object[] { this.zooKeeper.getSessionId(), getConnectString(),
                                    getSessionTimeout() });
                }
                restoreWatches();
            }

            this.currentSessionId = this.zooKeeper.getSessionId();
            this.sessionPassword = this.zooKeeper.getSessionPasswd();

            logger.info("SyncConnected:  notifying all waiters:  currentSessionId={}; connectString={}",
                    currentSessionId, getConnectString());
            synchronized (this) {
                // notify waiting threads that connection has been
                // established
                this.notifyAll();
            }
            logger.info("SyncConnected:  notified all waiters:  currentSessionId={}; connectString={}",
                    currentSessionId, getConnectString());

        } else if (eventState == Event.KeeperState.Disconnected) {
            // this.connected = false;

        } else if (eventState == Event.KeeperState.Expired) {
            // expired session; close ZK connection and reconnect
            if (!this.shutdown) {
                // if session has been expired, clear out the existing ID
                logger.info(
                        "Session has been expired by ZooKeeper cluster:  reconnecting to establish new session:  oldSessionId={}; connectString={}",
                        currentSessionId, getConnectString());

                // null out current session ID
                this.currentSessionId = null;

                // do connection in another thread so as to not block the ZK event thread
                spawnReconnectThread();
            }

        } else {
            logger.warn("Unhandled state:  eventType=" + event.getType() + "; eventState=" + eventState);
        }
        break;
    default:
        logger.warn("Unhandled event type:  eventType=" + event.getType() + "; eventState=" + event.getState());
    }

}

From source file:org.apache.accumulo.fate.zookeeper.ZooCacheTest.java

License:Apache License

private void testWatchDataNode(byte[] initialData, Watcher.Event.EventType eventType, boolean stillCached)
        throws Exception {
    WatchedEvent event = new WatchedEvent(eventType, Watcher.Event.KeeperState.SyncConnected, ZPATH);
    TestWatcher exw = new TestWatcher(event);
    zc = new ZooCache(zr, exw);

    Watcher w = watchData(initialData);
    w.process(event);
    assertTrue(exw.wasCalled());//from w w w  .ja  v  a  2  s  . co  m
    assertEquals(stillCached, zc.dataCached(ZPATH));
}

From source file:org.apache.accumulo.fate.zookeeper.ZooCacheTest.java

License:Apache License

private void testWatchDataNode_Clear(Watcher.Event.KeeperState state) throws Exception {
    WatchedEvent event = new WatchedEvent(Watcher.Event.EventType.None, state, null);
    TestWatcher exw = new TestWatcher(event);
    zc = new ZooCache(zr, exw);

    Watcher w = watchData(DATA);
    assertTrue(zc.dataCached(ZPATH));/*  ww  w.  j a  v a 2 s  .co m*/
    w.process(event);
    assertTrue(exw.wasCalled());
    assertFalse(zc.dataCached(ZPATH));
}