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"));
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));
}
}

View File

@ -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) {

View File

@ -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<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.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<Hint, String> 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<String, Object> 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));

View File

@ -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));