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/01/08 16:37:40 UTC

[lucene-solr] 03/03: SOLR-13579: Several bug fixes.

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

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

commit 88c80fe90c0130a8af949945b480bcec2e69b34e
Author: Andrzej Bialecki <ab...@apache.org>
AuthorDate: Wed Jan 8 17:37:12 2020 +0100

    SOLR-13579: Several bug fixes.
---
 .../solr/handler/admin/ResourceManagerHandler.java | 125 ++++++++++++---------
 .../managed/DefaultResourceManagerPoolFactory.java |   2 +-
 .../apache/solr/managed/ResourceManagerPool.java   |  12 +-
 .../apache/solr/managed/SolrResourceContext.java   |   8 ++
 .../solr/managed/types/CacheManagerPool.java       |  22 ++++
 .../solrj/impl/SolrClientNodeStateProvider.java    |   1 -
 6 files changed, 114 insertions(+), 56 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ResourceManagerHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ResourceManagerHandler.java
index 1c776e0..72da514 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/ResourceManagerHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/ResourceManagerHandler.java
@@ -17,9 +17,14 @@
 package org.apache.solr.handler.admin;
 
 import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.params.CommonParams;
@@ -140,7 +145,7 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
           perPool.add("size", pool.getComponents().size());
           perPool.add("poolLimits", pool.getPoolLimits());
           perPool.add("poolParams", pool.getParams());
-          perPool.add("resources", pool.getComponents().keySet());
+          perPool.add("resources", new TreeSet<>(pool.getComponents().keySet()));
         });
         break;
       case STATUS:
@@ -152,10 +157,10 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
         result.add("size", pool.getComponents().size());
         result.add("poolLimits", pool.getPoolLimits());
         result.add("poolParams", pool.getParams());
-        result.add("resources", pool.getComponents().keySet());
+        result.add("resources", new TreeSet<>(pool.getComponents().keySet()));
         try {
           Map<String, Map<String, Object>> values = pool.getCurrentValues();
-          result.add("totalValues", pool.aggregateTotalValues(values));
+          result.add("totalValues", new TreeMap<>(pool.aggregateTotalValues(values)));
         } catch (Exception e) {
           log.warn("Error getting current values from pool " + name, e);
           result.add("error", "Error getting current values: " + e.toString());
@@ -214,7 +219,18 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
       if (val.isBlank()) {
         val = null;
       }
-      result.put(entry.getKey().substring(prefix.length()), val);
+      // convert into a number if possible
+      Object newVal;
+      try {
+        newVal = Long.parseLong(val);
+      } catch (NumberFormatException nfe) {
+        try {
+          newVal = Double.parseDouble(val);
+        } catch (NumberFormatException nfe2) {
+          newVal = val;
+        }
+      }
+      result.put(entry.getKey().substring(prefix.length()), newVal);
     });
     return result;
   }
@@ -238,6 +254,11 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
       throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing '" + CommonParams.NAME + "' parameter.");
     }
     NamedList<Object> result = new SimpleOrderedMap<>();
