From 97e2607a691af5f1aa2c92e445128fdee8934c73 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Tue, 18 Apr 2017 16:00:42 +0930 Subject: [PATCH] SOLR-10278: refcatored suggester API --- .../solr/cloud/policy/TestPolicyCloud.java | 2 +- .../solr/recipe/AddReplicaSuggester.java | 2 ++ .../solr/recipe/MoveReplicaSuggester.java | 3 ++ .../java/org/apache/solr/recipe/Policy.java | 35 +++++++++++++------ .../org/apache/solr/recipe/TestPolicy.java | 7 ++-- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/cloud/policy/TestPolicyCloud.java b/solr/core/src/test/org/apache/solr/cloud/policy/TestPolicyCloud.java index 457e8c2ea03..746ec7c5275 100644 --- a/solr/core/src/test/org/apache/solr/cloud/policy/TestPolicyCloud.java +++ b/solr/core/src/test/org/apache/solr/cloud/policy/TestPolicyCloud.java @@ -63,7 +63,7 @@ public class TestPolicyCloud extends SolrCloudTestCase { Map val = provider.getNodeValues(rulesCollection.getReplicas().get(0).getNodeName(), Arrays.asList("freedisk", "cores")); assertTrue(((Number) val.get("cores")).intValue() > 0); - assertTrue("freedisk value is "+((Number) val.get("freedisk")).intValue() , ((Number) val.get("freedisk")).intValue() > 0); + assertTrue("freedisk value is "+((Number) val.get("freedisk")).longValue() , ((Number) val.get("freedisk")).longValue() > 0); System.out.println(Utils.toJSONString(val)); } } diff --git a/solr/solrj/src/java/org/apache/solr/recipe/AddReplicaSuggester.java b/solr/solrj/src/java/org/apache/solr/recipe/AddReplicaSuggester.java index e20bf265e2c..450225e2535 100644 --- a/solr/solrj/src/java/org/apache/solr/recipe/AddReplicaSuggester.java +++ b/solr/solrj/src/java/org/apache/solr/recipe/AddReplicaSuggester.java @@ -39,6 +39,8 @@ class AddReplicaSuggester extends Suggester { //iterate through elements and identify the least loaded for (int i = getMatrix().size() - 1; i >= 0; i--) { Row row = getMatrix().get(i); + String coll = hints.get(Hint.COLL); + String shard = hints.get(Hint.SHARD); row = row.addReplica(coll, shard); row.violations.clear(); for (Clause clause : session.getPolicy().clauses) { diff --git a/solr/solrj/src/java/org/apache/solr/recipe/MoveReplicaSuggester.java b/solr/solrj/src/java/org/apache/solr/recipe/MoveReplicaSuggester.java index 1fcb79b04f0..43e1fe6fc11 100644 --- a/solr/solrj/src/java/org/apache/solr/recipe/MoveReplicaSuggester.java +++ b/solr/solrj/src/java/org/apache/solr/recipe/MoveReplicaSuggester.java @@ -30,6 +30,7 @@ import static org.apache.solr.common.params.CoreAdminParams.NODE; import static org.apache.solr.common.params.CoreAdminParams.REPLICA; public class MoveReplicaSuggester extends Suggester { + @Override Map init() { Map operation = tryEachNode(true); @@ -39,6 +40,8 @@ public class MoveReplicaSuggester extends Suggester { Map tryEachNode(boolean strict) { //iterate through elements and identify the least loaded + String coll = hints.get(Hint.COLL); + String shard = hints.get(Hint.SHARD); for (int i = 0; i < getMatrix().size(); i++) { Row fromRow = getMatrix().get(i); Pair pair = fromRow.removeReplica(coll, shard); diff --git a/solr/solrj/src/java/org/apache/solr/recipe/Policy.java b/solr/solrj/src/java/org/apache/solr/recipe/Policy.java index b3cff392fb4..5a8857db801 100644 --- a/solr/solrj/src/java/org/apache/solr/recipe/Policy.java +++ b/solr/solrj/src/java/org/apache/solr/recipe/Policy.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -43,6 +44,8 @@ import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.apache.solr.common.params.CollectionParams.CollectionAction.ADDREPLICA; import static org.apache.solr.common.util.Utils.getDeepCopy; +import static org.apache.solr.recipe.Policy.Suggester.Hint.COLL; +import static org.apache.solr.recipe.Policy.Suggester.Hint.SHARD; public class Policy implements MapWriter { public static final String EACH = "#EACH"; @@ -156,10 +159,11 @@ public class Policy implements MapWriter { .collect(Collectors.toMap(r -> r.node, r -> r.violations)); } - public Suggester getSuggester(CollectionAction action, String collection, String shard) { + public Suggester getSuggester(CollectionAction action) { Suggester op = ops.get(action).get(); if (op == null) throw new UnsupportedOperationException(action.toString() + "is not supported"); - return op.init(collection, shard, this); + op._init(this); + return op; } @Override @@ -243,17 +247,17 @@ public class Policy implements MapWriter { public static abstract class Suggester { - String coll; - String shard; + protected final EnumMap hints = new EnumMap<>(Hint.class); Policy.Session session; - Map operation; + private boolean isInitialized = false; - Suggester init(String coll, String shard, Policy.Session session) { - this.coll = coll; - this.shard = shard; + private void _init(Session session) { this.session = session.copy(); - this.operation = init(); + } + + public Suggester hint(Hint hint, String value) { + hints.put(hint, value); return this; } @@ -261,6 +265,10 @@ public class Policy implements MapWriter { public Map getOperation() { + if (!isInitialized) { + this.operation = init(); + isInitialized = true; + } return operation; } @@ -273,6 +281,10 @@ public class Policy implements MapWriter { } + enum Hint { + COLL, SHARD, SRC_NODE, TARGET_NODE + } + } @@ -289,12 +301,13 @@ public class Policy implements MapWriter { Map defaultPolicy = (Map) Utils.getObjectByPath(autoScalingJson, false, asList("policies", "default")); Map merged = Policy.mergePolicies(collName, policyJson, defaultPolicy); - System.out.println(Utils.toJSONString(merged)); Policy policy = new Policy(merged); Policy.Session session = policy.createSession(cdp); for (String shardName : shardNames) { for (int i = 0; i < repFactor; i++) { - Policy.Suggester suggester = session.getSuggester(ADDREPLICA, collName, shardName); + Policy.Suggester suggester = session.getSuggester(ADDREPLICA) + .hint(COLL, collName) + .hint(SHARD, shardName); Map op = suggester.getOperation(); if (op == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No node can satisfy the rules "+ Utils.toJSONString(policy)); diff --git a/solr/solrj/src/test/org/apache/solr/recipe/TestPolicy.java b/solr/solrj/src/test/org/apache/solr/recipe/TestPolicy.java index 809f805cb9d..ba9081df5f6 100644 --- a/solr/solrj/src/test/org/apache/solr/recipe/TestPolicy.java +++ b/solr/solrj/src/test/org/apache/solr/recipe/TestPolicy.java @@ -29,6 +29,7 @@ import java.util.Map; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.Utils; import org.apache.solr.common.util.ValidatingJsonMap; +import org.apache.solr.recipe.Policy.Suggester.Hint; import static org.apache.solr.common.params.CollectionParams.CollectionAction.ADDREPLICA; import static org.apache.solr.common.util.Utils.getObjectByPath; @@ -160,11 +161,11 @@ public class TestPolicy extends SolrTestCaseJ4 { assertEquals(v.get(0).replica.op, Operand.LESS_THAN); assertEquals(v.get(0).replica.val, 1); assertEquals(v.get(0).tag.val, "node3"); - - Policy.Suggester suggester = session.getSuggester(ADDREPLICA, "gettingstarted", "r1"); + Policy.Suggester suggester = session.getSuggester(ADDREPLICA) + .hint(Hint.COLL, "gettingstarted") + .hint(Hint.SHARD, "r1"); Map operation = suggester.getOperation(); assertEquals("node2", operation.get("node")); - System.out.println(Utils.toJSONString(operation));