You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2020/08/11 06:07:27 UTC

[lucene-solr] 01/04: @493 Fix XPath object sharing, optimize some collection sizes.

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

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

commit 0aa9f56ab6bb2adbae82302f48602c1bf7c4d40c
Author: markrmiller@gmail.com <ma...@gmail.com>
AuthorDate: Mon Aug 10 22:49:23 2020 -0500

    @493 Fix XPath object sharing, optimize some collection sizes.
---
 solr/core/src/java/org/apache/solr/api/ApiBag.java |  3 ++-
 .../src/java/org/apache/solr/core/PluginBag.java   |  7 +++----
 .../org/apache/solr/core/SolrResourceLoader.java   | 22 ++++++++--------------
 .../apache/solr/schema/FieldTypePluginLoader.java  | 11 +++++------
 .../java/org/apache/solr/schema/IndexSchema.java   | 15 +++++++--------
 .../org/apache/solr/schema/ManagedIndexSchema.java |  4 ++--
 .../solr/util/plugin/AbstractPluginLoader.java     | 10 ++++++----
 .../client/solrj/io/stream/expr/StreamFactory.java |  2 +-
 8 files changed, 34 insertions(+), 40 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/api/ApiBag.java b/solr/core/src/java/org/apache/solr/api/ApiBag.java
index 1b3b89c..03c3d39 100644
--- a/solr/core/src/java/org/apache/solr/api/ApiBag.java
+++ b/solr/core/src/java/org/apache/solr/api/ApiBag.java
@@ -144,7 +144,8 @@ public class ApiBag {
   }
 
   static void registerIntrospect(List<String> l, PathTrie<Api> registry, Map<String, String> substitutes, Api introspect) {
-    ArrayList<String> copy = new ArrayList<>(l);
+    ArrayList<String> copy = new ArrayList<>(l.size() + 1);
+    copy.addAll(l);
     copy.add("_introspect");
     registry.insert(copy, substitutes, introspect);
   }
