Skip to content

Apache Lucene open-source search software

SOLR-11157: remove-policy must fail if a policy to be deleted is used by a collection

Noble Paulb89015a2d45b
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index cae77547510..cc759a9d881 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -133,6 +133,8 @@ Other Changes
* SOLR-10397: Port 'autoAddReplicas' feature to the autoscaling framework and make it work with non-shared filesystems
(Cao Manh Dat, shalin)
+* SOLR-11157: remove-policy must fail if a policy to be deleted is used by a collection (noble)
+
================== 7.0.0 ==================
Versions of Major Components
diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java
index 837f7fd3180..0e075e827a8 100644
--- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java
+++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java
@@ -273,6 +273,11 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
if (policies == null || !policies.containsKey(policyName)) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No policy exists with name: " + policyName);
}
+ container.getZkController().getZkStateReader().getClusterState().forEachCollection(coll -> {
+ if (policyName.equals(coll.getPolicyName())) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
+ StrUtils.formatString("policy : {0} is being used by collection {1}", policyName, coll.getName()));
+ });
+
policies = new HashMap<>(policies);
policies.remove(policyName);
Policy p = currentConfig.getPolicy().withPolicies(policies);
diff --git a/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoScalingHandlerTest.java b/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoScalingHandlerTest.java
index 0d97909ce31..7ce59a3e6f5 100644
--- a/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoScalingHandlerTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoScalingHandlerTest.java
@@ -737,6 +737,28 @@ public class AutoScalingHandlerTest extends SolrCloudTestCase {
return count;
}
+ @Test
+ public void testDeleteUsedPolicy() throws Exception {
+ CloudSolrClient solrClient = cluster.getSolrClient();
+ // add multiple policies
+ String setPolicyCommand = "{'set-policy': {" +
+ " 'nodelete':[" +
+ " {'nodeRole':'overseer', 'replica':0}]}}";
+ solrClient.request(createAutoScalingRequest(SolrRequest.METHOD.POST, setPolicyCommand));
+ CollectionAdminRequest.createCollection("COLL1", "conf", 1, 1)
+ .setPolicy("nodelete")
+ .process(cluster.getSolrClient());
+ String removePolicyCommand = "{remove-policy : nodelete}";
+ createAutoScalingRequest(SolrRequest.METHOD.POST, removePolicyCommand);
+ try {
+ solrClient.request(createAutoScalingRequest(SolrRequest.METHOD.POST, removePolicyCommand));
+ fail("should have failed");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().contains("is being used by collection"));
+ }
+ solrClient.request(CollectionAdminRequest.deleteCollection("COLL1"));
+ }
+
public static SolrRequest createAutoScalingRequest(SolrRequest.METHOD m, String message) {
return createAutoScalingRequest(m, null, message);
}