SOLR-10278: renamed RuleSorted to Policy

This commit is contained in:
Noble Paul 2017-03-28 23:48:25 +10:30
parent e3a46732bb
commit 83541fa968
8 changed files with 39 additions and 38 deletions

View File

@ -20,8 +20,8 @@ package org.apache.solr.recipe;
import java.util.Map;
import org.apache.solr.common.util.Utils;
import org.apache.solr.recipe.RuleSorter.BaseSuggester;
import org.apache.solr.recipe.RuleSorter.Session;
import org.apache.solr.recipe.Policy.BaseSuggester;
import org.apache.solr.recipe.Policy.Session;
import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;
import static org.apache.solr.common.cloud.ZkStateReader.SHARD_ID_PROP;

View File

@ -27,7 +27,7 @@ import java.util.concurrent.atomic.AtomicReference;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.util.Utils;
import org.apache.solr.recipe.RuleSorter.ReplicaStat;
import org.apache.solr.recipe.Policy.ReplicaStat;
import static java.util.Collections.singletonMap;
import static org.apache.solr.common.params.CoreAdminParams.COLLECTION;
@ -41,8 +41,8 @@ import static org.apache.solr.recipe.Operand.GREATER_THAN;
import static org.apache.solr.recipe.Operand.LESS_THAN;
import static org.apache.solr.recipe.Operand.NOT_EQUAL;
import static org.apache.solr.recipe.Operand.WILDCARD;
import static org.apache.solr.recipe.RuleSorter.ANY;
import static org.apache.solr.recipe.RuleSorter.EACH;
import static org.apache.solr.recipe.Policy.ANY;
import static org.apache.solr.recipe.Policy.EACH;
// a set of conditions in a policy
public class Clause implements MapWriter {

View File

@ -21,8 +21,8 @@ import java.util.Map;
import org.apache.solr.common.util.Pair;
import org.apache.solr.common.util.Utils;
import org.apache.solr.recipe.RuleSorter.BaseSuggester;
import org.apache.solr.recipe.RuleSorter.Session;
import org.apache.solr.recipe.Policy.BaseSuggester;
import org.apache.solr.recipe.Policy.Session;
import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;
import static org.apache.solr.common.cloud.ZkStateReader.SHARD_ID_PROP;
@ -46,7 +46,7 @@ public class MoveReplicaSuggester extends BaseSuggester{
//iterate through elements and identify the least loaded
for (int i = 0; i < matrix.size(); i++) {
Row fromRow = matrix.get(i);
Pair<Row, RuleSorter.ReplicaStat> pair = fromRow.removeReplica(coll, shard);
Pair<Row, Policy.ReplicaStat> pair = fromRow.removeReplica(coll, shard);
fromRow = pair.first();
if(fromRow == null){
//no such replica available

View File

@ -21,7 +21,7 @@ import java.util.Objects;
import org.apache.solr.recipe.Clause.TestStatus;
import static org.apache.solr.recipe.Clause.TestStatus.*;
import static org.apache.solr.recipe.RuleSorter.ANY;
import static org.apache.solr.recipe.Policy.ANY;
public enum Operand {
@ -34,7 +34,7 @@ public enum Operand {
@Override
public Object parse(String val) {
if(val == null) return ANY;
return ANY.equals(val) || RuleSorter.EACH.equals(val) ? val : null;
return ANY.equals(val) || Policy.EACH.equals(val) ? val : null;
}
},
EQUAL(""),

View File

@ -37,7 +37,7 @@ import org.apache.solr.common.util.Utils;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
public class RuleSorter {
public class Policy {
public static final String EACH = "#EACH";
public static final String ANY = "#ANY";
List<Clause> clauses = new ArrayList<>();
@ -45,7 +45,7 @@ public class RuleSorter {
List<String> params= new ArrayList<>();
public RuleSorter(Map<String, Object> jsonMap) {
public Policy(Map<String, Object> jsonMap) {
List<Map<String, Object>> l = getListOfMap("conditions", jsonMap);
clauses = l.stream().map(Clause::new).collect(toList());
l = getListOfMap("preferences", jsonMap);
@ -86,8 +86,8 @@ public class RuleSorter {
.collect(Collectors.toList());
}
RuleSorter getRuleSorter() {
return RuleSorter.this;
Policy getRuleSorter() {
return Policy.this;
}
@ -226,10 +226,10 @@ public class RuleSorter {
static class BaseSuggester {
final String coll;
final String shard;
final RuleSorter.Session session;
final Policy.Session session;
List<Row> matrix;
BaseSuggester(String coll, String shard, RuleSorter.Session session) {
BaseSuggester(String coll, String shard, Policy.Session session) {
this.coll = coll;
this.shard = shard;
this.session = session;

View File

@ -21,15 +21,15 @@ import java.util.List;
import java.util.Map;
class Preference {
final RuleSorter.SortParam name;
final Policy.SortParam name;
Integer precision;
final RuleSorter.Sort sort;
final Policy.Sort sort;
Preference next;
public int idx;
Preference(Map<String, Object> m) {
sort = RuleSorter.Sort.get(m);
name = RuleSorter.SortParam.get(m.get(sort.name()).toString());
sort = Policy.Sort.get(m);
name = Policy.SortParam.get(m.get(sort.name()).toString());
Object p = m.getOrDefault("precision", 0);
precision = p instanceof Number ? ((Number) p).intValue() : Integer.parseInt(p.toString());

View File

@ -28,6 +28,7 @@ import org.apache.solr.common.IteratorWriter;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.util.Pair;
import org.apache.solr.common.util.Utils;
import org.apache.solr.recipe.Policy.ReplicaStat;
import static org.apache.solr.common.params.CoreAdminParams.NODE;
@ -35,11 +36,11 @@ import static org.apache.solr.common.params.CoreAdminParams.NODE;
class Row implements MapWriter {
public final String node;
final Cell[] cells;
Map<String, Map<String, List<RuleSorter.ReplicaStat>>> replicaInfo;
Map<String, Map<String, List<ReplicaStat>>> replicaInfo;
List<Clause> violations = new ArrayList<>();
boolean anyValueMissing = false;
Row(String node, List<String> params, RuleSorter.NodeValueProvider snitch) {
Row(String node, List<String> params, Policy.NodeValueProvider snitch) {
replicaInfo = snitch.getReplicaCounts(node, params);
if (replicaInfo == null) replicaInfo = Collections.emptyMap();
this.node = node;
@ -53,7 +54,7 @@ class Row implements MapWriter {
}
}
Row(String node, Cell[] cells, boolean anyValueMissing, Map<String, Map<String, List<RuleSorter.ReplicaStat>>> replicaInfo, List<Clause> violations) {
Row(String node, Cell[] cells, boolean anyValueMissing, Map<String, Map<String, List<ReplicaStat>>> replicaInfo, List<Clause> violations) {
this.node = node;
this.cells = new Cell[cells.length];
for (int i = 0; i < this.cells.length; i++) {
@ -89,20 +90,20 @@ class Row implements MapWriter {
Row addReplica(String coll, String shard) {
Row row = copy();
Map<String, List<RuleSorter.ReplicaStat>> c = row.replicaInfo.get(coll);
Map<String, List<ReplicaStat>> c = row.replicaInfo.get(coll);
if (c == null) row.replicaInfo.put(coll, c = new HashMap<>());
List<RuleSorter.ReplicaStat> s = c.get(shard);
List<ReplicaStat> s = c.get(shard);
if (s == null) c.put(shard, s = new ArrayList<>());
return row;
}
Pair<Row, RuleSorter.ReplicaStat> removeReplica(String coll, String shard) {
Pair<Row, ReplicaStat> removeReplica(String coll, String shard) {
Row row = copy();
Map<String, List<RuleSorter.ReplicaStat>> c = row.replicaInfo.get(coll);
Map<String, List<ReplicaStat>> c = row.replicaInfo.get(coll);
if(c == null) return null;
List<RuleSorter.ReplicaStat> s = c.get(shard);
List<ReplicaStat> s = c.get(shard);
if (s == null || s.isEmpty()) return null;
return new Pair(row,s.remove(0));

View File

@ -31,7 +31,7 @@ import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.util.Utils;
import org.apache.solr.common.util.ValidatingJsonMap;
public class TestRuleSorter extends SolrTestCaseJ4 {
public class TestPolicy extends SolrTestCaseJ4 {
public void testOperands() {
Clause c = new Clause((Map<String, Object>) Utils.fromJSONString("{replica:'<2', node:'#ANY'}"));
@ -113,9 +113,9 @@ public class TestRuleSorter extends SolrTestCaseJ4 {
.getDeepCopy((Map) Utils.fromJSONString(clusterState), 6, true);
RuleSorter ruleSorter = new RuleSorter((Map<String, Object>) Utils.fromJSONString(rules));
RuleSorter.Session session;
RuleSorter.NodeValueProvider snitch = new RuleSorter.NodeValueProvider() {
Policy policy = new Policy((Map<String, Object>) Utils.fromJSONString(rules));
Policy.Session session;
Policy.NodeValueProvider snitch = new Policy.NodeValueProvider() {
@Override
public Map<String,Object> getValues(String node, Collection<String> keys) {
Map<String,Object> result = new LinkedHashMap<>();
@ -124,8 +124,8 @@ public class TestRuleSorter extends SolrTestCaseJ4 {
}
@Override
public Map<String, Map<String, List<RuleSorter.ReplicaStat>>> getReplicaCounts(String node, Collection<String> keys) {
Map<String, Map<String, List<RuleSorter.ReplicaStat>>> result = new LinkedHashMap<>();
public Map<String, Map<String, List<Policy.ReplicaStat>>> getReplicaCounts(String node, Collection<String> keys) {
Map<String, Map<String, List<Policy.ReplicaStat>>> result = new LinkedHashMap<>();
m.forEach((collName, o) -> {
ValidatingJsonMap coll = (ValidatingJsonMap) o;
@ -135,11 +135,11 @@ public class TestRuleSorter extends SolrTestCaseJ4 {
ValidatingJsonMap r = (ValidatingJsonMap) o2;
String node_name = (String) r.get("node_name");
if (!node_name.equals(node)) return;
Map<String, List<RuleSorter.ReplicaStat>> shardVsReplicaStats = result.get(collName);
Map<String, List<Policy.ReplicaStat>> shardVsReplicaStats = result.get(collName);
if (shardVsReplicaStats == null) result.put(collName, shardVsReplicaStats = new HashMap<>());
List<RuleSorter.ReplicaStat> replicaStats = shardVsReplicaStats.get(shard);
List<Policy.ReplicaStat> replicaStats = shardVsReplicaStats.get(shard);
if (replicaStats == null) shardVsReplicaStats.put(shard, replicaStats = new ArrayList<>());
replicaStats.add(new RuleSorter.ReplicaStat(replicaName, new HashMap<>()));
replicaStats.add(new Policy.ReplicaStat(replicaName, new HashMap<>()));
});
});
});
@ -150,7 +150,7 @@ public class TestRuleSorter extends SolrTestCaseJ4 {
};
session = ruleSorter.createSession(Arrays.asList("node1", "node2", "node3", "node4"), snitch);
session = policy.createSession(Arrays.asList("node1", "node2", "node3", "node4"), snitch);
session.applyRules();
List<Row> l = session.getSorted();