Skip to content

Apache Lucene open-source search software

SOLR-14210: Include replica health in healtcheck handler (#1387)

Jan Høydahlf2114b9935fb
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 289a43c3117..2daa3648bac 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -61,7 +61,8 @@ Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this r
New Features
---------------------
-(No changes)
+* SOLR-14210: HealthCheckHandler can now require that all cores are healthy before returning 'OK'
+ To enable, add &requireHealthyCores=true to enable (janhoy, Houston Putman, shalin)
Improvements
---------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java
index cc2a649a0eb..afa841640e6 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java
@@ -18,8 +18,15 @@
package org.apache.solr.handler.admin;
import java.lang.invoke.MethodHandles;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.solr.cloud.CloudDescriptor;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.common.cloud.Replica.State;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.CoreContainer;
@@ -29,7 +36,6 @@ import org.apache.solr.response.SolrQueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import static org.apache.solr.common.params.CommonParams.FAILURE;
import static org.apache.solr.common.params.CommonParams.OK;
import static org.apache.solr.common.params.CommonParams.STATUS;
@@ -37,14 +43,17 @@ import static org.apache.solr.common.params.CommonParams.STATUS;
/*
* Health Check Handler for reporting the health of a specific node.
*
- * This checks if the node is:
+ * This checks if:
* 1. Cores container is active.
- * 1. Connected to zookeeper.
- * 2. Listed in 'live_nodes' in zookeeper.
+ * 2. Node connected to zookeeper.
+ * 3. Node listed in 'live_nodes' in zookeeper.
+ * 4. No RECOVERING or DOWN cores (if request param requireHealthyCores=true)
*/
public class HealthCheckHandler extends RequestHandlerBase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ private static final String PARAM_REQUIRE_HEALTHY_CORES = "requireHealthyCores";
+ private static final List<State> UNHEALTHY_STATES = Arrays.asList(State.DOWN, State.RECOVERING);
CoreContainer coreContainer;
@@ -67,6 +76,7 @@ public class HealthCheckHandler extends RequestHandlerBase {
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
CoreContainer cores = getCoreContainer();
+ rsp.setHttpCaching(false);
// Core container should not be null and active (redundant check)
if(cores == null || cores.isShutDown()) {
@@ -88,15 +98,46 @@ public class HealthCheckHandler extends RequestHandlerBase {
return;
}
- // Set status to true if this node is in live_nodes
- if (clusterState.getLiveNodes().contains(cores.getZkController().getNodeName())) {
- rsp.add(STATUS, OK);
- } else {
+ // Fail if not in live_nodes
+ if (!clusterState.getLiveNodes().contains(cores.getZkController().getNodeName())) {
rsp.add(STATUS, FAILURE);
rsp.setException(new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "Host Unavailable: Not in live nodes as per zk"));
+ return;
}
- rsp.setHttpCaching(false);
+ // Optionally require that all cores on this node are active if param 'requireHealthyCores=true'
+ if (req.getParams().getBool(PARAM_REQUIRE_HEALTHY_CORES, false)) {
+ Collection<CloudDescriptor> coreDescriptors = cores.getCores().stream()
+ .map(c -> c.getCoreDescriptor().getCloudDescriptor()).collect(Collectors.toList());
+ long unhealthyCores = findUnhealthyCores(coreDescriptors, clusterState);
+ if (unhealthyCores > 0) {
+ rsp.add(STATUS, FAILURE);
+ rsp.add("num_cores_unhealthy", unhealthyCores);
+ rsp.setException(new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, unhealthyCores + " out of "
+ + cores.getAllCoreNames().size() + " replicas are currently initializing or recovering"));
+ return;
+ }
+ rsp.add("message", "All cores are healthy");
+ }
+
+ // All lights green, report healthy
+ rsp.add(STATUS, OK);
+ }
+
+ /**
+ * Find replicas DOWN or RECOVERING, or replicas in clusterstate that do not exist on local node.
+ * We first find local cores which are either not registered or unhealthy, and check each of these against
+ * the clusterstate, and return a count of unhealthy replicas
+ * @param cores list of core cloud descriptors to iterate
+ * @param clusterState clusterstate from ZK
+ * @return number of unhealthy cores, either in DOWN or RECOVERING state
+ */
+ static long findUnhealthyCores(Collection<CloudDescriptor> cores, ClusterState clusterState) {
+ return cores.stream()
+ .filter(c -> !c.hasRegistered() || UNHEALTHY_STATES.contains(c.getLastPublished())) // Find candidates locally
+ .filter(c -> clusterState.hasCollection(c.getCollectionName())) // Only care about cores for actual collections
+ .filter(c -> clusterState.getCollection(c.getCollectionName()).getActiveSlicesMap().containsKey(c.getShardId()))
+ .count();
}
@Override
diff --git a/solr/core/src/test/org/apache/solr/cloud/HealthCheckHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/HealthCheckHandlerTest.java
similarity index 74%
rename from solr/core/src/test/org/apache/solr/cloud/HealthCheckHandlerTest.java
rename to solr/core/src/test/org/apache/solr/handler/admin/HealthCheckHandlerTest.java
index e42678c8b3c..7d517f2f95b 100644
--- a/solr/core/src/test/org/apache/solr/cloud/HealthCheckHandlerTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/HealthCheckHandlerTest.java
@@ -15,9 +15,12 @@
* limitations under the License.
*/
-package org.apache.solr.cloud;
+package org.apache.solr.handler.admin;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Properties;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrResponse;
@@ -32,9 +35,16 @@ import org.apache.solr.client.solrj.request.V2Request;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.HealthCheckResponse;
import org.apache.solr.client.solrj.response.V2Response;
+import org.apache.solr.cloud.CloudDescriptor;
+import org.apache.solr.cloud.ClusterStateMockUtil;
+import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.core.CoreDescriptor;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -177,4 +187,50 @@ public class HealthCheckHandlerTest extends SolrCloudTestCase {
}
}
+ @Test
+ public void testFindUnhealthyCores() throws Exception {
+ // Simulate two nodes, with two collections:
+ // node1: collection1 -> shard1: [ replica1 (active), replica3 (down) ]
+ // collection2 -> shard1: [ replica2 (recovering) ]
+ // node2: collection1 -> shard1: [ replica2 (active), replica4 (down) ]
+ // collection2 -> shard1: [ replica1 (active) ]
+ try (ZkStateReader reader = ClusterStateMockUtil.buildClusterState(
+ "csrr2rDr2Dcsr2FrR", 1, 1, "node1", "node2")) {
+ ClusterState clusterState = reader.getClusterState();
+
+ // Node 1
+ Collection<CloudDescriptor> node1Cores = Arrays.asList(
+ mockCD("collection1", "slice1_replica1", "slice1", true, Replica.State.ACTIVE),
+ mockCD("collection1", "slice1_replica3", "slice1", true, Replica.State.DOWN),
+ mockCD("collection2", "slice1_replica5", "slice1", true, Replica.State.RECOVERING),
+ // A dangling core for a non-existant collection will not fail the check
+ mockCD("invalid", "invalid", "slice1", false, Replica.State.RECOVERING),
+ // A core for a slice that is not an active slice will not fail the check
+ mockCD("collection1", "invalid_replica1", "invalid", true, Replica.State.DOWN)
+ );
+ long unhealthy1 = HealthCheckHandler.findUnhealthyCores(node1Cores, clusterState);
+ assertEquals(2, unhealthy1);
+
+ // Node 2
+ Collection<CloudDescriptor> node2Cores = Arrays.asList(
+ mockCD("collection1", "slice1_replica2", "slice1", true, Replica.State.ACTIVE),
+ mockCD("collection1", "slice1_replica4", "slice1", true, Replica.State.DOWN),
+ mockCD("collection2", "slice1_replica1", "slice1", true, Replica.State.RECOVERY_FAILED)
+ );
+ long unhealthy2 = HealthCheckHandler.findUnhealthyCores(node2Cores, clusterState);
+ assertEquals(1, unhealthy2);
+ }
+ }
+
+ /* Creates a minimal cloud descriptor for a core */
+ private CloudDescriptor mockCD(String collection, String name, String shardId, boolean registered, Replica.State state) {
+ Properties props = new Properties();
+ props.put(CoreDescriptor.CORE_SHARD, shardId);
+ props.put(CoreDescriptor.CORE_COLLECTION, collection);
+ props.put(CoreDescriptor.CORE_NODE_NAME, name);
+ CloudDescriptor cd = new CloudDescriptor(null, name, props);
+ cd.setHasRegistered(registered);
+ cd.setLastPublished(state);
+ return cd;
+ }
}
\ No newline at end of file