Class PatchedLBHttpSolrClient

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable

    public class PatchedLBHttpSolrClient
    extends org.apache.solr.client.solrj.impl.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

    Since:
    solr 1.4
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addQueryParams​(java.lang.String queryOnlyParam)  
      void addSolrServer​(java.lang.String server)  
      protected java.lang.Exception addZombie​(org.apache.solr.client.solrj.impl.HttpSolrClient server, java.lang.Exception e)  
      void close()  
      protected java.lang.Exception doRequest​(org.apache.solr.client.solrj.impl.HttpSolrClient client, PatchedLBHttpSolrClient.Req req, PatchedLBHttpSolrClient.Rsp rsp, boolean isNonRetryable, boolean isZombie, java.lang.String zombieKey)  
      protected void finalize()  
      org.apache.http.client.HttpClient getHttpClient()
      Return the HttpClient this instance uses.
      org.apache.solr.client.solrj.ResponseParser getParser()  
      java.util.Set<java.lang.String> getQueryParams()  
      org.apache.solr.client.solrj.request.RequestWriter getRequestWriter()  
      protected org.apache.solr.client.solrj.impl.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.
      org.apache.solr.common.util.NamedList<java.lang.Object> request​(org.apache.solr.client.solrj.SolrRequest request, java.lang.String collection)
      Tries to query a live server.
      org.apache.solr.common.util.NamedList<java.lang.Object> request​(org.apache.solr.client.solrj.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​(org.apache.solr.client.solrj.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​(org.apache.solr.client.solrj.request.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.
      • Methods inherited from class org.apache.solr.client.solrj.impl.LBHttpSolrClient

        doRequest, request
      • Methods 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, getBinder, getById, getById, getById, getById, getById, getById, getById, getById, optimize, optimize, optimize, optimize, optimize, optimize, ping, query, query, query, query, queryAndStreamResponse, queryAndStreamResponse, request, rollback, rollback
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getQueryParams

        public java.util.Set<java.lang.String> getQueryParams()
        Overrides:
        getQueryParams in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • setQueryParams

        public void setQueryParams​(java.util.Set<java.lang.String> queryParams)
        Expert Method.
        Overrides:
        setQueryParams in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Parameters:
        queryParams - set of param keys to only send via the query string
      • addQueryParams

        public void addQueryParams​(java.lang.String queryOnlyParam)
        Overrides:
        addQueryParams in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • normalize

        public static java.lang.String normalize​(java.lang.String server)
      • makeSolrClient

        protected org.apache.solr.client.solrj.impl.HttpSolrClient makeSolrClient​(java.lang.String server)
        Overrides:
        makeSolrClient in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • request

        public PatchedLBHttpSolrClient.Rsp request​(PatchedLBHttpSolrClient.Req req)
                                            throws org.apache.solr.client.solrj.SolrServerException,
                                                   java.io.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:
        java.io.IOException - If there is a low-level I/O error.
        org.apache.solr.client.solrj.SolrServerException
      • addZombie

        protected java.lang.Exception addZombie​(org.apache.solr.client.solrj.impl.HttpSolrClient server,
                                                java.lang.Exception e)
        Overrides:
        addZombie in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • doRequest

        protected java.lang.Exception doRequest​(org.apache.solr.client.solrj.impl.HttpSolrClient client,
                                                PatchedLBHttpSolrClient.Req req,
                                                PatchedLBHttpSolrClient.Rsp rsp,
                                                boolean isNonRetryable,
                                                boolean isZombie,
                                                java.lang.String zombieKey)
                                         throws org.apache.solr.client.solrj.SolrServerException,
                                                java.io.IOException
        Throws:
        org.apache.solr.client.solrj.SolrServerException
        java.io.IOException
      • addSolrServer

        public void addSolrServer​(java.lang.String server)
                           throws java.net.MalformedURLException
        Overrides:
        addSolrServer in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Throws:
        java.net.MalformedURLException
      • removeSolrServer

        public java.lang.String removeSolrServer​(java.lang.String server)
        Overrides:
        removeSolrServer in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • setConnectionTimeout

        @Deprecated
        public void setConnectionTimeout​(int timeout)
        Deprecated.
        since 7.0 Use PatchedLBHttpSolrClient.Builder methods instead.
        Overrides:
        setConnectionTimeout in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • setSoTimeout

        @Deprecated
        public void setSoTimeout​(int timeout)
        Deprecated.
        since 7.0 Use PatchedLBHttpSolrClient.Builder methods instead.
        set soTimeout (read timeout) on the underlying HttpConnectionManager. This is desirable for queries, but probably not for indexing.
        Overrides:
        setSoTimeout in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • request

        public org.apache.solr.common.util.NamedList<java.lang.Object> request​(org.apache.solr.client.solrj.SolrRequest request,
                                                                               java.lang.String collection)
                                                                        throws org.apache.solr.client.solrj.SolrServerException,
                                                                               java.io.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.
        Overrides:
        request in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Parameters:
        request - the SolrRequest.
        Returns:
        response
        Throws:
        java.io.IOException - If there is a low-level I/O error.
        org.apache.solr.client.solrj.SolrServerException
      • request

        public org.apache.solr.common.util.NamedList<java.lang.Object> request​(org.apache.solr.client.solrj.SolrRequest request,
                                                                               java.lang.String collection,
                                                                               java.lang.Integer numServersToTry)
                                                                        throws org.apache.solr.client.solrj.SolrServerException,
                                                                               java.io.IOException
        Overrides:
        request in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Throws:
        org.apache.solr.client.solrj.SolrServerException
        java.io.IOException
      • 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
        Overrides:
        setAliveCheckInterval in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Parameters:
        interval - time in milliseconds
      • getHttpClient

        public org.apache.http.client.HttpClient getHttpClient()
        Return the HttpClient this instance uses.
        Overrides:
        getHttpClient in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • getParser

        public org.apache.solr.client.solrj.ResponseParser getParser()
        Overrides:
        getParser in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • setParser

        public void setParser​(org.apache.solr.client.solrj.ResponseParser parser)
        Changes the ResponseParser that will be used for the internal SolrServer objects.
        Overrides:
        setParser in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Parameters:
        parser - Default Response Parser chosen to parse the response if the parser were not specified as part of the request.
        See Also:
        SolrRequest.getResponseParser()
      • setRequestWriter

        public void setRequestWriter​(org.apache.solr.client.solrj.request.RequestWriter requestWriter)
        Changes the RequestWriter that will be used for the internal SolrServer objects.
        Overrides:
        setRequestWriter in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
        Parameters:
        requestWriter - Default RequestWriter, used to encode requests sent to the server.
      • getRequestWriter

        public org.apache.solr.client.solrj.request.RequestWriter getRequestWriter()
        Overrides:
        getRequestWriter in class org.apache.solr.client.solrj.impl.LBHttpSolrClient
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable