Search in sources :

Example 36 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkStateReader method createClusterStateWatchersAndUpdate.

public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
    // We need to fetch the current cluster state and the set of live nodes
    LOG.debug("Updating cluster state from ZooKeeper... ");
    // Sanity check ZK structure.
    if (!zkClient.exists(CLUSTER_STATE, true)) {
        throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Cannot connect to cluster at " + zkClient.getZkServerAddress() + ": cluster not found/not ready");
    }
    // on reconnect of SolrZkClient force refresh and re-add watches.
    loadClusterProperties();
    refreshLiveNodes(new LiveNodeWatcher());
    refreshLegacyClusterState(new LegacyClusterStateWatcher());
    refreshStateFormat2Collections();
    refreshCollectionList(new CollectionsChildWatcher());
    synchronized (ZkStateReader.this.getUpdateLock()) {
        constructState(Collections.emptySet());
        zkClient.exists(ALIASES, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                // session events are not change events, and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        LOG.debug("Updating aliases... ");
                        // remake watch
                        final Watcher thisWatch = this;
                        final Stat stat = new Stat();
                        final byte[] data = zkClient.getData(ALIASES, thisWatch, stat, true);
                        ZkStateReader.this.aliases = ClusterState.load(data);
                        LOG.debug("New alias definition is: " + ZkStateReader.this.aliases.toString());
                    }
                } catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
                    LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
                } catch (KeeperException e) {
                    LOG.error("A ZK error has occurred", e);
                    throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "A ZK error has occurred", e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    LOG.warn("Interrupted", e);
                }
            }
        }, true);
    }
    updateAliases();
    if (securityNodeListener != null) {
        addSecuritynodeWatcher(pair -> {
            ConfigData cd = new ConfigData();
            cd.data = pair.first() == null || pair.first().length == 0 ? EMPTY_MAP : Utils.getDeepCopy((Map) fromJSON(pair.first()), 4, false);
            cd.version = pair.second() == null ? -1 : pair.second().getVersion();
            securityData = cd;
            securityNodeListener.run();
        });
        securityData = getSecurityProps(true);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException)

Example 37 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkStateReader method addSecuritynodeWatcher.

private void addSecuritynodeWatcher(final Callable<Pair<byte[], Stat>> callback) throws KeeperException, InterruptedException {
    zkClient.exists(SOLR_SECURITY_CONF_PATH, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            // session events are not change events, and do not remove the watcher
            if (EventType.None.equals(event.getType())) {
                return;
            }
            try {
                synchronized (ZkStateReader.this.getUpdateLock()) {
                    LOG.debug("Updating [{}] ... ", SOLR_SECURITY_CONF_PATH);
                    // remake watch
                    final Watcher thisWatch = this;
                    final Stat stat = new Stat();
                    final byte[] data = getZkClient().getData(SOLR_SECURITY_CONF_PATH, thisWatch, stat, true);
                    try {
                        callback.call(new Pair<>(data, stat));
                    } catch (Exception e) {
                        LOG.error("Error running collections node listener", e);
                    }
                }
            } catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
                LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
            } catch (KeeperException e) {
                LOG.error("A ZK error has occurred", e);
                throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
            } catch (InterruptedException e) {
                // Restore the interrupted status
                Thread.currentThread().interrupt();
                LOG.warn("Interrupted", e);
            }
        }
    }, true);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) TimeoutException(java.util.concurrent.TimeoutException) SolrException(org.apache.solr.common.SolrException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) KeeperException(org.apache.zookeeper.KeeperException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeeperException(org.apache.zookeeper.KeeperException) Pair(org.apache.solr.common.util.Pair)

Example 38 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkStateReader method waitForState.

/**
   * Block until a CollectionStatePredicate returns true, or the wait times out
   *
   * Note that the predicate may be called again even after it has returned true, so
   * implementors should avoid changing state within the predicate call itself.
   *
   * @param collection the collection to watch
   * @param wait       how long to wait
   * @param unit       the units of the wait parameter
   * @param predicate  the predicate to call on state changes
   * @throws InterruptedException on interrupt
   * @throws TimeoutException on timeout
   */
public void waitForState(final String collection, long wait, TimeUnit unit, CollectionStatePredicate predicate) throws InterruptedException, TimeoutException {
    final CountDownLatch latch = new CountDownLatch(1);
    CollectionStateWatcher watcher = (n, c) -> {
        boolean matches = predicate.matches(n, c);
        if (matches)
            latch.countDown();
        return matches;
    };
    registerCollectionStateWatcher(collection, watcher);
    try {
        // wait for the watcher predicate to return true, or time out
        if (!latch.await(wait, unit))
            throw new TimeoutException();
    } finally {
        removeCollectionStateWatcher(collection, watcher);
    }
}
Also used : URLDecoder(java.net.URLDecoder) Utils(org.apache.solr.common.util.Utils) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Stat(org.apache.zookeeper.data.Stat) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SolrException(org.apache.solr.common.SolrException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) EMPTY_MAP(java.util.Collections.EMPTY_MAP) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Pair(org.apache.solr.common.util.Pair) ErrorCode(org.apache.solr.common.SolrException.ErrorCode) ExecutorUtil(org.apache.solr.common.util.ExecutorUtil) EnumSet(java.util.EnumSet) ExecutorService(java.util.concurrent.ExecutorService) Callable(org.apache.solr.common.Callable) CoreAdminParams(org.apache.solr.common.params.CoreAdminParams) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Collections.emptySet(java.util.Collections.emptySet) Watcher(org.apache.zookeeper.Watcher) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) WatchedEvent(org.apache.zookeeper.WatchedEvent) Collectors(java.util.stream.Collectors) Utils.fromJSON(org.apache.solr.common.util.Utils.fromJSON) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Closeable(java.io.Closeable) Entry(java.util.Map.Entry) EventType(org.apache.zookeeper.Watcher.Event.EventType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException)

