public List<String> listConfigs() throws IOException { try { return zkClient.getChildren(ZkConfigManager.CONFIGS_ZKNODE, null, true); } catch (KeeperException.NoNodeException e) { return Collections.emptyList(); } catch (KeeperException | InterruptedException e) { throw new IOException("Error listing configs", SolrZkClient.checkInterrupted(e)); } }
public List<String> listConfigs() throws IOException { try { return zkClient.getChildren(ZkConfigManager.CONFIGS_ZKNODE, null, true); } catch (KeeperException.NoNodeException e) { return Collections.emptyList(); } catch (KeeperException | InterruptedException e) { throw new IOException("Error listing configs", SolrZkClient.checkInterrupted(e)); } } }
/** * Delete a config in ZooKeeper * * @param configName the config to delete * @throws IOException if an I/O error occurs */ public void deleteConfigDir(String configName) throws IOException { try { zkClient.clean(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName); } catch (KeeperException | InterruptedException e) { throw new IOException("Error checking whether config exists", SolrZkClient.checkInterrupted(e)); } }
/** * 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 (filename.startsWith(".")) return FileVisitResult.CONTINUE; String zkNode = createZkNodeName(zkPath, rootPath, file); try { 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; }
@Override public Map<String, Object> readIndexerProperties() { Properties props = new Properties(); try { byte[] data = zkClient.getData(path, null, null, true); if (data != null) { props.load(new StringReader(new String(data, StandardCharsets.UTF_8))); } } catch (Exception e) { SolrZkClient.checkInterrupted(e); log.warn("Could not read DIH properties from " + path + " :" + e.getClass(), e); } return propertiesToMap(props); } }
/** * Get collection properties for a given collection. If the collection is watched, simply return it from the cache, * otherwise fetch it directly from zookeeper. */ public Map<String, String> getCollectionProperties(final String collection) { Map<String, String> properties = watchedCollectionProps.get(collection); if (properties == null) { try { properties = fetchCollectionProperties(collection, null); // Not storing the value in watchedCollectionProps, because it can gat stale, since we have no watcher set. } catch (Exception e) { throw new SolrException(ErrorCode.SERVER_ERROR, "Error reading collection properties", SolrZkClient.checkInterrupted(e)); } } return properties; }
/** * 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)); } }
@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; }
/** * 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)); } }
private void downloadFromZK(String zkPath, Path dir) throws IOException { try { List<String> files = zkClient.getChildren(zkPath, null, true); Files.createDirectories(dir); for (String file : files) { List<String> children = zkClient.getChildren(zkPath + "/" + file, null, true); if (children.size() == 0) { byte[] data = zkClient.getData(zkPath + "/" + file, null, null, true); Path filename = dir.resolve(file); logger.info("Writing file {}", filename); Files.write(filename, data); } else { downloadFromZK(zkPath + "/" + file, dir.resolve(file)); } } } catch (KeeperException | InterruptedException e) { throw new IOException("Error downloading files from zookeeper path " + zkPath + " to " + dir.toString(), SolrZkClient.checkInterrupted(e)); } }
/** * Check that a path exists in this server * @param path the path to check * @throws IOException if an IO exception occurs */ public void ensurePathExists(String path) throws IOException { try (SolrZkClient client = new SolrZkClient(getZkHost(), 10000)) { client.makePath(path, false); } catch (InterruptedException | KeeperException e) { throw new IOException("Error checking path " + path, SolrZkClient.checkInterrupted(e)); } }
@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)); } }
SolrZkClient.checkInterrupted(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 void copyConfigDirFromZk(String fromZkPath, String toZkPath, Set<String> copiedToZkPaths) throws IOException { try { List<String> files = zkClient.getChildren(fromZkPath, null, true); for (String file : files) { List<String> children = zkClient.getChildren(fromZkPath + "/" + file, null, true); if (children.size() == 0) { final String toZkFilePath = toZkPath + "/" + file; log.info("Copying zk node {} to {}", fromZkPath + "/" + file, toZkFilePath); byte[] data = zkClient.getData(fromZkPath + "/" + file, null, null, true); zkClient.makePath(toZkFilePath, data, true); if (copiedToZkPaths != null) copiedToZkPaths.add(toZkFilePath); } else { copyConfigDirFromZk(fromZkPath + "/" + file, toZkPath + "/" + file, copiedToZkPaths); } } } catch (KeeperException | InterruptedException e) { throw new IOException("Error copying nodes from zookeeper path " + fromZkPath + " to " + toZkPath, SolrZkClient.checkInterrupted(e)); } }
throw new IOException("Error setting cluster property", SolrZkClient.checkInterrupted(e));
SolrZkClient.checkInterrupted(e); throw new SolrException(ErrorCode.SERVER_ERROR, "Error loading config name for collection " + collection, e);
throw new IOException("Error setting property for collection " + collection, SolrZkClient.checkInterrupted(e));