Apache Solr open-source search software
SOLR-16692: Let users set the CookieStore implementation in Http2SolrClient (#1445)
Tomas Eduardo Fernandez Lobbecc35c1e3bc46
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 18563ed053e..d00b183392f 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -59,6 +59,11 @@ Optimizations* SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (Tomás Fernández Löbbe, Alex Deparvu)+* SOLR-16692: Allow setting the a CookieStore implementation in Http2SolrClient. When Cookies are not needed, this can be set to+ `org.eclipse.jetty.util.HttpCookieStore.Empty`, which avoids some of the processing that happens in the default `java.net.InMemoryCookieStore`.+ Solr can also be started with the system property `solr.http.disableCookies` set to `true`, so that the internal clients+ that Solr uses to communicate between nodes use `HttpCookieStore.Empty`. (Tomás Fernández Löbbe)+Other Changes---------------------diff --git a/solr/bin/solr.in.sh b/solr/bin/solr.in.shindex 84d85981c69..9a87144a0bc 100644--- a/solr/bin/solr.in.sh+++ b/solr/bin/solr.in.sh@@ -284,4 +284,8 @@# Configure the default replica placement plugin to use if one is not configured in cluster properties# See https://solr.apache.org/guide/solr/latest/configuration-guide/replica-placement-plugins.html for details-#SOLR_PLACEMENTPLUGIN_DEFAULT=simple\ No newline at end of file+#SOLR_PLACEMENTPLUGIN_DEFAULT=simple++# Solr internally doesn't use cookies other than for modules such as Kerberos/Hadoop Auth. If you don't need any of those+# And you don't need them for an external system (such as a load balancer), you can disable the use of a CookieStore with:+# SOLR_OPTS="$SOLR_OPTS -Dsolr.http.disableCookies=true"\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.javaindex 7101c041949..1c2fe209ef0 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java@@ -27,6 +27,7 @@ import java.io.InputStream;import java.lang.invoke.MethodHandles;import java.lang.reflect.InvocationTargetException;import java.net.ConnectException;+import java.net.CookieStore;import java.net.MalformedURLException;import java.net.URL;import java.nio.ByteBuffer;@@ -101,6 +102,7 @@ import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;import org.eclipse.jetty.io.ClientConnector;import org.eclipse.jetty.util.BlockingArrayQueue;import org.eclipse.jetty.util.Fields;+import org.eclipse.jetty.util.HttpCookieStore;import org.eclipse.jetty.util.ssl.SslContextFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -272,6 +274,10 @@ public class Http2SolrClient extends SolrClient {httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, AGENT));httpClient.setIdleTimeout(idleTimeoutMillis);+ if (builder.cookieStore != null) {+ httpClient.setCookieStore(builder.cookieStore);+ }+this.authenticationStore = new AuthenticationStoreHolder();httpClient.setAuthenticationStore(this.authenticationStore);@@ -1037,6 +1043,7 @@ public class Http2SolrClient extends SolrClient {protected RequestWriter requestWriter;protected ResponseParser responseParser;private Set<String> urlParamNames = Set.of();+ private CookieStore cookieStore = getDefaultCookieStore();public Builder() {}@@ -1080,6 +1087,17 @@ public class Http2SolrClient extends SolrClient {}}+ private static CookieStore getDefaultCookieStore() {+ if (Boolean.getBoolean("solr.http.disableCookies")) {+ return new HttpCookieStore.Empty();+ }+ /*+ * We could potentially have a Supplier<CookieStore> if we ever need further customization support,+ * but for now it's only either Empty or default (in-memory).+ */+ return null;+ }+/** Reuse {@code httpClient} connections pool */public Builder withHttpClient(Http2SolrClient httpClient) {this.http2SolrClient = httpClient;@@ -1214,6 +1232,17 @@ public class Http2SolrClient extends SolrClient {this.requestTimeoutMillis = TimeUnit.MILLISECONDS.convert(requestTimeout, unit);return this;}++ /**+ * Set a cookieStore other than the default ({@code java.net.InMemoryCookieStore})+ *+ * @param cookieStore The CookieStore to set. {@code null} will set the default.+ * @return this Builder+ */+ public Builder withCookieStore(CookieStore cookieStore) {+ this.cookieStore = cookieStore;+ return this;+ }}/**diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.javaindex b56ece570e9..c52297677a3 100644--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java@@ -302,6 +302,7 @@ public abstract class SolrTestCaseJ4 extends SolrTestCase {System.setProperty("solr.clustering.enabled", "false");System.setProperty("solr.cloud.wait-for-updates-with-stale-state-pause", "500");System.setProperty("solr.filterCache.async", String.valueOf(random().nextBoolean()));+ System.setProperty("solr.http.disableCookies", Boolean.toString(rarely()));System.setProperty(ZK_WHITELIST_PROPERTY, "*");startTrackingSearchers();