lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject [lucene-solr] 04/36: Small refactorings.
Date Wed, 18 Dec 2019 16:38:42 GMT
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 2f7bdfed57d643d3cedb8577c251c0614487398d
Author: Andrzej Bialecki <ab@apache.org>
AuthorDate: Mon Jun 24 20:15:06 2019 +0200

    Small refactorings.
---
 .../solr/managed/DefaultResourceManager.java       | 54 ++++-------------
 .../org/apache/solr/managed/ResourceManager.java   | 70 +++++++++++++++++++---
 .../apache/solr/managed/ResourceManagerPool.java   | 30 +++++++---
 3 files changed, 93 insertions(+), 61 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java
index c509e2b..743b1d9 100644
--- a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java
+++ b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java
@@ -2,7 +2,6 @@ package org.apache.solr.managed;
 
 import java.io.IOException;
 import java.lang.invoke.MethodHandles;
-import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Executors;
@@ -15,25 +14,26 @@ import org.apache.solr.common.util.TimeSource;
 import org.apache.solr.core.PluginInfo;
 import org.apache.solr.core.SolrResourceLoader;
 import org.apache.solr.util.DefaultSolrThreadFactory;
-import org.apache.solr.util.SolrPluginUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  *
  */
-public class DefaultResourceManager implements ResourceManager {
+public class DefaultResourceManager extends ResourceManager {
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   public static final String SCHEDULE_DELAY_SECONDS_PARAM = "scheduleDelaySeconds";
   public static final String MAX_NUM_POOLS_PARAM = "maxNumPools";
 
-  public static final int DEFAULT_MAX_POOLS = 20;
+  public static final int DEFAULT_MAX_POOLS = 100;
+  public static final int DEFAULT_SCHEDULE_DELAY_SECONDS = 60;
+
+  protected int maxNumPools = DEFAULT_MAX_POOLS;
+
+  protected Map<String, ResourceManagerPool> resourcePools = new ConcurrentHashMap<>();
 
 
-  private Map<String, ResourceManagerPool> resourcePools = new ConcurrentHashMap<>();
-  private PluginInfo pluginInfo;
-  private int maxNumPools = DEFAULT_MAX_POOLS;
   private TimeSource timeSource;
 
   /**
@@ -52,18 +52,7 @@ public class DefaultResourceManager implements ResourceManager {
     this.timeSource = timeSource;
   }
 
-  @Override
-  public void init(PluginInfo info) {
-    if (info != null) {
-      this.pluginInfo = info.copy();
-      if (pluginInfo.initArgs != null) {
-        SolrPluginUtils.invokeSetters(this, this.pluginInfo.initArgs);
-      }
-    }
-    if (!enabled) {
-      log.debug("Resource manager " + getClass().getSimpleName() + " disabled.");
-      return;
-    }
+  protected void doInit() throws Exception {
     scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(maxNumPools,
         new DefaultSolrThreadFactory(getClass().getSimpleName()));
     scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true);
@@ -81,13 +70,6 @@ public class DefaultResourceManager implements ResourceManager {
   }
 
   @Override
-  public void setEnabled(Boolean enabled) {
-    if (enabled != null) {
-      this.enabled = enabled;
-    }
-  }
-
-  @Override
   public PluginInfo getPluginInfo() {
     return pluginInfo;
   }
@@ -101,8 +83,8 @@ public class DefaultResourceManager implements ResourceManager {
     if (resourcePools.size() >= maxNumPools) {
       throw new IllegalArgumentException("Maximum number of pools (" + maxNumPools + ") reached.");
     }
-    ResourceManagerPool newPool = new ResourceManagerPool(resourceManagerPluginFactory, type,
limits, params);
-    newPool.scheduleDelaySeconds = Integer.parseInt(String.valueOf(params.getOrDefault(SCHEDULE_DELAY_SECONDS_PARAM,
10)));
+    ResourceManagerPool newPool = new ResourceManagerPool(name, type, resourceManagerPluginFactory,
limits, params);
+    newPool.scheduleDelaySeconds = Integer.parseInt(String.valueOf(params.getOrDefault(SCHEDULE_DELAY_SECONDS_PARAM,
DEFAULT_SCHEDULE_DELAY_SECONDS)));
     resourcePools.putIfAbsent(name, newPool);
     newPool.scheduledFuture = scheduledThreadPoolExecutor.scheduleWithFixedDelay(newPool,
0,
         timeSource.convertDelay(TimeUnit.SECONDS, newPool.scheduleDelaySeconds, TimeUnit.MILLISECONDS),
@@ -126,17 +108,7 @@ public class DefaultResourceManager implements ResourceManager {
     if (pool == null) {
       throw new IllegalArgumentException("Pool '" + name + "' doesn't exist.");
     }
-    if (pool.scheduledFuture != null) {
-      pool.scheduledFuture.cancel(true);
-    }
-  }
-
-  @Override
-  public void addResources(String name, Collection<ManagedResource> managedResource)
{
-    ensureNotClosed();
-    for (ManagedResource resource : managedResource) {
-      addResource(name, resource);
-    }
+    IOUtils.closeQuietly(pool);
   }
 
   @Override
@@ -166,8 +138,4 @@ public class DefaultResourceManager implements ResourceManager {
     log.debug("Closed.");
   }
 
-  @Override
-  public boolean isClosed() {
-    return isClosed;
-  }
 }
diff --git a/solr/core/src/java/org/apache/solr/managed/ResourceManager.java b/solr/core/src/java/org/apache/solr/managed/ResourceManager.java
index a2e3fe3..e808bdb 100644
--- a/solr/core/src/java/org/apache/solr/managed/ResourceManager.java
+++ b/solr/core/src/java/org/apache/solr/managed/ResourceManager.java
@@ -1,39 +1,91 @@
 package org.apache.solr.managed;
 
+import java.lang.invoke.MethodHandles;
 import java.util.Collection;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 
 import org.apache.solr.common.SolrCloseable;
+import org.apache.solr.common.util.IOUtils;
 import org.apache.solr.core.PluginInfo;
+import org.apache.solr.util.DefaultSolrThreadFactory;
+import org.apache.solr.util.SolrPluginUtils;
 import org.apache.solr.util.plugin.PluginInfoInitialized;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  *
  */
-public interface ResourceManager extends SolrCloseable, PluginInfoInitialized {
+public abstract class ResourceManager implements SolrCloseable, PluginInfoInitialized {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-  void setEnabled(Boolean enabled);
+  protected PluginInfo pluginInfo;
+  protected boolean isClosed = false;
+  protected boolean enabled = true;
 
-  PluginInfo getPluginInfo();
+  @Override
+  public void init(PluginInfo info) {
+    if (info != null) {
+      this.pluginInfo = info.copy();
+      if (pluginInfo.initArgs != null) {
+        SolrPluginUtils.invokeSetters(this, this.pluginInfo.initArgs);
+      }
+    }
+    if (!enabled) {
+      log.debug("Resource manager " + getClass().getSimpleName() + " disabled.");
+      return;
+    }
+    try {
+      doInit();
+    } catch (Exception e) {
+      log.warn("Exception initializing resource manager " + getClass().getSimpleName() +
", disabling!");
+      IOUtils.closeQuietly(this);
+    }
+  }
+
+  /**
+   * Enable resource management, defaults to true. {@link #init(PluginInfo)} checks
+   * this flag before calling {@link #doInit()}.
+   * @param enabled - whether or not resource management is to be enabled
+   */
+  public void setEnabled(Boolean enabled) {
+    if (enabled != null) {
+      this.enabled = enabled;
+    }
+  }
+
+  public PluginInfo getPluginInfo() {
+    return pluginInfo;
+  }
 
-  void createPool(String name, String type, Map<String, Float> limits, Map<String,
Object> params) throws Exception;
+  protected abstract void doInit() throws Exception;
 
-  void modifyPoolLimits(String name, Map<String, Float> limits) throws Exception;
+  public abstract void createPool(String name, String type, Map<String, Float> limits,
Map<String, Object> params) throws Exception;
 
-  void removePool(String name) throws Exception;
+  public abstract void modifyPoolLimits(String name, Map<String, Float> limits) throws
Exception;
 
-  default void addResources(String pool, Collection<ManagedResource> managedResource)
{
+  public abstract void removePool(String name) throws Exception;
+
+  public void addResources(String pool, Collection<ManagedResource> managedResource)
{
     ensureNotClosed();
     for (ManagedResource resource : managedResource) {
       addResource(pool, resource);
     }
   }
 
-  void addResource(String pool, ManagedResource managedResource);
+  public abstract void addResource(String pool, ManagedResource managedResource);
 
-  default void ensureNotClosed() {
+  protected void ensureNotClosed() {
     if (isClosed()) {
       throw new IllegalStateException("Already closed.");
     }
   }
+
+  @Override
+  public synchronized boolean isClosed() {
+    return isClosed;
+  }
 }
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 88b4c06..952dbb5 100644
--- a/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java
+++ b/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java
@@ -2,6 +2,7 @@ package org.apache.solr.managed;
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.lang.invoke.MethodHandles;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -9,36 +10,47 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ScheduledFuture;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  *
  */
 public class ResourceManagerPool implements Runnable, Closeable {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
   private final Map<String, ManagedResource> resources = new ConcurrentHashMap<>();
   private Map<String, Float> limits;
   private final String type;
+  private final String name;
   private final ResourceManagerPlugin resourceManagerPlugin;
   private final Map<String, Object> params;
-  private Map<String, Map<String, Float>> currentValues = null;
   private Map<String, Float> totalValues = null;
   int scheduleDelaySeconds;
   ScheduledFuture<?> scheduledFuture;
 
-  public ResourceManagerPool(ResourceManagerPluginFactory factory, String type, Map<String,
Float> limits, Map<String, Object> params) throws Exception {
+  public ResourceManagerPool(String name, String type, ResourceManagerPluginFactory factory,
Map<String, Float> limits, Map<String, Object> params) throws Exception {
+    this.name = name;
     this.type = type;
     this.resourceManagerPlugin = factory.create(type, params);
     this.limits = new HashMap<>(limits);
     this.params = new HashMap<>(params);
   }
 
-  public synchronized void addResource(ManagedResource managedResource) {
-    if (resources.containsKey(managedResource.getName())) {
-      throw new IllegalArgumentException("Pool already has resource '" + managedResource.getName()
+ "'.");
-    }
+  public String getName() {
+    return name;
+  }
+
+  public void addResource(ManagedResource managedResource) {
     Collection<String> types = managedResource.getManagedResourceTypes();
     if (!types.contains(type)) {
-      throw new IllegalArgumentException("Pool type '" + type + "' is not supported by the
resource " + managedResource.getName());
+      log.debug("Pool type '" + type + "' is not supported by the resource " + managedResource.getName());
+      return;
+    }
+    ManagedResource existing = resources.putIfAbsent(managedResource.getName(), managedResource);
+    if (existing != null) {
+      throw new IllegalArgumentException("Resource '" + managedResource.getName() + "' already
exists in pool '" + name + "' !");
     }
-    resources.put(managedResource.getName(), managedResource);
   }
 
   public Map<String, ManagedResource> getResources() {
@@ -47,7 +59,7 @@ public class ResourceManagerPool implements Runnable, Closeable {
 
   public Map<String, Map<String, Float>> getCurrentValues() {
     // collect current values
-    currentValues = new HashMap<>();
+    Map<String, Map<String, Float>> currentValues = new HashMap<>();
     for (ManagedResource resource : resources.values()) {
       currentValues.put(resource.getName(), resource.getManagedValues(resourceManagerPlugin.getMonitoredTags()));
     }


Mime
View raw message