+    List<ManagedComponent> components = resName == null ? new ArrayList<>(pool.getComponents().values()) : pool.getComponents().values().stream()
+        .filter(c -> c.getManagedComponentId().toString().startsWith(resName)).collect(Collectors.toList());
+    if (op != ResOp.LIST && components.isEmpty()) {
+      throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Component(s) '" + resName + " not found in pool '" + poolName + "'.");
+    }
     switch (op) {
       case LIST:
         pool.getComponents().forEach((n, component) -> {
@@ -245,7 +266,7 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
           result.add(n, perRes);
           perRes.add("class", component.getClass().getName());
           try {
-            perRes.add("resourceLimits", pool.getResourceLimits(component));
+            perRes.add("resourceLimits", new TreeMap<>(pool.getResourceLimits(component)));
           } catch (Exception e) {
             log.warn("Error getting resourceLimits of " + component.getManagedComponentId(), e);
             result.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
@@ -253,65 +274,65 @@ public class ResourceManagerHandler extends RequestHandlerBase implements Permis
         });
         break;
       case STATUS:
-        ManagedComponent component = pool.getComponents().get(resName);
-        if (component == null) {
-          throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Component '" + resName + " not found in pool '" + poolName + "'.");
-        }
-        result.add("class", component.getClass().getName());
-        try {
-          result.add("resourceLimits", pool.getResourceLimits(component));
-        } catch (Exception e) {
-          log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
-          result.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
-        }
-        try {
-          result.add("monitoredValues", pool.getMonitoredValues(component));
-        } catch (Exception e) {
-          log.warn("Error getting monitored values of " + resName + "/" + poolName + " : " + e.toString(), e);
-          result.add("error", "Error getting monitored values of " + resName + ": " + e.toString());
+        for (ManagedComponent component : components) {
+          SimpleOrderedMap perComponent = new SimpleOrderedMap();
+          result.add(component.getManagedComponentId().toString(), perComponent);
+          perComponent.add("class", component.getClass().getName());
+          try {
+            perComponent.add("resourceLimits", new TreeMap<>(pool.getResourceLimits(component)));
+          } catch (Exception e) {
+            log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
+            perComponent.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
+          }
+          try {
+            perComponent.add("monitoredValues", new TreeMap<>(pool.getMonitoredValues(component)));
+          } catch (Exception e) {
+            log.warn("Error getting monitored values of " + resName + "/" + poolName + " : " + e.toString(), e);
+            perComponent.add("error", "Error getting monitored values of " + resName + ": " + e.toString());
+          }
         }
         break;
       case GETLIMITS:
-        ManagedComponent managedComponent1 = pool.getComponents().get(resName);
-        if (managedComponent1 == null) {
-          throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Component '" + resName + " not found in pool '" + poolName + "'.");
-        }
-        try {
-          result.add("resourceLimits", pool.getResourceLimits(managedComponent1));
-        } catch (Exception e) {
-          log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
-          result.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
+        for (ManagedComponent component : components) {
+          SimpleOrderedMap perComponent = new SimpleOrderedMap();
+          result.add(component.getManagedComponentId().toString(), perComponent);
+          try {
+            perComponent.add("resourceLimits", new TreeMap<>(pool.getResourceLimits(component)));
+          } catch (Exception e) {
+            log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
+            perComponent.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
+          }
         }
         break;
       case SETLIMITS:
-        ManagedComponent managedComponent2 = pool.getComponents().get(resName);
-        if (managedComponent2 == null) {
-          throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Resource '" + resName + " not found in pool '" + poolName + "'.");
-        }
-        try {
-          Map<String, Object> currentLimits = new HashMap<>(pool.getResourceLimits(managedComponent2));
-          Map<String, Object> newLimits = getMap(params, LIMIT_PREFIX_PARAM);
-          newLimits.forEach((k, v) -> {
-            if (v == null) {
-              currentLimits.remove(k);
-            } else {
-              currentLimits.put(k, v);
-            }
-          });
+        for (ManagedComponent cmp : components) {
           try {
-            pool.setResourceLimits(managedComponent2, newLimits, ChangeListener.Reason.USER);
-            result.add("success", newLimits);
+            Map<String, Object> currentLimits = new HashMap<>(pool.getResourceLimits(cmp));
+            Map<String, Object> newLimits = getMap(params, LIMIT_PREFIX_PARAM);
+            newLimits.forEach((k, v) -> {
+              if (v == null) {
+                currentLimits.remove(k);
+              } else {
+                currentLimits.put(k, v);
+              }
+            });
+            try {
+              pool.setResourceLimits(cmp, newLimits, ChangeListener.Reason.USER);
+              result.add("success", newLimits);
+            } catch (Exception e) {
+              log.warn("Error setting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
+              result.add("error", "Error setting resource limits of " + resName + ": " + e.toString());
+            }
           } catch (Exception e) {
-            log.warn("Error setting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
-            result.add("error", "Error setting resource limits of " + resName + ": " + e.toString());
+            log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
+            result.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
           }
-        } catch (Exception e) {
-          log.warn("Error getting resource limits of " + resName + "/" + poolName + " : " + e.toString(), e);
-          result.add("error", "Error getting resource limits of " + resName + ": " + e.toString());
         }
         break;
       case DELETE:
-        result.add("success", pool.unregisterComponent(resName) ? "removed" : "not found");
+        for (ManagedComponent component : components) {
+          result.add(component.getManagedComponentId().toString(), pool.unregisterComponent(component.getManagedComponentId().toString()) ? "removed" : "not found");
+        }
     }
     rsp.getValues().add("result", result);
   }
diff --git a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPoolFactory.java b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPoolFactory.java
index b371374..517f65e 100644
--- a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPoolFactory.java
+++ b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPoolFactory.java
@@ -103,7 +103,7 @@ public class DefaultResourceManagerPoolFactory implements ResourceManagerPoolFac
         null,
         new Class[]{String.class, String.class, ResourceManager.class, Map.class, Map.class},
         new Object[]{name, type, resourceManager, poolLimits, poolParams});
-    resourceManagerPool.initializeMetrics(resourceManager.getSolrMetricsContext(), "pool");
+    resourceManagerPool.initializeMetrics(resourceManager.getSolrMetricsContext(), name);
     return resourceManagerPool;
   }
 
diff --git a/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java b/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java
index 71ccf9d..ce0df6f 100644
--- a/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java
+++ b/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java
@@ -11,10 +11,12 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.ReentrantLock;
 
 import com.codahale.metrics.Timer;
 import org.apache.solr.core.SolrInfoBean;
+import org.apache.solr.metrics.MetricsMap;
 import org.apache.solr.metrics.SolrMetricsContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,6 +41,7 @@ public abstract class ResourceManagerPool<T extends ManagedComponent> implements
   protected ScheduledFuture<?> scheduledFuture;
   protected SolrMetricsContext solrMetricsContext;
   protected Timer manageTimer;
+  protected Map<ChangeListener.Reason, AtomicLong> changeCounts = new ConcurrentHashMap<>();
 
   public ResourceManagerPool(String name, String type, ResourceManager resourceManager,
                                 Map<String, Object> poolLimits, Map<String, Object> poolParams) {
@@ -71,7 +74,11 @@ public abstract class ResourceManagerPool<T extends ManagedComponent> implements
   @Override
   public void initializeMetrics(SolrMetricsContext parentContext, String childScope) {
     solrMetricsContext = parentContext.getChildContext(this, childScope);
-    manageTimer = solrMetricsContext.timer("manageTimes", getCategory().toString(), getType(), getName());
+    manageTimer = solrMetricsContext.timer("manageTimes", getCategory().toString(), "pool", getType());
+    MetricsMap changeMap = new MetricsMap((detailed, map) -> {
+      changeCounts.forEach((k, v) -> map.put(k.toString(), v.get()));
+    });
+    solrMetricsContext.gauge(changeMap, true, "changes", getCategory().toString(), "pool", getType());
   }
 
   @Override
@@ -97,7 +104,7 @@ public abstract class ResourceManagerPool<T extends ManagedComponent> implements
 
   /** Remove named component from this pool. */
   public boolean unregisterComponent(String componentId) {
-    return components.remove(name) != null;
+    return components.remove(componentId) != null;
   }
 
   /**
@@ -153,6 +160,7 @@ public abstract class ResourceManagerPool<T extends ManagedComponent> implements
 
   public Object setResourceLimit(T component, String limitName, Object value, ChangeListener.Reason reason) throws Exception {
     Object newActualLimit = doSetResourceLimit(component, limitName, value);
+    changeCounts.computeIfAbsent(reason, r -> new AtomicLong()).incrementAndGet();
     for (ChangeListener listener : listeners) {
       listener.changedLimit(getName(), component, limitName, value, newActualLimit, reason);
     }
diff --git a/solr/core/src/java/org/apache/solr/managed/SolrResourceContext.java b/solr/core/src/java/org/apache/solr/managed/SolrResourceContext.java
index 09d86dce..fcf6dce 100644
--- a/solr/core/src/java/org/apache/solr/managed/SolrResourceContext.java
+++ b/solr/core/src/java/org/apache/solr/managed/SolrResourceContext.java
@@ -17,14 +17,21 @@
 package org.apache.solr.managed;
 
 import java.io.Closeable;
+import java.lang.invoke.MethodHandles;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  *
  */
 public class SolrResourceContext implements Closeable {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
   private final ResourceManager resourceManager;
   private final String[] poolNames;
   private final ManagedComponent component;
@@ -53,6 +60,7 @@ public class SolrResourceContext implements Closeable {
 
   @Override
   public void close() {
+    log.info("-- closing managed component " + component.getManagedComponentId() + " in pools " + Arrays.toString(poolNames));
     for (String poolName : poolNames) {
       resourceManager.unregisterComponent(poolName, component.getManagedComponentId());
     }
diff --git a/solr/core/src/java/org/apache/solr/managed/types/CacheManagerPool.java b/solr/core/src/java/org/apache/solr/managed/types/CacheManagerPool.java
index 4e812d1..eb8febf 100644
--- a/solr/core/src/java/org/apache/solr/managed/types/CacheManagerPool.java
+++ b/solr/core/src/java/org/apache/solr/managed/types/CacheManagerPool.java
@@ -21,6 +21,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Function;
 
@@ -124,6 +125,7 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
   @Override
   public boolean unregisterComponent(String componentId) {
     lookups.remove(componentId);
+    hits.remove(componentId);
     initialComponentLimits.remove(componentId);
     return super.unregisterComponent(componentId);
   }
@@ -245,6 +247,9 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
     // changeRatio > 1.0 means there are available free resources
     // changeRatio < 1.0 means there's shortage of resources
     final AtomicReference<Double> changeRatio = new AtomicReference<>(poolLimitValue / totalValue);
+    log.info("-- initial changeRatio=" + changeRatio.get());
+    AtomicBoolean optAdjusted = new AtomicBoolean();
+    AtomicBoolean forceAdjusted = new AtomicBoolean();
 
     // ========================== OPTIMIZATION ==============================
     // if the situation is not critical (ie. total consumption is less than max)
@@ -305,6 +310,7 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
           newTotalValue.getAndUpdate(v -> v - currentLimit + actualNewLimit.doubleValue());
           lookups.put(component.getManagedComponentId().toString(), currentLookups);
           hits.put(component.getManagedComponentId().toString(), currentHits);
+          optAdjusted.set(true);
         } catch (Exception e) {
           log.warn("Failed to set managed limit " + limitName +
               " from " + currentLimit + " to " + newLimit + " on " + component.getManagedComponentId(), e);
@@ -321,12 +327,18 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
           newTotalValue.getAndUpdate(v -> v - currentLimit + actualNewLimit.doubleValue());
           lookups.put(component.getManagedComponentId().toString(), currentLookups);
           hits.put(component.getManagedComponentId().toString(), currentHits);
+          optAdjusted.set(true);
         } catch (Exception e) {
           log.warn("Failed to set managed limit " + limitName +
               " from " + currentLimit + " to " + newLimit + " on " + component.getManagedComponentId(), e);
         }
       }
     });
+    if (optAdjusted.get()) {
+      log.info("-- component limits " + limitName + " optimized, newTotalValue=" + newTotalValue.get());
+    } else {
+      log.info("-- component limits " + limitName + " not optimized");
+    }
 
     // ======================== HARD LIMIT ================
     // now re-calculate the new changeRatio based on possible
@@ -335,11 +347,14 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
 
     // dead band to avoid thrashing
     if (Math.abs(totalDelta / poolLimitValue) < deadBand) {
+      log.info("-- delta " + totalDelta + " within deadband, skipping...");
       return;
     }
 
     changeRatio.set(poolLimitValue / newTotalValue.get());
+    log.info("-- updated changeRatio=" + changeRatio.get());
     if (changeRatio.get() >= 1.0) { // there's no resource shortage
+      log.info("--- no shortage, skipping...");
       return;
     }
     // forcibly trim each resource limit (evenly) to fit within the total pool limit
@@ -349,16 +364,23 @@ public class CacheManagerPool extends ResourceManagerPool<SolrCache> {
       double newLimit = currentLimit * changeRatio.get();
       try {
         Number actualNewLimit = (Number) setResourceLimit(component, limitName, newLimit, ChangeListener.Reason.ABOVE_TOTAL_LIMIT);
+        log.info("-- forcing " + component.getManagedComponentId() + "/" + limitName + ": " + currentLimit + " -> " + actualNewLimit);
         newTotalValue.getAndUpdate(v -> v - currentLimit + actualNewLimit.doubleValue());
         long currentLookups = ((Number)currentValues.get(component.getManagedComponentId().toString()).get(SolrCache.CUMULATIVE_PREFIX + SolrCache.LOOKUPS_PARAM)).longValue();
         long currentHits = ((Number)currentValues.get(component.getManagedComponentId().toString()).get(SolrCache.CUMULATIVE_PREFIX + SolrCache.HITS_PARAM)).longValue();
         lookups.put(component.getManagedComponentId().toString(), currentLookups);
         hits.put(component.getManagedComponentId().toString(), currentHits);
+        forceAdjusted.set(true);
       } catch (Exception e) {
         log.warn("Failed to set managed limit " + limitName +
             " from " + currentLimit + " to " + newLimit + " on " + component.getManagedComponentId(), e);
       }
     });
+    if (forceAdjusted.get()) {
+      log.info("-- component limits " + limitName + " adjusted, new total " + newTotalValue.get());
+    } else {
+      log.info("-- component limits " + limitName + " unchanged, new total " + newTotalValue.get());
+    }
     // check that the adjustments were overall successful
     if (poolLimitValue < newTotalValue.get()) {
       log.warn("Pool {} / {}: unable to force the total {} resource usage {} to fit the total pool limit of {} !",
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java
index c32001f..a2a699e 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java
@@ -222,7 +222,6 @@ public class SolrClientNodeStateProvider implements NodeStateProvider, MapWriter
       
       
       SimpleSolrResponse frsp = rsp;
-      log.info("=== Rsp: " + rsp);
       metricsKeyVsTag.forEach((key, tag) -> {
         Object v = Utils.getObjectByPath(frsp.nl, true, Arrays.asList("metrics", key));
         if (tag instanceof Function) {