Example 39 with Watcher

use of org.apache.zookeeper.Watcher in project cdap by caskdata.

the class ResourceCoordinatorClient method watchAssignment.

/**
   * Starts watching ZK for ResourceAssignment changes for the given service.
   */
private void watchAssignment(final String serviceName) {
    final String zkPath = CoordinationConstants.ASSIGNMENTS_PATH + "/" + serviceName;
    // Watch for both getData() and exists() call
    Watcher watcher = wrapWatcher(new AssignmentWatcher(serviceName, EnumSet.of(Watcher.Event.EventType.NodeDataChanged, Watcher.Event.EventType.NodeDeleted)));
    Futures.addCallback(zkClient.getData(zkPath, watcher), wrapCallback(new FutureCallback<NodeData>() {

        @Override
        public void onSuccess(NodeData result) {
            try {
                ResourceAssignment assignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(result.getData());
                LOG.debug("Received resource assignment for {}. {}", serviceName, assignment.getAssignments());
                handleAssignmentChange(serviceName, assignment);
            } catch (Exception e) {
                LOG.error("Failed to decode ResourceAssignment {}", Bytes.toStringBinary(result.getData()), e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof KeeperException.NoNodeException) {
                // Treat it as assignment has been removed. If the node doesn't exists for the first time fetch data,
                // there will be no oldAssignment, hence the following call would be a no-op.
                handleAssignmentChange(serviceName, new ResourceAssignment(serviceName));
                // Watch for exists if it still interested
                synchronized (ResourceCoordinatorClient.this) {
                    if (changeListeners.containsKey(serviceName)) {
                        watchAssignmentOnExists(serviceName);
                    }
                }
            } else {
                LOG.error("Failed to getData on ZK {}{}", zkClient.getConnectString(), zkPath, t);
                doNotifyFailed(t);
            }
        }
    }), Threads.SAME_THREAD_EXECUTOR);
}
Also used : Watcher(org.apache.zookeeper.Watcher) FutureCallback(com.google.common.util.concurrent.FutureCallback) KeeperException(org.apache.zookeeper.KeeperException) NodeData(org.apache.twill.zookeeper.NodeData)

Example 40 with Watcher

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

the class InstanceContainer method processResult.

@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
    if (rc != KeeperException.Code.OK.intValue()) {
        // try it again
        zk.getChildren(assignmentsNode, true, this, null);
        return;
    }
    Map<String, Instance> newList = new HashMap<String, Instance>();
    // check for differences
    Stat stat = new Stat();
    for (String child : children) {
        Instance i = instances.remove(child);
        if (i == null) {
            // Start up a new instance
            byte[] data = null;
            String myNode = assignmentsNode + '/' + child;
            while (true) {
                try {
                    data = zk.getData(myNode, true, stat);
                    break;
                } catch (NoNodeException e) {
                    // The node doesn't exist anymore, so skip it
                    break;
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    return;
                }
            }
            if (data != null) {
                String instanceSpec = new String(data);
                int spaceIndex = instanceSpec.indexOf(' ');
                String clazz;
                String conf;
                if (spaceIndex == -1) {
                    clazz = instanceSpec;
                    conf = null;
                } else {
                    clazz = instanceSpec.substring(0, spaceIndex);
                    conf = instanceSpec.substring(spaceIndex + 1);
                }
                try {
                    Class<?> c = Class.forName(clazz);
                    i = (Instance) c.newInstance();
                    Reporter reporter = new MyReporter(child);
                    i.setReporter(reporter);
                    i.configure(conf);
                    i.start();
                    newList.put(child, i);
                    int ver = stat.getVersion();
                    Instance myInstance = i;
                    DataCallback dc = new MyDataCallback(myNode, myInstance, ver);
                    Watcher watcher = new MyWatcher(myNode, dc);
                    zk.getData(myNode, watcher, dc, watcher);
                } catch (Exception e) {
                    LOG.warn("Skipping " + child, e);
                    if (e.getCause() != null) {
                        LOG.warn("Caused by", e.getCause());
                    }
                }
            }
        } else {
            // just move it to the new list
            newList.put(child, i);
        }
    }
    // kill anything that was removed for the children
    for (Map.Entry<String, Instance> i : instances.entrySet()) {
        i.getValue().stop();
        try {
            rmnod(reportsNode + '/' + i.getKey());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
    instances = newList;
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) HashMap(java.util.HashMap) Reporter(org.apache.zookeeper.test.system.Instance.Reporter) Watcher(org.apache.zookeeper.Watcher) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Stat(org.apache.zookeeper.data.Stat) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

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