Skip to content

Apache Lucene open-source search software

SOLR-12612: Accept custom keys in cluster properties

Tomas Fernandez Lobbe0af269fb4975
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a36be8765bf..a975f06fdd2 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -213,6 +213,9 @@ New Features
* SOLR-11943: Add machine learning functions for location data (Joel Bernstein)
+* SOLR-12612: Cluster properties restriction of known keys only is relaxed, and now unknown properties starting with "ext."
+ will be allowed. This allows custom to plugins set their own cluster properties. (Jeffery Yuan via Tomás Fernández Löbbe)
+
Bug Fixes
----------------------
diff --git a/solr/core/src/test/org/apache/solr/cloud/TestClusterProperties.java b/solr/core/src/test/org/apache/solr/cloud/TestClusterProperties.java
index c5575af2979..c082e371ff4 100644
--- a/solr/core/src/test/org/apache/solr/cloud/TestClusterProperties.java
+++ b/solr/core/src/test/org/apache/solr/cloud/TestClusterProperties.java
@@ -18,6 +18,7 @@
package org.apache.solr.cloud;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterProperties;
import org.apache.solr.common.cloud.ZkStateReader;
import org.junit.BeforeClass;
@@ -25,14 +26,21 @@ import org.junit.Test;
public class TestClusterProperties extends SolrCloudTestCase {
+ private ClusterProperties props;
+
@BeforeClass
public static void setupCluster() throws Exception {
configureCluster(1).configure();
}
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ props = new ClusterProperties(zkClient());
+ }
+
@Test
public void testClusterProperties() throws Exception {
- ClusterProperties props = new ClusterProperties(zkClient());
assertEquals("false", props.getClusterProperty(ZkStateReader.LEGACY_CLOUD, "false"));
CollectionAdminRequest.setClusterProperty(ZkStateReader.LEGACY_CLOUD, "true").process(cluster.getSolrClient());
@@ -41,5 +49,20 @@ public class TestClusterProperties extends SolrCloudTestCase {
CollectionAdminRequest.setClusterProperty(ZkStateReader.LEGACY_CLOUD, "false").process(cluster.getSolrClient());
assertEquals("false", props.getClusterProperty(ZkStateReader.LEGACY_CLOUD, "true"));
}
+
+ @Test
+ public void testSetPluginClusterProperty() throws Exception {
+ String propertyName = ClusterProperties.EXT_PROPRTTY_PREFIX + "pluginA.propertyA";
+ CollectionAdminRequest.setClusterProperty(propertyName, "valueA")
+ .process(cluster.getSolrClient());
+ assertEquals("valueA", props.getClusterProperty(propertyName, null));
+ }
+
+ @Test(expected = SolrException.class)
+ public void testSetInvalidPluginClusterProperty() throws Exception {
+ String propertyName = "pluginA.propertyA";
+ CollectionAdminRequest.setClusterProperty(propertyName, "valueA")
+ .process(cluster.getSolrClient());
+ }
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterProperties.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterProperties.java
index 446923b81de..245254032f9 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterProperties.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterProperties.java
@@ -42,7 +42,8 @@ import org.slf4j.LoggerFactory;
public class ClusterProperties {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
+ public static final String EXT_PROPRTTY_PREFIX = "ext.";
+
private final SolrZkClient client;
/**
@@ -119,9 +120,7 @@ public class ClusterProperties {
@SuppressWarnings("unchecked")
public void setClusterProperty(String propertyName, String propertyValue) throws IOException {
- if (!ZkStateReader.KNOWN_CLUSTER_PROPS.contains(propertyName)) {
- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Not a known cluster property " + propertyName);
- }
+ validatePropertyName(propertyName);
for (; ; ) {
Stat s = new Stat();
@@ -155,4 +154,20 @@ public class ClusterProperties {
break;
}
}
+
+ /**
+ * The propertyName should be either: <br/>
+ * 1. <code>ZkStateReader.KNOWN_CLUSTER_PROPS</code> that is used by solr itself.<br/>
+ * 2. Custom property: it can be created by third-party extensions and should start with prefix <b>"ext."</b> and it's
+ * recommended to also add prefix of plugin name or company name or package name to avoid conflict.
+ *
+ * @param propertyName The property name to validate
+ */
+ private void validatePropertyName(String propertyName) {
+ if (!ZkStateReader.KNOWN_CLUSTER_PROPS.contains(propertyName)
+ && !propertyName.startsWith(EXT_PROPRTTY_PREFIX)) {
+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Not a known cluster property or starts with prefix "
+ + EXT_PROPRTTY_PREFIX + ", propertyName: " + propertyName);
+ }
+ }
}