You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2020/03/05 18:19:25 UTC

[lucene-solr] branch jira/solr-14275 updated: SOLR-14275: Test some other potential optimizations.

This is an automated email from the ASF dual-hosted git repository.

ab pushed a commit to branch jira/solr-14275
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/jira/solr-14275 by this push:
     new d656a2d  SOLR-14275: Test some other potential optimizations.
d656a2d is described below

commit d656a2df0e982890b3cdc07585e110ca61f46deb
Author: Andrzej Bialecki <ab...@apache.org>
AuthorDate: Thu Mar 5 18:47:14 2020 +0100

    SOLR-14275: Test some other potential optimizations.
---
 .../solrj/cloud/autoscaling/AddReplicaSuggester.java  |  5 +++++
 .../solrj/cloud/autoscaling/FreeDiskVariable.java     |  8 +++++---
 .../solr/client/solrj/cloud/autoscaling/Policy.java   |  7 ++++++-
 .../solr/client/solrj/cloud/autoscaling/Row.java      | 19 +++++++++++++++++--
 .../client/solrj/cloud/autoscaling/Suggester.java     |  2 +-
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
index 2e910db..df72f9f 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
@@ -47,6 +47,8 @@ class AddReplicaSuggester extends Suggester {
       //iterate through  nodes and identify the least loaded
       List<Violation> leastSeriousViolation = null;
       Row bestNode = null;
+      // compare only up to that many eligible rows from the top
+      int limitTopRows = 3;
       for (int i = getMatrix().size() - 1; i >= 0; i--) {
         Row row = getMatrix().get(i);
         if (!isNodeSuitableForReplicaAddition(row, null)) continue;
@@ -58,6 +60,9 @@ class AddReplicaSuggester extends Suggester {
             leastSeriousViolation = errs;
             bestNode = tmpRow;
           }
+          if (bestNode != null && limitTopRows-- < 0) {
+            break;
+          }
         }
       }
 
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/FreeDiskVariable.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/FreeDiskVariable.java
index 29de2a1..c61d7ee 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/FreeDiskVariable.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/FreeDiskVariable.java
@@ -37,6 +37,8 @@ import static org.apache.solr.common.params.CollectionParams.CollectionAction.MO
 
 public class FreeDiskVariable extends VariableBase {
 
+  public static final String FREEDISK_CACHE = "freedisk";
+
   public FreeDiskVariable(Type type) {
     super(type);
   }
@@ -144,7 +146,7 @@ public class FreeDiskVariable extends VariableBase {
 
   }
 
-  private static final class IndexSizeGetter {
+  public static final class IndexSizeGetter {
     final String collection, shard;
 
     IndexSizeGetter(String collection, String shard) {
@@ -164,7 +166,7 @@ public class FreeDiskVariable extends VariableBase {
           }
         }
       });
-      return result[0];
+      return result[0] != null ? result[0] : 0L;
     }
   }
 
@@ -177,7 +179,7 @@ public class FreeDiskVariable extends VariableBase {
       if (row.isEmpty() || !row.isLive() || !row.hasColl(ri.getCollection())) {
         continue;
       }
-      Object indexSize = row.computeCacheIfAbsent(ri.getCollection(), ri.getShard(), "freedisk", CORE_IDX.tagName, o -> getter.getIndexSize(row));
+      Object indexSize = row.computeCacheIfAbsent(ri.getCollection(), ri.getShard(), FREEDISK_CACHE, CORE_IDX.tagName, o -> getter.getIndexSize(row));
       if (indexSize != null) {
         ri.getVariables().put(CORE_IDX.tagName, validate(CORE_IDX.tagName, indexSize, false));
         break;
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
index e624920..cb25b82 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
@@ -320,7 +320,8 @@ public class Policy implements MapWriter {
         p.setApproxVal(tmpMatrix);
       }
       // the tmpMatrix was needed only to set the approximate values, now we sort the real matrix
-      // recursing through each preference
+      // recursing through each preference (Preference.compare uses Preference.next for recursion,
+      // which is set when Policy is initialized)
       matrix.sort((Row r1, Row r2) -> {
         int result = clusterPreferences.get(0).compare(r1, r2, true);
         if (result == 0) result = clusterPreferences.get(0).compare(r1, r2, false);
@@ -659,6 +660,10 @@ public class Policy implements MapWriter {
       return new Session(nodes, cloudManager, getMatrixCopy(), expandedClauses, znodeVersion, nodeStateProvider, transaction);
     }
 
+    Session shallowCopy() {
+      return new Session(nodes, cloudManager, new ArrayList<>(matrix), expandedClauses, znodeVersion, nodeStateProvider, transaction);
+    }
+
     public Row getNode(String node) {
       for (Row row : matrix) if (row.node.equals(node)) return row;
       return null;
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Row.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Row.java
index 196e17c..d4a8b12 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Row.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Row.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -78,6 +79,20 @@ public class Row implements MapWriter {
     }
     this.globalCache = new HashMap();
     this.perCollCache = new HashMap();
+
+    // pre-compute and cache common variables
+    computeCacheIfAbsent(CoresVariable.TOTALCORES, o -> {
+      int[] result = new int[1];
+      Row.this.forEachReplica(replicaInfo -> result[0]++);
+      return result[0];
+    });
+    collectionVsShardVsReplicas.forEach((collection, shardVsReplicas) -> {
+      shardVsReplicas.forEach((shard, replicas) -> {
+        FreeDiskVariable.IndexSizeGetter getter = new FreeDiskVariable.IndexSizeGetter(collection, shard);
+        computeCacheIfAbsent(collection, shard, FreeDiskVariable.FREEDISK_CACHE, Variable.Type.CORE_IDX.tagName,
+            o -> getter.getIndexSize(Row.this));
+      });
+    });
     isAlreadyCopied = true;
   }
 
@@ -255,13 +270,13 @@ public class Row implements MapWriter {
   private void lazyCopyReplicas(String coll, String shard) {
     globalCache = new HashMap();
     Map cacheCopy = new HashMap<>(perCollCache);
-    cacheCopy.remove(coll);//todo optimize at shard level later
+    ((Map)cacheCopy.getOrDefault(coll, Collections.emptyMap())).remove(shard);
     perCollCache = cacheCopy;
     if (isAlreadyCopied) return;//caches need to be invalidated but the rest can remain as is
 
     Map<String, Map<String, List<ReplicaInfo>>> replicasCopy = new HashMap<>(collectionVsShardVsReplicas);
     Map<String, List<ReplicaInfo>> oneColl = replicasCopy.get(coll);
-    if (oneColl != null) {
+    if (oneColl != null && oneColl.get(shard) != null) {
       replicasCopy.put(coll, Utils.getDeepCopy(oneColl, 2));
     }
     collectionVsShardVsReplicas = replicasCopy;
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggester.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggester.java
index 9dc7fec..7eacbd0 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggester.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Suggester.java
@@ -70,7 +70,7 @@ public abstract class Suggester implements MapWriter {
 
 
   void _init(Policy.Session session) {
-    this.session = session.copy();
+    this.session = session.shallowCopy();
   }
 
   boolean isLessDeviant() {