Class LBSolrClient
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable
- Direct Known Subclasses:
LBAsyncSolrClient
Do NOT use this class for indexing in leader/follower scenarios since documents must be sent to the correct leader; 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 endpoints. Endpoint URLs are expected to point to the Solr "root" path (i.e. "/solr").
SolrClient client = new LBSolrClient.Builder(httpSolrClient,
new LBSolrClient.Endpoint("http://host1:8080/solr"), new LBSolrClient.Endpoint("http://host2:8080/solr"))
.build();
Users who wish to balance traffic across a specific set of replicas or cores may specify each
endpoint as a root-URL and core-name pair. For example:
SolrClient client = new LBSolrClient.Builder(httpSolrClient,
new LBSolrClient.Endpoint("http://host1:8080/solr", "coreA"),
new LBSolrClient.Endpoint("http://host2:8080/solr", "coreB"))
.build();
If a request to an endpoint fails by an IOException due to a connection timeout or read timeout then the host is taken off the list of live endpoints and moved to a 'dead endpoint list' and the request is resent to the next live endpoint. This process is continued till it tries all the live endpoints. If at least one endpoint is alive, the request succeeds, and if not it fails.
Dead endpoints are periodically healthchecked on a fixed interval controlled by
LBSolrClient.Builder#setAliveCheckInterval(int, TimeUnit). 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 set up 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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classLBSolrClient.Builder<C extends HttpSolrClientBase>static classA Solr endpoint forLBSolrClientto include in its load-balancingstatic classprotected static classstatic classstatic classNested classes/interfaces inherited from class org.apache.solr.client.solrj.SolrClient
SolrClient.SolrClientFunction<C extends SolrClient,R> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longprotected ResponseParserprotected RequestWriterprotected static final Stringprotected final Map<String, LBSolrClient.EndpointWrapper> Fields inherited from class org.apache.solr.client.solrj.SolrClient
defaultCollection -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLBSolrClient(List<LBSolrClient.Endpoint> solrEndpoints) protectedLBSolrClient(LBSolrClient.Builder<?> builder) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSolrServer(LBSolrClient.Endpoint endpoint) voidclose()protected LBSolrClient.EndpointWrappercreateServerWrapper(LBSolrClient.Endpoint endpoint) protected ExceptiondoRequest(LBSolrClient.Endpoint baseUrl, LBSolrClient.Req req, LBSolrClient.Rsp rsp, boolean isNonRetryable, boolean isZombie) protected abstract SolrClientgetClient(LBSolrClient.Endpoint endpoint) protected voidmakeServerAZombie(LBSolrClient.Endpoint endpoint) protected ExceptionmakeServerAZombie(LBSolrClient.Endpoint endpoint, Exception e) protected voidstatic Stringprotected LBSolrClient.EndpointWrapperpickServer(LBSolrClient.EndpointWrapper[] aliveServerList, SolrRequest<?> request) Pick a server from list to execute request.removeSolrServer(LBSolrClient.Endpoint endpoint) request(LBSolrClient.Req req) Tries to query a live server from the list provided in Req.request(SolrRequest<?> request, String collection) Tries to query a live server.request(SolrRequest<?> request, String collection, Integer numServersToTry) protected voidreviveZombieServer(LBSolrClient.Endpoint endpoint) protected voidMethods inherited from class org.apache.solr.client.solrj.SolrClient
add, add, add, add, add, add, add, add, add, add, addBean, addBean, addBean, addBean, addBeans, addBeans, addBeans, addBeans, addBeans, addBeans, commit, commit, commit, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, deleteByQuery, deleteByQuery, getById, getById, getById, getById, getById, getById, getById, getById, getContext, getDefaultCollection, optimize, optimize, optimize, optimize, optimize, optimize, ping, ping, query, query, query, query, queryAndStreamResponse, queryAndStreamResponse, queryAndStreamResponse, request, rollback, rollback
-
Field Details
-
UPDATE_LIVE_SERVER_MESSAGE
- See Also:
-
RETRY_CODES
-
zombieServers
-
aliveCheckIntervalMillis
protected long aliveCheckIntervalMillis -
parser
-
requestWriter
-
-
Constructor Details
-
LBSolrClient
-
LBSolrClient
-
-
Method Details
-
updateAliveList
protected void updateAliveList() -
createServerWrapper
-
normalize
-
request
Tries to query a live server from the list provided in Req. Servers in the dead pool are skipped. If a request fails due to an IOException, the server is moved to the dead pool for a certain period of time, or until a test request on that server succeeds.Servers are queried in the exact order given (except servers currently in the dead pool are skipped). If no live servers from the provided list remain to be tried, a number of previously skipped dead servers will be tried. Req.getNumDeadServersToTry() controls how many dead servers will be tried.
If no live servers are found a SolrServerException is thrown.
- Parameters:
req- contains both the request and the list of servers to query- Returns:
- the result of the request
- Throws:
IOException- If there is a low-level I/O error.SolrServerException
-
doRequest
protected Exception doRequest(LBSolrClient.Endpoint baseUrl, LBSolrClient.Req req, LBSolrClient.Rsp rsp, boolean isNonRetryable, boolean isZombie) throws SolrServerException, IOException - Throws:
SolrServerExceptionIOException
-
getClient
-
getParser
-
getRequestWriter
-
reviveZombieServer
-
makeServerAZombie
-
makeServerAZombie
-
makeServerAZombie
-
addSolrServer
-
removeSolrServer
-
request
public NamedList<Object> request(SolrRequest<?> request, String collection) throws SolrServerException, IOException Tries to query a live server. A SolrServerException is thrown if all servers are dead. If the request failed due to IOException then the live server is moved to dead pool and the request is retried on another live server. After live servers are exhausted, any servers previously marked as dead will be tried before failing the request.- Specified by:
requestin classSolrClient- Parameters:
request- the SolrRequest.collection- the collection to execute the request against- Returns:
- response
- Throws:
IOException- If there is a low-level I/O error.SolrServerException- if there is an error on the server
-
request
public NamedList<Object> request(SolrRequest<?> request, String collection, Integer numServersToTry) throws SolrServerException, IOException - Throws:
SolrServerExceptionIOException
-
pickServer
protected LBSolrClient.EndpointWrapper pickServer(LBSolrClient.EndpointWrapper[] aliveServerList, SolrRequest<?> request) Pick a server from list to execute request. By default, servers are picked in round-robin manner, custom classes can override this method for more advance logic- Parameters:
aliveServerList- list of currently alive serversrequest- the request will be sent to the picked server- Returns:
- the picked server
-
close
public void close()
-