Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-4544: Refactor HttpShardHandlerFactory so load-balancing logic can be customized

Robert Muirc04a4a5c9633
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 7045a49a83e..0ae341cdc39 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -178,6 +178,9 @@ Other Changes
* SOLR-4603: CachingDirectoryFactory should use an IdentityHashMap for
byDirectoryCache. (Mark Miller)
+* SOLR-4544: Refactor HttpShardHandlerFactory so load-balancing logic can be customized.
+ (Ryan Ernst via Robert Muir)
+
================== 4.2.1 ==================
Versions of Major Components
diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
index cea5a7b3200..765aeb42b1a 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
@@ -18,7 +18,6 @@ package org.apache.solr.handler.component;
import java.net.ConnectException;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -27,7 +26,6 @@ import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import org.apache.http.client.HttpClient;
@@ -61,21 +59,20 @@ public class HttpShardHandler extends ShardHandler {
private HttpShardHandlerFactory httpShardHandlerFactory;
private CompletionService<ShardResponse> completionService;
- private Set<Future<ShardResponse>> pending;
+ private Set<Future<ShardResponse>> pending;
private Map<String,List<String>> shardToURLs;
private HttpClient httpClient;
-
public HttpShardHandler(HttpShardHandlerFactory httpShardHandlerFactory, HttpClient httpClient) {
this.httpClient = httpClient;
this.httpShardHandlerFactory = httpShardHandlerFactory;
- completionService = new ExecutorCompletionService<ShardResponse>(httpShardHandlerFactory.commExecutor);
+ completionService = httpShardHandlerFactory.newCompletionService();
pending = new HashSet<Future<ShardResponse>>();
// maps "localhost:8983|localhost:7574" to a shuffled List("http://localhost:8983","http://localhost:7574")
- // This is primarily to keep track of what order we should use to query the replicas of a shard
- // so that we use the same replica for all phases of a distributed request.
+ // This is primarily to keep track of what order we should use to query the replicas of a shard
+ // so that we use the same replica for all phases of a distributed request.
shardToURLs = new HashMap<String,List<String>>();
}
@@ -106,21 +103,8 @@ public class HttpShardHandler extends ShardHandler {
// Don't modify the returned URL list.
private List<String> getURLs(String shard) {
List<String> urls = shardToURLs.get(shard);
- if (urls==null) {
- urls = StrUtils.splitSmart(shard, "|", true);
-
- // convert shard to URL
- for (int i=0; i<urls.size(); i++) {
- urls.set(i, httpShardHandlerFactory.scheme + urls.get(i));
- }
-
- //
- // Shuffle the list instead of use round-robin by default.
- // This prevents accidental synchronization where multiple shards could get in sync
- // and query the same replica at the same time.
- //
- if (urls.size() > 1)
- Collections.shuffle(urls, httpShardHandlerFactory.r);
+ if (urls == null) {
+ urls = httpShardHandlerFactory.makeURLList(shard);
shardToURLs.put(shard, urls);
}
return urls;
@@ -171,7 +155,7 @@ public class HttpShardHandler extends ShardHandler {
SolrServer server = new HttpSolrServer(url, httpClient);
ssr.nl = server.request(req);
} else {
- LBHttpSolrServer.Rsp rsp = httpShardHandlerFactory.loadbalancer.request(new LBHttpSolrServer.Req(req, urls));
+ LBHttpSolrServer.Rsp rsp = httpShardHandlerFactory.makeLoadBalancedRequest(req, urls);
ssr.nl = rsp.getResponse();
srsp.setShardAddress(rsp.getServer());
}
diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java
index 27f9f797099..5b582f3b686 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java
@@ -16,24 +16,36 @@ package org.apache.solr.handler.component;
* limitations under the License.
*/
+import java.io.IOException;
import java.net.MalformedURLException;
+import java.util.Collections;
+import java.util.List;
import java.util.Random;
-import java.util.concurrent.*;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import org.apache.http.client.HttpClient;
+import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpClientUtil;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
+import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.util.DefaultSolrThreadFactory;
-import org.apache.solr.util.plugin.PluginInfoInitialized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class HttpShardHandlerFactory extends ShardHandlerFactory implements PluginInfoInitialized {
+
+public class HttpShardHandlerFactory extends ShardHandlerFactory implements org.apache.solr.util.plugin.PluginInfoInitialized {
protected static Logger log = LoggerFactory.getLogger(HttpShardHandlerFactory.class);
// We want an executor that doesn't take up any resources if
@@ -42,7 +54,7 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory implements Plug
//
// Consider CallerRuns policy and a lower max threads to throttle
// requests at some point (or should we simply return failure?)
- ThreadPoolExecutor commExecutor = new ThreadPoolExecutor(
+ private ThreadPoolExecutor commExecutor = new ThreadPoolExecutor(
0,
Integer.MAX_VALUE,
5, TimeUnit.SECONDS, // terminate idle threads after 5 sec
@@ -51,7 +63,7 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory implements Plug
);
private HttpClient defaultClient;
- LBHttpSolrServer loadbalancer;
+ private LBHttpSolrServer loadbalancer;
//default values:
int soTimeout = 0;
int connectionTimeout = 0;
@@ -62,9 +74,9 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory implements Plug
int queueSize = -1;
boolean accessPolicy = false;
- public String scheme = "http://"; //current default values
+ private String scheme = "http://"; //current default values
- final Random r = new Random();
+ private final Random r = new Random();
// URL scheme to be used in distributed search.
static final String INIT_URL_SCHEME = "urlScheme";
@@ -177,4 +189,48 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory implements Plug
}
}
+
+ /**
+ * Makes a request to one or more of the given urls, using the configured load balancer.
+ *
+ * @param req The solr search request that should be sent through the load balancer
+ * @param urls The list of solr server urls to load balance across
+ * @return The response from the request
+ */
+ public LBHttpSolrServer.Rsp makeLoadBalancedRequest(final QueryRequest req, List<String> urls)
+ throws SolrServerException, IOException {
+ return loadbalancer.request(new LBHttpSolrServer.Req(req, urls));
+ }
+
+ /**
+ * Creates a randomized list of urls for the given shard.
+ *
+ * @param shard the urls for the shard (minus "http://"), separated by '|'
+ * @return A list of valid urls (including protocol) that are replicas for the shard
+ */
+ public List<String> makeURLList(String shard) {
+ List<String> urls = StrUtils.splitSmart(shard, "|", true);
+
+ // convert shard to URL
+ for (int i=0; i<urls.size(); i++) {
+ urls.set(i, scheme + urls.get(i));
+ }
+
+ //
+ // Shuffle the list instead of use round-robin by default.
+ // This prevents accidental synchronization where multiple shards could get in sync
+ // and query the same replica at the same time.
+ //
+ if (urls.size() > 1)
+ Collections.shuffle(urls, r);
+
+ return urls;
+ }
+
+ /**
+ * Creates a new completion service for use by a single set of distributed requests.
+ */
+ public CompletionService newCompletionService() {
+ return new ExecutorCompletionService<ShardResponse>(commExecutor);
+ }
}