@Override public boolean hasData(String path) throws IOException, KeeperException, InterruptedException { try { return zkClient.exists(path, true); } catch (InterruptedException e) { throw e; } }
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 } }
private static boolean isEphemeral(SolrZkClient zkClient, String zkPath) throws KeeperException, InterruptedException { Stat znodeStat = zkClient.exists(zkPath, null, true); return znodeStat.getEphemeralOwner() != 0; }
public void ensureExists(final String path, final byte[] data, CreateMode createMode, final SolrZkClient zkClient) throws KeeperException, InterruptedException { if (zkClient.exists(path, true)) { return; } try { zkClient.makePath(path, data, createMode, true); } catch (NodeExistsException e) { // it's okay if another beats us creating the node } }
/** * Check whether a config exists in Zookeeper * * @param configName the config to check existance on * @return whether the config exists or not * @throws IOException if an I/O error occurs */ public Boolean configExists(String configName) throws IOException { try { return zkClient.exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName, true); } catch (KeeperException | InterruptedException e) { throw new IOException("Error checking whether config exists", SolrZkClient.checkInterrupted(e)); } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String filename = file.getFileName().toString(); if (filenameExclusions != null && filenameExclusions.matcher(filename).matches()) { log.info("uploadToZK skipping '{}' due to filenameExclusions '{}'", filename, filenameExclusions); return FileVisitResult.CONTINUE; } String zkNode = createZkNodeName(zkPath, rootPath, file); try { // if the path exists (and presumably we're uploading data to it) just set its data if (file.toFile().getName().equals(ZKNODE_DATA_FILE) && zkClient.exists(zkNode, true)) { zkClient.setData(zkNode, file.toFile(), true); } else { zkClient.makePath(zkNode, file.toFile(), false, true); } } catch (KeeperException | InterruptedException e) { throw new IOException("Error uploading file " + file.toString() + " to zookeeper path " + zkNode, SolrZkClient.checkInterrupted(e)); } return FileVisitResult.CONTINUE; }
private Set<String> getStateFormat2CollectionNames() throws KeeperException, InterruptedException { List<String> children = null; try { children = zkClient.getChildren(COLLECTIONS_ZKNODE, null, true); } catch (KeeperException.NoNodeException e) { log.warn("Error fetching collection names"); return new HashSet<>(); } if (children == null || children.isEmpty()) return new HashSet<>(); HashSet<String> result = new HashSet<>(children.size(), 1.0f); for (String c : children) { try { // this exists call is necessary because we only want to return // those collections which have their own state.json. // The getCollectionPath() calls returns the complete path to the // collection's state.json if (zkClient.exists(getCollectionPath(c), true)) { result.add(c); } } catch (Exception e) { log.warn("Error reading collections nodes", e); } } return result; }
private void refreshAliases(AliasesManager watcher) throws KeeperException, InterruptedException { synchronized (getUpdateLock()) { constructState(Collections.emptySet()); zkClient.exists(ALIASES, watcher, true); } aliasesManager.update(); }
@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)); } }
private void waitForAllNodes(int numServers, int timeout) throws IOException, InterruptedException { try (SolrZkClient zkClient = new SolrZkClient(zkServer.getZkHost(), AbstractZkTestCase.TIMEOUT)) { int numliveNodes = 0; int retries = timeout; String liveNodesPath = "/solr/live_nodes"; // Wait up to {timeout} seconds for number of live_nodes to match up number of servers do { if (zkClient.exists(liveNodesPath, true)) { numliveNodes = zkClient.getChildren(liveNodesPath, null, true).size(); if (numliveNodes == numServers) { break; } } retries--; if (retries == 0) { throw new IllegalStateException("Solr servers failed to register with ZK." + " Current count: " + numliveNodes + "; Expected count: " + numServers); } Thread.sleep(1000); } while (numliveNodes != numServers); } catch (KeeperException e) { throw new IOException("Error communicating with zookeeper", e); } }
@Override public void persist(Map<String, Object> propObjs) { Properties existing = mapToProperties(readIndexerProperties()); existing.putAll(mapToProperties(propObjs)); StringWriter output = new StringWriter(); try { existing.store(output, null); byte[] bytes = output.toString().getBytes(StandardCharsets.UTF_8); if (!zkClient.exists(path, false)) { try { zkClient.makePath(path, false); } catch (NodeExistsException e) {} } zkClient.setData(path, bytes, false); } catch (Exception e) { SolrZkClient.checkInterrupted(e); log.warn("Could not persist properties to " + path + " :" + e.getClass(), e); } }
private static void checkAllZnodesThere(SolrZkClient zkClient, String src, String dst) throws KeeperException, InterruptedException, SolrServerException { for (String node : zkClient.getChildren(src, null, true)) { if (zkClient.exists(dst + "/" + node, true) == false) { throw new SolrServerException("mv command did not move node " + dst + "/" + node + " source left intact"); } checkAllZnodesThere(zkClient, src + "/" + node, dst + "/" + node); } }
@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; }
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) ; } }
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; } } }
@SuppressWarnings("unchecked") private Map<String, String> fetchCollectionProperties(String collection, Watcher watcher) throws KeeperException, InterruptedException { final String znodePath = getCollectionPropsPath(collection); while (true) { try { byte[] data = zkClient.getData(znodePath, watcher, null, true); return (Map<String, String>) Utils.fromJSON(data); } catch (ClassCastException e) { throw new SolrException(ErrorCode.SERVER_ERROR, "Unable to parse collection properties for collection " + collection, e); } catch (KeeperException.NoNodeException e) { if (watcher != null) { // Leave an exists watch in place in case a collectionprops.json is created later. Stat exists = zkClient.exists(znodePath, 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 Collections.emptyMap(); } } }
protected void assertCollectionNotExists(String collectionName, int timeoutSeconds) throws Exception { waitForCollectionToDisappear(collectionName, getCommonCloudSolrClient().getZkStateReader(), false, true, timeoutSeconds); assertFalse(cloudClient.getZkStateReader().getZkClient().exists(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collectionName, true)); }
/** * Returns the content of /security.json from ZooKeeper as a Map * If the files doesn't exist, it returns null. */ public ConfigData getSecurityProps(boolean getFresh) { if (!getFresh) { if (securityData == null) return new ConfigData(EMPTY_MAP,-1); return new ConfigData(securityData.data, securityData.version); } try { Stat stat = new Stat(); if(getZkClient().exists(SOLR_SECURITY_CONF_PATH, true)) { final byte[] data = getZkClient().getData(ZkStateReader.SOLR_SECURITY_CONF_PATH, null, stat, true); return data != null && data.length > 0 ? new ConfigData((Map<String, Object>) Utils.fromJSON(data), stat.getVersion()) : null; } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new SolrException(ErrorCode.SERVER_ERROR,"Error reading security properties", e) ; } catch (KeeperException e) { throw new SolrException(ErrorCode.SERVER_ERROR,"Error reading security properties", e) ; } return null; }
/** * Returns the content of /security.json from ZooKeeper as a Map * If the files doesn't exist, it returns null. */ public ConfigData getSecurityProps(boolean getFresh) { if (!getFresh) { if (securityData == null) return new ConfigData(EMPTY_MAP, -1); return new ConfigData(securityData.data, securityData.version); } try { Stat stat = new Stat(); if(getZkClient().exists(SOLR_SECURITY_CONF_PATH, true)) { byte[] data = getZkClient() .getData(ZkStateReader.SOLR_SECURITY_CONF_PATH, null, stat, true); return data != null && data.length > 0 ? new ConfigData((Map<String, Object>) Utils.fromJSON(data), stat.getVersion()) : null; } } catch (KeeperException | InterruptedException e) { throw new SolrException(ErrorCode.SERVER_ERROR,"Error reading security properties",e) ; } return null; } /**