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; }); }
/** * 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)); } }
@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)); } }