diff --git a/solr/core/src/java/org/apache/solr/core/PluginBag.java b/solr/core/src/java/org/apache/solr/core/PluginBag.java
index 861e41f..34e57d0 100644
--- a/solr/core/src/java/org/apache/solr/core/PluginBag.java
+++ b/solr/core/src/java/org/apache/solr/core/PluginBag.java
@@ -307,8 +307,8 @@ public class PluginBag<T> implements AutoCloseable {
    */
   void init(Map<String, T> defaults, SolrCore solrCore, List<PluginInfo> infos) {
     core = solrCore;
-    List<Runnable> otherPlugins = new ArrayList<>();
-    List<Runnable> reqHandlerPlugins = new ArrayList<>();
+    List<Runnable> otherPlugins = new ArrayList<>(32);
+    List<Runnable> reqHandlerPlugins = new ArrayList<>(32);
 
     for (PluginInfo info : infos) {
       List<Runnable> list;
@@ -331,9 +331,8 @@ public class PluginBag<T> implements AutoCloseable {
     }
     try (ParWork worker = new ParWork(this)) {
       worker.collect(otherPlugins);
-      worker.addCollect("initOtherPlugins");
       worker.collect(reqHandlerPlugins);
-      worker.addCollect("initReqHandlerPlugins");
+      worker.addCollect("plugins");
     }
     if (infos.size() > 0) { // Aggregate logging
       if (log.isDebugEnabled()) {
diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
index c750743..43748c1 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
@@ -87,8 +87,8 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
   private final SystemIdResolver sysIdResolver;
 
   private String name = "";
-  protected URLClassLoader classLoader;
-  protected URLClassLoader resourceClassLoader;
+  protected volatile URLClassLoader classLoader;
+  protected volatile URLClassLoader resourceClassLoader;
   private final Path instanceDir;
 
   private final Set<SolrCoreAware> waitingForCore = ConcurrentHashMap.newKeySet(5000);
@@ -104,7 +104,7 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
   private RestManager.Registry managedResourceRegistry;
 
   /** @see #reloadLuceneSPI() */
-  private boolean needToReloadLuceneSPI = false; // requires synchronization
+  private volatile boolean needToReloadLuceneSPI = false; // requires synchronization
 
   public synchronized RestManager.Registry getManagedResourceRegistry() {
     if (managedResourceRegistry == null) {
@@ -177,7 +177,7 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
    *
    * @param urls    the URLs of files to add
    */
-  synchronized void addToClassLoader(List<URL> urls) {
+  void addToClassLoader(List<URL> urls) {
     URLClassLoader newLoader = addURLsToClassLoader(classLoader, urls);
     URLClassLoader newResourceClassLoader = addURLsToClassLoader(resourceClassLoader, urls);
     if (newLoader == classLoader) {
@@ -334,12 +334,12 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
   public InputStream openResource(String resource) throws IOException {
 
     Path inConfigDir = getInstancePath().resolve("conf").resolve(resource);
-    if (Files.exists(inConfigDir) && Files.isReadable(inConfigDir)) {
+    if (Files.exists(inConfigDir)) {
       return Files.newInputStream(checkPathIsSafe(inConfigDir));
     }
 
     Path inInstanceDir = getInstancePath().resolve(resource);
-    if (Files.exists(inInstanceDir) && Files.isReadable(inInstanceDir)) {
+    if (Files.exists(inInstanceDir)) {
       return Files.newInputStream(checkPathIsSafe(inInstanceDir));
     }
 
@@ -347,12 +347,6 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
     // We need a ClassLoader-compatible (forward-slashes) path here!
     InputStream is = resourceClassLoader.getResourceAsStream(resource.replace(File.separatorChar, '/'));
 
-    // This is a hack just for tests (it is not done in ZKResourceLoader)!
-    // TODO can we nuke this?
-    if (is == null && System.getProperty("jetty.testMode") != null) {
-      is = resourceClassLoader.getResourceAsStream(("conf/" + resource).replace(File.separatorChar, '/'));
-    }
-
     if (is == null) {
       throw new SolrResourceNotFoundException("Can't find resource '" + resource + "' in classpath or '" + instanceDir + "'");
     }
@@ -364,11 +358,11 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {
    */
   public String resourceLocation(String resource) {
     Path inConfigDir = getInstancePath().resolve("conf").resolve(resource);
-    if (Files.exists(inConfigDir) && Files.isReadable(inConfigDir))
+    if (Files.exists(inConfigDir))
       return inConfigDir.toAbsolutePath().normalize().toString();
 
     Path inInstanceDir = getInstancePath().resolve(resource);
-    if (Files.exists(inInstanceDir) && Files.isReadable(inInstanceDir))
+    if (Files.exists(inInstanceDir))
       return inInstanceDir.toAbsolutePath().normalize().toString();
 
     try (InputStream is = resourceClassLoader.getResourceAsStream(resource.replace(File.separatorChar, '/'))) {
diff --git a/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java b/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java
index 4749e6e..3bb9483 100644
--- a/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java
+++ b/solr/core/src/java/org/apache/solr/schema/FieldTypePluginLoader.java
@@ -79,8 +79,7 @@ public final class FieldTypePluginLoader
   protected FieldType create( SolrResourceLoader loader, 
                               String name, 
                               String className,
-                              Node node ) throws Exception {
-    XPath xpath = IndexSchema.getXpath();
+                              Node node, XPath xpath) throws Exception {
 
     FieldType ft = loader.newInstance(className, FieldType.class, "schema.");
     ft.setTypeName(name);
@@ -254,7 +253,7 @@ public final class FieldTypePluginLoader
       ("[schema.xml] analyzer/charFilter", CharFilterFactory.class, false, false) {
 
       @Override
-      protected CharFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
+      protected CharFilterFactory create(SolrResourceLoader loader, String name, String className, Node node, XPath xpath) throws Exception {
         final Map<String,String> params = DOMUtil.toMap(node.getAttributes());
         String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
         params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, CharFilterFactory.class.getSimpleName()).toString());
@@ -304,7 +303,7 @@ public final class FieldTypePluginLoader
       ("[schema.xml] analyzer/tokenizer", TokenizerFactory.class, false, false) {
       
       @Override
-      protected TokenizerFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
+      protected TokenizerFactory create(SolrResourceLoader loader, String name, String className, Node node, XPath xpath) throws Exception {
         final Map<String,String> params = DOMUtil.toMap(node.getAttributes());
         String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
         params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenizerFactory.class.getSimpleName()).toString());
@@ -352,13 +351,13 @@ public final class FieldTypePluginLoader
     // Load the Filters
 
     final ArrayList<TokenFilterFactory> filters 
-      = new ArrayList<>();
+      = new ArrayList<>(64);
 
     AbstractPluginLoader<TokenFilterFactory> filterLoader = 
       new AbstractPluginLoader<TokenFilterFactory>("[schema.xml] analyzer/filter", TokenFilterFactory.class, false, false)
     {
       @Override
-      protected TokenFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
+      protected TokenFilterFactory create(SolrResourceLoader loader, String name, String className, Node node, XPath xpath) throws Exception {
         final Map<String,String> params = DOMUtil.toMap(node.getAttributes());
         String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
         params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenFilterFactory.class.getSimpleName()).toString());
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index 47640c9..74aeac1 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -136,11 +136,11 @@ public class IndexSchema {
   protected final SolrResourceLoader loader;
   protected final Properties substitutableProperties;
 
-  protected Map<String,SchemaField> fields = new HashMap<>();
-  protected Map<String,FieldType> fieldTypes = new HashMap<>();
+  protected Map<String,SchemaField> fields = new HashMap<>(128);
+  protected Map<String,FieldType> fieldTypes = new HashMap<>(64);
 
-  protected List<SchemaField> fieldsWithDefaultValue = new ArrayList<>();
-  protected Collection<SchemaField> requiredFields = new HashSet<>();
+  protected List<SchemaField> fieldsWithDefaultValue = new ArrayList<>(64);
+  protected Collection<SchemaField> requiredFields = new HashSet<>(32);
   protected DynamicField[] dynamicFields = new DynamicField[] {};
 
   public DynamicField[] getDynamicFields() { return dynamicFields; }
@@ -150,9 +150,9 @@ public class IndexSchema {
   private Analyzer indexAnalyzer;
   private Analyzer queryAnalyzer;
 
-  protected List<SchemaAware> schemaAware = new ArrayList<>();
+  protected List<SchemaAware> schemaAware = new ArrayList<>(64);
 
-  protected Map<String, List<CopyField>> copyFieldsMap = new HashMap<>();
+  protected Map<String, List<CopyField>> copyFieldsMap = new HashMap<>(64);
   public Map<String,List<CopyField>> getCopyFieldsMap() { return Collections.unmodifiableMap(copyFieldsMap); }
 
   protected DynamicCopy[] dynamicCopyFields = new DynamicCopy[] {};
@@ -194,8 +194,7 @@ public class IndexSchema {
     this.substitutableProperties = substitutableProperties;
   }
 
-  public static synchronized XPath getXpath() {
-    XmlConfigFile.xpathFactory.newXPath();
+  public static XPath getXpath() {
     XPath xPath = THREAD_LOCAL_XPATH.get();
     if (xPath == null) {
       xPath = XmlConfigFile.xpathFactory.newXPath();
diff --git a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
index 37a6803..9712b10 100644
--- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchema.java
@@ -1293,8 +1293,8 @@ public final class ManagedIndexSchema extends IndexSchema {
     // build the new FieldType using the existing FieldTypePluginLoader framework
     // which expects XML, so we use a JSON to XML adapter to transform the JSON object
     // provided in the request into the XML format supported by the plugin loader
-    Map<String,FieldType> newFieldTypes = new HashMap<>();
-    List<SchemaAware> schemaAwareList = new ArrayList<>();
+    Map<String,FieldType> newFieldTypes = new HashMap<>(64);
+    List<SchemaAware> schemaAwareList = new ArrayList<>(64);
     FieldTypePluginLoader typeLoader = new FieldTypePluginLoader(this, newFieldTypes, schemaAwareList);
     typeLoader.loadSingle(loader, FieldTypeXmlAdapter.toNode(options));
     FieldType ft = newFieldTypes.get(typeName);
diff --git a/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java b/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java
index 36ee2da..e55fee9 100644
--- a/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java
+++ b/solr/core/src/java/org/apache/solr/util/plugin/AbstractPluginLoader.java
@@ -25,6 +25,7 @@ import org.apache.solr.common.ParWork;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrException.ErrorCode;
 import org.apache.solr.core.SolrResourceLoader;
+import org.apache.solr.schema.IndexSchema;
 import org.apache.solr.util.DOMUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,6 +33,7 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 import static org.apache.solr.common.params.CommonParams.NAME;
+import javax.xml.xpath.XPath;
 
 /**
  * An abstract super class that manages standard solr-style plugin configuration.
@@ -87,7 +89,7 @@ public abstract class AbstractPluginLoader<T>
    * @param node - the XML node defining this plugin
    */
   @SuppressWarnings("unchecked")
-  protected T create( SolrResourceLoader loader, String name, String className, Node node ) throws Exception
+  protected T create( SolrResourceLoader loader, String name, String className, Node node, XPath xpath) throws Exception
   {
     return loader.newInstance(className, pluginClassType, getDefaultPackages());
   }
@@ -140,7 +142,7 @@ public abstract class AbstractPluginLoader<T>
   {
     List<PluginInitInfo> info = new ArrayList<>();
     T defaultPlugin = null;
-    
+    XPath xpath = IndexSchema.getXpath();
     if (nodes !=null ) {
       for (int i=0; i<nodes.getLength(); i++) {
         Node node = nodes.item(i);
@@ -155,7 +157,7 @@ public abstract class AbstractPluginLoader<T>
             throw new RuntimeException(type + ": missing mandatory attribute 'class' or 'name'");
           }
 
-          T plugin = create(loader, name, className, node);
+          T plugin = create(loader, name, className, node, xpath);
           if (log.isDebugEnabled()) {
             log.debug("created {}: {}", ((name != null) ? name : ""), plugin.getClass().getName());
           }
@@ -234,7 +236,7 @@ public abstract class AbstractPluginLoader<T>
     try {
       String name = DOMUtil.getAttr(node, NAME, requireName ? type : null);
       String className = DOMUtil.getAttr(node, "class", type);
-      plugin = create(loader, name, className, node);
+      plugin = create(loader, name, className, node, IndexSchema.getXpath());
       if (log.isDebugEnabled()) {
         log.debug("created {}: {}", name, plugin.getClass().getName());
       }
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/expr/StreamFactory.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/expr/StreamFactory.java
index 451add8..588024f 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/expr/StreamFactory.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/expr/StreamFactory.java
@@ -53,7 +53,7 @@ public class StreamFactory implements Serializable {
   
   public StreamFactory(){
     collectionZkHosts = new HashMap<>();
-    functionNames = new HashMap<>();
+    functionNames = new HashMap<>(1200);
   }
 
   public StreamFactory(HashMap<String, Supplier<Class<? extends Expressible>>> functionNames) {