public void setClusterProperties(Map<String, Object> properties) throws IOException, KeeperException, InterruptedException { client.atomicUpdate(ZkStateReader.CLUSTER_PROPS, zkData -> { if (zkData == null) return Utils.toJSON(convertCollectionDefaultsToNestedFormat(properties)); Map<String, Object> zkJson = (Map<String, Object>) Utils.fromJSON(zkData); zkJson = convertCollectionDefaultsToNestedFormat(zkJson); boolean modified = Utils.mergeJson(zkJson, convertCollectionDefaultsToNestedFormat(properties)); return modified ? Utils.toJSON(zkJson) : null; }); }
/** * Read the value of a cluster property, returning a default if it is not set * @param key the property name or the full path to the property. * @param defaultValue the default value * @param <T> the type of the property * @return the property value * @throws IOException if there is an error reading the value from the cluster */ @SuppressWarnings("unchecked") public <T> T getClusterProperty(String key, T defaultValue) throws IOException { T value = (T) Utils.getObjectByPath(getClusterProperties(), false, key); if (value == null) return defaultValue; return value; }
/** * Configure, run and return the {@link MiniSolrCloudCluster} * @throws Exception if an error occurs on startup */ public MiniSolrCloudCluster build() throws Exception { MiniSolrCloudCluster cluster = new MiniSolrCloudCluster(nodeCount, baseDir, solrxml, jettyConfig, null, securityJson); CloudSolrClient client = cluster.getSolrClient(); for (Config config : configs) { ((ZkClientClusterStateProvider)client.getClusterStateProvider()).uploadConfig(config.path, config.name); } if (clusterProperties.size() > 0) { ClusterProperties props = new ClusterProperties(cluster.getSolrClient().getZkStateReader().getZkClient()); for (Map.Entry<String, String> entry : clusterProperties.entrySet()) { props.setClusterProperty(entry.getKey(), entry.getValue()); } } return cluster; }
public void setClusterProperty(String propertyName, String propertyValue) throws IOException { validatePropertyName(propertyName);
/** * 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)); } }
/** * Read the value of a cluster property, returning a default if it is not set * * @param key the property name or the full path to the property as a list of parts. * @param defaultValue the default value * @param <T> the type of the property * @return the property value * @throws IOException if there is an error reading the value from the cluster */ @SuppressWarnings("unchecked") public <T> T getClusterProperty(List<String> key, T defaultValue) throws IOException { T value = (T) Utils.getObjectByPath(getClusterProperties(), false, key); if (value == null) return defaultValue; return value; }
@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)); } }