Configuring the Solr heartbeat mechanism
Solr is designed to be scalable, fault tolerant, and have a high up time so that we can have our search service always ready. Many of the deployments, whether they are still master-slave setups or SolrCloud ones, still use some kind of load-balancing and health-checking mechanism. Solr comes with a request handler that is designed to handle health-checking requests, and this recipe will show you how to set it up.
How to do it...
Setting up the heartbeat mechanism in Solr is very easy. One just needs to add the following section to the solrconfig.xml
file:
<requestHandler name="/admin/ping" class="solr.PingRequestHandler"> <lst name="invariants"> <str name="q">solrpingquery</str> </lst> </requestHandler>
This is all. Of course, if we need all our cores and collections to respond to the health requests, we should include the previous section in the solrconfig.xml
files for all of them. After this, run a query to the admin/ping handler of our Solr instance, for example:
curl 'localhost:8983/solr/heartbeat_core/admin/ping'
Solr will respond with a status response, for example:
<?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"><int name="status">0</int><int name="QTime">6</int><lst name="params"/></lst><str name="status">OK</str> </response>
How it works...
The configuration is really simple; we defined a new request handler that will be available under the /admin/ping
address (of course, we have to prefix it with the host address and core name). The class implementing the handle is the one dedicated to handle the heartbeat mechanism request, solr.PingRequestHandler
. We also defined that the q
parameter for all the ping requests will be solrpingquery
and the request won't be able to overwrite this parameter (because we included it in the invariants
section). The ping query should be as simple as it can get so that it runs blazingly fast; what's more, it is usually good for it not to return any search results.
As you can see, the response contains the status
section, which in our case has the value of OK
. In the case of an error, the status
section will contain the error code.
There's more...
The solr.PingRequestHandler
handler allows us to enable and disable the heartbeat mechanism without shutting down the whole Solr instance.
Enabling and disabling the heartbeat mechanism
If we want to disable and enable the heartbeat mechanism without taking down the whole Solr instance, we need to introduce a property called healthcheckFile
to our request handler configuration, for example:
<requestHandler name="/admin/ping" class="solr.PingRequestHandler">
<lst name="invariants">
<str name="q">solrpingquery</str>
</lst>
<str name="healthcheckFile">server-enabled.txt</str>
</requestHandler>
Now, to enable the heartbeat mechanism, one should run the following command:
curl 'localhost:8983/solr/heartbeat_core/admin/ping?action=enable'
By running this, Solr will create a file named server-enabled.txt
in the directory the data directory is located at. This file will contain information about when the heartbeat mechanism is enabled.
To disable the heartbeat mechanism, one should run the following command:
curl 'localhost:8983/ solr/heartbeat_core/admin/ping?action=disable'
This command will delete the previously created file.
We can also check the heartbeat status by running the following command:
curl 'localhost:8983/solr/heartbeat_core/admin/ping?action=status'