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);
}
}
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);
}
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);
}
}
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);
}
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;
}
Aggregations