Apache Solr open-source search software
SOLR-18474: InternalSolrClientCache separate from SolrClientCache (#4934)
David Smiley54a46689b153
diff --git a/changelog/unreleased/SOLR-18474-internal-solr-client-cache.yml b/changelog/unreleased/SOLR-18474-internal-solr-client-cache.ymlnew file mode 100644index 00000000000..8d64ce5697b--- /dev/null+++ b/changelog/unreleased/SOLR-18474-internal-solr-client-cache.yml@@ -0,0 +1,8 @@+title: Server-side streaming and cross-collection join use an internal SolrClientCache that only connects to other clusters allowed by allowZkHosts or allowUrls.+type: changed+authors:+ - name: David Smiley+ - name: vyatkin.v+links:+ - name: SOLR-18474+ url: https://issues.apache.org/jira/browse/SOLR-18474diff --git a/solr/core/src/java/org/apache/solr/cloud/InternalSolrClientCache.java b/solr/core/src/java/org/apache/solr/cloud/InternalSolrClientCache.javanew file mode 100644index 00000000000..4657c9175e2--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cloud/InternalSolrClientCache.java@@ -0,0 +1,105 @@+/*+ * 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+ *+ * http://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.cloud;++import java.net.MalformedURLException;+import java.net.URISyntaxException;+import java.util.concurrent.TimeUnit;+import java.util.function.Consumer;+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.HttpSolrClient;+import org.apache.solr.client.solrj.io.SolrClientCache;+import org.apache.solr.client.solrj.jetty.CloudJettySolrClient;+import org.apache.solr.client.solrj.jetty.HttpJettySolrClient;+import org.apache.solr.common.SolrException;+import org.apache.solr.common.util.URLUtil;++/**+ * A restricted {@link SolrClientCache} for internal Solr use. Connections to clusters other than+ * the local one must pass a validator; see {@link ZkController#validateSolrConnection}.+ */+public class InternalSolrClientCache extends SolrClientCache {++ private final CloudSolrClient.CloudSolrClientConnection defaultConnection;+ private final Consumer<CloudSolrClient.CloudSolrClientConnection> connectionValidator;++ public InternalSolrClientCache(+ HttpJettySolrClient httpSolrClient,+ CloudSolrClient.CloudSolrClientConnection solrConnection,+ Consumer<CloudSolrClient.CloudSolrClientConnection> connectionValidator) {+ super(); // not passing httpSolrClient down ...+ this.defaultConnection = solrConnection;+ this.connectionValidator = connectionValidator;+ // ... create one internal CloudSolrClient that is a bit special.+ var httpBuilder =+ new HttpJettySolrClient.Builder()+ .withIdleTimeout(minSocketTimeout, TimeUnit.MILLISECONDS)+ .withHttpClient(httpSolrClient);+ cloudSolClients.put(+ solrConnection,+ new CloudJettySolrClient.Builder(solrConnection)+ .canUseZkACLs(true)+ .withHttpClientBuilder(httpBuilder)+ .build());+ }++ @Override+ protected boolean isInternal() {+ return true;+ }++ @Override+ public synchronized CloudSolrClient getCloudSolrClient(+ CloudSolrClient.CloudSolrClientConnection solrConnection) {+ if (solrConnection == null) {+ solrConnection = defaultConnection;+ }+ CloudSolrClient client = cloudSolClients.get(solrConnection);+ if (client != null) {+ return client;+ }+ connectionValidator.accept(solrConnection); // throws if not allowed+ return super.getCloudSolrClient(solrConnection);+ }++ @Override+ protected HttpSolrClient.BuilderBase<?, ?> newHttpSolrClientBuilder(String url) {+ if (url != null) {+ // override to attempt to use the jetty client inside a matching CloudSolrClient.+ String baseUrl = URLUtil.isBaseUrl(url) ? url : URLUtil.extractBaseUrl(url);+ try {+ String nodeName = URLUtil.getNodeNameForBaseUrl(baseUrl);+ for (CloudSolrClient cloudSolrClient : cloudSolClients.values()) {+ if (cloudSolrClient.getClusterStateProvider().getLiveNodes().contains(nodeName)) {+ final var builder = new HttpJettySolrClient.Builder(baseUrl);+ if (!URLUtil.isBaseUrl(url)) {+ builder.withDefaultCollection(URLUtil.extractCoreFromCoreUrl(url));+ }+ builder.withHttpClient(+ (HttpJettySolrClient) ((CloudHttp2SolrClient) cloudSolrClient).getHttpClient());+ return builder;+ }+ }+ } catch (MalformedURLException | URISyntaxException e) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);+ }+ }+ return super.newHttpSolrClientBuilder(url);+ }+}diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex 6d8310c01f7..657b2728d36 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -32,6 +32,7 @@ import java.io.Closeable;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.lang.reflect.Array;+import java.net.MalformedURLException;import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.Arrays;@@ -66,6 +67,7 @@ import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.impl.SolrClientCloudManager;import org.apache.solr.client.solrj.impl.SolrZkClientTimeout;import org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider;+import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.client.solrj.jetty.CloudJettySolrClient;import org.apache.solr.client.solrj.jetty.HttpJettySolrClient;import org.apache.solr.client.solrj.request.CoreAdminRequest.WaitForState;@@ -124,6 +126,7 @@ import org.apache.solr.core.SolrCoreInitializationException;import org.apache.solr.handler.component.HttpShardHandler;import org.apache.solr.logging.MDCLoggingContext;import org.apache.solr.search.SolrIndexSearcher;+import org.apache.solr.security.AllowListUrlChecker;import org.apache.solr.security.AllowListZkHostChecker;import org.apache.solr.update.UpdateLog;import org.apache.solr.util.AddressUtils;@@ -211,6 +214,8 @@ public class ZkController implements Closeable {private CloudSolrClient cloudSolrClient;+ private volatile InternalSolrClientCache internalSolrClientCache;+private final ExecutorService zkConnectionListenerCallbackExecutor =ExecutorUtil.newMDCAwareSingleThreadExecutor(new SolrNamedThreadFactory("zkConnectionListenerCallback"));@@ -677,6 +682,22 @@ public class ZkController implements Closeable {return getSolrCloudManager().getSolrClient();}+ public SolrClientCache getSolrClientCache() {+ if (internalSolrClientCache == null) {+ synchronized (this) {+ if (internalSolrClientCache == null) {+ var connection = CloudSolrClient.CloudSolrClientConnection.parse(zkServerAddress);+ internalSolrClientCache =+ new InternalSolrClientCache(+ (HttpJettySolrClient) cc.getDefaultHttpSolrClient(),+ connection,+ this::validateSolrConnection);+ }+ }+ }+ return internalSolrClientCache;+ }+public int getLeaderVoteWait() {return leaderVoteWait;}@@ -876,6 +897,7 @@ public class ZkController implements Closeable {customThreadPool.execute(() -> IOUtils.closeQuietly(cloudManager));customThreadPool.execute(() -> IOUtils.closeQuietly(cloudSolrClient));+ customThreadPool.execute(() -> IOUtils.closeQuietly(internalSolrClientCache));try {try {@@ -1022,6 +1044,26 @@ public class ZkController implements Closeable {return allowListZkHostChecker;}+ /**+ * Validates a connection to a SolrCloud cluster: ZooKeeper via {@link+ * #getAllowListZkHostChecker()}, HTTP via {@link AllowListUrlChecker} (live nodes of this cluster+ * are allowed).+ *+ * @throws SolrException FORBIDDEN if not allowed+ */+ public void validateSolrConnection(CloudSolrClient.CloudSolrClientConnection solrConnection) {+ if (solrConnection.isZookeeper()) {+ allowListZkHostChecker.checkAllowList(solrConnection.toString());+ } else {+ try {+ cc.getAllowListUrlChecker().checkAllowList(solrConnection.quorumItems(), getClusterState());+ } catch (MalformedURLException e) {+ throw new SolrException(+ ErrorCode.BAD_REQUEST, "Invalid URL in solrConnection: " + solrConnection, e);+ }+ }+ }+boolean isClosed() {return isClosed;}diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex 9bacc62746a..d5df49756eb 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -40,7 +40,6 @@ import io.opentelemetry.api.trace.Tracer;import jakarta.inject.Singleton;import java.io.IOException;import java.lang.invoke.MethodHandles;-import java.net.MalformedURLException;import java.nio.file.Files;import java.nio.file.Path;import java.text.SimpleDateFormat;@@ -69,7 +68,6 @@ import org.apache.solr.api.ClusterPluginsSource;import org.apache.solr.api.ContainerPluginsRegistry;import org.apache.solr.api.JerseyResource;import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.client.solrj.util.SolrIdentifierValidator;@@ -281,8 +279,6 @@ public class CoreContainer {protected MetricsHandler metricsHandler;- private volatile SolrClientCache solrClientCache;-private volatile Map<String, SolrCache<?, ?>> caches;private final ObjectCache objectCache = new ObjectCache();@@ -711,8 +707,8 @@ public class CoreContainer {*/@Deprecated(since = "10.0")public SolrClientCache getSolrClientCache() {- // TODO put in the objectCache instead- return solrClientCache;+ // TODO put in the objectCache instead?+ return isZooKeeperAware() ? getZkController().getSolrClientCache() : null;}public ObjectCache getObjectCache() {@@ -799,10 +795,6 @@ public class CoreContainer {solrClientProvider =new HttpSolrClientProvider(cfg.getUpdateShardHandlerConfig(), solrMetricsContext);updateShardHandler.initializeMetrics(solrMetricsContext, Attributes.empty());- solrClientCache = new SolrClientCache(solrClientProvider.getSolrClient());- // Validate caller-supplied zkHost/solrConnection (cross-collection join, streaming- // expressions).- solrClientCache.setConnectionValidator(this::validateSolrConnection);Map<String, CacheConfig> cachesConfig = cfg.getCachesConfig();if (cachesConfig.isEmpty()) {@@ -829,7 +821,6 @@ public class CoreContainer {zkSys.initZooKeeper(this, cfg.getCloudConfig());if (isZooKeeperAware()) {- solrClientCache.setDefaultZKHost(getZkController().getZkServerAddress());// initialize ZkClient metricszkSys.getZkMetricsProducer()@@ -1298,9 +1289,6 @@ public class CoreContainer {} catch (Exception e) {log.warn("Error shutting down CoreAdminHandler. Continuing to close CoreContainer.", e);}- if (solrClientCache != null) {- solrClientCache.close();- }if (containerPluginsRegistry != null) {IOUtils.closeQuietly(containerPluginsRegistry);}@@ -1641,38 +1629,6 @@ public class CoreContainer {return allowListUrlChecker;}- /**- * Validates a connection to a SolrCloud cluster: ZooKeeper via {@link- * ZkController#getAllowListZkHostChecker()} (never allowed in standalone mode), HTTP via {@link- * AllowListUrlChecker} (live nodes of this cluster are allowed).- *- * @throws SolrException FORBIDDEN if not allowed- */- private void validateSolrConnection(CloudSolrClient.CloudSolrClientConnection solrConnection) {- ZkController zkController = getZkController();- if (solrConnection.isZookeeper()) {- String zkHost = solrConnection.toString();- if (zkController == null) {- throw new SolrException(- ErrorCode.FORBIDDEN,- "ZooKeeper host '" + zkHost + "' is not allowed when Solr is not in SolrCloud mode.");- }- if (!zkController.getAllowListZkHostChecker().isAllowed(zkHost)) {- throw new SolrException(- ErrorCode.FORBIDDEN, SolrClientCache.zkHostRejectionMessage(zkHost));- }- } else {- try {- allowListUrlChecker.checkAllowList(- solrConnection.quorumItems(),- zkController == null ? null : zkController.getClusterState());- } catch (MalformedURLException e) {- throw new SolrException(- ErrorCode.BAD_REQUEST, "Invalid URL in solrConnection: " + solrConnection, e);- }- }- }-/*** Creates a new core based on a CoreDescriptor.*diff --git a/solr/core/src/java/org/apache/solr/handler/ExportHandler.java b/solr/core/src/java/org/apache/solr/handler/ExportHandler.javaindex 5a8b4bddfac..d488089825d 100644--- a/solr/core/src/java/org/apache/solr/handler/ExportHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ExportHandler.java@@ -99,10 +99,10 @@ public class ExportHandler extends SearchHandler {String defaultCollection;String defaultZkhost;CoreContainer coreContainer = core.getCoreContainer();- this.solrClientCache = coreContainer.getSolrClientCache();this.coreName = core.getName();if (coreContainer.isZooKeeperAware()) {+ this.solrClientCache = coreContainer.getZkController().getSolrClientCache();defaultCollection = core.getCoreDescriptor().getCollectionName();defaultZkhost = core.getCoreContainer().getZkController().getZkServerAddress();var solrConnection = CloudSolrClient.CloudSolrClientConnection.parse(defaultZkhost);diff --git a/solr/core/src/java/org/apache/solr/handler/GraphHandler.java b/solr/core/src/java/org/apache/solr/handler/GraphHandler.javaindex 28fe28d708e..8bec0fcbfea 100644--- a/solr/core/src/java/org/apache/solr/handler/GraphHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/GraphHandler.java@@ -75,9 +75,9 @@ public class GraphHandler extends RequestHandlerBaseString defaultZkhost;CoreContainer coreContainer = core.getCoreContainer();this.coreName = core.getName();- this.solrClientCache = coreContainer.getSolrClientCache();if (coreContainer.isZooKeeperAware()) {+ this.solrClientCache = coreContainer.getZkController().getSolrClientCache();defaultCollection = core.getCoreDescriptor().getCollectionName();defaultZkhost = core.getCoreContainer().getZkController().getZkServerAddress();var solrConnection = CloudSolrClient.CloudSolrClientConnection.parse(defaultZkhost);diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex b62bda76d5f..42319c5fb5b 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -101,7 +101,6 @@ public class StreamHandler extends RequestHandlerBaseString defaultCollection;String defaultZkhost;CoreContainer coreContainer = core.getCoreContainer();- this.solrClientCache = coreContainer.getSolrClientCache();this.coreName = core.getName();String cacheKey = this.getClass().getName() + "_" + coreName + "_";this.objectCache =@@ -110,6 +109,7 @@ public class StreamHandler extends RequestHandlerBase.computeIfAbsent(cacheKey + "objectCache", ConcurrentHashMap.class, k -> new ConcurrentHashMap<>());if (coreContainer.isZooKeeperAware()) {+ this.solrClientCache = coreContainer.getZkController().getSolrClientCache();defaultCollection = core.getCoreDescriptor().getCollectionName();defaultZkhost = core.getCoreContainer().getZkController().getZkServerAddress();var solrConnection = CloudSolrClient.CloudSolrClientConnection.parse(defaultZkhost);diff --git a/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQParser.java b/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQParser.javaindex b336124bb4b..28b08154f57 100644--- a/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQParser.java+++ b/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQParser.java@@ -88,6 +88,10 @@ public class CrossCollectionJoinQParser extends QParser {throw new SyntaxError("zkHost and solrUrl are mutually exclusive; specify at most one of them.");}+ CoreContainer cc = req.getCoreContainer(); // null in some unit tests+ if (cc != null && cc.getZkController() == null) {+ throw new SyntaxError("Cross-collection join requires SolrCloud.");+ }// Test if this is a valid solr url.if (solrUrl != null) {if (allowSolrUrls == null) {@@ -125,10 +129,9 @@ public class CrossCollectionJoinQParser extends QParser {} else if (zkHost != null && !zkHost.isBlank()) {solrConnection = CloudSolrClient.CloudSolrClientConnection.parse(zkHost);}- CoreContainer cc = req.getCoreContainer();if (solrConnection != null && cc != null) { // no CoreContainer -- unit testtry {- cc.getSolrClientCache().validateConnection(solrConnection);+ cc.getZkController().validateSolrConnection(solrConnection);} catch (SolrException e) {throw new SyntaxError(e.getMessage(), e);}diff --git a/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQuery.java b/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQuery.javaindex f1172149262..31832d68830 100644--- a/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQuery.java+++ b/solr/core/src/java/org/apache/solr/search/join/CrossCollectionJoinQuery.java@@ -39,7 +39,6 @@ import org.apache.lucene.search.Weight;import org.apache.lucene.util.BytesRefBuilder;import org.apache.lucene.util.FixedBitSet;import org.apache.solr.client.solrj.impl.CloudSolrClient;-import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.client.solrj.io.Tuple;import org.apache.solr.client.solrj.io.eq.FieldEqualitor;import org.apache.solr.client.solrj.io.stream.CloudSolrStream;@@ -222,8 +221,11 @@ public class CrossCollectionJoinQuery extends Query implements SolrSearcherRequi}}- private TupleStream createCloudSolrStream(SolrClientCache solrClientCache) throws IOException {+ private TupleStream createCloudSolrStream() throws IOException {ZkController zkController = searcher.getCore().getCoreContainer().getZkController();+ if (zkController == null) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SolrCloud is required");+ }CloudSolrClient.CloudSolrClientConnection streamingSolrConnection;if (solrConnection != null) {@@ -244,22 +246,20 @@ public class CrossCollectionJoinQuery extends Query implements SolrSearcherRequiparams.set(CommonParams.WT, CommonParams.JAVABIN);StreamContext streamContext = new StreamContext();- streamContext.setSolrClientCache(solrClientCache);+ streamContext.setSolrClientCache(zkController.getSolrClientCache());streamContext.setRequestParams(new ModifiableSolrParams(otherParams));- if (zkController != null) {- RequestReplicaListTransformerGenerator rltg =- new RequestReplicaListTransformerGenerator(- zkController- .getZkStateReader()- .getClusterProperties()- .getOrDefault(ZkStateReader.DEFAULT_SHARD_PREFERENCES, "")- .toString(),- zkController.getNodeName(),- zkController.getBaseUrl(),- zkController.getHostName(),- zkController.getSysPropsCacher());- streamContext.setRequestReplicaListTransformerGenerator(rltg);- }+ RequestReplicaListTransformerGenerator rltg =+ new RequestReplicaListTransformerGenerator(+ zkController+ .getZkStateReader()+ .getClusterProperties()+ .getOrDefault(ZkStateReader.DEFAULT_SHARD_PREFERENCES, "")+ .toString(),+ zkController.getNodeName(),+ zkController.getBaseUrl(),+ zkController.getHostName(),+ zkController.getSysPropsCacher());+ streamContext.setRequestReplicaListTransformerGenerator(rltg);TupleStream cloudSolrStream =new CloudSolrStream(streamingSolrConnection, collection, "/export", params);@@ -297,14 +297,18 @@ public class CrossCollectionJoinQuery extends Query implements SolrSearcherRequiparams.set("expr", uniqueExpr.toString());params.set(CommonParams.WT, CommonParams.JAVABIN);- return new SolrStream(solrUrl, collection, "/stream", params);+ var solrStream = new SolrStream(solrUrl, collection, "/stream", params);+ StreamContext streamContext = new StreamContext();+ streamContext.setSolrClientCache(+ searcher.getCore().getCoreContainer().getZkController().getSolrClientCache());+ solrStream.setStreamContext(streamContext);+ return solrStream;}private DocSet getDocSet() throws IOException {TupleStream solrStream;if (solrConnection != null || solrUrl == null) {- var solrClientCache = searcher.getCore().getCoreContainer().getSolrClientCache();- solrStream = createCloudSolrStream(solrClientCache);+ solrStream = createCloudSolrStream();} else {solrStream = createSolrStream();}diff --git a/solr/core/src/java/org/apache/solr/security/AllowListZkHostChecker.java b/solr/core/src/java/org/apache/solr/security/AllowListZkHostChecker.javaindex c6ec76f7f4c..5d75b4b6210 100644--- a/solr/core/src/java/org/apache/solr/security/AllowListZkHostChecker.java+++ b/solr/core/src/java/org/apache/solr/security/AllowListZkHostChecker.java@@ -19,6 +19,7 @@ package org.apache.solr.security;import java.util.Collection;import java.util.Set;+import org.apache.solr.common.SolrException;import org.apache.solr.core.NodeConfig;/**@@ -73,4 +74,22 @@ public class AllowListZkHostChecker {}return allowedZkHosts.contains(zkHost);}++ /**+ * Like {@link #isAllowed(String)} but throws if not allowed, as {@link+ * AllowListUrlChecker#checkAllowList} does.+ *+ * @throws SolrException FORBIDDEN if not allowed+ */+ public void checkAllowList(String zkHost) {+ if (!isAllowed(zkHost)) {+ throw new SolrException(+ SolrException.ErrorCode.FORBIDDEN,+ "ZooKeeper host '"+ + zkHost+ + "' is not on the '"+ + ZK_HOST_ALLOW_LIST+ + "' allow-list in solr.xml and does not match the local cluster's ZK ensemble.");+ }+ }}diff --git a/solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/CloudJettySolrClient.java b/solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/CloudJettySolrClient.javaindex 2e00c97c276..f725fd162b8 100644--- a/solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/CloudJettySolrClient.java+++ b/solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/CloudJettySolrClient.java@@ -42,6 +42,10 @@ public class CloudJettySolrClient extends CloudHttp2SolrClient {super(stateProvider);}+ public Builder(CloudSolrClient.CloudSolrClientConnection connection) {+ super(connection);+ }+@Overridepublic CloudJettySolrClient build() {return new CloudJettySolrClient(this);diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.javaindex 89a89ea9eb6..16b9ca767ad 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java@@ -20,16 +20,14 @@ import java.io.Closeable;import java.util.HashMap;import java.util.Map;import java.util.Objects;-import java.util.Optional;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;-import java.util.concurrent.atomic.AtomicReference;-import java.util.function.Consumer;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.impl.SolrHttpConstants;import org.apache.solr.common.AlreadyClosedException;+import org.apache.solr.common.util.ExecutorUtil;import org.apache.solr.common.util.IOUtils;import org.apache.solr.common.util.URLUtil;@@ -39,74 +37,47 @@ public class SolrClientCache implements Closeable {// Set the floor for timeouts to 60 seconds.// Timeouts can be increased by setting the system properties defined below.private static final int MIN_TIMEOUT = 60000;- private static final int minConnTimeout =+ protected static final int minConnTimeout =Math.max(Integer.getInteger(SolrHttpConstants.PROP_CONNECTION_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);- private static final int minSocketTimeout =+ protected static final int minSocketTimeout =Math.max(Integer.getInteger(SolrHttpConstants.PROP_SO_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);- protected String basicAuthCredentials = null; // Only support with the httpJettySolrClient+ private String basicAuthCredentials = null; // Only support with the httpJettySolrClient- private final Map<String, SolrClient> httpSolrClients = new HashMap<>();- private final Map<CloudSolrClient.CloudSolrClientConnection, CloudSolrClient> cloudSolClients =+ protected final Map<String, SolrClient> httpSolrClients = new HashMap<>();+ protected final Map<CloudSolrClient.CloudSolrClientConnection, CloudSolrClient> cloudSolClients =new HashMap<>();- private final HttpSolrClient httpSolrClient;+ protected final HttpSolrClient httpSolrClient;private final AtomicBoolean isClosed = new AtomicBoolean(false);- private final AtomicReference<String> defaultZkHost = new AtomicReference<>();- private volatile Consumer<CloudSolrClient.CloudSolrClientConnection> connectionValidator;public SolrClientCache() {- this.httpSolrClient = null;+ this(null);}public SolrClientCache(HttpSolrClient httpSolrClient) {this.httpSolrClient = httpSolrClient;- }-- public void setBasicAuthCredentials(String basicAuthCredentials) {- this.basicAuthCredentials = basicAuthCredentials;+ checkNotRunningInSolr();}/**- * Installs a connection validator that {@link- * #getCloudSolrClient(CloudSolrClient.CloudSolrClientConnection)} consults on every call, before- * any ZK or HTTP contact. The validator rejects a connection by throwing, typically a {@code 403- * Forbidden} {@link org.apache.solr.common.SolrException}. A {@code null} validator disables- * validation.- *- * <p>The validator is invoked while holding the cache monitor, so it must be non-blocking and- * must not perform I/O.+ * Whether this is Solr's own cache, the only kind permitted inside a Solr server. Only Solr's+ * internal subclass returns true.*/- public void setConnectionValidator(- Consumer<CloudSolrClient.CloudSolrClientConnection> connectionValidator) {- this.connectionValidator = connectionValidator;+ protected boolean isInternal() {+ return false;}- /** Throws if the installed validator rejects the connection. */- public void validateConnection(CloudSolrClient.CloudSolrClientConnection solrConnection) {- var validator = connectionValidator;- if (validator != null) {- validator.accept(solrConnection);+ private void checkNotRunningInSolr() {+ if (ExecutorUtil.isSolrServerThread() && !isInternal()) {+ throw new IllegalStateException(+ "Inside Solr, use InternalSolrClientCache instead of " + this.getClass().getName());}}- /** Message for a rejected ZooKeeper connection string. */- public static String zkHostRejectionMessage(String connectionString) {- return "ZooKeeper host '"- + connectionString- + "' is not on the 'allowZkHosts' allow-list in solr.xml and does not match the local"- + " cluster's ZK ensemble.";- }-- public void setDefaultZKHost(String zkHost) {- if (zkHost != null) {- zkHost = zkHost.split("/")[0];- if (!zkHost.isEmpty()) {- defaultZkHost.set(zkHost);- } else {- defaultZkHost.set(null);- }- }+ @Deprecated(since = "10.1") // instead use the constructor providing a client. Or subclass.+ public void setBasicAuthCredentials(String basicAuthCredentials) {+ this.basicAuthCredentials = basicAuthCredentials;}/**@@ -121,35 +92,12 @@ public class SolrClientCache implements Closeable {public synchronized CloudSolrClient getCloudSolrClient(CloudSolrClient.CloudSolrClientConnection solrConnection) {ensureOpen();- // before the cache lookup, so cached clients are also subject to the validator- validateConnection(solrConnection);- return cloudSolClients.computeIfAbsent(- solrConnection, sc -> newCloudSolrClient(sc, httpSolrClient, useAclForZookeeper(sc)));- }-- // Can only use ZK ACLs if there is a default ZK Host, and the given ZK host contains that- // default.- // Basically the ZK ACLs are assumed to be only used for the default ZK host,- // thus we should only provide the ACLs to that Zookeeper instance.- private boolean useAclForZookeeper(CloudSolrClient.CloudSolrClientConnection solrConnection) {- boolean canUseACLs = false;- if (solrConnection.isZookeeper()) {- String zkHostNoChroot = String.join(",", solrConnection.quorumItems());- canUseACLs =- Optional.ofNullable(defaultZkHost.get()).map(zkHostNoChroot::equals).orElse(false);- }- return canUseACLs;+ return cloudSolClients.computeIfAbsent(solrConnection, this::newCloudSolrClient);}protected CloudSolrClient newCloudSolrClient(- CloudSolrClient.CloudSolrClientConnection cloudClientConnection,- HttpSolrClient httpSolrClient,- boolean canUseACLs) {- var builder = new CloudSolrClient.Builder(cloudClientConnection);- builder.canUseZkACLs(canUseACLs);- // using internal builder to ensure the internal client gets closed- builder = builder.withHttpClientBuilder(newHttpSolrClientBuilder(null, httpSolrClient));- var client = builder.build();+ CloudSolrClient.CloudSolrClientConnection cloudClientConnection) {+ var client = newCloudSolrClientBuilder(cloudClientConnection).build();try {// force a connection now, so a bad cluster fails here rather than on first useclient.getClusterStateProvider().getLiveNodes();@@ -160,24 +108,30 @@ public class SolrClientCache implements Closeable {return client;}+ protected CloudSolrClient.Builder newCloudSolrClientBuilder(+ CloudSolrClient.CloudSolrClientConnection cloudClientConnection) {+ return new CloudSolrClient.Builder(cloudClientConnection)+ .canUseZkACLs(false)+ .withHttpClientBuilder(newHttpSolrClientBuilder(null));+ }+/*** Create (and cache) a SolrClient based around the provided URL*- * @param baseUrl a Solr URL. May either be a "base" URL (i.e. ending in "/solr"), or point to a+ * @param url a Solr URL. May either be a "base" URL (i.e. ending in "/solr"), or point to a* particular collection or core.* @return a SolrClient configured to use the provided URL. The cache retains a reference to the* returned client, and will close it when callers invoke {@link SolrClientCache#close()}*/- public synchronized SolrClient getHttpSolrClient(String baseUrl) {+ public synchronized SolrClient getHttpSolrClient(String url) {ensureOpen();- Objects.requireNonNull(baseUrl, "Url cannot be null!");return httpSolrClients.computeIfAbsent(- baseUrl, url -> newHttpSolrClientBuilder(url, httpSolrClient).build());+ Objects.requireNonNull(url, "Url cannot be null!"),+ url_ -> newHttpSolrClientBuilder(url_).build());}@SuppressWarnings({"unchecked", "rawtypes"})- protected HttpSolrClient.BuilderBase<?, ?> newHttpSolrClientBuilder(- String url, HttpSolrClient httpSolrClient) {+ protected HttpSolrClient.BuilderBase<?, ?> newHttpSolrClientBuilder(String url) {final var builder =(url == null || URLUtil.isBaseUrl(url)) // URL may be null here and set by caller? HttpSolrClient.builder(url)@@ -193,7 +147,7 @@ public class SolrClientCache implements Closeable {}builder.withIdleTimeout(Math.max(minSocketTimeout, builder.getIdleTimeoutMillis()), TimeUnit.MILLISECONDS);- builder.withOptionalBasicAuthCredentials(basicAuthCredentials);+ builder.withOptionalBasicAuthCredentials(basicAuthCredentials); // deprecatedreturn builder;}diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.javaindex 09ae6a0a186..be629c827c7 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java@@ -47,7 +47,8 @@ class ConnectionImpl implements Connection {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());private final String url;- private final SolrClientCache solrClientCache = new SolrClientCache();+ private final SolrClientCache solrClientCache;+ private final boolean closeClientCache;private final CloudSolrClient client;private final Properties properties;private final DatabaseMetaData databaseMetaData;@@ -60,9 +61,12 @@ class ConnectionImpl implements Connection {String url,CloudSolrClient.CloudSolrClientConnection solrConnection,String collection,- Properties properties)+ Properties properties,+ SolrClientCache solrClientCache)throws SQLException {this.url = url;+ this.closeClientCache = solrClientCache == null;+ this.solrClientCache = closeClientCache ? new SolrClientCache() : solrClientCache;this.client = this.solrClientCache.getCloudSolrClient(solrConnection);this.collection = collection;this.properties = properties;@@ -137,7 +141,7 @@ class ConnectionImpl implements Connection {this.connectionStatement.close();}} finally {- if (this.solrClientCache != null) {+ if (closeClientCache) {this.solrClientCache.close();}}diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.javaindex a6f07878937..23a6d6cc372 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java@@ -29,6 +29,7 @@ import java.util.Locale;import java.util.Properties;import java.util.logging.Logger;import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.common.util.SuppressForbidden;/**@@ -48,17 +49,26 @@ public class DriverImpl implements Driver {}}+ /**+ * Property naming a {@link SolrClientCache} for this connection to use instead of creating one+ * (which isn't permitted within Solr). The caller retains ownership.+ */+ public static final String SOLR_CLIENT_CACHE_PROP = "solrClientCache";+@Overridepublic Connection connect(String url, Properties props) throws SQLException {if (!acceptsURL(url)) {return null;}var jdbcConnMetadata = JdbcConnectionMetadata.parse(url, props);+ var properties = jdbcConnMetadata.properties();+ var solrClientCache = (SolrClientCache) properties.remove(SOLR_CLIENT_CACHE_PROP);return new ConnectionImpl(jdbcConnMetadata.originalUrl(),jdbcConnMetadata.solrConnection(),jdbcConnMetadata.collection(),- jdbcConnMetadata.properties());+ properties,+ solrClientCache);}public Connection connect(String url) throws SQLException {diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.javaindex 5f16e7348b2..888a4b3d772 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java@@ -37,6 +37,7 @@ import java.util.concurrent.Callable;import java.util.stream.Collectors;import java.util.stream.Stream;import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.client.solrj.io.Tuple;import org.apache.solr.client.solrj.io.comp.ComparatorOrder;import org.apache.solr.client.solrj.io.comp.FieldComparator;@@ -55,6 +56,7 @@ import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.Slice;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SolrParams;+import org.apache.solr.common.util.IOUtils;import org.apache.solr.common.util.URLUtil;/**@@ -79,6 +81,8 @@ public class CloudSolrStream extends TupleStream implements Expressible {protected transient List<TupleStream> solrStreams;protected transient TreeSet<TupleWrapper> tuples;protected transient StreamContext streamContext;+ // created when the StreamContext has none; shared by the SolrStreams+ private transient SolrClientCache localSolrClientCache;// Used by parallel streamprotected CloudSolrStream() {}@@ -403,6 +407,12 @@ public class CloudSolrStream extends TupleStream implements Expressible {protected void constructStreams() throws IOException {final ModifiableSolrParams mParams = adjustParams(new ModifiableSolrParams(params));mParams.set(DISTRIB, "false"); // We are the aggregator.+ SolrClientCache contextCache =+ streamContext != null ? streamContext.getSolrClientCache() : null;+ if (contextCache == null && localSolrClientCache == null) {+ // create here, not in each SolrStream, since they're opened on pool threads+ localSolrClientCache = new SolrClientCache();+ }try {final Stream<SolrStream> streamOfSolrStream;if (streamContext != null && streamContext.get("shards") != null) {@@ -440,6 +450,9 @@ public class CloudSolrStream extends TupleStream implements Expressible {ss.setDistrib(false);}}+ if (contextCache == null) {+ ss.setClientCache(localSolrClientCache);+ }ss.setFieldMappings(this.fieldMappings);solrStreams.add(ss);});@@ -468,6 +481,8 @@ public class CloudSolrStream extends TupleStream implements Expressible {solrStream.close();}}+ IOUtils.closeQuietly(localSolrClientCache);+ localSolrClientCache = null;}/** Return the stream sort - ie, the order in which records are returned */diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/JDBCStream.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/JDBCStream.javaindex 0252212fbb4..83f2ca9b97f 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/JDBCStream.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/JDBCStream.java@@ -41,6 +41,7 @@ import java.util.Properties;import org.apache.solr.client.solrj.io.Tuple;import org.apache.solr.client.solrj.io.comp.FieldComparator;import org.apache.solr.client.solrj.io.comp.StreamComparator;+import org.apache.solr.client.solrj.io.sql.DriverImpl;import org.apache.solr.client.solrj.io.stream.expr.Explanation;import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;import org.apache.solr.client.solrj.io.stream.expr.Expressible;@@ -319,7 +320,18 @@ public class JDBCStream extends TupleStream implements Expressible {@Overridepublic void open() throws IOException {try {- connection = getDriver().connect(connectionUrl, connectionProperties);+ Properties props = new Properties();+ if (connectionProperties != null) {+ props.putAll(connectionProperties);+ }+ // Solr's own driver must not create a SolrClientCache when we're within Solr+ if (connectionUrl != null+ && connectionUrl.startsWith("jdbc:solr")+ && streamContext != null+ && streamContext.getSolrClientCache() != null) {+ props.put(DriverImpl.SOLR_CLIENT_CACHE_PROP, streamContext.getSolrClientCache());+ }+ connection = getDriver().connect(connectionUrl, props);} catch (SQLException e) {throw new IOException(String.format(Locale.ROOT, "Failed to open JDBC connection to '%s'", connectionUrl), e);diff --git a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/SolrStream.java b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/SolrStream.javaindex 3b5e4b2164a..284e9f381fd 100644--- a/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/SolrStream.java+++ b/solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/SolrStream.java@@ -141,6 +141,11 @@ public class SolrStream extends TupleStream {this.clientCache = context.getSolrClientCache();}+ /** Uses this cache instead of creating one; the caller closes it. */+ void setClientCache(SolrClientCache clientCache) {+ this.clientCache = clientCache;+ }+public void setCredentials(String user, String password) {this.user = user;this.password = password;diff --git a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/SolrClientCacheTest.java b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/SolrClientCacheTest.javaindex 809a6abe363..e86be036a1b 100644--- a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/SolrClientCacheTest.java+++ b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/SolrClientCacheTest.java@@ -64,10 +64,12 @@ public class SolrClientCacheTest extends SolrCloudTestCase {}@Test- public void testZkACLsNotUsedWithDifferentZkHost() {+ public void testCloudClientFailsWhenDirectZooKeeperAccessRequiresACLs() {+ /* Passing ZooKeeper credentials is intentionally disabled by default for security reasons.+ Applications that require direct ZooKeeper-based connections with credentials may override+ newCloudSolrClient() to provide a custom CloudSolrClient configuration.+ */try (SolrClientCache cache = new SolrClientCache()) {- // This ZK Host is fake, thus the ZK ACLs should not be used- cache.setDefaultZKHost("test:2181");expectThrows(SolrException.class,() ->@@ -78,7 +80,7 @@ public class SolrClientCacheTest extends SolrCloudTestCase {}@Test- public void testGetClientWithHttp() {+ public void testCloudClientCanConnectUsingHttpWithoutZooKeeperAccess() {String solrUrl = cluster.getJettySolrRunner(0).getBaseUrl().toString();try (SolrClientCache cache = new SolrClientCache()) {CloudSolrClient cloudSolrClient =@@ -87,27 +89,4 @@ public class SolrClientCacheTest extends SolrCloudTestCase {Assert.assertEquals(1, clusterState.getLiveNodes().size());}}-- @Test- public void testGetClientWithZookeeper() {- String zkConnectionString = zkClient().getZkServerAddress();- try (SolrClientCache cache = new SolrClientCache()) {- cache.setDefaultZKHost(zkClient().getZkServerAddress());- CloudSolrClient cloudSolrClient =- cache.getCloudSolrClient(- CloudSolrClient.CloudSolrClientConnection.parse(zkConnectionString));- ClusterState clusterState = cloudSolrClient.getClusterStateProvider().getClusterState();- Assert.assertEquals(1, clusterState.getLiveNodes().size());- }- }-- @Test- public void testZkACLsUsedWithDifferentChroot() {- try (SolrClientCache cache = new SolrClientCache()) {- // The same ZK Host is used, so the ZK ACLs should still be applied- cache.setDefaultZKHost(zkClient().getZkServerAddress() + "/random/chroot");- cache.getCloudSolrClient(- CloudSolrClient.CloudSolrClientConnection.parse(zkClient().getZkServerAddress()));- }- }}diff --git a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.javaindex 6506e076ba1..1f70c01bd0d 100644--- a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java+++ b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java@@ -32,6 +32,7 @@ import java.util.Properties;import java.util.TreeSet;import org.apache.lucene.tests.util.LuceneTestCase;import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.io.SolrClientCache;import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.cloud.AbstractFullDistribZkTestBase;@@ -117,6 +118,25 @@ public class JdbcTest extends SolrCloudTestCase {zkHost = cluster.getZkServer().getZkAddress();}+ @Test+ public void testSuppliedSolrClientCache() throws Exception {+ // Inside Solr, the driver must use the caller's cache rather than creating one.+ try (SolrClientCache clientCache = new SolrClientCache()) {+ Properties props = new Properties();+ props.put(DriverImpl.SOLR_CLIENT_CACHE_PROP, clientCache);+ try (Connection con =+ DriverManager.getConnection(+ "jdbc:solr://" + zkHost + "?collection=" + COLLECTIONORALIAS, props);+ Statement stmt = con.createStatement();+ ResultSet rs = stmt.executeQuery("select id from " + COLLECTIONORALIAS + " limit 1")) {+ assertTrue(rs.next());+ }+ // the cache is ours; closing the connection must not have closed it+ assertNotNull(+ clientCache.getCloudSolrClient(CloudSolrClient.CloudSolrClientConnection.parse(zkHost)));+ }+ }+@Testpublic void doTest() throws Exception {