Search in sources :

Example 56 with Watcher

use of org.apache.zookeeper.Watcher in project commons by twitter.

the class ZooKeeperClient method get.

/**
   * Returns the current active ZK connection or establishes a new one if none has yet been
   * established or a previous connection was disconnected or had its session time out.  This
   * method will attempt to re-use sessions when possible.
   *
   * @param connectionTimeout the maximum amount of time to wait for the connection to the ZK
   *     cluster to be established; 0 to wait forever
   * @return a connected ZooKeeper client
   * @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
   * @throws InterruptedException if interrupted while waiting for a connection to be established
   * @throws TimeoutException if a connection could not be established within the configured
   *     session timeout
   */
public synchronized ZooKeeper get(Amount<Long, Time> connectionTimeout) throws ZooKeeperConnectionException, InterruptedException, TimeoutException {
    if (zooKeeper == null) {
        final CountDownLatch connected = new CountDownLatch(1);
        Watcher watcher = new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                switch(event.getType()) {
                    // the client outside our control.
                    case None:
                        switch(event.getState()) {
                            case Expired:
                                LOG.info("Zookeeper session expired. Event: " + event);
                                close();
                                break;
                            case SyncConnected:
                                connected.countDown();
                                break;
                        }
                }
                eventQueue.offer(event);
            }
        };
        try {
            zooKeeper = (sessionState != null) ? new ZooKeeper(zooKeeperServers, sessionTimeoutMs, watcher, sessionState.sessionId, sessionState.sessionPasswd) : new ZooKeeper(zooKeeperServers, sessionTimeoutMs, watcher);
        } catch (IOException e) {
            throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
        }
        if (connectionTimeout.getValue() > 0) {
            if (!connected.await(connectionTimeout.as(Time.MILLISECONDS), TimeUnit.MILLISECONDS)) {
                close();
                throw new TimeoutException("Timed out waiting for a ZK connection after " + connectionTimeout);
            }
        } else {
            try {
                connected.await();
            } catch (InterruptedException ex) {
                LOG.info("Interrupted while waiting to connect to zooKeeper");
                close();
                throw ex;
            }
        }
        credentials.authenticate(zooKeeper);
        sessionState = new SessionState(zooKeeper.getSessionId(), zooKeeper.getSessionPasswd());
    }
    return zooKeeper;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException)

Example 57 with Watcher

use of org.apache.zookeeper.Watcher in project commons by twitter.

the class ZooKeeperMap method addChild.

// TODO(Adam Samet) - Make this use the ZooKeeperNode class.
private void addChild(final String child) throws InterruptedException, KeeperException, ZooKeeperConnectionException {
    final Watcher nodeWatcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
                try {
                    tryAddChild(child);
                } catch (InterruptedException e) {
                    LOG.log(Level.WARNING, "Interrupted while trying to add a child.", e);
                    Thread.currentThread().interrupt();
                }
            } else if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
                removeEntry(child);
            }
        }
    };
    try {
        V value = deserializer.apply(zkClient.get().getData(makePath(child), nodeWatcher, null));
        putEntry(child, value);
    } catch (KeeperException.NoNodeException e) {
        // This node doesn't exist anymore, remove it from the map and we're done.
        removeEntry(child);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) KeeperException(org.apache.zookeeper.KeeperException)

Example 58 with Watcher

use of org.apache.zookeeper.Watcher in project commons by twitter.

the class ZooKeeperMap method watchChildren.

private synchronized void watchChildren() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
    /*
     * Add a watch on the parent node itself, and attempt to rewatch if it
     * gets deleted
     */
    zkClient.get().exists(nodePath, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
                // If the parent node no longer exists
                localMap.clear();
                try {
                    tryWatchChildren();
                } catch (InterruptedException e) {
                    LOG.log(Level.WARNING, "Interrupted while trying to watch children.", e);
                    Thread.currentThread().interrupt();
                }
            }
        }
    });
    final Watcher childWatcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) {
                try {
                    tryWatchChildren();
                } catch (InterruptedException e) {
                    LOG.log(Level.WARNING, "Interrupted while trying to watch children.", e);
                    Thread.currentThread().interrupt();
                }
            }
        }
    };
    List<String> children = zkClient.get().getChildren(nodePath, childWatcher);
    updateChildren(Sets.newHashSet(children));
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher)

Example 59 with Watcher

use of org.apache.zookeeper.Watcher in project commons by twitter.

the class ZooKeeperMap method init.

/**
   * Initialize zookeeper tracking for this {@link Map}.  Once this call returns, this object
   * will be tracking data in zookeeper.
   *
   * @throws InterruptedException if the underlying zookeeper server transaction is interrupted
   * @throws KeeperException if the server signals an error
   * @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
   *     cluster
   */
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
    Watcher watcher = zkClient.registerExpirationHandler(new Command() {

        @Override
        public void execute() {
            /*
         * First rewatch all of our locally cached children.  Some of them may not exist anymore,
         * which will lead to caught KeeperException.NoNode whereafter we'll remove that child
         * from the cached map.
         *
         * Next, we'll establish our top level child watch and add any new nodes that might exist.
         */
            try {
                synchronized (safeToRewatchLock) {
                    if (safeToRewatch) {
                        rewatchDataNodes();
                        tryWatchChildren();
                    }
                }
            } catch (InterruptedException e) {
                LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
                Thread.currentThread().interrupt();
            }
        }
    });
    try {
        // before we update safeToRewatch.
        synchronized (safeToRewatchLock) {
            watchChildren();
            safeToRewatch = true;
        }
    } catch (InterruptedException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (KeeperException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (ZooKeeperConnectionException e) {
        zkClient.unregister(watcher);
        throw e;
    }
}
Also used : Command(com.twitter.common.base.Command) Watcher(org.apache.zookeeper.Watcher) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 60 with Watcher

use of org.apache.zookeeper.Watcher in project distributedlog by twitter.

the class TestZooKeeperClient method expireZooKeeperSession.

private void expireZooKeeperSession(ZooKeeper zk, int timeout) throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }
    }, zk.getSessionId(), zk.getSessionPasswd());
    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
    newZk.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)34 CountDownLatch (java.util.concurrent.CountDownLatch)26 ZooKeeper (org.apache.zookeeper.ZooKeeper)24 Stat (org.apache.zookeeper.data.Stat)21 Test (org.junit.Test)18 IOException (java.io.IOException)11 AsyncCallback (org.apache.zookeeper.AsyncCallback)10 List (java.util.List)8 Test (org.testng.annotations.Test)8 None (com.linkedin.common.util.None)7 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Map (java.util.Map)5 Set (java.util.Set)5 HashMap (java.util.HashMap)4 TimeoutException (java.util.concurrent.TimeoutException)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3