SOLR-10278: refcatored suggester API

This commit is contained in:
Noble Paul 2017-04-18 16:00:42 +09:30
parent 66562ff85f
commit 97e2607a69
5 changed files with 34 additions and 15 deletions

View File

@ -63,7 +63,7 @@ public class TestPolicyCloud extends SolrCloudTestCase {
Map<String, Object> val = provider.getNodeValues(rulesCollection.getReplicas().get(0).getNodeName(), Arrays.asList("freedisk", "cores")); Map<String, Object> val = provider.getNodeValues(rulesCollection.getReplicas().get(0).getNodeName(), Arrays.asList("freedisk", "cores"));
assertTrue(((Number) val.get("cores")).intValue() > 0); 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)); System.out.println(Utils.toJSONString(val));
} }
} }

View File

@ -39,6 +39,8 @@ class AddReplicaSuggester extends Suggester {
//iterate through elements and identify the least loaded //iterate through elements and identify the least loaded
for (int i = getMatrix().size() - 1; i >= 0; i--) { for (int i = getMatrix().size() - 1; i >= 0; i--) {
Row row = getMatrix().get(i); Row row = getMatrix().get(i);
String coll = hints.get(Hint.COLL);
String shard = hints.get(Hint.SHARD);
row = row.addReplica(coll, shard); row = row.addReplica(coll, shard);
row.violations.clear(); row.violations.clear();
for (Clause clause : session.getPolicy().clauses) { for (Clause clause : session.getPolicy().clauses) {

View File

@ -30,6 +30,7 @@ import static org.apache.solr.common.params.CoreAdminParams.NODE;
import static org.apache.solr.common.params.CoreAdminParams.REPLICA; import static org.apache.solr.common.params.CoreAdminParams.REPLICA;
public class MoveReplicaSuggester extends Suggester { public class MoveReplicaSuggester extends Suggester {
@Override @Override
Map init() { Map init() {
Map operation = tryEachNode(true); Map operation = tryEachNode(true);
@ -39,6 +40,8 @@ public class MoveReplicaSuggester extends Suggester {
Map tryEachNode(boolean strict) { Map tryEachNode(boolean strict) {
//iterate through elements and identify the least loaded //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++) { for (int i = 0; i < getMatrix().size(); i++) {
Row fromRow = getMatrix().get(i); Row fromRow = getMatrix().get(i);
Pair<Row, Policy.ReplicaInfo> pair = fromRow.removeReplica(coll, shard); Pair<Row, Policy.ReplicaInfo> pair = fromRow.removeReplica(coll, shard);

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -43,6 +44,8 @@ import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.apache.solr.common.params.CollectionParams.CollectionAction.ADDREPLICA; import static org.apache.solr.common.params.CollectionParams.CollectionAction.ADDREPLICA;
import static org.apache.solr.common.util.Utils.getDeepCopy; 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 class Policy implements MapWriter {
public static final String EACH = "#EACH"; public static final String EACH = "#EACH";
@ -156,10 +159,11 @@ public class Policy implements MapWriter {
.collect(Collectors.toMap(r -> r.node, r -> r.violations)); .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(); Suggester op = ops.get(action).get();
if (op == null) throw new UnsupportedOperationException(action.toString() + "is not supported"); if (op == null) throw new UnsupportedOperationException(action.toString() + "is not supported");
return op.init(collection, shard, this); op._init(this);
return op;
} }
@Override @Override
@ -243,17 +247,17 @@ public class Policy implements MapWriter {
public static abstract class Suggester { public static abstract class Suggester {
String coll; protected final EnumMap<Hint, String> hints = new EnumMap<>(Hint.class);
String shard;
Policy.Session session; Policy.Session session;
Map operation; Map operation;
private boolean isInitialized = false;
Suggester init(String coll, String shard, Policy.Session session) { private void _init(Session session) {
this.coll = coll;
this.shard = shard;
this.session = session.copy(); this.session = session.copy();
this.operation = init(); }
public Suggester hint(Hint hint, String value) {
hints.put(hint, value);
return this; return this;
} }
@ -261,6 +265,10 @@ public class Policy implements MapWriter {
public Map getOperation() { public Map getOperation() {
if (!isInitialized) {
this.operation = init();
isInitialized = true;
}
return operation; 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 defaultPolicy = (Map) Utils.getObjectByPath(autoScalingJson, false, asList("policies", "default"));
Map<String, Object> merged = Policy.mergePolicies(collName, policyJson, defaultPolicy); Map<String, Object> merged = Policy.mergePolicies(collName, policyJson, defaultPolicy);
System.out.println(Utils.toJSONString(merged));
Policy policy = new Policy(merged); Policy policy = new Policy(merged);
Policy.Session session = policy.createSession(cdp); Policy.Session session = policy.createSession(cdp);
for (String shardName : shardNames) { for (String shardName : shardNames) {
for (int i = 0; i < repFactor; i++) { 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(); Map op = suggester.getOperation();
if (op == null) { if (op == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No node can satisfy the rules "+ Utils.toJSONString(policy)); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No node can satisfy the rules "+ Utils.toJSONString(policy));

View File

@ -29,6 +29,7 @@ import java.util.Map;
import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.util.Utils; import org.apache.solr.common.util.Utils;
import org.apache.solr.common.util.ValidatingJsonMap; 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.params.CollectionParams.CollectionAction.ADDREPLICA;
import static org.apache.solr.common.util.Utils.getObjectByPath; 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.op, Operand.LESS_THAN);
assertEquals(v.get(0).replica.val, 1); assertEquals(v.get(0).replica.val, 1);
assertEquals(v.get(0).tag.val, "node3"); assertEquals(v.get(0).tag.val, "node3");
Policy.Suggester suggester = session.getSuggester(ADDREPLICA)
Policy.Suggester suggester = session.getSuggester(ADDREPLICA, "gettingstarted", "r1"); .hint(Hint.COLL, "gettingstarted")
.hint(Hint.SHARD, "r1");
Map operation = suggester.getOperation(); Map operation = suggester.getOperation();
assertEquals("node2", operation.get("node")); assertEquals("node2", operation.get("node"));
System.out.println(Utils.toJSONString(operation));