Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-17304: PKG_VERSIONS not honored when loading the schema plugins (#2471)

Andrey Bozhko18ab94781d35
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index bbac86c4c16..c5f753d93c8 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -142,6 +142,8 @@ Bug Fixes
* SOLR-17247: 'WWW-Authenticate' headers missing in MultiAuthPlugin (Lamine Idjeraoui via Eric Pugh)
+* SOLR-17304: Respect PKG_VERSIONS constraints in params.json when loading the schema plugins (Andrey Bozhko)
+
* SOLR-17118: Simplify CoreContainerProvider initialization and JettySolrRunner. Avoid deadlock/hang. (David Smiley)
Dependency Upgrades
diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
index 4aa5990e0ea..68654851d5b 100644
--- a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
+++ b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
@@ -25,7 +25,6 @@ import java.nio.file.Path;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.cloud.ZooKeeperException;
import org.apache.solr.common.util.Pair;
-import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.core.SolrResourceNotFoundException;
import org.apache.solr.schema.ZkIndexSchemaReader;
@@ -38,7 +37,7 @@ import org.slf4j.LoggerFactory;
public class ZkSolrResourceLoader extends SolrResourceLoader {
private final String configSetZkPath;
- private ZkController zkController;
+ private final ZkController zkController;
private ZkIndexSchemaReader zkIndexSchemaReader;
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@@ -52,7 +51,8 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {
Path instanceDir, String configSet, ClassLoader parent, ZkController zooKeeperController) {
super(instanceDir, parent);
this.zkController = zooKeeperController;
- configSetZkPath = ZkConfigSetService.CONFIGS_ZKNODE + "/" + configSet;
+ this.configSetZkPath = ZkConfigSetService.CONFIGS_ZKNODE + "/" + configSet;
+ setCoreContainer(zooKeeperController.getCoreContainer());
}
public Pair<String, Integer> getZkResourceInfo(String resource) {
@@ -90,7 +90,7 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {
byte[] bytes = zkController.getZkClient().getData(file, null, stat, true);
return new ZkByteArrayInputStream(bytes, file, stat);
} else {
- // Path does not exists. We only retry for session expired exceptions.
+ // Path does not exist. We only retry for session expired exceptions.
break;
}
} catch (KeeperException.SessionExpiredException e) {
@@ -175,9 +175,4 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {
public ZkIndexSchemaReader getZkIndexSchemaReader() {
return zkIndexSchemaReader;
}
-
- @Override
- public CoreContainer getCoreContainer() {
- return zkController.getCoreContainer();
- }
}
diff --git a/solr/core/src/java/org/apache/solr/core/SolrConfig.java b/solr/core/src/java/org/apache/solr/core/SolrConfig.java
index 7fb27347818..21256317926 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrConfig.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrConfig.java
@@ -201,13 +201,11 @@ public class SolrConfig implements MapSerializable {
* true otherwise
* @param substitutableProperties optional properties to substitute into the XML
*/
- @SuppressWarnings("unchecked")
private SolrConfig(
SolrResourceLoader loader,
String name,
boolean isConfigsetTrusted,
- Properties substitutableProperties)
- throws IOException {
+ Properties substitutableProperties) {
this.resourceLoader = loader;
this.resourceName = name;
this.substituteProperties = substitutableProperties;
@@ -390,6 +388,9 @@ public class SolrConfig implements MapSerializable {
solrRequestParsers = new SolrRequestParsers(this);
log.debug("Loaded SolrConfig: {}", name);
+
+ // make resource loader aware of the config early on
+ this.resourceLoader.setSolrConfig(this);
} finally {
ConfigNode.SUBSTITUTES.remove();
}
diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java
index 92a963f6bbe..21cb8f1889c 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java
@@ -1076,17 +1076,17 @@ public class SolrCore implements SolrInfoBean, Closeable {
this.configSet = configSet;
this.coreDescriptor = Objects.requireNonNull(coreDescriptor, "coreDescriptor cannot be null");
this.name = Objects.requireNonNull(coreDescriptor.getName());
- coreProvider = new Provider(coreContainer, getName(), uniqueId);
+ this.coreProvider = new Provider(coreContainer, getName(), uniqueId);
this.solrConfig = configSet.getSolrConfig();
this.resourceLoader = configSet.getSolrConfig().getResourceLoader();
- this.resourceLoader.initCore(this);
+ this.resourceLoader.setSolrCore(this);
IndexSchema schema = configSet.getIndexSchema();
this.configSetProperties = configSet.getProperties();
// Initialize the metrics manager
this.coreMetricManager = initCoreMetricManager(solrConfig);
- solrMetricsContext = coreMetricManager.getSolrMetricsContext();
+ this.solrMetricsContext = coreMetricManager.getSolrMetricsContext();
this.coreMetricManager.loadReporters();
if (updateHandler == null) {
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 898f2801304..e657b2bf714 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
@@ -585,7 +585,7 @@ public class SolrResourceLoader
Class<?> type = assertAwareCompatibility(ResourceLoaderAware.class, aware);
if (schemaResourceLoaderComponents.contains(type)) {
// this is a schema component
- // lets use schema classloader
+ // let's use package-aware schema classloader
return getSchemaLoader().findClass(cname, expectedType);
}
}
@@ -694,16 +694,32 @@ public class SolrResourceLoader
}
}
- void initCore(SolrCore core) {
+ protected final void setSolrConfig(SolrConfig config) {
+ if (this.config != null && this.config != config) {
+ throw new IllegalStateException("SolrConfig instance is already associated with this loader");
+ }
+ this.config = config;
+ }
+
+ protected final void setCoreContainer(CoreContainer coreContainer) {
+ if (this.coreContainer != null && this.coreContainer != coreContainer) {
+ throw new IllegalStateException(
+ "CoreContainer instance is already associated with this loader");
+ }
+ this.coreContainer = coreContainer;
+ }
+
+ protected final void setSolrCore(SolrCore core) {
+ setCoreContainer(core.getCoreContainer());
+ setSolrConfig(core.getSolrConfig());
+
this.coreName = core.getName();
- this.config = core.getSolrConfig();
this.coreId = core.uniqueId;
- this.coreContainer = core.getCoreContainer();
SolrCore.Provider coreProvider = core.coreProvider;
this.coreReloadingClassLoader =
new PackageListeningClassLoader(
- core.getCoreContainer(), this, s -> config.maxPackageVersion(s), null) {
+ core.getCoreContainer(), this, pkg -> config.maxPackageVersion(pkg), null) {
@Override
protected void doReloadAction(Ctx ctx) {
log.info("Core reloading classloader issued reload for: {}/{} ", coreName, coreId);
@@ -716,7 +732,9 @@ public class SolrResourceLoader
/** Tell all {@link SolrCoreAware} instances about the SolrCore */
@Override
public void inform(SolrCore core) {
- if (getSchemaLoader() != null) core.getPackageListeners().addListener(schemaLoader);
+ if (getSchemaLoader() != null) {
+ core.getPackageListeners().addListener(schemaLoader);
+ }
// make a copy to avoid potential deadlock of a callback calling newInstance and trying to
// add something to waitingForCore.
@@ -941,24 +959,25 @@ public class SolrResourceLoader
}
private PackageListeningClassLoader createSchemaLoader() {
- CoreContainer cc = getCoreContainer();
- if (cc == null) {
- // corecontainer not available . can't load from packages
+ if (coreContainer == null || coreContainer.getPackageLoader() == null) {
+ // can't load from packages if core container is not available,
+ // or if Solr is not in SolrCloud mode
return null;
}
+ if (config == null) {
+ throw new IllegalStateException(
+ "cannot create package-aware schema loader - no SolrConfig instance is associated with this loader");
+ }
return new PackageListeningClassLoader(
- cc,
+ coreContainer,
this,
- pkg -> {
- if (getSolrConfig() == null) return null;
- return getSolrConfig().maxPackageVersion(pkg);
- },
+ pkg -> config.maxPackageVersion(pkg),
() -> {
- if (getCoreContainer() == null || config == null || coreName == null || coreId == null)
- return;
- try (SolrCore c = getCoreContainer().getCore(coreName, coreId)) {
- if (c != null) {
- c.fetchLatestSchema();
+ if (coreContainer != null && coreName != null && coreId != null) {
+ try (SolrCore c = coreContainer.getCore(coreName, coreId)) {
+ if (c != null) {
+ c.fetchLatestSchema();
+ }
}
}
});
diff --git a/solr/core/src/java/org/apache/solr/pkg/PackageListeners.java b/solr/core/src/java/org/apache/solr/pkg/PackageListeners.java
index 326a9c96118..7535bb2c7fe 100644
--- a/solr/core/src/java/org/apache/solr/pkg/PackageListeners.java
+++ b/solr/core/src/java/org/apache/solr/pkg/PackageListeners.java
@@ -46,7 +46,7 @@ public class PackageListeners {
// this registry only keeps a weak reference because it does not want to
// cause a memory leak if the listener forgets to unregister itself
- private List<Reference<Listener>> listeners = new CopyOnWriteArrayList<>();
+ private final List<Reference<Listener>> listeners = new CopyOnWriteArrayList<>();
public synchronized void addListener(Listener listener) {
listeners.add(new SoftReference<>(listener));
diff --git a/solr/core/src/java/org/apache/solr/pkg/PackageListeningClassLoader.java b/solr/core/src/java/org/apache/solr/pkg/PackageListeningClassLoader.java
index df598a5c60d..65d46edd5ad 100644
--- a/solr/core/src/java/org/apache/solr/pkg/PackageListeningClassLoader.java
+++ b/solr/core/src/java/org/apache/solr/pkg/PackageListeningClassLoader.java
@@ -45,7 +45,7 @@ public class PackageListeningClassLoader implements SolrClassLoader, PackageList
private Map<String, PackageAPI.PkgVersion> packageVersions = new ConcurrentHashMap<>(1);
private Map<String, String> classNameVsPackageName = new ConcurrentHashMap<>();
- private final Runnable onReload;
+ private final Runnable reloadAction;
/**
* @param fallbackClassLoader The {@link SolrClassLoader} to use if no package is specified
@@ -60,11 +60,13 @@ public class PackageListeningClassLoader implements SolrClassLoader, PackageList
this.coreContainer = coreContainer;
this.fallbackClassLoader = fallbackClassLoader;
this.pkgVersionSupplier = pkgVersionSupplier;
- this.onReload =
+ this.reloadAction =
() -> {
packageVersions = new ConcurrentHashMap<>();
classNameVsPackageName = new ConcurrentHashMap<>();
- onReload.run();
+ if (onReload != null) {
+ onReload.run();
+ }
};
}
@@ -183,7 +185,6 @@ public class PackageListeningClassLoader implements SolrClassLoader, PackageList
}
protected void doReloadAction(Ctx ctx) {
- if (onReload == null) return;
- ctx.runLater(null, onReload);
+ ctx.runLater(null, reloadAction);
}
}
diff --git a/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/params.json b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/params.json
new file mode 100644
index 00000000000..b442da29de6
--- /dev/null
+++ b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/params.json
@@ -0,0 +1,7 @@
+{
+ "params": {
+ "PKG_VERSIONS": {
+ "mypkg": "v1"
+ }
+ }
+}
diff --git a/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/schema.xml b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/schema.xml
new file mode 100644
index 00000000000..7f98ce061d0
--- /dev/null
+++ b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/schema.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<schema name="schema-with-dependency-on-mypkg" version="1.1">
+
+ <uniqueKey>id</uniqueKey>
+
+ <fieldType name="int" class="solr.IntPointField" docValues="false"/>
+ <fieldType name="long" class="solr.LongPointField" docValues="false"/>
+ <fieldType name="string" class="solr.StrField"/>
+
+ <fieldType name="my_text_field" class="mypkg:my.pkg.MyTextField">
+ <analyzer>
+ <tokenizer name="whitespace"/>
+ </analyzer>
+ </fieldType>
+
+ <field name="id" type="string" indexed="true" stored="true"/>
+ <field name="_version_" type="long" indexed="true" stored="true"/>
+
+</schema>
diff --git a/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/solrconfig.xml b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/solrconfig.xml
new file mode 100644
index 00000000000..1c999fdc8a6
--- /dev/null
+++ b/solr/core/src/test-files/solr/configsets/conf-using-mypkg-version-1/conf/solrconfig.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<config>
+
+ <dataDir>${solr.data.dir:}</dataDir>
+
+ <directoryFactory name="DirectoryFactory"
+ class="${solr.directoryFactory:solr.NRTCachingDirectoryFactory}"/>
+
+ <luceneMatchVersion>${tests.luceneMatchVersion:LATEST}</luceneMatchVersion>
+
+ <updateHandler class="solr.DirectUpdateHandler2">
+ <updateLog class="${solr.ulog:solr.UpdateLog}"/>
+ </updateHandler>
+
+ <requestHandler name="/select" class="solr.SearchHandler"/>
+
+</config>
diff --git a/solr/core/src/test/org/apache/solr/core/TestConfLoadPerf.java b/solr/core/src/test/org/apache/solr/core/TestConfLoadPerf.java
index f8c7bc84c41..ee9177b2105 100644
--- a/solr/core/src/test/org/apache/solr/core/TestConfLoadPerf.java
+++ b/solr/core/src/test/org/apache/solr/core/TestConfLoadPerf.java
@@ -61,9 +61,9 @@ public class TestConfLoadPerf extends SolrTestCaseJ4 {
container.solrHome,
container.getResourceLoader().classLoader) {
- @Override
- public CoreContainer getCoreContainer() {
- return container;
+ // instance initializer block
+ {
+ setCoreContainer(container);
}
@Override
diff --git a/solr/core/src/test/org/apache/solr/pkg/PackageStoreSchemaPluginsTest.java b/solr/core/src/test/org/apache/solr/pkg/PackageStoreSchemaPluginsTest.java
new file mode 100644
index 00000000000..0d94bcf1f5a
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/pkg/PackageStoreSchemaPluginsTest.java
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.pkg;
+
+import static org.apache.solr.filestore.TestDistribFileStore.uploadKey;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.Signature;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.V2Request;
+import org.apache.solr.client.solrj.response.SolrResponseBase;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.filestore.FileStoreAPI;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PackageStoreSchemaPluginsTest extends SolrCloudTestCase {
+
+ private static final KeyPair KEY_PAIR;
+
+ static {
+ try {
+ KEY_PAIR = KeyPairGenerator.getInstance("RSA").generateKeyPair();
+ } catch (NoSuchAlgorithmException e) {
+ throw new AssertionError("should not happen", e);
+ }
+ }
+
+ private final Path pluginJarPath = getFile("runtimecode/schema-plugins.jar.bin").toPath();
+ private final Path bogusJarPath = getFile("runtimecode/runtimelibs.jar.bin").toPath();
+
+ private SolrClient client;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ System.setProperty("enable.packages", "true");
+ configureCluster(2)
+ // add a configset where one schema field is of type `my.pkg.MyTextField`
+ // this class is available via schema-plugins.jar.bin
+ .addConfig("conf", configset("conf-using-mypkg-version-1"))
+ .configure();
+
+ client = cluster.getSolrClient();
+ uploadKey(KEY_PAIR.getPublic().getEncoded(), FileStoreAPI.KEYS_DIR + "/pub.der", cluster);
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ client = null;
+ if (cluster != null) {
+ cluster.shutdown();
+ cluster = null;
+ }
+ System.clearProperty("enable.packages");
+ super.tearDown();
+ }
+
+ @Test
+ public void testCreateCollection_withPkgVersionsConstraint() throws Exception {
+ // register schema-plugins.jar.bin as package mypkg (version 1);
+ // this is the jar that has necessary class `my.pkg.MyTextField`,
+ // and we expect our configset to load this jar because of the
+ // constraint in params.json of the configset
+ uploadPluginJar("v1", pluginJarPath);
+ registerPackage("v1");
+
+ // register some other jar as package mypkg (versions 0 and 2);
+ // this jar does NOT include `my.pkg.MyTextField` -
+ // but that shouldn't matter because the configset
+ // does not reference these versions of the package
+ uploadPluginJar("v0", bogusJarPath);
+ registerPackage("v0");
+ uploadPluginJar("v2", bogusJarPath);
+ registerPackage("v2");
+
+ // create a collection that uses configset `conf`
+ // which in turn references package mypkg (version 1)
+ createCollection();
+ cluster.waitForActiveCollection("coll", 1, 1);
+
+ // ...and then reload it to make sure everything is still OK
+ reloadCollection();
+ }
+
+ // utility methods
+
+ private static String signature(byte[] content) throws Exception {
+ Signature signature = Signature.getInstance("SHA1WithRSA");
+
+ signature.initSign(KEY_PAIR.getPrivate());
+ signature.update(content);
+ byte[] result = signature.sign();
+
+ return Base64.getEncoder().encodeToString(result);
+ }
+
+ private void uploadPluginJar(String version, Path jarPath) throws Exception {
+ var pluginRequest =
+ new V2Request.Builder("/cluster/files/my-plugin/plugin-" + version + ".jar")
+ .PUT()
+ .withParams(params("sig", signature(Files.readAllBytes(jarPath))))
+ .withPayload(Files.newInputStream(jarPath))
+ .forceV2(true)
+ .build();
+ processRequest(client, pluginRequest);
+ }
+
+ private void registerPackage(String version) throws Exception {
+ var packageRequest =
+ new V2Request.Builder("/cluster/package")
+ .POST()
+ .forceV2(true)
+ .withPayload(
+ Map.of(
+ "add",
+ Map.of(
+ "package",
+ "mypkg",
+ "version",
+ version,
+ "files",
+ List.of("/my-plugin/plugin-" + version + ".jar"))))
+ .build();
+ processRequest(client, packageRequest);
+ }
+
+ private void createCollection() throws Exception {
+ var createRequest = CollectionAdminRequest.createCollection("coll", "conf", 1, 1);
+ processRequest(client, createRequest);
+ }
+
+ private void reloadCollection() throws Exception {
+ var reloadRequest = CollectionAdminRequest.reloadCollection("coll");
+ processRequest(client, reloadRequest);
+ }
+
+ private static void processRequest(SolrClient client, SolrRequest<?> request) throws Exception {
+ var response = (SolrResponseBase) request.process(client);
+ if (response.getException() != null) {
+ throw response.getException();
+ }
+ assertEquals(0, response.getStatus());
+ }
+}