Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-17066: Add 'defaultCollection' to SolrClients #2066

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1215,4 +1216,13 @@ public DocumentObjectBinder getBinder() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 received
private static final int MAX_STALE_RETRIES =
Integer.parseInt(System.getProperty("cloudSolrClientMaxStaleRetries", "5"));
Expand Down Expand Up @@ -292,11 +291,6 @@ public RequestWriter getRequestWriter() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected ConcurrentUpdateHttp2SolrClient(Builder builder) {
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);

Expand Down Expand Up @@ -359,6 +360,7 @@ private void addRunner() {
@Override
public 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);
Expand Down Expand Up @@ -697,6 +699,7 @@ public void shutdownNow() {
public static class Builder {
protected Http2SolrClient client;
protected String baseSolrUrl;
protected String defaultCollection;
protected int queueSize = 10;
protected int threadCount;
protected ExecutorService executorService;
Expand Down Expand Up @@ -787,6 +790,12 @@ public Builder neverStreamDeletes() {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected ConcurrentUpdateSolrClient(Builder builder) {
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
Expand Down Expand Up @@ -475,6 +476,7 @@ public void setCollection(String collection) {
@Override
public 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ protected Http2SolrClient(String serverBaseUrl, Builder builder) {
this.parser = builder.responseParser;
}
updateDefaultMimeTypeForParser();
this.defaultCollection = builder.defaultCollection;
if (builder.requestTimeoutMillis != null) {
this.requestTimeoutMillis = builder.requestTimeoutMillis;
} else {
Expand Down Expand Up @@ -520,7 +521,7 @@ public void onFailure(Response response, Throwable failure) {
@Override
public 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;
Expand Down Expand Up @@ -1036,6 +1037,7 @@ public static class Builder {
private ExecutorService executor;
protected RequestWriter requestWriter;
protected ResponseParser responseParser;
protected String defaultCollection;
private Set<String> urlParamNames;
private CookieStore cookieStore = getDefaultCookieStore();
private String proxyHost;
Expand Down Expand Up @@ -1148,6 +1150,12 @@ public Builder withResponseParser(ResponseParser responseParser) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ protected HttpSolrClient(Builder builder) {
this.soTimeout = builder.socketTimeoutMillis;
this.useMultiPartPost = builder.useMultiPartPost;
this.urlParamNames = builder.urlParamNames;
this.defaultCollection = builder.defaultCollection;
}

public Set<String> getUrlParamNames() {
Expand Down Expand Up @@ -239,9 +240,20 @@ public NamedList<Object> request(final SolrRequest<?> request, final ResponsePar
return request(request, processor, null);
}

private String resolveCollectionForRequest(SolrRequest<?> request, String explicitCollection) {
if (request.getCollection() != null) {
return request.getCollection();
} else if (explicitCollection != null) {
return explicitCollection;
} else {
return defaultCollection;
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -86,9 +85,11 @@
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
Expand Down Expand Up @@ -258,6 +259,7 @@ public static class Builder {
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;
Expand All @@ -279,11 +281,14 @@ public LBHttp2SolrClient.Builder setAliveCheckInterval(int aliveCheckInterval, T
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ protected LBHttpSolrClient(Builder builder) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ public NamedList<Object> request(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class SolrClientBuilder<B extends SolrClientBuilder<B>> {
protected int socketTimeoutMillis = 120000; // 120 seconds
private boolean socketTimeoutMillisUpdate = false;
protected boolean followRedirects = false;
protected String defaultCollection;
protected Set<String> urlParamNames;

/** The solution for the unchecked cast warning. */
Expand Down Expand Up @@ -97,6 +98,11 @@ public B withFollowRedirects(boolean followRedirects) {
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.
Expand Down