org.apache.solr.client.solrj.impl

## Class LBHttpSolrServer

- java.lang.Object
- - org.apache.solr.client.solrj.SolrServer
  - - org.apache.solr.client.solrj.impl.LBHttpSolrServer

All Implemented Interfaces:
  Serializable

    public class LBHttpSolrServer
    extends SolrServer

LBHttpSolrServer or "LoadBalanced HttpSolrServer" is a load balancing wrapper around `HttpSolrServer`. This is useful when you have multiple SolrServers 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, this class may be used for updates since updates will be forwarded to the appropriate leader. Also see the wiki page.

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.

>      SolrServer lbHttpSolrServer = new LBHttpSolrServer("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();
>      SolrServer lbHttpSolrServer = new LBHttpSolrServer(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

Since:
  solr 1.4
See Also:
  Serialized Form

### Nested Class Summary

Nested Classes
`static class` `LBHttpSolrServer.Req`
`static class` `LBHttpSolrServer.Rsp`
`protected static class` `LBHttpSolrServer.ServerWrapper`

### Field Summary

Fields
`protected Map<String,LBHttpSolrServer.ServerWrapper>` `zombieServers`

### Constructor Summary

Constructors
Constructor and Description
`LBHttpSolrServer(org.apache.http.client.HttpClient httpClient, ResponseParser parser, String... solrServerUrl)` The provided httpClient should use a multi-threaded connection manager
`LBHttpSolrServer(org.apache.http.client.HttpClient httpClient, String... solrServerUrl)` The provided httpClient should use a multi-threaded connection manager
`LBHttpSolrServer(String... solrServerUrls)`

### Method Summary

Methods
`void` `addSolrServer(String server)`
`protected Exception` `addZombie(HttpSolrServer server, Exception e)`
`protected void` `finalize()`
`org.apache.http.client.HttpClient` `getHttpClient()`
`ResponseParser` `getParser()`
`protected HttpSolrServer` `makeServer(String server)`
`static String` `normalize(String server)`
`String` `removeSolrServer(String server)`
`LBHttpSolrServer.Rsp` `request(LBHttpSolrServer.Req req)` Tries to query a live server from the list provided in Req.
`NamedList<Object>` `request(SolrRequest request)` Tries to query a live server.
`void` `setAliveCheckInterval(int interval)` LBHttpSolrServer keeps pinging the dead servers at fixed interval to find if it is alive.
`void` `setConnectionTimeout(int timeout)`
`void` `setParser(ResponseParser parser)`
`void` `setRequestWriter(RequestWriter requestWriter)`
`void` `setSoTimeout(int timeout)` set soTimeout (read timeout) on the underlying HttpConnectionManager.
`void` `shutdown()` Release allocated resources.

### Methods inherited from class org.apache.solr.client.solrj.SolrServer

`add, add, add, add, addBean, addBean, addBeans, addBeans, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, getBinder, optimize, optimize, optimize, ping, query, query, queryAndStreamResponse, rollback`

### Methods inherited from class java.lang.Object

`clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### zombieServers

    protected final Map<String,LBHttpSolrServer.ServerWrapper> zombieServers

### Constructor Detail

#### LBHttpSolrServer

    public LBHttpSolrServer(String... solrServerUrls)
                     throws MalformedURLException

Throws:
  `MalformedURLException`

#### LBHttpSolrServer

    public LBHttpSolrServer(org.apache.http.client.HttpClient httpClient,
                    String... solrServerUrl)
                     throws MalformedURLException

The provided httpClient should use a multi-threaded connection manager

Throws:
  `MalformedURLException`

#### LBHttpSolrServer

    public LBHttpSolrServer(org.apache.http.client.HttpClient httpClient,
                    ResponseParser parser,
                    String... solrServerUrl)
                     throws MalformedURLException

The provided httpClient should use a multi-threaded connection manager

Throws:
  `MalformedURLException`

### Method Detail

#### normalize

    public static String normalize(String server)

#### makeServer

    protected HttpSolrServer makeServer(String server)
                                 throws MalformedURLException

Throws:
  `MalformedURLException`

#### request

    public LBHttpSolrServer.Rsp request(LBHttpSolrServer.Req req)
                                 throws SolrServerException,
                                        IOException

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 as well as the list of servers to query
Returns:
  the result of the request
Throws:
  `IOException` - If there is a low-level I/O error.
  `SolrServerException`

#### addZombie

    protected Exception addZombie(HttpSolrServer server,
                      Exception e)

#### addSolrServer

    public void addSolrServer(String server)
                       throws MalformedURLException

Throws:
  `MalformedURLException`

#### removeSolrServer

    public String removeSolrServer(String server)

#### setConnectionTimeout

    public void setConnectionTimeout(int timeout)

#### setSoTimeout

    public void setSoTimeout(int timeout)

set soTimeout (read timeout) on the underlying HttpConnectionManager. This is desirable for queries, but probably not for indexing.

#### shutdown

    public void shutdown()

Description copied from class: `SolrServer`
Release allocated resources.

Specified by:
  `shutdown` in class `SolrServer`

#### request

    public NamedList<Object> request(SolrRequest request)
                              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:
  `request` in class `SolrServer`
Parameters:
  `request` - the SolrRequest.
Returns:
  response
Throws:
  `IOException` - If there is a low-level I/O error.
  `SolrServerException`

#### setAliveCheckInterval

    public void setAliveCheckInterval(int interval)

LBHttpSolrServer keeps pinging the dead servers at fixed interval to find if it is alive. Use this to set that interval

Parameters:
  `interval` - time in milliseconds

#### getHttpClient

    public org.apache.http.client.HttpClient getHttpClient()

#### getParser

    public ResponseParser getParser()

#### setParser

    public void setParser(ResponseParser parser)

#### setRequestWriter

    public void setRequestWriter(RequestWriter requestWriter)

#### finalize

    protected void finalize()
                     throws Throwable

Overrides:
  `finalize` in class `Object`
Throws:
  `Throwable`