Apache Solr open-source search software
SOLR-17066: Add 'defaultCollection' to SolrClients (#2066)
Jason Gerlowski0b2326d701f7
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9505b2c6f53..6f8e0477bf9 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -124,6 +124,10 @@ Improvements* SOLR-17063: Do not retain log param references in LogWatcher (Michael Gibney)+* SOLR-17066: SolrClient builders now allow users to specify a "default" collection or core+ using the `withDefaultCollection` method. This is preferable to including the collection+ in the base URL accepted by certain client implementations. (Jason Gerlowski)+Optimizations---------------------* SOLR-17084: LBSolrClient (used by CloudSolrClient) now returns the count of core tracked as not live AKA zombiesdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.javaindex f79bba0a64b..a134e2f001a 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java@@ -52,6 +52,7 @@ public abstract class SolrClient implements Serializable, Closeable {private static final long serialVersionUID = 1L;private DocumentObjectBinder binder;+ protected String defaultCollection;/*** Adds a collection of documents@@ -1215,4 +1216,13 @@ public abstract class SolrClient implements Serializable, Closeable {public SolrRequest.SolrClientContext getContext() {return SolrRequest.SolrClientContext.CLIENT;}++ /**+ * Gets the collection used by default for collection or core-based requests+ *+ * <p>If no value is specified at client-creation time, this method will return null.+ */+ public String getDefaultCollection() {+ return defaultCollection;+ }}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudLegacySolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudLegacySolrClient.javaindex c1bc6811411..3547c652293 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudLegacySolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudLegacySolrClient.java@@ -167,7 +167,6 @@ public class CloudLegacySolrClient extends CloudSolrClient {protected boolean shardLeadersOnly = true;protected boolean directUpdatesToLeadersOnly = false;protected boolean parallelUpdates = true;- protected String defaultCollection;protected long retryExpiryTimeNano =TimeUnit.NANOSECONDS.convert(3, TimeUnit.SECONDS); // 3 seconds or 3 million nanosprotected ClusterStateProvider stateProvider;@@ -343,12 +342,6 @@ public class CloudLegacySolrClient extends CloudSolrClient {return this;}- /** Sets the default collection for request. */- public Builder withDefaultCollection(String collection) {- this.defaultCollection = collection;- return this;- }-/*** Sets the Zk connection timeout*diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.javaindex 58f98c6bbc8..2d08fac6a35 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java@@ -90,7 +90,6 @@ public abstract class CloudSolrClient extends SolrClient {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- protected volatile String defaultCollection;// no of times collection state to be reloaded if stale state error is receivedprivate static final int MAX_STALE_RETRIES =Integer.parseInt(System.getProperty("cloudSolrClientMaxStaleRetries", "5"));@@ -292,11 +291,6 @@ public abstract class CloudSolrClient extends SolrClient {return getLbClient().getRequestWriter();}- /** Gets the default collection for request */- public String getDefaultCollection() {- return defaultCollection;- }-/** Gets whether direct updates are sent in parallel */public boolean isParallelUpdates() {return parallelUpdates;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateHttp2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateHttp2SolrClient.javaindex 69c54ba693a..617e853db52 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateHttp2SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateHttp2SolrClient.java@@ -149,6 +149,7 @@ public class ConcurrentUpdateHttp2SolrClient extends SolrClient {this.runners = new ArrayDeque<>();this.streamDeletes = builder.streamDeletes;this.basePath = builder.baseSolrUrl;+ this.defaultCollection = builder.defaultCollection;this.pollQueueTimeMillis = builder.pollQueueTimeMillis;this.stallTimeMillis = Integer.getInteger("solr.cloud.client.stallTime", 15000);@@ -359,6 +360,7 @@ public class ConcurrentUpdateHttp2SolrClient extends SolrClient {@Overridepublic NamedList<Object> request(final SolrRequest<?> request, String collection)throws SolrServerException, IOException {+ if (collection == null) collection = defaultCollection;if (!(request instanceof UpdateRequest)) {request.setBasePath(basePath);return client.request(request, collection);@@ -697,6 +699,7 @@ public class ConcurrentUpdateHttp2SolrClient extends SolrClient {public static class Builder {protected Http2SolrClient client;protected String baseSolrUrl;+ protected String defaultCollection;protected int queueSize = 10;protected int threadCount;protected ExecutorService executorService;@@ -787,6 +790,12 @@ public class ConcurrentUpdateHttp2SolrClient extends SolrClient {return this;}+ /** Sets a default collection for collection-based requests. */+ public Builder withDefaultCollection(String defaultCollection) {+ this.defaultCollection = defaultCollection;+ return this;+ }+/*** @param pollQueueTime time for an open connection to wait for updates when the queue is empty.*/diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.javaindex 298811c8f39..6bac5c0457b 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java@@ -116,6 +116,7 @@ public class ConcurrentUpdateSolrClient extends SolrClient {this.soTimeout = builder.socketTimeoutMillis;this.pollQueueTimeMillis = builder.pollQueueTime;this.stallTimeMillis = Integer.getInteger("solr.cloud.client.stallTime", 15000);+ this.defaultCollection = builder.defaultCollection;// make sure the stall time is larger than the polling time// to give a chance for the queue to change@@ -475,6 +476,7 @@ public class ConcurrentUpdateSolrClient extends SolrClient {@Overridepublic NamedList<Object> request(final SolrRequest<?> request, String collection)throws SolrServerException, IOException {+ if (collection == null) collection = defaultCollection;if (!(request instanceof UpdateRequest)) {return client.request(request, collection);}diff --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 00e94715cfd..af5164f2611 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@@ -191,6 +191,7 @@ public class Http2SolrClient extends SolrClient {this.parser = builder.responseParser;}updateDefaultMimeTypeForParser();+ this.defaultCollection = builder.defaultCollection;if (builder.requestTimeoutMillis != null) {this.requestTimeoutMillis = builder.requestTimeoutMillis;} else {@@ -554,7 +555,7 @@ public class Http2SolrClient extends SolrClient {@Overridepublic NamedList<Object> request(SolrRequest<?> solrRequest, String collection)throws SolrServerException, IOException {-+ if (collection == null) collection = defaultCollection;String url = getRequestPath(solrRequest, collection);Throwable abortCause = null;Request req = null;@@ -1070,6 +1071,7 @@ public class Http2SolrClient extends SolrClient {private ExecutorService executor;protected RequestWriter requestWriter;protected ResponseParser responseParser;+ protected String defaultCollection;private Set<String> urlParamNames;private CookieStore cookieStore = getDefaultCookieStore();private String proxyHost;@@ -1194,6 +1196,12 @@ public class Http2SolrClient extends SolrClient {return this;}+ /** Sets a default collection for collection-based requests. */+ public Builder withDefaultCollection(String defaultCollection) {+ this.defaultCollection = defaultCollection;+ return this;+ }+public Builder withFollowRedirects(boolean followRedirects) {this.followRedirects = followRedirects;return this;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.javaindex 67f3162f1cb..9d11dbe7768 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java@@ -188,6 +188,7 @@ public class HttpSolrClient extends BaseHttpSolrClient {this.soTimeout = builder.socketTimeoutMillis;this.useMultiPartPost = builder.useMultiPartPost;this.urlParamNames = builder.urlParamNames;+ this.defaultCollection = builder.defaultCollection;}public Set<String> getUrlParamNames() {@@ -242,6 +243,7 @@ public class HttpSolrClient extends BaseHttpSolrClient {public NamedList<Object> request(final SolrRequest<?> request, final ResponseParser processor, String collection)throws SolrServerException, IOException {+ if (collection == null) collection = defaultCollection;HttpRequestBase method = createMethod(request, collection);setBasicAuthHeader(request, method);if (request.getHeaders() != null) {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.javaindex 18b89440568..82650e525b1 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttp2SolrClient.java@@ -23,7 +23,6 @@ import java.net.ConnectException;import java.net.SocketException;import java.net.SocketTimeoutException;import java.util.Arrays;-import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;@@ -86,9 +85,11 @@ import org.slf4j.MDC;public class LBHttp2SolrClient extends LBSolrClient {private final Http2SolrClient solrClient;- private LBHttp2SolrClient(Http2SolrClient solrClient, List<String> baseSolrUrls) {- super(baseSolrUrls);- this.solrClient = solrClient;+ private LBHttp2SolrClient(Builder builder) {+ super(Arrays.asList(builder.baseSolrUrls));+ this.solrClient = builder.http2SolrClient;+ this.aliveCheckIntervalMillis = builder.aliveCheckIntervalMillis;+ this.defaultCollection = builder.defaultCollection;}@Override@@ -258,6 +259,7 @@ public class LBHttp2SolrClient extends LBSolrClient {private final String[] baseSolrUrls;private long aliveCheckIntervalMillis =TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS); // 1 minute between checks+ protected String defaultCollection;public Builder(Http2SolrClient http2Client, String... baseSolrUrls) {this.http2SolrClient = http2Client;@@ -279,11 +281,14 @@ public class LBHttp2SolrClient extends LBSolrClient {return this;}+ /** Sets a default collection for collection-based requests. */+ public LBHttp2SolrClient.Builder withDefaultCollection(String defaultCollection) {+ this.defaultCollection = defaultCollection;+ return this;+ }+public LBHttp2SolrClient build() {- LBHttp2SolrClient solrClient =- new LBHttp2SolrClient(this.http2SolrClient, Arrays.asList(this.baseSolrUrls));- solrClient.aliveCheckIntervalMillis = this.aliveCheckIntervalMillis;- return solrClient;+ return new LBHttp2SolrClient(this);}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.javaindex 1ba16f521f2..923d651afb3 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java@@ -93,6 +93,10 @@ public class LBHttpSolrClient extends LBSolrClient {builder.httpClient == null? constructClient(builder.baseSolrUrls.toArray(new String[0])): builder.httpClient;+ this.defaultCollection = builder.defaultCollection;+ if (httpSolrClientBuilder != null && this.defaultCollection != null) {+ httpSolrClientBuilder.defaultCollection = this.defaultCollection;+ }this.connectionTimeoutMillis = builder.connectionTimeoutMillis;this.soTimeoutMillis = builder.socketTimeoutMillis;this.parser = builder.responseParser;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.javaindex 2723465b739..6acf04aea57 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBSolrClient.java@@ -574,6 +574,7 @@ public abstract class LBSolrClient extends SolrClient {final int maxTries = (numServersToTry == null ? serverList.length : numServersToTry.intValue());int numServersTried = 0;Map<String, ServerWrapper> justFailed = null;+ if (collection == null) collection = defaultCollection;boolean timeAllowedExceeded = false;long timeAllowedNano = getTimeAllowedInNanos(request);diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientBuilder.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientBuilder.javaindex d0c97f8ed22..aee3bf55f23 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientBuilder.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientBuilder.java@@ -41,6 +41,7 @@ public abstract class SolrClientBuilder<B extends SolrClientBuilder<B>> {protected int socketTimeoutMillis = 120000; // 120 secondsprivate boolean socketTimeoutMillisUpdate = false;protected boolean followRedirects = false;+ protected String defaultCollection;protected Set<String> urlParamNames;/** The solution for the unchecked cast warning. */@@ -97,6 +98,11 @@ public abstract class SolrClientBuilder<B extends SolrClientBuilder<B>> {return getThis();}+ public B withDefaultCollection(String defaultCollection) {+ this.defaultCollection = defaultCollection;+ return getThis();+ }+/*** Tells {@link Builder} that created clients should obey the following timeout when connecting to* Solr servers.diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientBuilderTest.javaindex 6cf26e440b6..b776755a2ed 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientBuilderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientBuilderTest.java@@ -161,4 +161,15 @@ public class CloudHttp2SolrClientBuilderTest extends SolrTestCase {// it's external, should be NOT closed when closing CloudSolrClientverify(http2Client, never()).close();}++ @Test+ public void testDefaultCollectionPassedFromBuilderToClient() throws IOException {+ try (CloudHttp2SolrClient createdClient =+ new CloudHttp2SolrClient.Builder(+ Collections.singletonList(ANY_ZK_HOST), Optional.of(ANY_CHROOT))+ .withDefaultCollection("aCollection")+ .build()) {+ assertEquals("aCollection", createdClient.getDefaultCollection());+ }+ }}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.javaindex b96d5762c5d..348e8e1d65e 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java@@ -101,4 +101,14 @@ public class CloudSolrClientBuilderTest extends SolrTestCase {assertNotNull(createdClient);}}++ @Test+ public void testDefaultCollectionPassedFromBuilderToClient() throws IOException {+ try (CloudSolrClient createdClient =+ new Builder(Collections.singletonList(ANY_ZK_HOST), Optional.of(ANY_CHROOT))+ .withDefaultCollection("aCollection")+ .build()) {+ assertEquals("aCollection", createdClient.getDefaultCollection());+ }+ }}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClientBuilderTest.javaindex 902aeb61842..c6cd4779f96 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClientBuilderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClientBuilderTest.java@@ -23,6 +23,7 @@ import java.net.ServerSocket;import java.net.SocketTimeoutException;import java.util.concurrent.TimeUnit;import org.apache.solr.SolrTestCase;+import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient.Builder;import org.junit.Test;@@ -71,4 +72,14 @@ public class ConcurrentUpdateSolrClientBuilderTest extends SolrTestCase {// else test passses}}++ @Test+ public void testDefaultCollectionPassedFromBuilderToClient() throws IOException {+ try (SolrClient createdClient =+ new ConcurrentUpdateSolrClient.Builder("someurl")+ .withDefaultCollection("aCollection")+ .build()) {+ assertEquals("aCollection", createdClient.getDefaultCollection());+ }+ }}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpSolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpSolrClientBuilderTest.javaindex eea4fcfc3b8..6aba55d47bf 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpSolrClientBuilderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/HttpSolrClientBuilderTest.java@@ -22,6 +22,7 @@ import org.apache.http.client.HttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.solr.SolrTestCase;import org.apache.solr.client.solrj.ResponseParser;+import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.impl.HttpSolrClient.Builder;import org.apache.solr.common.params.ModifiableSolrParams;import org.junit.Test;@@ -83,4 +84,12 @@ public class HttpSolrClientBuilderTest extends SolrTestCase {assertTrue(usedParser instanceof BinaryResponseParser);}}++ @Test+ public void testDefaultCollectionPassedFromBuilderToClient() throws IOException {+ try (final SolrClient createdClient =+ new Builder(ANY_BASE_SOLR_URL).withDefaultCollection("aCollection").build()) {+ assertEquals("aCollection", createdClient.getDefaultCollection());+ }+ }}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttpSolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttpSolrClientBuilderTest.javaindex 25a70f5460c..6db869cf96b 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttpSolrClientBuilderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttpSolrClientBuilderTest.java@@ -77,4 +77,15 @@ public class LBHttpSolrClientBuilderTest extends SolrTestCase {}HttpClientUtil.close(httpClient);}++ @Test+ public void testDefaultCollectionPassedFromBuilderToClient() throws IOException {+ try (LBHttpSolrClient createdClient =+ new LBHttpSolrClient.Builder()+ .withBaseSolrUrl(ANY_BASE_SOLR_URL)+ .withDefaultCollection("aCollection")+ .build()) {+ assertEquals("aCollection", createdClient.getDefaultCollection());+ }+ }}