Skip to content

Commit

Permalink
SOLR-14525 For components loaded from packages SolrCoreAware, Resourc…
Browse files Browse the repository at this point in the history
…eLoaderAware are not honored (#1547)
  • Loading branch information
noblepaul authored Jun 2, 2020
1 parent cb7e948 commit e841d76
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 87 deletions.
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ Bug Fixes

* SOLR-14491: Intercepting internode requests in KerberosPlugin when HTTP/2 client is used (Ishan Chattopadhyaya, Moshe Bla)

* SOLR-14525: SolrCoreAware, ResourceLoaderAware should be honored for plugin loaded from packages (noble)


Other Changes
---------------------
Expand Down
72 changes: 41 additions & 31 deletions solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
*/
package org.apache.solr.core;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.google.common.annotations.VisibleForTesting;
import java.io.*;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
Expand All @@ -34,24 +30,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.google.common.annotations.VisibleForTesting;
import org.apache.lucene.analysis.WordlistLoader;
import org.apache.lucene.analysis.util.CharFilterFactory;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.lucene.analysis.util.TokenFilterFactory;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.apache.lucene.analysis.util.*;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.PostingsFormat;
Expand Down Expand Up @@ -538,7 +523,7 @@ public <T> T newInstance(String cName, Class<T> expectedType, String[] subPackag
Class<? extends T> clazz = findClass(cName, expectedType, subPackages);
if (clazz == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Can not find class: " + cName + " in " + classLoader);
"Can not find class: " + cName + " in " + classLoader);
}

T obj = null;
Expand Down Expand Up @@ -566,25 +551,50 @@ public <T> T newInstance(String cName, Class<T> expectedType, String[] subPackag

} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Error instantiating class: '" + clazz.getName() + "'", e);
"Error instantiating class: '" + clazz.getName() + "'", e);
}

if (!live) {
if (obj instanceof SolrCoreAware) {
assertAwareCompatibility(SolrCoreAware.class, obj);
waitingForCore.add((SolrCoreAware) obj);
addToCoreAware(obj);
addToResourceLoaderAware(obj);
addToInfoBeans(obj);
return obj;
}

public <T> void addToInfoBeans(T obj) {
if(!live) {
if (obj instanceof SolrInfoBean) {
//TODO: Assert here?
infoMBeans.add((SolrInfoBean) obj);
}
}
}

public <T> boolean addToResourceLoaderAware(T obj) {
if (!live) {
if (obj instanceof ResourceLoaderAware) {
assertAwareCompatibility(ResourceLoaderAware.class, obj);
waitingForResources.add((ResourceLoaderAware) obj);
}
if (obj instanceof SolrInfoBean) {
//TODO: Assert here?
infoMBeans.add((SolrInfoBean) obj);
}
return true;
} else {
return false;
}
}

return obj;
/** the inform() callback should be invoked on the listener.
* If this is 'live', the callback is not called so currently this returns 'false'
*
*/
public <T> boolean addToCoreAware(T obj) {
if (!live) {
if (obj instanceof SolrCoreAware) {
assertAwareCompatibility(SolrCoreAware.class, obj);
waitingForCore.add((SolrCoreAware) obj);
}
return true;
} else {
return false;
}
}


Expand Down Expand Up @@ -713,7 +723,7 @@ public Path getInstancePath() {
/**
* Utility function to throw an exception if the class is invalid
*/
static void assertAwareCompatibility(Class aware, Object obj) {
public static void assertAwareCompatibility(Class aware, Object obj) {
Class[] valid = awareCompatibility.get(aware);
if (valid == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
Expand Down
41 changes: 30 additions & 11 deletions solr/core/src/java/org/apache/solr/pkg/PackageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.solr.common.MapWriter;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.core.CoreContainer;
Expand Down Expand Up @@ -269,7 +260,7 @@ public void writeMap(EntryWriter ew) throws IOException {
paths.add(coreContainer.getPackageStoreAPI().getPackageStore().getRealpath(file));
}

loader = new SolrResourceLoader(
loader = new PackageResourceLoader(
"PACKAGE_LOADER: " + parent.name() + ":" + version,
paths,
Paths.get(coreContainer.getSolrHome()),
Expand Down Expand Up @@ -301,6 +292,34 @@ public String toString() {
}
}
}
static class PackageResourceLoader extends SolrResourceLoader {

PackageResourceLoader(String name, List<Path> classpath, Path instanceDir, ClassLoader parent) {
super(name, classpath, instanceDir, parent);
}

@Override
public <T> boolean addToCoreAware(T obj) {
//do not do anything
//this class is not aware of a SolrCore and it is totally not tied to
// the lifecycle of SolrCore. So, this returns 'false' & it should be
// taken care of by the caller
return false;
}

@Override
public <T> boolean addToResourceLoaderAware(T obj) {
// do not do anything
// this should be invoked only after the init() is invoked.
// The caller should take care of that
return false;
}

@Override
public <T> void addToInfoBeans(T obj) {
//do not do anything. It should be handled externally
}
}

private static String findBiggest(String lessThan, List<String> sortedList) {
String latest = null;
Expand Down
34 changes: 27 additions & 7 deletions solr/core/src/java/org/apache/solr/pkg/PackagePluginHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

package org.apache.solr.pkg;

import java.io.IOException;
import java.lang.invoke.MethodHandles;

import org.apache.solr.core.PluginBag;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.RequestParams;
import org.apache.solr.core.SolrConfig;
import org.apache.solr.core.SolrCore;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.solr.common.SolrException;
import org.apache.solr.core.*;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -98,7 +97,7 @@ private synchronized void reload(PackageLoader.Package pkg) {

if (pkgVersion != null) {
if (newest == pkgVersion) {
//I'm already using the latest classloder in the package. nothing to do
//I'm already using the latest classloader in the package. nothing to do
return;
}
}
Expand All @@ -117,6 +116,7 @@ protected void initNewInstance(PackageLoader.Package.Version newest) {
Object instance = SolrCore.createInstance(pluginInfo.className,
pluginMeta.clazz, pluginMeta.getCleanTag(), core, newest.getLoader());
PluginBag.initInstance(instance, pluginInfo);
handleAwareCallbacks(newest.getLoader(), instance);
T old = inst;
inst = (T) instance;
if (old instanceof AutoCloseable) {
Expand All @@ -129,4 +129,24 @@ protected void initNewInstance(PackageLoader.Package.Version newest) {
}
}

private void handleAwareCallbacks(SolrResourceLoader loader, Object instance) {
if (instance instanceof SolrCoreAware) {
SolrCoreAware coreAware = (SolrCoreAware) instance;
if (!core.getResourceLoader().addToCoreAware(coreAware)) {
coreAware.inform(core);
}
}
if (instance instanceof ResourceLoaderAware) {
SolrResourceLoader.assertAwareCompatibility(ResourceLoaderAware.class, instance);
try {
((ResourceLoaderAware) instance).inform(loader);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
}
}
if (instance instanceof SolrInfoBean) {
core.getResourceLoader().addToInfoBeans(instance);
}
}

}
Loading

0 comments on commit e841d76

Please sign in to comment.