Apache Lucene open-source search software
SOLR-14525 For components loaded from packages SolrCoreAware, ResourceLoaderAware are not honored (#1547)
Noble Paule841d7625cc9
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6040f64d833..296d7b5cb75 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -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---------------------diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.javaindex afe07cd01e5..9678d689709 100644--- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java+++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java@@ -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;@@ -34,24 +30,13 @@ import java.nio.file.DirectoryStream;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;@@ -538,7 +523,7 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {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;@@ -566,25 +551,50 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {} 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;+ }}@@ -713,7 +723,7 @@ public class SolrResourceLoader implements ResourceLoader, Closeable {/*** 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,diff --git a/solr/core/src/java/org/apache/solr/pkg/PackageLoader.java b/solr/core/src/java/org/apache/solr/pkg/PackageLoader.javaindex b38628e5d5c..7dcc968c12a 100644--- a/solr/core/src/java/org/apache/solr/pkg/PackageLoader.java+++ b/solr/core/src/java/org/apache/solr/pkg/PackageLoader.java@@ -22,18 +22,9 @@ import java.io.IOException;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;@@ -269,7 +260,7 @@ public class PackageLoader implements Closeable {paths.add(coreContainer.getPackageStoreAPI().getPackageStore().getRealpath(file));}- loader = new SolrResourceLoader(+ loader = new PackageResourceLoader("PACKAGE_LOADER: " + parent.name() + ":" + version,paths,Paths.get(coreContainer.getSolrHome()),@@ -301,6 +292,34 @@ public class PackageLoader implements Closeable {}}}+ 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;diff --git a/solr/core/src/java/org/apache/solr/pkg/PackagePluginHolder.java b/solr/core/src/java/org/apache/solr/pkg/PackagePluginHolder.javaindex cf2382d6aa0..78a16587710 100644--- a/solr/core/src/java/org/apache/solr/pkg/PackagePluginHolder.java+++ b/solr/core/src/java/org/apache/solr/pkg/PackagePluginHolder.java@@ -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;@@ -98,7 +97,7 @@ public class PackagePluginHolder<T> extends PluginBag.PluginHolder<T> {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 doreturn;}}@@ -117,6 +116,7 @@ public class PackagePluginHolder<T> extends PluginBag.PluginHolder<T> {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) {@@ -129,4 +129,24 @@ public class PackagePluginHolder<T> extends PluginBag.PluginHolder<T> {}}+ 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);+ }+ }+}\ No newline at end of filediff --git a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java b/solr/core/src/test/org/apache/solr/pkg/TestPackages.javaindex 55f3c1b79cd..5a5476ea9c9 100644--- a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java+++ b/solr/core/src/test/org/apache/solr/pkg/TestPackages.java@@ -19,12 +19,12 @@ package org.apache.solr.pkg;import java.io.IOException;import java.nio.ByteBuffer;-import java.util.Arrays;-import java.util.Collections;-import java.util.Map;+import java.util.*;import java.util.concurrent.Callable;import org.apache.commons.codec.digest.DigestUtils;+import org.apache.lucene.analysis.util.ResourceLoader;+import org.apache.lucene.analysis.util.ResourceLoaderAware;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrRequest;@@ -40,19 +40,27 @@ import org.apache.solr.client.solrj.request.V2Request;import org.apache.solr.client.solrj.request.beans.Package;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.client.solrj.util.ClientUtils;-import org.apache.solr.cloud.ConfigRequest;import org.apache.solr.cloud.MiniSolrCloudCluster;import org.apache.solr.cloud.SolrCloudTestCase;import org.apache.solr.common.MapWriterMap;import org.apache.solr.common.NavigableObject;import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.common.annotation.JsonProperty;import org.apache.solr.common.params.MapSolrParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SolrParams;+import org.apache.solr.common.util.ReflectMapWriter;import org.apache.solr.common.util.Utils;+import org.apache.solr.core.SolrCore;import org.apache.solr.filestore.PackageStoreAPI;import org.apache.solr.filestore.TestDistribPackageStore;+import org.apache.solr.handler.RequestHandlerBase;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.search.QParser;+import org.apache.solr.search.QParserPlugin;import org.apache.solr.util.LogLevel;+import org.apache.solr.util.plugin.SolrCoreAware;import org.apache.zookeeper.data.Stat;import org.junit.After;import org.junit.Before;@@ -80,7 +88,13 @@ public class TestPackages extends SolrCloudTestCase {public void teardown() {System.clearProperty("enable.packages");}-+ public static class ConfigPlugin implements ReflectMapWriter {+ @JsonProperty+ public String name;++ @JsonProperty("class")+ public String klass;+ }@Testpublic void testPluginLoading() throws Exception {MiniSolrCloudCluster cluster =@@ -98,8 +112,6 @@ public class TestPackages extends SolrCloudTestCase {String COLLECTION_NAME = "testPluginLoadingColl";byte[] derFile = readFile("cryptokeys/pub_key512.der");uploadKey(derFile, PackageStoreAPI.KEYS_DIR+"/pub_key512.der", cluster);-// cluster.getZkClient().makePath("/keys/exe", true);-// cluster.getZkClient().create("/keys/exe/pub_key512.der", derFile, CreateMode.PERSISTENT, true);postFileAndWait(cluster, "runtimecode/runtimelibs.jar.bin", FILE1,"L3q/qIGs4NaF6JiO0ZkMUFa88j0OmYc+I6O7BOdNuMct/xoZ4h73aZHZGc0+nmI1f/U3bOlMPINlSOM6LK3JpQ==");@@ -136,20 +148,39 @@ public class TestPackages extends SolrCloudTestCase {":result:packages:mypkg[0]:version", "1.0",":result:packages:mypkg[0]:files[0]", FILE1));-- String payload = "{\n" +- "'create-requesthandler' : { 'name' : '/runtime', 'class': 'mypkg:org.apache.solr.core.RuntimeLibReqHandler' }," +- "'create-searchcomponent' : { 'name' : 'get', 'class': 'mypkg:org.apache.solr.core.RuntimeLibSearchComponent' }," +- "'create-queryResponseWriter' : { 'name' : 'json1', 'class': 'mypkg:org.apache.solr.core.RuntimeLibResponseWriter' }" +- "'create-updateProcessor' : { 'name' : 'myurp', 'class': 'mypkg:org.apache.solr.update.TestVersionedURP' }," +- " create-expressible: {name: mincopy , class: 'mypkg:org.apache.solr.client.solrj.io.stream.metrics.MinCopyMetric'}" +- "}";- cluster.getSolrClient().request(new ConfigRequest(payload) {- @Override- public String getCollection() {- return COLLECTION_NAME;- }- });+ Map<String,ConfigPlugin> plugins = new LinkedHashMap<>();+ ConfigPlugin p = new ConfigPlugin();+ p.klass = "mypkg:org.apache.solr.core.RuntimeLibReqHandler";+ p.name = "/runtime";+ plugins.put("create-requesthandler", p);++ p = new ConfigPlugin();+ p.klass = "mypkg:org.apache.solr.core.RuntimeLibSearchComponent";+ p.name = "get";+ plugins.put("create-searchcomponent", p);++ p = new ConfigPlugin();+ p.klass = "mypkg:org.apache.solr.core.RuntimeLibResponseWriter";+ p.name = "json1";+ plugins.put("create-queryResponseWriter", p);++ p = new ConfigPlugin();+ p.klass = "mypkg:org.apache.solr.update.TestVersionedURP";+ p.name = "myurp";+ plugins.put("create-updateProcessor", p);++ p = new ConfigPlugin();+ p.klass = "mypkg:org.apache.solr.client.solrj.io.stream.metrics.MinCopyMetric";+ p.name = "mincopy";+ plugins.put("create-expressible", p);+++ V2Request v2r = new V2Request.Builder( "/c/"+COLLECTION_NAME+ "/config")+ .withMethod(SolrRequest.METHOD.POST)+ .withPayload(plugins)+ .forceV2(true)+ .build();+ cluster.getSolrClient().request(v2r);verifyCmponent(cluster.getSolrClient(),COLLECTION_NAME, "queryResponseWriter", "json1",@@ -373,6 +404,27 @@ public class TestPackages extends SolrCloudTestCase {verifyCmponent(cluster.getSolrClient(),COLLECTION_NAME, "requestHandler", "/runtime","mypkg", "2.1" );++ plugins.clear();+ p = new ConfigPlugin();+ p.name = "/rt_2";+ p.klass = "mypkg:"+ C.class.getName();+ plugins.put("create-requesthandler", p);++ p = new ConfigPlugin();+ p.name = "qp1";+ p.klass = "mypkg:"+ C2.class.getName();+ plugins.put("create-queryparser", p);++ v2r = new V2Request.Builder( "/c/"+COLLECTION_NAME+ "/config")+ .withMethod(SolrRequest.METHOD.POST)+ .withPayload(plugins)+ .forceV2(true)+ .build();+ cluster.getSolrClient().request(v2r);+ assertTrue(C.informCalled);+ assertTrue(C2.informCalled);+//we create a new node. This node does not have the packages. But it should download it from another nodeJettySolrRunner jetty = cluster.startJettySolrRunner();//create a new replica for this collection. it should end up@@ -390,21 +442,6 @@ public class TestPackages extends SolrCloudTestCase {}}- /* new V2Request.Builder("/c/"+COLLECTIONORALIAS+"/config").withMethod(SolrRequest.METHOD.POST)- .withPayload("{add-expressible: {name: mincopy , class: org.apache.solr.client.solrj.io.stream.metrics.MinCopyMetric}}")- .build().process(cluster.getSolrClient());-- ModifiableSolrParams _params = new ModifiableSolrParams();- QueryRequest query = new QueryRequest(new MapSolrParams("action","plugins", "collection", COLLECTIONORALIAS, "wt", "javabin"));- query.setPath("/stream");- NamedList<Object> rsp = cluster.getSolrClient().request(query);- assertEquals("org.apache.solr.client.solrj.io.stream.metrics.MinCopyMetric", rsp._getStr("/plugins/mincopy", null));- _params = new ModifiableSolrParams();- query = new QueryRequest(new MapSolrParams("componentName","mincopy", "meta" ,"true", "collection", COLLECTIONORALIAS, "wt", "javabin"));- query.setPath("/config/expressible");- rsp = cluster.getSolrClient().request(query);-- System.out.println();*/private void executeReq(String uri, JettySolrRunner jetty, Utils.InputStreamConsumer parser, Map expected) throws Exception {try(HttpSolrClient client = (HttpSolrClient) jetty.newClient()){@@ -480,13 +517,11 @@ public class TestPackages extends SolrCloudTestCase {"L3q/qIGs4NaF6JiO0ZkMUFa88j0OmYc+I6O7BOdNuMct/xoZ4h73aZHZGc0+nmI1f/U3bOlMPINlSOM6LK3JpQ==");// with correct signature//after uploading the file, let's delete the keys to see if we get proper error message-// cluster.getZkClient().delete("/keys/exe/pub_key512.der", -1, true);add.files = Arrays.asList(new String[]{FILE2});/*expectError(req, cluster.getSolrClient(), errPath,"ZooKeeper does not have any public keys");*///Now lets' put the keys back-// cluster.getZkClient().create("/keys/exe/pub_key512.der", derFile, CreateMode.PERSISTENT, true);//this time we have a file with proper signature, public keys are in ZK// so the add {} command should succeed@@ -567,6 +602,41 @@ public class TestPackages extends SolrCloudTestCase {cluster.shutdown();}}+ public static class C extends RequestHandlerBase implements SolrCoreAware {+ static boolean informCalled = false;++ @Override+ public void inform(SolrCore core) {+ informCalled = true;++ }++ @Override+ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {++ }++ @Override+ public String getDescription() {+ return "test";+ }+ }++ public static class C2 extends QParserPlugin implements ResourceLoaderAware {+ static boolean informCalled = false;+++ @Override+ public void inform(ResourceLoader loader) throws IOException {+ informCalled = true;++ }++ @Override+ public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {+ return null;+ }+ }static void postFileAndWait(MiniSolrCloudCluster cluster, String fname, String path, String sig) throws Exception {ByteBuffer fileContent = getFileContent(fname);