Apache Lucene open-source search software
SOLR-12522: Support a runtime function `#ALL` for 'replica' in autoscaling policies
Noble Paul49b1fe2b6d42
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d8823db7cf2..425a3dbb818 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -105,13 +105,15 @@ New Features* SOLR-12530: Ability to disable configset upload via -Dconfigset.upload.enabled=false startup parameter(Ishan Chattopadhyaya)-* SOLR-12495: An #EQUALS function for replica in autoscaling policy to equally distribute replicas (noble)+* SOLR-12495: An #EQUAL function for replica in autoscaling policy to equally distribute replicas (noble)* SOLR-12441: New NestedUpdateProcessorFactory (URP) to populate special fields _nest_parent_ and _nest_path_ of nested(child) documents. It will generate a uniqueKey of nested docs if they were blank too. (Moshe Bla, David Smiley)* SOLR-11986: Allow percentage in freedisk attribute in autoscaling policy rules (noble)+* SOLR-12522: Support a runtime function `#ALL` for 'replica' in autoscaling policies (noble)+Bug Fixes----------------------diff --git a/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc b/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adocindex c7f46c9584a..539c4085e1b 100644--- a/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc+++ b/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc@@ -107,8 +107,10 @@ This is a required attribute. The number of replicas that must exist to satisfy* a positive integer . e.g : "`3`"* a number with a decimal value . e.g: "`1.66`" . This means both 1 and 2 are acceptable values but the system would prefer `2`* a number range. Such as `"3-5"` . This means `3,4,5` are acceptable values+* `#ALL` : All replicas of a given collection or shard* a percentage value . e.g: `33%` . This is computed to a decimal value at runtime-* `#EQUAL` : means divide it equally among all the nodes qualifying a certain property+* `#EQUAL` : Divide the no:of replicas equally among all the nodes qualifying a certain property and place equal no:of them in each node+`strict`::An optional boolean value. The default is `true`. If true, the rule must be satisfied. If false, Solr tries to satisfy the rule on a best effort basis but if no node can satisfy the rule then any node may be chosen.@@ -167,6 +169,8 @@ This supports values calculated at the time of execution.* `%` : A certain percentage of the value. This is supported by the following attributes** `replica`** `freedisk`+* `#ALL` : This is applied to the `replica` attribute only. This means all replicas qualifying a certain clause+* `#EQUAL`: This is applied to the `replica` attribute only. This means equal no:of replicas in each bucket=== Examples of Policy Rules@@ -189,10 +193,10 @@ Place exactly 1 replica of each shard of collection `xyz` on a node running on p{"replica": 1, "shard": "#EACH", "collection": "xyz", "port": "8983"}==== Place Replicas Based on a System Property-Place all replicas on a node with system property `availability_zone=us-east-1a`. Note that we have to write this rule in the negative sense i.e., *0* replicas must be on nodes *not* having the system property `availability_zone=us-east-1a`+Place all replicas on a node with system property `availability_zone=us-east-1a`.[source,json]-{"replica": 0, "sysprop.availability_zone": "!us-east-1a"}+{"replica": "#ALL", "sysprop.availability_zone": "us-east-1a"}===== Use Percentage@@ -233,28 +237,28 @@ Do not place any replica on a node which has the overseer role. Note that the roPlace all replicas in nodes with freedisk more than 500GB. Here again, we have to write the rule in the negative sense.[source,json]-{"replica": 0, "freedisk": "<500"}+{"replica": "#ALL", "freedisk": ">500"}Do not use more than `50%` of diskspace in any node[source,json]-{"replica": 0, "freedisk": "<50%"}+{"replica": "#ALL", "freedisk": ">50%"}==== Try to Place Replicas Based on Free DiskPlace all replicas in nodes with freedisk more than 500GB when possible. Here we use the strict keyword to signal that this rule is to be honored on a best effort basis.[source,json]-{"replica": 0, "freedisk": "<500", "strict" : false}+{"replica": "#ALL", "freedisk": ">500", "strict" : false}==== Try to Place all Replicas of type TLOG on Nodes with SSD Drives[source,json]-{ "replica": 0, "diskType" : "!ssd", "type" : "TLOG" }+{ "replica": "#ALL","type" : "TLOG", "diskType" : "ssd" }==== Try to Place all Replicas of type PULL on Nodes with Rotational Disk Drives[source,json]-{ "replica": 0, "diskType" : "!rotational", "type" : "PULL" }+{ "replica": "#ALL", "type" : "PULL" , "diskType" : "rotational"}[[collection-specific-policy]]== Defining Collection-Specific Policiesdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.javaindex 06dc7a0a873..7bce283c3dc 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java@@ -387,8 +387,19 @@ public class Clause implements MapWriter, Comparable<Clause> {}},+ ALL() {+ @Override+ public String wrap(String value) {+ return "#ALL";+ }+ @Override+ public String match(String val) {+ if ("#ALL".equals(val)) return "1";+ return null;+ }+ },PERCENT {@Overridepublic String wrap(String value) {@@ -512,9 +523,17 @@ public class Clause implements MapWriter, Comparable<Clause> {} else {return 0d;}- } else return op- .opposite(getClause().isReplicaZero() && this == getClause().getTag())- .delta(this.val, val);+ } else {+ if (this == getClause().getReplica()) {+ Double delta = op.delta(this.val, val);+ return getClause().isReplicaZero() ? -1 * delta : delta;+ } else {+ return op+ .opposite(getClause().isReplicaZero() && this == getClause().getTag())+ .delta(this.val, val);+ }++ }}public String getName() {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Operand.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Operand.javaindex bda1e1fc7bb..abd65314948 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Operand.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Operand.java@@ -73,7 +73,7 @@ public enum Operand {EQUAL("", 0) {@Overridepublic double _delta(double expected, double actual) {- return expected - actual;+ return actual - expected;}},RANGE_NOT_EQUAL("", 2) {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggestion.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggestion.javaindex 97b26fca54f..01877df4608 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggestion.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggestion.java@@ -171,7 +171,7 @@ public class Suggestion {@Meta(name = "replica",type = Double.class,min = 0, max = -1,- computedValues = {ComputedType.EQUAL, ComputedType.PERCENT})+ computedValues = {ComputedType.EQUAL, ComputedType.PERCENT, ComputedType.ALL})REPLICA() {@Overridepublic Object validate(String name, Object val, boolean isRuleVal) {@@ -180,6 +180,7 @@ public class Suggestion {@Overridepublic Operand getOperand(Operand expected, Object strVal, ComputedType computedType) {+ if (computedType == ComputedType.ALL) return expected;if (strVal instanceof String) {String s = ((String) strVal).trim();int hyphenIdx = s.indexOf('-');@@ -218,6 +219,8 @@ public class Suggestion {@Overridepublic Object computeValue(Policy.Session session, Clause.Condition cv, String collection, String shard, String node) {+ if (cv.computedType == ComputedType.ALL)+ return new Double(getRelevantReplicasCount(session, cv, collection, shard));if (cv.computedType == ComputedType.EQUAL) {int relevantReplicasCount = getRelevantReplicasCount(session, cv, collection, shard);if (relevantReplicasCount == 0) return 0;@@ -474,12 +477,6 @@ public class Suggestion {}- /*@Override- public void addViolatingReplicas(ViolationCtx ctx) {- for (Row r : ctx.allRows) {- if(r.node.equals(ctx.tagKey)) collectViolatingReplicas(ctx,r);- }- }*/},@Meta(name = "LAZY",diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.javaindex 5147049ad96..0d250fdfc81 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java@@ -282,6 +282,9 @@ public class TestPolicy extends SolrTestCaseJ4 {() -> Clause.create("{replica:'<3', shard: '#ANV', node:'#ANY'}"));expectThrows(IllegalArgumentException.class,() -> Clause.create("{replica:'<3', shard: '#EACH', node:'#E4CH'}"));++ clause = Clause.create("{replica: '#ALL', freedisk:'>20%'}");+ clause = Clause.create("{replica: '#ALL', sysprop.zone :'west'}");}@@ -1880,6 +1883,7 @@ public class TestPolicy extends SolrTestCaseJ4 {List<Violation> violations = cfg.getPolicy().createSession(cloudManagerWithData(dataproviderdata)).getViolations();assertEquals(1, violations.size());assertEquals(4, violations.get(0).getViolatingReplicas().size());+ assertEquals(-4, violations.get(0).replicaCountDelta, 0.1);for (Violation.ReplicaInfoAndErr r : violations.get(0).getViolatingReplicas()) {assertEquals(500d, r.delta, 0.1);@@ -1899,6 +1903,34 @@ public class TestPolicy extends SolrTestCaseJ4 {assertEquals("node1", Utils.getObjectByPath(m, true, "operation/command/move-replica/targetNode"));+ autoScalingjson = " { cluster-policy:[" ++ " { replica :'#ALL', freedisk:'>1000'}," ++ " { nodeRole : overseer, replica :0}]," ++ " cluster-preferences :[{ minimize : cores, precision : 2 }]}";+ cfg = new AutoScalingConfig((Map<String, Object>) Utils.fromJSONString(autoScalingjson));+ violations = cfg.getPolicy().createSession(cloudManagerWithData(dataproviderdata)).getViolations();+ assertEquals(-4, violations.get(0).replicaCountDelta, 0.1);+ assertEquals(1, violations.size());+ assertEquals(4, violations.get(0).getViolatingReplicas().size());+ for (Violation.ReplicaInfoAndErr r : violations.get(0).getViolatingReplicas()) {+ assertEquals(500d, r.delta, 0.1);++ }+ l = PolicyHelper.getSuggestions(cfg, cloudManagerWithData(dataproviderdata));+ assertEquals(3, l.size());+ m = l.get(0).toMap(new LinkedHashMap<>());+ assertEquals("r4", Utils.getObjectByPath(m, true, "operation/command/move-replica/replica"));+ assertEquals("node1", Utils.getObjectByPath(m, true, "operation/command/move-replica/targetNode"));++ m = l.get(1).toMap(new LinkedHashMap<>());+ assertEquals("r3", Utils.getObjectByPath(m, true, "operation/command/move-replica/replica"));+ assertEquals("node1", Utils.getObjectByPath(m, true, "operation/command/move-replica/targetNode"));++ m = l.get(2).toMap(new LinkedHashMap<>());+ assertEquals("r2", Utils.getObjectByPath(m, true, "operation/command/move-replica/replica"));+ assertEquals("node1", Utils.getObjectByPath(m, true, "operation/command/move-replica/targetNode"));++}@@ -2944,6 +2976,22 @@ public void testUtilizeNodeFailure2() throws Exception {for (Violation.ReplicaInfoAndErr r : violations.get(0).getViolatingReplicas()) {assertEquals(10.0d, r.delta.doubleValue(), 0.1);}+ autoScalingjson = "{" ++ " 'cluster-preferences': [" ++ " { 'maximize': 'freedisk', 'precision': 50}," ++ " { 'minimize': 'cores', 'precision': 3}" ++ " ]," ++ " 'cluster-policy': [" ++ " { 'replica':'#ALL' , freedisk : '>30%'}" ++ " ]" ++ "}";+ cfg = new AutoScalingConfig((Map<String, Object>) Utils.fromJSONString(autoScalingjson));+ violations = cfg.getPolicy().createSession(cloudManagerWithData((Map) Utils.fromJSONString(dataproviderdata))).getViolations();+ assertEquals(1, violations.size());+ assertEquals(4, violations.get(0).getViolatingReplicas().size());+ for (Violation.ReplicaInfoAndErr r : violations.get(0).getViolatingReplicas()) {+ assertEquals(10.0d, r.delta.doubleValue(), 0.1);+ }}