static void tryCleanPath(String zkHost, String path) throws Exception { SolrZkClient zkClient = new SolrZkClient(zkHost, TIMEOUT); if (zkClient.exists(path, true)) { zkClient.clean(path); } zkClient.close(); } }
public Map getClusterProps(){ Map result = null; try { if(getZkClient().exists(ZkStateReader.CLUSTER_PROPS, true)){ result = (Map) Utils.fromJSON(getZkClient().getData(ZkStateReader.CLUSTER_PROPS, null, new Stat(), true)) ; } else { result= new LinkedHashMap(); } return result; } catch (Exception e) { throw new SolrException(ErrorCode.SERVER_ERROR,"Error reading cluster properties",e) ; } }
public void atomicUpdate(String path, Function<byte[], byte[]> editor) throws KeeperException, InterruptedException { for (; ; ) { byte[] modified = null; byte[] zkData = null; Stat s = new Stat(); try { if (exists(path, true)) { zkData = getData(path, null, s, true); modified = editor.apply(zkData); if (modified == null) { //no change , no need to persist return; } setData(path, modified, s.getVersion(), true); break; } else { modified = editor.apply(null); if (modified == null) { //no change , no need to persist return; } create(path, modified, CreateMode.PERSISTENT, true); break; } } catch (KeeperException.BadVersionException | KeeperException.NodeExistsException e) { continue; } } }
public void ensureExists(final String path, final byte[] data, CreateMode createMode, final SolrZkClient zkClient, int skipPathParts) throws KeeperException, InterruptedException { if (zkClient.exists(path, true)) { return; } try { zkClient.makePath(path, data, createMode, null, true, true, skipPathParts); } catch (NodeExistsException e) { // it's okay if another beats us creating the node } }
public static void makeSolrZkNode(String zkHost) throws Exception { SolrZkClient zkClient = new SolrZkClient(zkHost, TIMEOUT); zkClient.makePath("/solr", false, true); zkClient.close(); }
@Override public AutoScalingConfig getAutoScalingConfig(Watcher watcher) throws InterruptedException, IOException { Map<String, Object> map = new HashMap<>(); Stat stat = new Stat(); try { byte[] bytes = zkClient.getData(ZkStateReader.SOLR_AUTOSCALING_CONF_PATH, watcher, stat, true); if (bytes != null && bytes.length > 0) { map = (Map<String, Object>) fromJSON(bytes); } } catch (KeeperException.NoNodeException e) { // ignore } catch (KeeperException e) { throw new IOException(e); } map.put(AutoScalingParams.ZK_VERSION, stat.getVersion()); return new AutoScalingConfig(map); }
@Override public VersionedData getData(String path, Watcher watcher) throws NoSuchElementException, IOException, KeeperException, InterruptedException { Stat stat = new Stat(); try { byte[] bytes = zkClient.getData(path, watcher, stat, true); return new VersionedData(stat.getVersion(), bytes, String.valueOf(stat.getEphemeralOwner())); } catch (KeeperException.NoNodeException e) { throw new NoSuchElementException(path); } catch (InterruptedException e) { throw e; } }
throw new SolrException(ErrorCode.BAD_REQUEST, "Not a known cluster property " + propertyName); Stat s = new Stat(); try { if (getZkClient().exists(CLUSTER_PROPS, true)) { int v = 0; Map properties = (Map) Utils.fromJSON(getZkClient().getData(CLUSTER_PROPS, null, s, true)); if (propertyValue == null) { getZkClient().setData(CLUSTER_PROPS, Utils.toJSON(properties), s.getVersion(), true); getZkClient().setData(CLUSTER_PROPS, Utils.toJSON(properties), s.getVersion(), true); Map properties = new LinkedHashMap(); properties.put(propertyName, propertyValue); getZkClient().create(CLUSTER_PROPS, Utils.toJSON(properties), CreateMode.PERSISTENT, true); } catch (Exception ex) { log.error("Error updating path " + CLUSTER_PROPS, ex); throw new SolrException(ErrorCode.SERVER_ERROR, "Error updating cluster property " + propertyName, ex);
public static DocCollection getCollectionLive(ZkStateReader zkStateReader, String coll) { String collectionPath = getCollectionPath(coll); try { Stat stat = new Stat(); byte[] data = zkStateReader.getZkClient().getData(collectionPath, null, stat, true); ClusterState state = ClusterState.load(stat.getVersion(), data, Collections.<String> emptySet(), collectionPath); ClusterState.CollectionRef collectionRef = state.getCollectionStates().get(coll); return collectionRef == null ? null : collectionRef.get(); } catch (KeeperException.NoNodeException e) { log.warn("No node available : " + collectionPath, e); return null; } catch (KeeperException e) { throw new SolrException(ErrorCode.BAD_REQUEST, "Could not load collection from ZK:" + coll, e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new SolrException(ErrorCode.BAD_REQUEST, "Could not load collection from ZK:" + coll, e); } }
private DocCollection fetchCollectionState(String coll, Watcher watcher) throws KeeperException, InterruptedException { String collectionPath = getCollectionPath(coll); while (true) { try { Stat stat = new Stat(); byte[] data = zkClient.getData(collectionPath, watcher, stat, true); ClusterState state = ClusterState.load(stat.getVersion(), data, Collections.<String>emptySet(), collectionPath); ClusterState.CollectionRef collectionRef = state.getCollectionStates().get(coll); return collectionRef == null ? null : collectionRef.get(); } catch (KeeperException.NoNodeException e) { if (watcher != null) { // Leave an exists watch in place in case a state.json is created later. Stat exists = zkClient.exists(collectionPath, watcher, true); if (exists != null) { // Rare race condition, we tried to fetch the data and couldn't find it, then we found it exists. // Loop and try again. continue; } } return null; } } }
/** * Ensures the internal aliases is up to date. If there is a change, return true. * * @return true if an update was performed */ public boolean update() throws KeeperException, InterruptedException { log.debug("Checking ZK for most up to date Aliases {}", ALIASES); // Call sync() first to ensure the subsequent read (getData) is up to date. zkClient.getSolrZooKeeper().sync(ALIASES, null, null); Stat stat = new Stat(); final byte[] data = zkClient.getData(ALIASES, null, stat, true); return setIfNewer(Aliases.fromJSON(data, stat.getVersion())); }
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()); refreshAliases(aliasesManager); 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); } collectionPropsWatches.forEach((k,v) -> { new PropsWatcher(k).refreshAndWatch(true); }); }
@SuppressWarnings("unchecked") private void loadClusterProperties() { try { while (true) { try { byte[] data = zkClient.getData(ZkStateReader.CLUSTER_PROPS, clusterPropertiesWatcher, new Stat(), true); this.clusterProperties = ClusterProperties.convertCollectionDefaultsToNestedFormat((Map<String, Object>) Utils.fromJSON(data)); log.debug("Loaded cluster properties: {}", this.clusterProperties); return; } catch (KeeperException.NoNodeException e) { this.clusterProperties = Collections.emptyMap(); log.debug("Loaded empty cluster properties"); // set an exists watch, and if the node has been created since the last call, // read the data again if (zkClient.exists(ZkStateReader.CLUSTER_PROPS, clusterPropertiesWatcher, true) == null) return; } } } catch (KeeperException | InterruptedException e) { log.error("Error reading cluster properties from zookeeper", SolrZkClient.checkInterrupted(e)); } }
/** * Return the collection properties * @throws IOException if there is an error reading properties from zookeeper */ @SuppressWarnings("unchecked") public Map<String, String> getCollectionProperties(String collection) throws IOException { try { return (Map<String, String>) Utils.fromJSON(client.getData(ZkStateReader.getCollectionPropsPath(collection), null, new Stat(), true)); } catch (KeeperException.NoNodeException e) { return Collections.emptyMap(); } catch (KeeperException | InterruptedException e) { throw new IOException("Error reading properties for collection " + collection, SolrZkClient.checkInterrupted(e)); } }
/** * Return the cluster properties * @throws IOException if there is an error reading properties from the cluster */ @SuppressWarnings("unchecked") public Map<String, Object> getClusterProperties() throws IOException { try { Map<String, Object> properties = (Map<String, Object>) Utils.fromJSON(client.getData(ZkStateReader.CLUSTER_PROPS, null, new Stat(), true)); return convertCollectionDefaultsToNestedFormat(properties); } catch (KeeperException.NoNodeException e) { return Collections.emptyMap(); } catch (KeeperException | InterruptedException e) { throw new IOException("Error reading cluster property", SolrZkClient.checkInterrupted(e)); } }
final Stat stat = getZkClient().setData(ALIASES, modAliasesJson, curAliases.getZNodeVersion(), true); setIfNewer(Aliases.fromJSON(modAliasesJson, stat.getVersion())); return; } catch (KeeperException.BadVersionException e) { throw new SolrException(ErrorCode.SERVER_ERROR, "Timed out trying to update aliases! " + "Either zookeeper or this node may be overloaded."); throw new SolrException(ErrorCode.SERVER_ERROR, "Too many successive version failures trying to update aliases");
@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); } }
@Override public synchronized DocCollection get(boolean allowCached) { gets.incrementAndGet(); if (!allowCached || lastUpdateTime < 0 || System.nanoTime() - lastUpdateTime > LAZY_CACHE_TIME) { boolean shouldFetch = true; if (cachedDocCollection != null) { Stat exists = null; try { exists = zkClient.exists(getCollectionPath(collName), null, true); } catch (Exception e) {} if (exists != null && exists.getVersion() == cachedDocCollection.getZNodeVersion()) { shouldFetch = false; } } if (shouldFetch) { cachedDocCollection = getCollectionLive(ZkStateReader.this, collName); lastUpdateTime = System.nanoTime(); } } return cachedDocCollection; }
private static boolean isEphemeral(SolrZkClient zkClient, String zkPath) throws KeeperException, InterruptedException { Stat znodeStat = zkClient.exists(zkPath, null, true); return znodeStat.getEphemeralOwner() != 0; }
synchronized (getUpdateLock()) { Stat stat = zkClient.exists(CLUSTER_STATE, new Watcher() { throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Cannot connect to cluster at " + zkClient.getZkServerAddress() + ": cluster not found/not ready"); synchronized (ZkStateReader.this.getUpdateLock()) { List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, new Watcher() { this.clusterState = constructState(liveNodeSet, null); zkClient.exists(ALIASES, new Watcher() {