public class PatchedLBHttpSolrClient
extends LBHttpSolrClient
Hybris note: This is a patched version of LBHttpSolrClient which we had to create due to
https://issues.apache.org/jira/browse/SOLR-12415.
LBHttpSolrClient or "LoadBalanced HttpSolrClient" is a load balancing wrapper around HttpSolrClient. This is
useful when you have multiple Solr servers and the requests need to be Load Balanced among them.
Do NOT use this class for indexing in master/slave scenarios since documents must be sent to the correct
master; no inter-node routing is done.
In SolrCloud (leader/replica) scenarios, it is usually better to use CloudSolrClient, but this class may be
used for updates because the server will forward them to the appropriate leader.
It offers automatic failover when a server goes down and it detects when the server comes back up.
Load balancing is done using a simple round-robin on the list of servers.
If a request to a server fails by an IOException due to a connection timeout or read timeout then the host is taken off the list of live servers and moved to a 'dead server list' and the request is resent to the next live server. This process is continued till it tries all the live servers. If at least one server is alive, the request succeeds, and if not it fails.
SolrClient lbHttpSolrClient = new LBHttpSolrClient("http://host1:8080/solr/", "http://host2:8080/solr",
"http://host2:8080/solr");
//or if you wish to pass the HttpClient do as follows
httpClient httpClient = new HttpClient();
SolrClient lbHttpSolrClient = new LBHttpSolrClient(httpClient, "http://host1:8080/solr/", "http://host2:8080/solr",
"http://host2:8080/solr");
This detects if a dead server comes alive automatically. The check is done in fixed intervals in a
dedicated thread. This interval can be set using setAliveCheckInterval(int) , the default is set to one minute.
When to use this?
This can be used as a software load balancer when you do not wish to setup an external load balancer. Alternatives to
this code are to use a dedicated hardware load balancer or using Apache httpd with mod_proxy_balancer as a load
balancer. See Load balancing on Wikipedia
| Modifier and Type | Class and Description |
|---|---|
static class |
PatchedLBHttpSolrClient.Builder |
static class |
PatchedLBHttpSolrClient.Req |
static class |
PatchedLBHttpSolrClient.Rsp |
protected static class |
PatchedLBHttpSolrClient.ServerWrapper |
| Modifier and Type | Field and Description |
|---|---|
protected java.util.Map<java.lang.String,PatchedLBHttpSolrClient.ServerWrapper> |
zombieServers |
| Modifier | Constructor and Description |
|---|---|
protected |
PatchedLBHttpSolrClient(PatchedLBHttpSolrClient.Builder builder) |
| Modifier and Type | Method and Description |
|---|---|
void |
addQueryParams(java.lang.String queryOnlyParam) |
void |
addSolrServer(java.lang.String server) |
protected java.lang.Exception |
addZombie(HttpSolrClient server,
java.lang.Exception e) |
void |
close() |
protected java.lang.Exception |
doRequest(HttpSolrClient client,
PatchedLBHttpSolrClient.Req req,
PatchedLBHttpSolrClient.Rsp rsp,
boolean isNonRetryable,
boolean isZombie,
java.lang.String zombieKey) |
protected void |
finalize() |
HttpClient |
getHttpClient()
Return the HttpClient this instance uses.
|
ResponseParser |
getParser() |
java.util.Set<java.lang.String> |
getQueryParams() |
RequestWriter |
getRequestWriter() |
protected HttpSolrClient |
makeSolrClient(java.lang.String server) |
static java.lang.String |
normalize(java.lang.String server) |
java.lang.String |
removeSolrServer(java.lang.String server) |
PatchedLBHttpSolrClient.Rsp |
request(PatchedLBHttpSolrClient.Req req)
Tries to query a live server from the list provided in Req.
|
<any> |
request(SolrRequest request,
java.lang.String collection)
Tries to query a live server.
|
<any> |
request(SolrRequest request,
java.lang.String collection,
java.lang.Integer numServersToTry) |
void |
setAliveCheckInterval(int interval)
LBHttpSolrServer keeps pinging the dead servers at fixed interval to find if it is alive.
|
void |
setConnectionTimeout(int timeout)
Deprecated.
since 7.0 Use
PatchedLBHttpSolrClient.Builder methods instead. |
void |
setParser(ResponseParser parser)
Changes the
ResponseParser that will be used for the internal SolrServer objects. |
void |
setQueryParams(java.util.Set<java.lang.String> queryParams)
Expert Method.
|
void |
setRequestWriter(RequestWriter requestWriter)
Changes the
RequestWriter that will be used for the internal SolrServer objects. |
void |
setSoTimeout(int timeout)
Deprecated.
since 7.0 Use
PatchedLBHttpSolrClient.Builder methods instead. |
protected final java.util.Map<java.lang.String,PatchedLBHttpSolrClient.ServerWrapper> zombieServers
protected PatchedLBHttpSolrClient(PatchedLBHttpSolrClient.Builder builder)
public java.util.Set<java.lang.String> getQueryParams()
public void setQueryParams(java.util.Set<java.lang.String> queryParams)
queryParams - set of param keys to only send via the query stringpublic void addQueryParams(java.lang.String queryOnlyParam)
public static java.lang.String normalize(java.lang.String server)
protected HttpSolrClient makeSolrClient(java.lang.String server)
public PatchedLBHttpSolrClient.Rsp request(PatchedLBHttpSolrClient.Req req) throws SolrServerException, java.io.IOException
req - contains both the request as well as the list of servers to queryjava.io.IOException - If there is a low-level I/O error.SolrServerExceptionprotected java.lang.Exception addZombie(HttpSolrClient server,
java.lang.Exception e)
protected java.lang.Exception doRequest(HttpSolrClient client,
PatchedLBHttpSolrClient.Req req,
PatchedLBHttpSolrClient.Rsp rsp,
boolean isNonRetryable,
boolean isZombie,
java.lang.String zombieKey)
throws SolrServerException,
java.io.IOException
SolrServerExceptionjava.io.IOExceptionpublic void addSolrServer(java.lang.String server)
throws java.net.MalformedURLException
java.net.MalformedURLExceptionpublic java.lang.String removeSolrServer(java.lang.String server)
@Deprecated public void setConnectionTimeout(int timeout)
PatchedLBHttpSolrClient.Builder methods instead.@Deprecated public void setSoTimeout(int timeout)
PatchedLBHttpSolrClient.Builder methods instead.public void close()
public <any> request(SolrRequest request,
java.lang.String collection)
throws SolrServerException,
java.io.IOException
request - the SolrRequest.java.io.IOException - If there is a low-level I/O error.SolrServerExceptionpublic <any> request(SolrRequest request,
java.lang.String collection,
java.lang.Integer numServersToTry)
throws SolrServerException,
java.io.IOException
SolrServerExceptionjava.io.IOExceptionpublic void setAliveCheckInterval(int interval)
interval - time in millisecondspublic HttpClient getHttpClient()
public ResponseParser getParser()
public void setParser(ResponseParser parser)
ResponseParser that will be used for the internal SolrServer objects.parser - Default Response Parser chosen to parse the response if the parser were not specified as part of the
request.org.apache.solr.client.solrj.SolrRequest#getResponseParser()public void setRequestWriter(RequestWriter requestWriter)
RequestWriter that will be used for the internal SolrServer objects.requestWriter - Default RequestWriter, used to encode requests sent to the server.public RequestWriter getRequestWriter()
protected void finalize()
throws java.lang.Throwable
java.lang.ThrowableCopyright © 2018 SAP SE. All Rights Reserved.